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