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