ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMailGlobalServices.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 {
31  public const CACHE_TYPE_REF_ID = 0;
32  public const CACHE_TYPE_NEW_MAILS = 1;
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  null !== self::$global_mail_services_cache[self::CACHE_TYPE_REF_ID]) {
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 
73  public static function getNewMailsData(ilObjUser $user, int $leftInterval = 0): array
74  {
75  global $DIC;
76 
77  if ($user->isAnonymous() || 0 === $user->getId()) {
78  return [
79  'count' => 0,
80  'max_time' => (new DateTimeImmutable('@' . time()))->format('Y-m-d H:i:s')
81  ];
82  }
83 
84  $cacheKey = implode('_', [self::CACHE_TYPE_NEW_MAILS, $user->getId(), $leftInterval]);
85 
86  if (
87  isset(self::$global_mail_services_cache[$cacheKey]) &&
88  null !== self::$global_mail_services_cache[$cacheKey]) {
89  return self::$global_mail_services_cache[$cacheKey];
90  }
91 
92  $query = '
93  SELECT COUNT(mail_id) cnt, MAX(send_time) send_time
94  FROM mail
95  WHERE folder_id = %s AND user_id = %s AND m_status = %s
96  ';
97  if ($leftInterval > 0) {
98  $query .= ' AND send_time > '
99  . $DIC->database()->quote(date('Y-m-d H:i:s', $leftInterval), 'timestamp');
100  }
101 
102  $res = $DIC->database()->queryF(
103  $query,
104  ['integer', 'integer', 'text'],
105  [0, $user->getId(), 'unread']
106  );
107  $row = $DIC->database()->fetchAssoc($res);
108 
109  $query = '
110  SELECT COUNT(mail_id) cnt, MAX(m.send_time) send_time
111  FROM mail m
112  INNER JOIN mail_obj_data mo
113  ON mo.user_id = m.user_id
114  AND mo.obj_id = m.folder_id
115  AND mo.m_type = %s
116  WHERE m.user_id = %s
117  AND m.m_status = %s';
118  if ($leftInterval > 0) {
119  $query .= ' AND m.send_time > '
120  . $DIC->database()->quote(date('Y-m-d H:i:s', $leftInterval), 'timestamp');
121  }
122 
123  $res = $DIC->database()->queryF(
124  $query,
125  ['text', 'integer', 'text'],
126  ['inbox', $user->getId(), 'unread']
127  );
128  $row2 = $DIC->database()->fetchAssoc($res);
129 
130  self::$global_mail_services_cache[$cacheKey] = [
131  'count' => ((int) $row['cnt'] + (int) $row2['cnt']),
132  'max_time' => max(
133  (string) $row['send_time'],
134  (string) $row2['send_time']
135  ),
136  ];
137 
138  return self::$global_mail_services_cache[$cacheKey];
139  }
140 }
$res
Definition: ltiservices.php:69
const SYSTEM_FOLDER_ID
Definition: constants.php:35
global $DIC
Definition: feed.php:28
static getNewMailsData(ilObjUser $user, int $leftInterval=0)
$query
const MAIL_SETTINGS_ID
Definition: constants.php:36