ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
5include_once "Services/Cron/classes/class.ilCronJob.php";
6
14{
18 protected $lng;
19
23 protected $access;
24
28 protected $book_log;
29
33 public function __construct()
34 {
35 global $DIC;
36
37 $this->lng = $DIC->language();
38 if (isset($DIC["ilAccess"])) {
39 $this->access = $DIC->access();
40 }
41
42 $this->book_log = ilLoggerFactory::getLogger("book");
43 }
44
45 public function getId()
46 {
47 return "book_notification";
48 }
49
50 public function getTitle()
51 {
53
54 $lng->loadLanguageModule("book");
55 return $lng->txt("book_notification");
56 }
57
58 public function getDescription()
59 {
61
62 $lng->loadLanguageModule("book");
63 return $lng->txt("book_notification_info");
64 }
65
66 public function getDefaultScheduleType()
67 {
69 }
70
71 public function getDefaultScheduleValue()
72 {
73 return;
74 }
75
76 public function hasAutoActivation()
77 {
78 return false;
79 }
80
81 public function hasFlexibleSchedule()
82 {
83 return false;
84 }
85
86 public function run()
87 {
89
90 $count = $this->sendNotifications();
91
92 if ($count > 0) {
94 }
95
97 $result->setStatus($status);
98
99 return $result;
100 }
101
108 protected function sendNotifications()
109 {
111
112 // get all booking pools with notification setting
113
114
115
116 $log->debug("start");
117
118
119 $notifications = [];
120
121 /*
122 * pool id 123 > 2 days, ...
123 */
125 // determine reservations from max(next day $last_to_ts) up to "rmd_day" days + 1
126 // per pool id
127 $next_day_ts = mktime(0, 0, 0, date('n'), date('j') + 1);
128 $log->debug("next day ts: " . $next_day_ts);
129 $last_reminder_to_ts = $p["last_remind_ts"];
130 // for debug purposes
131 // $last_reminder_to_ts-= 24*60*60;
132 $log->debug("last_reminder ts: " . $last_reminder_to_ts);
133 $from_ts = max($next_day_ts, $last_reminder_to_ts);
134 $log->debug("from ts: " . $from_ts);
135 $to_ts = mktime(0, 0, 0, date('n'), date('j') + $p["reminder_day"] + 1);
136 $res = [];
137
138 // overwrite from to current time, see #26216, this ensures
139 // that all reservations are sent, some multiple times (each day)
140 // we include all reservations from now to the period set in the pool settings
141 $from_ts = time();
142
143 // additional logging info, see #26216
144 $log->debug("pool id: "
145 . $p["booking_pool_id"]
146 . "(" . ilObject::_lookupTitle($p["booking_pool_id"]) . ") "
147 . ", "
148 . date("Y-m-d, H:i:s", $from_ts)
149 . " to " . date("Y-m-d, H:i:s", $to_ts));
150
151
152 if ($to_ts > $from_ts) {
154 "from" => $from_ts,
155 "to" => $to_ts
156 ], [$p["booking_pool_id"]]);
157 }
158
159 $log->debug("reservations: " . count($res));
160
161 //var_dump($res); exit;
162
163 // get subscriber of pool id
165
166 $log->debug("users: " . count($user_ids));
167
168 // group by user, type, pool
169 foreach ($res as $r) {
170
171 // users
172 $log->debug("check notification of user id: " . $r["user_id"]);
173 if (in_array($r["user_id"], $user_ids)) {
174 if ($this->checkAccess("read", $r["user_id"], $p["booking_pool_id"])) {
175 $log->debug("got read");
176 $notifications[$r["user_id"]]["personal"][$r["pool_id"]][] = $r;
177 }
178 }
179
180 // admins
181 foreach ($user_ids as $uid) {
182 $log->debug("check write for user id: " . $uid . ", pool: " . $p["booking_pool_id"]);
183
184 if ($this->checkAccess("write", $uid, $p["booking_pool_id"])) {
185 $log->debug("got write");
186 $notifications[$uid]["admin"][$r["pool_id"]][] = $r;
187 }
188 }
189 }
190 ilObjBookingPool::writeLastReminderTimestamp($p["booking_pool_id"], $to_ts);
191 }
192
193 $log->debug("notifications to users: " . count($notifications));
194
195 // send mails
196 $this->sendMails($notifications);
197
198 return count($notifications);
199 }
200
206 protected function sendMails($notifications)
207 {
208 foreach ($notifications as $uid => $n) {
209 include_once "./Services/Notification/classes/class.ilSystemNotification.php";
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"] . "), " .
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"] . ", " .
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.
static getListByDate( $a_has_schedule, array $a_object_ids=null, array $filter=null, array $a_pool_ids=null)
List all reservations by date.
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:11
$r
Definition: example_031.php:79
$log
Definition: sabredav.php:21
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res