ILIAS  release_8 Revision v8.23
NotificationsCollector.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ReportDto;
26 use ilSetting;
27 use ilDBConstants;
29 use ilDBInterface;
30 
32 {
33  private const PING_THRESHOLD = 500;
34 
37  private array $collection = [];
38  private ilDBInterface $db;
41 
42  public function __construct(
44  ?ilDBInterface $db = null,
45  ?ilSetting $settings = null,
46  ?ClockInterface $clock = null
47  ) {
48  global $DIC;
49 
50  $this->db = $db ?? $DIC->database();
51  $this->setting = $settings ?? $DIC->settings();
52  $this->clock = $clock ?? (new Factory())->clock()->system();
53 
54  $this->job = $job;
55 
56  $this->collect();
57  }
58 
59  private function collect(): void
60  {
61  $mail_expiration_days = (int) $this->setting->get('mail_threshold', '0');
62  $mail_expiration_warning_days = (int) $this->setting->get('mail_notify_orphaned', '0');
63 
64  if ($mail_expiration_days > $mail_expiration_warning_days) {
65  $notify_days_before = $mail_expiration_days - $mail_expiration_warning_days;
66  } else {
67  $notify_days_before = 1;
68  }
69 
70  $left_interval_datetime = $this->clock->now()->modify('- ' . $notify_days_before . ' days');
71 
72  $types = [ilDBConstants::T_TIMESTAMP];
73  $data = [$left_interval_datetime->format('Y-m-d 23:59:59')];
74 
75  $notification_query = "
76  SELECT m.mail_id, m.user_id, m.folder_id, m.send_time, m.m_subject, mdata.title
77  FROM mail m
78  LEFT JOIN mail_obj_data mdata ON mdata.obj_id = m.folder_id
79  LEFT JOIN mail_cron_orphaned mco ON mco.mail_id = m.mail_id
80  WHERE mco.mail_id IS NULL AND m.send_time <= %s
81  ";
82 
83  if ((int) $this->setting->get('mail_only_inbox_trash', '0') > 0) {
84  $notification_query .= " AND ((mdata.m_type = %s OR mdata.m_type = %s) OR mdata.obj_id IS NULL)";
85  array_push($types, ilDBConstants::T_TEXT, ilDBConstants::T_TEXT);
86  array_push($data, 'inbox', 'trash');
87  }
88 
89  $notification_query .= " ORDER BY m.user_id, m.folder_id, m.mail_id";
90 
92  $collection_obj = null;
93 
94  $res = $this->db->queryF($notification_query, $types, $data);
95  $i = 0;
96  while ($row = $this->db->fetchAssoc($res)) {
97  if ($i > 0 && $i % self::PING_THRESHOLD === 0) {
98  $this->job->ping();
99  }
100 
101  if ($collection_obj !== null && !$this->existsCollectionObjForUserId((int) $row['user_id'])) {
102  // The user changed, so we'll have to set the collection to NULL after adding it to the queue
103  $collection_obj = null;
104  }
105 
106  if ($collection_obj === null) {
107  // For the first user or if the user changed, we'll create a new collection object
108  $collection_obj = new ReportDto((int) $row['user_id']);
109  $this->addCollectionObject($collection_obj);
110  }
111 
112  $folder_obj = $collection_obj->getFolderObjectById((int) $row['folder_id']);
113  if ($folder_obj === null) {
114  $folder_obj = new FolderDto((int) $row['folder_id'], $row['title']);
115  $collection_obj->addFolderObject($folder_obj);
116  }
117 
118  $orphaned_mail_obj = new MailDto(
119  (int) $row['mail_id'],
120  $row['m_subject']
121  );
122  $folder_obj->addMailObject($orphaned_mail_obj);
123  ++$i;
124  }
125  }
126 
127  private function existsCollectionObjForUserId(int $user_id): bool
128  {
129  return isset($this->collection[$user_id]);
130  }
131 
132  private function addCollectionObject(ReportDto $collection_obj): void
133  {
134  $this->collection[$collection_obj->getUserId()] = $collection_obj;
135  }
136 
140  public function getCollection(): array
141  {
142  return $this->collection;
143  }
144 }
$res
Definition: ltiservices.php:69
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
global $DIC
Definition: feed.php:28
__construct(ilMailCronOrphanedMails $job, ?ilDBInterface $db=null, ?ilSetting $settings=null, ?ClockInterface $clock=null)
$i
Definition: metadata.php:41