ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilBookCronNotification.php
Go to the documentation of this file.
1<?php
2
31
37{
38 protected \ILIAS\BookingManager\InternalRepoService $repo;
39 protected ilLanguage $lng;
44
45 public function __construct()
46 {
47 global $DIC;
48
49 $this->lng = $DIC->language();
50 $this->book_log = ilLoggerFactory::getLogger("book");
51 $this->repo = $DIC->bookingManager()
52 ->internal()
53 ->repo();
54 $this->access = $DIC->access();
55 $this->booking_access = $DIC->bookingManager()->internal()->domain()->access();
56 $this->uri_builder = $DIC['static_url']->builder();
57 }
58
59 public function getId(): string
60 {
61 return "book_notification";
62 }
63
64 public function getTitle(): string
65 {
67
68 $lng->loadLanguageModule("book");
69 return $lng->txt("book_notification");
70 }
71
72 public function getDescription(): string
73 {
75
76 $lng->loadLanguageModule("book");
77 return $lng->txt("book_notification_info");
78 }
79
81 {
82 return JobScheduleType::DAILY;
83 }
84
85 public function getDefaultScheduleValue(): ?int
86 {
87 return null;
88 }
89
90 public function hasAutoActivation(): bool
91 {
92 return false;
93 }
94
95 public function hasFlexibleSchedule(): bool
96 {
97 return false;
98 }
99
100 public function run(): JobResult
101 {
102 $status = JobResult::STATUS_NO_ACTION;
103
104 $count = $this->sendNotifications();
105
106 if ($count > 0) {
107 $status = JobResult::STATUS_OK;
108 }
109
110 $result = new JobResult();
111 $result->setStatus($status);
112
113 return $result;
114 }
115
116 protected function sendNotifications(): int
117 {
119
120 $log->debug("start");
121
122
123 $notifications = [];
124
125 /*
126 * pool id 123 > 2 days, ...
127 */
129 // determine reservations from max(next day $last_to_ts) up to "rmd_day" days + 1
130 // per pool id
131 $next_day_ts = mktime(0, 0, 0, date('n'), (int) date('j') + 1);
132 $log->debug("next day ts: " . $next_day_ts);
133 $last_reminder_to_ts = $p["last_remind_ts"];
134 // for debug purposes
135 // $last_reminder_to_ts-= 24*60*60;
136 $log->debug("last_reminder ts: " . $last_reminder_to_ts);
137 $from_ts = max($next_day_ts, $last_reminder_to_ts);
138 $log->debug("from ts: " . $from_ts);
139 $to_ts = mktime(0, 0, 0, date('n'), (int) date('j') + $p["reminder_day"] + 1);
140 $res = [];
141
142 // overwrite from to current time, see #26216, this ensures
143 // that all reservations are sent, some multiple times (each day)
144 // we include all reservations from now to the period set in the pool settings
145 $from_ts = time();
146
147 // additional logging info, see #26216
148 $log->debug("pool id: "
149 . $p["booking_pool_id"]
150 . "(" . ilObject::_lookupTitle($p["booking_pool_id"]) . ") "
151 . ", "
152 . date("Y-m-d, H:i:s", $from_ts)
153 . " to " . date("Y-m-d, H:i:s", $to_ts));
154
155
156 if ($to_ts > $from_ts) {
157 $repo = $this->repo->reservation();
158 $res = $repo->getListByDate(true, null, [
159 "from" => $from_ts,
160 "to" => $to_ts
161 ], [$p["booking_pool_id"]]);
162 }
163
164 $log->debug("reservations: " . count($res));
165
166 //var_dump($res); exit;
167
168 // get subscriber of pool id
170 $log->debug("users: " . count($user_ids));
171
172 // group by user, type, pool
173 foreach ($res as $r) {
174 // users
175 $log->debug("check notification of user id: " . $r["user_id"]);
176 if (in_array($r["user_id"], $user_ids)) {
177 if ($this->booking_access->canRetrieveNotificationsForOwnReservationsByObjId(
178 (int) $p["booking_pool_id"],
179 (int) $r["user_id"]
180 )) {
181 $log->debug("got read");
182 $notifications[$r["user_id"]]["personal"][$r["pool_id"]][] = $r;
183 }
184 }
185
186 // admins
187 foreach ($user_ids as $uid) {
188 $log->debug("check write for user id: " . $uid . ", pool: " . $p["booking_pool_id"]);
189
190 if ($this->booking_access->canRetrieveNotificationsForAllReservationsByObjId(
191 (int) $p["booking_pool_id"],
192 (int) $uid
193 )) {
194 $log->debug("got write");
195 $notifications[$uid]["admin"][$r["pool_id"]][] = $r;
196 }
197 }
198 }
199 ilObjBookingPool::writeLastReminderTimestamp($p["booking_pool_id"], $to_ts);
200 }
201
202 // send mails
203 $this->sendMails($notifications);
204
205 return count($notifications);
206 }
207
208 protected function sendMails(
209 array $notifications
210 ): void {
211 foreach ($notifications as $uid => $n) {
212 $ntf = new ilSystemNotification();
213 $lng = $ntf->getUserLanguage($uid);
214 $lng->loadLanguageModule("book");
215
216 $txt = $this->buildReminderBody($lng, $n, (int) $uid);
217 $ntf->setLangModules(array("book"));
218 $ntf->setSubjectLangId("book_booking_reminders");
219 $ntf->setIntroductionLangId("book_rem_intro");
220 $ntf->addAdditionalInfo("", $txt);
221 $ntf->setReasonLangId("book_rem_reason");
222 $this->book_log->debug("send Mail: " . $uid);
223 $ntf->sendMailAndReturnRecipients([$uid]);
224 }
225 }
226
227 protected function buildReminderBody(ilLanguage $language, array $notification, int $user_id): string
228 {
229 return (string) new ReminderMailText(
230 $this->personalSection($language, $notification['personal'] ?? null, $user_id),
231 $this->adminSection($language, $notification['admin'] ?? null, $user_id)
232 );
233 }
234
235 private function personalSection(ilLanguage $language, mixed $pools, int $user_id): ?ReminderSection
236 {
237 if (!is_array($pools)) {
238 return null;
239 }
240
241 $blocks = [];
242 foreach ($pools as $pool_id => $reservations) {
243 $lines = [];
244 foreach ($reservations as $reservation) {
245 $lines[] = new PersonalReservation(
246 (string) $reservation['title'],
247 (int) $reservation['counter'],
248 ilDatePresentation::formatDate(new ilDate((string) $reservation['date'], IL_CAL_DATE))
249 . ', ' . $reservation['slot']
250 );
251 }
252 $blocks[] = $this->poolBlock((int) $pool_id, $user_id, $lines);
253 }
254
255 return new ReminderSection($language->txt('book_your_reservations'), $blocks);
256 }
257
258 private function adminSection(ilLanguage $language, mixed $pools, int $user_id): ?ReminderSection
259 {
260 if (!is_array($pools)) {
261 return null;
262 }
263
264 $blocks = [];
265 foreach ($pools as $pool_id => $reservations) {
266 $lines = [];
267 foreach ($reservations as $reservation) {
268 $lines[] = new AdminReservation(
269 (string) $reservation['title'],
270 (int) $reservation['counter'],
271 (string) $reservation['user_name'],
272 ilDatePresentation::formatDate(new ilDate((string) $reservation['date'], IL_CAL_DATE))
273 . ', ' . $reservation['slot'],
274 $reservation['message'] != ''
275 ? $language->txt('book_message') . ': ' . $reservation['message']
276 : ''
277 );
278 }
279 $blocks[] = $this->poolBlock((int) $pool_id, $user_id, $lines);
280 }
281
282 return new ReminderSection($language->txt('book_reservation_overview'), $blocks);
283 }
284
288 private function poolBlock(int $pool_id, int $user_id, array $reservations): PoolReservations
289 {
290 return new PoolReservations(
291 ilObject::_lookupTitle($pool_id),
292 $this->poolGotoLink($pool_id, $user_id),
293 $reservations
294 );
295 }
296
297 private function poolGotoLink(int $pool_obj_id, int $user_id): string
298 {
299 foreach (ilObject::_getAllReferences($pool_obj_id) as $ref_id) {
300 if ($this->access->checkAccessOfUser($user_id, 'read', '', $ref_id)) {
301 return (string) $this->uri_builder->build('book', new ReferenceId((int) $ref_id));
302 }
303 }
304
305 return '';
306 }
307}
const IL_CAL_DATE
Cron for booking manager notification.
adminSection(ilLanguage $language, mixed $pools, int $user_id)
personalSection(ilLanguage $language, mixed $pools, int $user_id)
ILIAS BookingManager InternalRepoService $repo
hasAutoActivation()
Is to be activated on "installation", does only work for ILIAS core cron jobs.
buildReminderBody(ilLanguage $language, array $notification, int $user_id)
poolBlock(int $pool_id, int $user_id, array $reservations)
poolGotoLink(int $pool_obj_id, int $user_id)
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|LanguageIdentifier $a_topic, ?int $n=null)
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 _getAllReferences(int $id)
get all reference ids for object ID
static _lookupTitle(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
foreach( $report as $row)
Combines all available interfaces which can be called via global $ilAccess.
$log
Definition: ltiauth.php:50
foreach($_POST as $key=> $value) $res
global $DIC
Definition: shib_login.php:26