ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilBookCronNotification.php
Go to the documentation of this file.
1 <?php
2 
20 
26 {
27  protected \ILIAS\BookingManager\InternalRepoService $repo;
28  protected ilLanguage $lng;
29  protected ilLogger $book_log;
30 
31  public function __construct()
32  {
33  global $DIC;
34 
35  $this->lng = $DIC->language();
36 
37  $this->book_log = ilLoggerFactory::getLogger("book");
38  $this->repo = $DIC->bookingManager()
39  ->internal()
40  ->repo();
41  }
42 
43  public function getId(): string
44  {
45  return "book_notification";
46  }
47 
48  public function getTitle(): string
49  {
50  $lng = $this->lng;
51 
52  $lng->loadLanguageModule("book");
53  return $lng->txt("book_notification");
54  }
55 
56  public function getDescription(): string
57  {
58  $lng = $this->lng;
59 
60  $lng->loadLanguageModule("book");
61  return $lng->txt("book_notification_info");
62  }
63 
65  {
66  return CronJobScheduleType::SCHEDULE_TYPE_DAILY;
67  }
68 
69  public function getDefaultScheduleValue(): ?int
70  {
71  return null;
72  }
73 
74  public function hasAutoActivation(): bool
75  {
76  return false;
77  }
78 
79  public function hasFlexibleSchedule(): bool
80  {
81  return false;
82  }
83 
84  public function run(): ilCronJobResult
85  {
87 
88  $count = $this->sendNotifications();
89 
90  if ($count > 0) {
92  }
93 
94  $result = new ilCronJobResult();
95  $result->setStatus($status);
96 
97  return $result;
98  }
99 
100  protected function sendNotifications(): int
101  {
102  global $DIC;
103 
104  $access = $DIC->bookingManager()->internal()->domain()->access();
105 
107 
108  $log->debug("start");
109 
110 
111  $notifications = [];
112 
113  /*
114  * pool id 123 > 2 days, ...
115  */
116  foreach (ilObjBookingPool::getPoolsWithReminders() as $p) {
117  // determine reservations from max(next day $last_to_ts) up to "rmd_day" days + 1
118  // per pool id
119  $next_day_ts = mktime(0, 0, 0, date('n'), (int) date('j') + 1);
120  $log->debug("next day ts: " . $next_day_ts);
121  $last_reminder_to_ts = $p["last_remind_ts"];
122  // for debug purposes
123  // $last_reminder_to_ts-= 24*60*60;
124  $log->debug("last_reminder ts: " . $last_reminder_to_ts);
125  $from_ts = max($next_day_ts, $last_reminder_to_ts);
126  $log->debug("from ts: " . $from_ts);
127  $to_ts = mktime(0, 0, 0, date('n'), (int) date('j') + $p["reminder_day"] + 1);
128  $res = [];
129 
130  // overwrite from to current time, see #26216, this ensures
131  // that all reservations are sent, some multiple times (each day)
132  // we include all reservations from now to the period set in the pool settings
133  $from_ts = time();
134 
135  // additional logging info, see #26216
136  $log->debug("pool id: "
137  . $p["booking_pool_id"]
138  . "(" . ilObject::_lookupTitle($p["booking_pool_id"]) . ") "
139  . ", "
140  . date("Y-m-d, H:i:s", $from_ts)
141  . " to " . date("Y-m-d, H:i:s", $to_ts));
142 
143 
144  if ($to_ts > $from_ts) {
145  $repo = $this->repo->reservation();
146  $res = $repo->getListByDate(true, null, [
147  "from" => $from_ts,
148  "to" => $to_ts
149  ], [$p["booking_pool_id"]]);
150  }
151 
152  $log->debug("reservations: " . count($res));
153 
154  //var_dump($res); exit;
155 
156  // get subscriber of pool id
157  $user_ids = ilNotification::getNotificationsForObject(ilNotification::TYPE_BOOK, $p["booking_pool_id"]);
158  $log->debug("users: " . count($user_ids));
159 
160  // group by user, type, pool
161  foreach ($res as $r) {
162  // users
163  $log->debug("check notification of user id: " . $r["user_id"]);
164  if (in_array($r["user_id"], $user_ids)) {
165  if ($access->canRetrieveNotificationsForOwnReservationsByObjId(
166  (int) $p["booking_pool_id"],
167  (int) $r["user_id"]
168  )) {
169  $log->debug("got read");
170  $notifications[$r["user_id"]]["personal"][$r["pool_id"]][] = $r;
171  }
172  }
173 
174  // admins
175  foreach ($user_ids as $uid) {
176  $log->debug("check write for user id: " . $uid . ", pool: " . $p["booking_pool_id"]);
177 
178  if ($access->canRetrieveNotificationsForAllReservationsByObjId(
179  (int) $p["booking_pool_id"],
180  (int) $r["user_id"]
181  )) {
182  $log->debug("got write");
183  $notifications[$uid]["admin"][$r["pool_id"]][] = $r;
184  }
185  }
186  }
187  ilObjBookingPool::writeLastReminderTimestamp($p["booking_pool_id"], $to_ts);
188  }
189 
190  // send mails
191  $this->sendMails($notifications);
192 
193  return count($notifications);
194  }
195 
196  protected function sendMails(
197  array $notifications
198  ): void {
199  foreach ($notifications as $uid => $n) {
200  $ntf = new ilSystemNotification();
201  $lng = $ntf->getUserLanguage($uid);
202  $lng->loadLanguageModule("book");
203 
204  $txt = "";
205  if (is_array($n["personal"] ?? null)) {
206  $txt .= "\n" . $lng->txt("book_your_reservations") . "\n";
207  $txt .= "-----------------------------------------\n";
208  foreach ($n["personal"] as $obj_id => $reservs) {
209  $txt .= ilObject::_lookupTitle($obj_id) . ":\n";
210  foreach ($reservs as $r) {
211  $txt .= "- " . $r["title"] . " (" . $r["counter"] . "), " .
212  ilDatePresentation::formatDate(new ilDate($r["date"], IL_CAL_DATE)) . ", " .
213  $r["slot"] . "\n";
214  }
215  }
216  }
217 
218  if (is_array($n["admin"] ?? null)) {
219  $txt .= "\n" . $lng->txt("book_reservation_overview") . "\n";
220  $txt .= "-----------------------------------------\n";
221  foreach ($n["admin"] as $obj_id => $reservs) {
222  $txt .= ilObject::_lookupTitle($obj_id) . ":\n";
223  foreach ($reservs as $r) {
224  $txt .= "- " . $r["title"] . " (" . $r["counter"] . "), " . $r["user_name"] . ", " .
225  ilDatePresentation::formatDate(new ilDate($r["date"], IL_CAL_DATE)) . ", " .
226  $r["slot"] . "\n";
227  if ($r["message"] != "") {
228  $txt .= " " . $lng->txt("book_message") .
229  ": " . $r["message"];
230  }
231  }
232  }
233  }
234  $ntf->setLangModules(array("book"));
235  $ntf->setSubjectLangId("book_booking_reminders");
236  $ntf->setIntroductionLangId("book_rem_intro");
237  $ntf->addAdditionalInfo("", $txt);
238  $ntf->setReasonLangId("book_rem_reason");
239  $this->book_log->debug("send Mail: " . $uid);
240  $ntf->sendMailAndReturnRecipients([$uid]);
241  }
242  }
243 
244 
245 }
$res
Definition: ltiservices.php:69
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ilObjUser $user=null,)
static getLogger(string $a_component_id)
Get component logger.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
getUserLanguage()
Return language of user.
loadLanguageModule(string $a_module)
Load language module.
static getPoolsWithReminders()
Get pools with reminders.
static getNotificationsForObject(int $type, int $id, ?int $page_id=null, bool $ignore_threshold=false)
Get all users/recipients for given object.
static writeLastReminderTimestamp(int $a_obj_id, int $a_ts)
static _lookupTitle(int $obj_id)
final const STATUS_NO_ACTION
Cron for booking manager notification.
global $DIC
Definition: shib_login.php:25
$log
Definition: ltiresult.php:34
$txt
Definition: error.php:30
ILIAS BookingManager InternalRepoService $repo
const IL_CAL_DATE
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$r