ILIAS  trunk Revision v11.0_alpha-1843-g9e1fad99175
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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 
36  private array $collection = [];
37  private readonly ilDBInterface $db;
38  private readonly ilSetting $setting;
39  private readonly ClockInterface $clock;
40 
41  public function __construct(
42  private readonly ilMailCronOrphanedMails $job,
43  ?ilDBInterface $db = null,
44  ?ilSetting $settings = null,
45  ?ClockInterface $clock = null
46  ) {
47  global $DIC;
48 
49  $this->db = $db ?? $DIC->database();
50  $this->setting = $settings ?? $DIC->settings();
51  $this->clock = $clock ?? (new Factory())->clock()->system();
52 
53  $this->collect();
54  }
55 
56  private function collect(): void
57  {
58  $mail_expiration_days = (int) $this->setting->get('mail_threshold', '0');
59  $mail_expiration_warning_days = (int) $this->setting->get('mail_notify_orphaned', '0');
60 
61  if ($mail_expiration_days > $mail_expiration_warning_days) {
62  $notify_days_before = $mail_expiration_days - $mail_expiration_warning_days;
63  } else {
64  $notify_days_before = 1;
65  }
66 
67  $left_interval_datetime = $this->clock->now()->modify('- ' . $notify_days_before . ' days');
68 
69  $types = [ilDBConstants::T_TIMESTAMP];
70  $data = [$left_interval_datetime->format('Y-m-d 23:59:59')];
71 
72  $notification_query = "
73  SELECT m.mail_id, m.user_id, m.folder_id, m.send_time, m.m_subject, mdata.title
74  FROM mail m
75  LEFT JOIN mail_obj_data mdata ON mdata.obj_id = m.folder_id
76  LEFT JOIN mail_cron_orphaned mco ON mco.mail_id = m.mail_id
77  WHERE mco.mail_id IS NULL AND m.send_time <= %s
78  ";
79 
80  if ((int) $this->setting->get('mail_only_inbox_trash', '0') > 0) {
81  $notification_query .= " AND ((mdata.m_type = %s OR mdata.m_type = %s) OR mdata.obj_id IS NULL)";
82  $types[] = ilDBConstants::T_TEXT;
83  $types[] = ilDBConstants::T_TEXT;
84  $data[] = 'inbox';
85  $data[] = 'trash';
86  }
87 
88  $notification_query .= " ORDER BY m.user_id, m.folder_id, m.mail_id";
89 
91  $collection_obj = null;
92 
93  $res = $this->db->queryF($notification_query, $types, $data);
94  $i = 0;
95  while ($row = $this->db->fetchAssoc($res)) {
96  if ($i > 0 && $i % self::PING_THRESHOLD === 0) {
97  $this->job->ping();
98  }
99 
100  if ($collection_obj !== null && !$this->existsCollectionObjForUserId((int) $row['user_id'])) {
101  // The user changed, so we'll have to set the collection to NULL after adding it to the queue
102  $collection_obj = null;
103  }
104 
105  if ($collection_obj === null) {
106  // For the first user or if the user changed, we'll create a new collection object
107  $collection_obj = new ReportDto((int) $row['user_id']);
108  $this->addCollectionObject($collection_obj);
109  }
110 
111  $folder_obj = $collection_obj->getFolderObjectById((int) $row['folder_id']);
112  if ($folder_obj === null) {
113  $folder_obj = new FolderDto((int) $row['folder_id'], $row['title']);
114  $collection_obj->addFolderObject($folder_obj);
115  }
116 
117  $orphaned_mail_obj = new MailDto(
118  (int) $row['mail_id'],
119  $row['m_subject']
120  );
121  $folder_obj->addMailObject($orphaned_mail_obj);
122  ++$i;
123  }
124  }
125 
126  private function existsCollectionObjForUserId(int $user_id): bool
127  {
128  return isset($this->collection[$user_id]);
129  }
130 
131  private function addCollectionObject(ReportDto $collection_obj): void
132  {
133  $this->collection[$collection_obj->getUserId()] = $collection_obj;
134  }
135 
139  public function getCollection(): array
140  {
141  return $this->collection;
142  }
143 }
$res
Definition: ltiservices.php:66
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct(private readonly ilMailCronOrphanedMails $job, ?ilDBInterface $db=null, ?ilSetting $settings=null, ?ClockInterface $clock=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22
Builds data types.
Definition: Factory.php:35