ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilMailGlobalServices.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 {
30  final public const int CACHE_TYPE_REF_ID = 0;
31  final public const int CACHE_TYPE_NEW_MAILS = 1;
32 
33  protected static array $global_mail_services_cache = [];
34 
35  public static function getMailObjectRefId(): int
36  {
37  global $DIC;
38 
39  if (isset(self::$global_mail_services_cache[self::CACHE_TYPE_REF_ID]) &&
40  self::$global_mail_services_cache[self::CACHE_TYPE_REF_ID] !== null) {
41  return (int) self::$global_mail_services_cache[self::CACHE_TYPE_REF_ID];
42  }
43 
44  // mail settings id is set by a constant in ilias.ini.
45  // Keep the select for some time until everyone has updated his ilias.ini
46  if (!MAIL_SETTINGS_ID) {
47  $res = $DIC->database()->queryF(
48  '
49  SELECT object_reference.ref_id FROM object_reference, tree, object_data
50  WHERE tree.parent = %s
51  AND object_data.type = %s
52  AND object_reference.ref_id = tree.child
53  AND object_reference.obj_id = object_data.obj_id',
54  ['integer', 'text'],
55  [SYSTEM_FOLDER_ID, 'mail']
56  );
57 
58  while ($row = $DIC->database()->fetchAssoc($res)) {
59  self::$global_mail_services_cache[self::CACHE_TYPE_REF_ID] = (int) $row['ref_id'];
60  }
61  } else {
62  self::$global_mail_services_cache[self::CACHE_TYPE_REF_ID] = MAIL_SETTINGS_ID;
63  }
64 
65  return (int) self::$global_mail_services_cache[self::CACHE_TYPE_REF_ID];
66  }
67 
71  public static function getNewMailsData(ilObjUser $user, int $left_interval = 0): array
72  {
73  global $DIC;
74 
75  if ($user->isAnonymous() || $user->getId() === 0) {
76  return [
77  'count' => 0,
78  'max_time' => (new DateTimeImmutable('@' . time()))->format('Y-m-d H:i:s')
79  ];
80  }
81 
82  $cache_key = implode('_', [self::CACHE_TYPE_NEW_MAILS, $user->getId(), $left_interval]);
83 
84  if (
85  isset(self::$global_mail_services_cache[$cache_key]) &&
86  self::$global_mail_services_cache[$cache_key] !== null) {
87  return self::$global_mail_services_cache[$cache_key];
88  }
89 
90  $query = '
91  SELECT COUNT(mail_id) cnt, MAX(send_time) send_time
92  FROM mail
93  WHERE folder_id = %s AND user_id = %s AND m_status = %s
94  ';
95  if ($left_interval > 0) {
96  $query .= ' AND send_time > '
97  . $DIC->database()->quote(date('Y-m-d H:i:s', $left_interval), 'timestamp');
98  }
99 
100  $res = $DIC->database()->queryF(
101  $query,
102  ['integer', 'integer', 'text'],
103  [0, $user->getId(), 'unread']
104  );
105  $row = $DIC->database()->fetchAssoc($res);
106 
107  $query = '
108  SELECT COUNT(mail_id) cnt, MAX(m.send_time) send_time
109  FROM mail m
110  INNER JOIN mail_obj_data mo
111  ON mo.user_id = m.user_id
112  AND mo.obj_id = m.folder_id
113  AND mo.m_type = %s
114  WHERE m.user_id = %s
115  AND m.m_status = %s';
116  if ($left_interval > 0) {
117  $query .= ' AND m.send_time > '
118  . $DIC->database()->quote(date('Y-m-d H:i:s', $left_interval), 'timestamp');
119  }
120 
121  $res = $DIC->database()->queryF(
122  $query,
123  ['text', 'integer', 'text'],
124  ['inbox', $user->getId(), 'unread']
125  );
126  $row2 = $DIC->database()->fetchAssoc($res);
127 
128  self::$global_mail_services_cache[$cache_key] = [
129  'count' => ((int) $row['cnt'] + (int) $row2['cnt']),
130  'max_time' => max(
131  (string) $row['send_time'],
132  (string) $row2['send_time']
133  ),
134  ];
135 
136  return self::$global_mail_services_cache[$cache_key];
137  }
138 }
$res
Definition: ltiservices.php:66
const SYSTEM_FOLDER_ID
Definition: constants.php:35
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:26
static getNewMailsData(ilObjUser $user, int $left_interval=0)
const MAIL_SETTINGS_ID
Definition: constants.php:36