ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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;
30  protected ilLogger $book_log;
31 
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  $this->repo = $DIC->bookingManager()
43  ->internal()
44  ->repo();
45  }
46 
47  public function getId(): string
48  {
49  return "book_notification";
50  }
51 
52  public function getTitle(): string
53  {
54  $lng = $this->lng;
55 
56  $lng->loadLanguageModule("book");
57  return $lng->txt("book_notification");
58  }
59 
60  public function getDescription(): string
61  {
62  $lng = $this->lng;
63 
64  $lng->loadLanguageModule("book");
65  return $lng->txt("book_notification_info");
66  }
67 
69  {
70  return CronJobScheduleType::SCHEDULE_TYPE_DAILY;
71  }
72 
73  public function getDefaultScheduleValue(): ?int
74  {
75  return null;
76  }
77 
78  public function hasAutoActivation(): bool
79  {
80  return false;
81  }
82 
83  public function hasFlexibleSchedule(): bool
84  {
85  return false;
86  }
87 
88  public function run(): ilCronJobResult
89  {
91 
92  $count = $this->sendNotifications();
93 
94  if ($count > 0) {
96  }
97 
98  $result = new ilCronJobResult();
99  $result->setStatus($status);
100 
101  return $result;
102  }
103 
104  protected function sendNotifications(): int
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 ($this->checkAccess("read", $r["user_id"], $p["booking_pool_id"])) {
166  $log->debug("got read");
167  $notifications[$r["user_id"]]["personal"][$r["pool_id"]][] = $r;
168  }
169  }
170 
171  // admins
172  foreach ($user_ids as $uid) {
173  $log->debug("check write for user id: " . $uid . ", pool: " . $p["booking_pool_id"]);
174 
175  if ($this->checkAccess("write", $uid, $p["booking_pool_id"])) {
176  $log->debug("got write");
177  $notifications[$uid]["admin"][$r["pool_id"]][] = $r;
178  }
179  }
180  }
181  ilObjBookingPool::writeLastReminderTimestamp($p["booking_pool_id"], $to_ts);
182  }
183 
184  // send mails
185  $this->sendMails($notifications);
186 
187  return count($notifications);
188  }
189 
190  protected function sendMails(
191  array $notifications
192  ): void {
193  foreach ($notifications as $uid => $n) {
194  $ntf = new ilSystemNotification();
195  $lng = $ntf->getUserLanguage($uid);
196  $lng->loadLanguageModule("book");
197 
198  $txt = "";
199  if (is_array($n["personal"] ?? null)) {
200  $txt .= "\n" . $lng->txt("book_your_reservations") . "\n";
201  $txt .= "-----------------------------------------\n";
202  foreach ($n["personal"] as $obj_id => $reservs) {
203  $txt .= ilObject::_lookupTitle($obj_id) . ":\n";
204  foreach ($reservs as $r) {
205  $txt .= "- " . $r["title"] . " (" . $r["counter"] . "), " .
206  ilDatePresentation::formatDate(new ilDate($r["date"], IL_CAL_DATE)) . ", " .
207  $r["slot"] . "\n";
208  }
209  }
210  }
211 
212  if (is_array($n["admin"] ?? null)) {
213  $txt .= "\n" . $lng->txt("book_reservation_overview") . "\n";
214  $txt .= "-----------------------------------------\n";
215  foreach ($n["admin"] as $obj_id => $reservs) {
216  $txt .= ilObject::_lookupTitle($obj_id) . ":\n";
217  foreach ($reservs as $r) {
218  $txt .= "- " . $r["title"] . " (" . $r["counter"] . "), " . $r["user_name"] . ", " .
219  ilDatePresentation::formatDate(new ilDate($r["date"], IL_CAL_DATE)) . ", " .
220  $r["slot"] . "\n";
221  if ($r["message"] != "") {
222  $txt .= " " . $lng->txt("book_message") .
223  ": " . $r["message"];
224  }
225  }
226  }
227  }
228  $ntf->setLangModules(array("book"));
229  $ntf->setSubjectLangId("book_booking_reminders");
230  $ntf->setIntroductionLangId("book_rem_intro");
231  $ntf->addAdditionalInfo("", $txt);
232  $ntf->setReasonLangId("book_rem_reason");
233  $this->book_log->debug("send Mail: " . $uid);
234  $ntf->sendMailAndReturnRecipients([$uid]);
235  }
236  }
237 
238 
239  // check access on obj id
240  protected function checkAccess(
241  string $perm,
242  int $uid,
243  int $obj_id
244  ): bool {
245  $access = $this->access;
246  foreach (ilObject::_getAllReferences($obj_id) as $ref_id) {
247  if ($access->checkAccessOfUser($uid, $perm, "", $ref_id)) {
248  return true;
249  }
250  }
251  return false;
252  }
253 }
$res
Definition: ltiservices.php:69
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.
static _getAllReferences(int $id)
get all reference ids for object ID
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
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.
global $DIC
Definition: feed.php:28
static writeLastReminderTimestamp(int $a_obj_id, int $a_ts)
$ref_id
Definition: ltiauth.php:67
static _lookupTitle(int $obj_id)
$log
Definition: result.php:33
final const STATUS_NO_ACTION
Cron for booking manager notification.
$txt
Definition: error.php:14
ILIAS BookingManager InternalRepoService $repo
const IL_CAL_DATE
checkAccess(string $perm, int $uid, int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
checkAccessOfUser(int $a_user_id, string $a_permission, string $a_cmd, int $a_ref_id, string $a_type="", ?int $a_obj_id=null, ?int $a_tree_id=null)
check access for an object (provide $a_type and $a_obj_id if available for better performance) ...
$r