ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilBookCronNotification.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
13{
17 protected $lng;
18
22 protected $access;
23
27 protected $book_log;
28
32 public function __construct()
33 {
34 global $DIC;
35
36 $this->lng = $DIC->language();
37 if (isset($DIC["ilAccess"])) {
38 $this->access = $DIC->access();
39 }
40
41 $this->book_log = ilLoggerFactory::getLogger("book");
42 }
43
44 public function getId()
45 {
46 return "book_notification";
47 }
48
49 public function getTitle()
50 {
52
53 $lng->loadLanguageModule("book");
54 return $lng->txt("book_notification");
55 }
56
57 public function getDescription()
58 {
60
61 $lng->loadLanguageModule("book");
62 return $lng->txt("book_notification_info");
63 }
64
65 public function getDefaultScheduleType()
66 {
68 }
69
70 public function getDefaultScheduleValue()
71 {
72 return;
73 }
74
75 public function hasAutoActivation()
76 {
77 return false;
78 }
79
80 public function hasFlexibleSchedule()
81 {
82 return false;
83 }
84
85 public function run()
86 {
88
89 $count = $this->sendNotifications();
90
91 if ($count > 0) {
93 }
94
96 $result->setStatus($status);
97
98 return $result;
99 }
100
107 protected function sendNotifications()
108 {
110
111 // get all booking pools with notification setting
112
113
114
115 $log->debug("start");
116
117
118 $notifications = [];
119
120 /*
121 * pool id 123 > 2 days, ...
122 */
124 // determine reservations from max(next day $last_to_ts) up to "rmd_day" days + 1
125 // per pool id
126 $next_day_ts = mktime(0, 0, 0, date('n'), date('j') + 1);
127 $log->debug("next day ts: " . $next_day_ts);
128 $last_reminder_to_ts = $p["last_remind_ts"];
129 // for debug purposes
130 // $last_reminder_to_ts-= 24*60*60;
131 $log->debug("last_reminder ts: " . $last_reminder_to_ts);
132 $from_ts = max($next_day_ts, $last_reminder_to_ts);
133 $log->debug("from ts: " . $from_ts);
134 $to_ts = mktime(0, 0, 0, date('n'), date('j') + $p["reminder_day"] + 1);
135 $res = [];
136
137 // overwrite from to current time, see #26216, this ensures
138 // that all reservations are sent, some multiple times (each day)
139 // we include all reservations from now to the period set in the pool settings
140 $from_ts = time();
141
142 // additional logging info, see #26216
143 $log->debug("pool id: "
144 . $p["booking_pool_id"]
145 . "(" . ilObject::_lookupTitle($p["booking_pool_id"]) . ") "
146 . ", "
147 . date("Y-m-d, H:i:s", $from_ts)
148 . " to " . date("Y-m-d, H:i:s", $to_ts));
149
150
151 if ($to_ts > $from_ts) {
153 $repo = $f->getRepo();
154 $res = $repo->getListByDate(true, null, [
155 "from" => $from_ts,
156 "to" => $to_ts
157 ], [$p["booking_pool_id"]]);
158 }
159
160 $log->debug("reservations: " . count($res));
161
162 //var_dump($res); exit;
163
164 // get subscriber of pool id
166
167 $log->debug("users: " . count($user_ids));
168
169 // group by user, type, pool
170 foreach ($res as $r) {
171
172 // users
173 $log->debug("check notification of user id: " . $r["user_id"]);
174 if (in_array($r["user_id"], $user_ids)) {
175 if ($this->checkAccess("read", $r["user_id"], $p["booking_pool_id"])) {
176 $log->debug("got read");
177 $notifications[$r["user_id"]]["personal"][$r["pool_id"]][] = $r;
178 }
179 }
180
181 // admins
182 foreach ($user_ids as $uid) {
183 $log->debug("check write for user id: " . $uid . ", pool: " . $p["booking_pool_id"]);
184
185 if ($this->checkAccess("write", $uid, $p["booking_pool_id"])) {
186 $log->debug("got write");
187 $notifications[$uid]["admin"][$r["pool_id"]][] = $r;
188 }
189 }
190 }
191 ilObjBookingPool::writeLastReminderTimestamp($p["booking_pool_id"], $to_ts);
192 }
193
194 $log->debug("notifications to users: " . count($notifications));
195
196 // send mails
197 $this->sendMails($notifications);
198
199 return count($notifications);
200 }
201
207 protected function sendMails($notifications)
208 {
209 foreach ($notifications as $uid => $n) {
210 $ntf = new ilSystemNotification();
211 $lng = $ntf->getUserLanguage($uid);
212 $lng->loadLanguageModule("book");
213
214 $txt = "";
215 if (is_array($n["personal"])) {
216 $txt .= "\n" . $lng->txt("book_your_reservations") . "\n";
217 $txt .= "-----------------------------------------\n";
218 foreach ($n["personal"] as $obj_id => $reservs) {
219 $txt .= ilObject::_lookupTitle($obj_id) . ":\n";
220 foreach ($reservs as $r) {
221 $txt .= "- " . $r["title"] . " (" . $r["counter"] . "), " .
222 ilDatePresentation::formatDate(new ilDate($r["date"], IL_CAL_DATE)) . ", " .
223 $r["slot"] . "\n";
224 }
225 }
226 }
227
228 if (is_array($n["admin"])) {
229 $txt .= "\n" . $lng->txt("book_reservation_overview") . "\n";
230 $txt .= "-----------------------------------------\n";
231 foreach ($n["admin"] as $obj_id => $reservs) {
232 $txt .= ilObject::_lookupTitle($obj_id) . ":\n";
233 foreach ($reservs as $r) {
234 $txt .= "- " . $r["title"] . " (" . $r["counter"] . "), " . $r["user_name"] . ", " .
235 ilDatePresentation::formatDate(new ilDate($r["date"], IL_CAL_DATE)) . ", " .
236 $r["slot"] . "\n";
237 }
238 }
239 }
240
241 $ntf->setLangModules(array("book"));
242 $ntf->setSubjectLangId("book_booking_reminders");
243 $ntf->setIntroductionLangId("book_rem_intro");
244 $ntf->addAdditionalInfo("", $txt);
245 $ntf->setReasonLangId("book_rem_reason");
246 $ntf->sendMail(array($uid));
247 }
248 }
249
250
257 protected function checkAccess($perm, $uid, $obj_id)
258 {
260 foreach (ilObject::_getAllReferences($obj_id) as $ref_id) {
261 if ($access->checkAccessOfUser($uid, $perm, "", $ref_id)) {
262 return true;
263 }
264 }
265 return false;
266 }
267
268
274 protected function sendUserNotifications($res)
275 {
276 /*
277 * Your reservations for tomorrow
278 *
279 * Pool Title
280 * Pool Link
281 * - Object (cnt), From - To
282 * - ...
283 *
284 * Reservations for tomorrow
285 *
286 * Pool Title
287 * Pool Link
288 * - Object (cnt), From - To
289 * - ...
290 *
291 */
292 }
293
299 protected function sendAdminNotifications($res)
300 {
301 }
302}
$result
$n
Definition: RandomTest.php:85
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATE
Cron for booking manager notification.
checkAccess($perm, $uid, $obj_id)
check access on obj id
hasFlexibleSchedule()
Can the schedule be configured?
sendUserNotifications($res)
Send user notifications.
getDefaultScheduleType()
Get schedule type.
hasAutoActivation()
Is to be activated on "installation".
sendAdminNotifications($res)
Send admin notifications.
sendMails($notifications)
Send mails.
getDefaultScheduleValue()
Get schedule value.
Cron job result data container.
Cron job application base class.
const SCHEDULE_TYPE_DAILY
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
Class for single dates.
static getLogger($a_component_id)
Get component logger.
static getNotificationsForObject($type, $id, $page_id=null, $ignore_threshold=false)
Get all users for given object.
static writeLastReminderTimestamp($a_obj_id, $a_ts)
Write last reminder timestamp.
static getPoolsWithReminders()
Get poos with reminders.
static _lookupTitle($a_id)
lookup object title
static _getAllReferences($a_id)
get all reference ids of object
Wrapper classes for system notifications.
$txt
Definition: error.php:13
$log
Definition: result.php:15
foreach($_POST as $key=> $value) $res
$DIC
Definition: xapitoken.php:46