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