ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilNotification.php
Go to the documentation of this file.
1<?php
2
23{
24 public const THRESHOLD = 180; // time between mails in minutes
25 public const TYPE_EXERCISE_SUBMISSION = 1;
26 public const TYPE_WIKI = 2;
27 public const TYPE_WIKI_PAGE = 3;
28 public const TYPE_BLOG = 4;
29 public const TYPE_DATA_COLLECTION = 5;
30 public const TYPE_POLL = 6;
31 public const TYPE_LM_BLOCKED_USERS = 7;
32 public const TYPE_BOOK = 8;
33 public const TYPE_LM = 9;
34 public const TYPE_LM_PAGE = 10;
35
39 public static function hasNotification(
40 int $type,
41 int $user_id,
42 int $id
43 ): bool {
44 global $DIC;
45
46 $ilDB = $DIC->database();
47 $tree = $DIC->repositoryTree();
48
49 $notification = false;
50
51 $setting = new ilObjNotificationSettings($id);
53 // check membership, members should be notidifed...
55 $grp_ref_id = $tree->checkForParentType($ref_id, 'grp');
56 if (($grp_ref_id > 0) && ilGroupParticipants::_isParticipant($grp_ref_id, $user_id)) {
57 $notification = true;
58 }
59 $crs_ref_id = $tree->checkForParentType($ref_id, 'crs');
60 if (($crs_ref_id > 0) && ilCourseParticipants::_isParticipant($crs_ref_id, $user_id)) {
61 $notification = true;
62 }
63 }
64
65 if ($notification && $setting->getMode() === ilObjNotificationSettings::MODE_DEF_ON_OPT_OUT) {
66 $set = $ilDB->query("SELECT user_id FROM notification" .
67 " WHERE type = " . $ilDB->quote($type, "integer") .
68 " AND user_id = " . $ilDB->quote($user_id, "integer") .
69 " AND id = " . $ilDB->quote($id, "integer") .
70 " AND activated = " . $ilDB->quote(0, "integer"));
71 $rec = $ilDB->fetchAssoc($set);
72 // if there is no user record, take the default value
73 if (!isset($rec["user_id"])) {
74 return $notification;
75 }
76 // ... except when the opted out
77 return isset($rec["user_id"]) && ((int) $rec["user_id"] !== $user_id);
78 }
79
80 if ($notification && $setting->getMode() === ilObjNotificationSettings::MODE_DEF_ON_NO_OPT_OUT) {
81 return true;
82 }
83 }
84
85
86 $set = $ilDB->query("SELECT user_id FROM notification" .
87 " WHERE type = " . $ilDB->quote($type, "integer") .
88 " AND user_id = " . $ilDB->quote($user_id, "integer") .
89 " AND id = " . $ilDB->quote($id, "integer") .
90 " AND activated = " . $ilDB->quote(1, "integer"));
91
92 return (bool) $ilDB->numRows($set);
93 }
94
98 public static function hasOptOut(int $obj_id): bool
99 {
100 $setting = new ilObjNotificationSettings($obj_id);
101 return $setting->getMode() !== ilObjNotificationSettings::MODE_DEF_ON_NO_OPT_OUT;
102 }
103
107 public static function getNotificationsForObject(
108 int $type,
109 int $id,
110 ?int $page_id = null,
111 bool $ignore_threshold = false
112 ): array {
113 global $DIC;
114
115 $ilDB = $DIC->database();
116 $tree = $DIC->repositoryTree();
117
118
120 $log->debug("Get notifications for type " . $type . ", id " . $id . ", page_id " . $page_id .
121 ", ignore threshold " . $ignore_threshold);
122
123 // currently done for blog
124 $recipients = array();
125 $setting = new ilObjNotificationSettings($id);
128 $grp_ref_id = $tree->checkForParentType($ref_id, 'grp');
129 if ($grp_ref_id > 0) {
131 foreach ($p->getMembers() as $user_id) {
132 if (!in_array($user_id, $recipients)) {
133 $recipients[$user_id] = $user_id;
134 }
135 }
136 }
137 $crs_ref_id = $tree->checkForParentType($ref_id, 'crs');
138 if ($crs_ref_id > 0) {
140 foreach ($p->getMembers() as $user_id) {
141 if (!in_array($user_id, $recipients)) {
142 $recipients[$user_id] = $user_id;
143 }
144 }
145 }
146 }
147 }
148
149 $log->debug("Step 1 recipients: " . print_r($recipients, true));
150
151 // remove all users that deactivated the feature
152 if ($setting->getMode() === ilObjNotificationSettings::MODE_DEF_ON_OPT_OUT) {
153 $sql = "SELECT user_id FROM notification" .
154 " WHERE type = " . $ilDB->quote($type, "integer") .
155 " AND id = " . $ilDB->quote($id, "integer") .
156 " AND activated = " . $ilDB->quote(0, "integer") .
157 " AND " . $ilDB->in("user_id", $recipients, false, "integer");
158 $set = $ilDB->query($sql);
159 while ($rec = $ilDB->fetchAssoc($set)) {
160 unset($recipients[$rec["user_id"]]);
161 $log->debug("Remove due to deactivation: " . $rec["user_id"]);
162 }
163 }
164
165 // get single subscriptions
166 if ($setting->getMode() !== ilObjNotificationSettings::MODE_DEF_ON_NO_OPT_OUT) {
167 $sql = "SELECT user_id FROM notification" .
168 " WHERE type = " . $ilDB->quote($type, "integer") .
169 " AND id = " . $ilDB->quote($id, "integer") .
170 " AND activated = " . $ilDB->quote(1, "integer");
171 if (!$ignore_threshold) {
172 $sql .= " AND (last_mail < " . $ilDB->quote(date(
173 "Y-m-d H:i:s",
174 strtotime("-" . self::THRESHOLD . "minutes")
175 ), "timestamp") .
176 " OR last_mail IS NULL";
177 if ($page_id) {
178 $sql .= " OR page_id <> " . $ilDB->quote($page_id, "integer");
179 }
180 $sql .= ")";
181 }
182 $set = $ilDB->query($sql);
183 while ($row = $ilDB->fetchAssoc($set)) {
184 $recipients[$row["user_id"]] = $row["user_id"];
185 $log->debug("Adding single subscription: " . $row["user_id"]);
186 }
187 }
188
189 // remove all users that got a mail
190 // see #22773
191 //if ($setting->getMode() !== ilObjNotificationSettings::MODE_DEF_OFF_USER_ACTIVATION && !$ignore_threshold) {
192 if (!$ignore_threshold) {
193 $sql = "SELECT user_id FROM notification" .
194 " WHERE type = " . $ilDB->quote($type, "integer") .
195 " AND id = " . $ilDB->quote($id, "integer") .
196 " AND " . $ilDB->in("user_id", $recipients, false, "integer");
197 $sql .= " AND (last_mail > " . $ilDB->quote(date(
198 "Y-m-d H:i:s",
199 strtotime("-" . self::THRESHOLD . "minutes")
200 ), "timestamp");
201 if ($page_id) {
202 $sql .= " AND page_id = " . $ilDB->quote($page_id, "integer");
203 }
204 $sql .= ")";
205 $set = $ilDB->query($sql);
206 while ($rec = $ilDB->fetchAssoc($set)) {
207 unset($recipients[$rec["user_id"]]);
208 $log->debug("Remove due to got mail: " . $rec["user_id"]);
209 }
210
211 if ($type === self::TYPE_WIKI) {
212 $sql = "SELECT user_id FROM notification" .
213 " WHERE type = " . $ilDB->quote(self::TYPE_WIKI_PAGE, "integer") .
214 " AND id = " . $ilDB->quote($page_id, "integer") .
215 " AND " . $ilDB->in("user_id", $recipients, false, "integer");
216 $sql .= " AND (last_mail > " . $ilDB->quote(date(
217 "Y-m-d H:i:s",
218 strtotime("-" . self::THRESHOLD . "minutes")
219 ), "timestamp");
220 $sql .= ")";
221 $set = $ilDB->query($sql);
222 while ($rec = $ilDB->fetchAssoc($set)) {
223 unset($recipients[$rec["user_id"]]);
224 $log->debug("Remove due to got mail: " . $rec["user_id"]);
225 }
226 }
227 }
228
229
230 return $recipients;
231 }
232
236 public static function setNotification(
237 int $type,
238 int $user_id,
239 int $id,
240 bool $status = true
241 ): void {
242 global $DIC;
243
244 $ilDB = $DIC->database();
245
246 $fields = array(
247 "type" => array("integer", $type),
248 "user_id" => array("integer", $user_id),
249 "id" => array("integer", $id)
250 );
251 $ilDB->replace("notification", $fields, array("activated" => array("integer", (int) $status)));
252 }
253
257 public static function updateNotificationTime(
258 int $type,
259 int $id,
260 array $user_ids,
261 ?int $page_id = null,
262 bool $activate_new_entries = true
263 ): void {
264 global $DIC;
265
266 $ilDB = $DIC->database();
267
268 // create initial entries, if not existing
269 // see #22773, currently only done for wiki, might be feasible for other variants
270 if (in_array($type, [self::TYPE_WIKI_PAGE, self::TYPE_WIKI, self::TYPE_BLOG])) {
271 $set = $ilDB->queryF(
272 "SELECT user_id FROM notification " .
273 " WHERE type = %s AND id = %s AND " .
274 $ilDB->in("user_id", $user_ids, false, "integer"),
275 ["integer", "integer"],
276 [$type, $id]
277 );
278 $noti_users = [];
279 while ($rec = $ilDB->fetchAssoc($set)) {
280 $noti_users[] = $rec["user_id"];
281 }
282 foreach ($user_ids as $user_id) {
283 if (!in_array($user_id, $noti_users)) {
284 $ilDB->insert("notification", [
285 "type" => ["integer", $type],
286 "id" => ["integer", $id],
287 "user_id" => ["integer", $user_id],
288 "page_id" => ["integer", (int) $page_id],
289 "activated" => ["integer", (int) $activate_new_entries]
290 ]);
291 }
292 }
293 }
294
295 $sql = "UPDATE notification" .
296 " SET last_mail = " . $ilDB->quote(date("Y-m-d H:i:s"), "timestamp");
297
298 if ($page_id) {
299 $sql .= ", page_id = " . $ilDB->quote($page_id, "integer");
300 }
301
302 $sql .= " WHERE type = " . $ilDB->quote($type, "integer") .
303 " AND id = " . $ilDB->quote($id, "integer") .
304 " AND " . $ilDB->in("user_id", $user_ids, false, "integer");
305 $ilDB->query($sql);
306 }
307
311 public static function removeForObject(
312 int $type,
313 int $id
314 ): void {
315 global $DIC;
316
317 $ilDB = $DIC->database();
318
319 $ilDB->query("DELETE FROM notification" .
320 " WHERE type = " . $ilDB->quote($type, "integer") .
321 " AND id = " . $ilDB->quote($id, "integer"));
322 }
323
327 public static function removeForUser(
328 int $user_id
329 ): void {
330 global $DIC;
331
332 $ilDB = $DIC->database();
333
334 $ilDB->query("DELETE FROM notification" .
335 " WHERE user_id = " . $ilDB->quote($user_id, "integer"));
336 }
337
342 public static function getActivatedNotifications(
343 int $type,
344 int $user_id
345 ): array {
346 global $DIC;
347
348 $db = $DIC->database();
349
350 $set = $db->queryF(
351 "SELECT id FROM notification " .
352 " WHERE type = %s " .
353 " AND user_id = %s " .
354 " AND activated = %s ",
355 array("integer", "integer", "integer"),
356 array($type, $user_id, 1)
357 );
358 $ids = [];
359 while ($rec = $db->fetchAssoc($set)) {
360 $ids[] = $rec["id"];
361 }
362
363 return $ids;
364 }
365}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static _getInstanceByObjId(int $a_obj_id)
static _isParticipant(int $a_ref_id, int $a_usr_id)
Static function to check if a user is a participant of the container object.
static _getInstanceByObjId(int $a_obj_id)
Get singleton instance.
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...
static updateNotificationTime(int $type, int $id, array $user_ids, ?int $page_id=null, bool $activate_new_entries=true)
Update the last mail timestamp for given object and users.
static hasOptOut(int $obj_id)
Is opt out (disable notification) allowed?
static getNotificationsForObject(int $type, int $id, ?int $page_id=null, bool $ignore_threshold=false)
Get all users/recipients for given object.
static setNotification(int $type, int $user_id, int $id, bool $status=true)
Set notification status for object and user.
static hasNotification(int $type, int $user_id, int $id)
Check notification status for object and user.
static getActivatedNotifications(int $type, int $user_id)
Get activated notifications of give type for user.
static removeForUser(int $user_id)
Remove all notifications for given user.
static removeForObject(int $type, int $id)
Remove all notifications for given object.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupObjectId(int $ref_id)
static _getAllReferences(int $id)
get all reference ids for object ID
static _isParticipant(int $a_ref_id, int $a_usr_id)
Static function to check if a user is a participant of the container object.
$ref_id
Definition: ltiauth.php:66
$log
Definition: ltiresult.php:34
global $DIC
Definition: shib_login.php:26