ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilNotificationDatabaseHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Notifications;
22
26use ilSetting;
27use stdClass;
29
34{
39 public static function getTranslatedLanguageVariablesOfNotificationParameters(array $vars = []): array
40 {
41 global $DIC;
42
43 $ilDB = $DIC->database();
44
45 $where = [];
46 $lang_var_to_type_dict = [];
47
48 foreach ($vars as $type => $var) {
49 $where[] = sprintf(
50 'module = %s AND identifier = %s',
51 $ilDB->quote($var->getLanguageModule()),
52 $ilDB->quote($var->getName())
53 );
54
55 $lang_var_to_type_dict[$var->getName()] = $type;
56 }
57
58 if (!$where) {
59 return [];
60 }
61
62 $query = 'SELECT identifier, lang_key, value FROM lng_data WHERE (' . implode(') OR (', $where) . ')';
63 $res = $ilDB->query($query);
64 $results = [];
65
66 while ($row = $ilDB->fetchAssoc($res)) {
67 if (!isset($results[$row['identifier']])) {
68 $results[$row['identifier']] = new stdClass();
69 $results[$row['identifier']]->lang_untouched = [];
70 $results[$row['identifier']]->params = [];
71 }
72 $results[$row['identifier']]->lang_untouched[$row['lang_key']] = $row['value'];
73 }
74
75 return self::fillPlaceholders($results, $vars, $lang_var_to_type_dict);
76 }
77
84 protected static function fillPlaceholders(array $results, array $vars, array $lang_var_to_type_dict): array
85 {
86 $pattern_old = '/##(.+?)##/im';
87 $pattern = '/\[(.+?)\]/im';
88
89 foreach ($results as $lang_var => $res) {
90 $placeholders_stack = [];
91 $res->lang = [];
92
93 foreach ($res->lang_untouched as $iso2_short_handle => $translation) {
94 $translation = str_replace("\\n", "\n", (string) $translation);
95 $placeholders_stack[] = self::findPlaceholders($pattern, $translation);
96 $translation = self::replaceFields(
97 $translation,
98 $placeholders_stack[count($placeholders_stack) - 1],
99 $vars[$lang_var_to_type_dict[$lang_var]]->getParameters(),
100 '[',
101 ']'
102 );
103 $placeholders_stack[] = self::findPlaceholders($pattern_old, $translation);
104 $res->lang[$iso2_short_handle] = self::replaceFields(
105 $translation,
106 $placeholders_stack[count($placeholders_stack) - 1],
107 $vars[$lang_var_to_type_dict[$lang_var]]->getParameters(),
108 '##',
109 '##'
110 );
111 }
112
113 $res->params = array_diff(
114 array_unique(
115 array_merge(...$placeholders_stack)
116 ),
117 array_keys($vars[$lang_var_to_type_dict[$lang_var]]->getParameters())
118 );
119 }
120
121 return $results;
122 }
123
127 protected static function findPlaceholders(string $pattern, string $translation): array
128 {
129 $foundPlaceholders = [];
130 preg_match_all($pattern, $translation, $foundPlaceholders);
131
132 return (array) $foundPlaceholders[1];
133 }
134
139 private static function replaceFields(
140 string $string,
141 array $foundPlaceholders,
142 array $params,
143 string $startTag,
144 string $endTage
145 ): string {
146 $result = $string;
147 foreach ($foundPlaceholders as $placeholder) {
148 if (array_key_exists(strtoupper($placeholder), $params)) {
149 $result = str_ireplace($startTag . $placeholder . $endTage, $params[strtoupper($placeholder)], $result);
150 }
151 if (array_key_exists(strtolower($placeholder), $params)) {
152 $result = str_ireplace($startTag . $placeholder . $endTage, $params[strtolower($placeholder)], $result);
153 }
154 }
155 return $result;
156 }
157
161 public static function setUserConfig(int $userid, array $configArray): void
162 {
163 global $DIC;
164
165 $ilDB = $DIC->database();
166
167 if ($userid !== -1) {
168 $channels = self::getAvailableChannels(['set_by_user']);
169 $types = self::getAvailableTypes(['set_by_user']);
170 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id = %s AND ' . $ilDB->in(
171 'module',
172 array_keys($types),
173 false,
175 ) . ' AND ' . $ilDB->in('channel', array_keys($channels), false, ilDBConstants::T_TEXT);
176 } else {
177 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id = %s';
178 }
179
180 $types = [ilDBConstants::T_INTEGER];
181 $values = [$userid];
182
183 // delete old settings
184 $ilDB->manipulateF($query, $types, $values);
185
186 foreach ($configArray as $type => $channels) {
187 foreach ($channels as $channel => $value) {
188 if (!$value) {
189 continue;
190 }
191 $ilDB->insert(
193 [
194 'usr_id' => [ilDBConstants::T_INTEGER, $userid],
195 'module' => [ilDBConstants::T_TEXT, $type],
196 'channel' => [ilDBConstants::T_TEXT, $channel],
197 ]
198 );
199 }
200 }
201 }
202
206 public static function loadUserConfig(int $userid): array
207 {
208 global $DIC;
209
210 $ilDB = $DIC->database();
211
212 $query = 'SELECT module, channel FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id = %s';
213 $types = [ilDBConstants::T_INTEGER];
214 $values = [$userid];
215
216 $res = $ilDB->queryF($query, $types, $values);
217
218 $result = [];
219
220 while ($row = $ilDB->fetchAssoc($res)) {
221 if (!isset($result[$row['module']])) {
222 $result[$row['module']] = [];
223 }
224
225 $result[$row['module']][] = $row['channel'];
226 }
227
228 return $result;
229 }
230
234 public static function enqueueByUsers(ilNotificationConfig $notification, array $usr_ids): void
235 {
236 if (!$usr_ids) {
237 return;
238 }
239
240 global $DIC;
241
242 $ilDB = $DIC->database();
243
244 $notification_id = self::storeNotification($notification);
245 $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
246
247 foreach ($usr_ids as $userid) {
248 $ilDB->insert(
250 [
251 'notification_id' => [ilDBConstants::T_INTEGER, $notification_id],
252 'usr_id' => [ilDBConstants::T_INTEGER, $userid],
253 'valid_until' => [ilDBConstants::T_INTEGER, $valid_until],
254 'visible_for' => [ilDBConstants::T_INTEGER, $notification->getVisibleForSeconds()]
255 ]
256 );
257 }
258 }
259
260 public static function enqueueByListener(ilNotificationConfig $notification, int $ref_id): void
261 {
262 global $DIC;
263
264 $ilDB = $DIC->database();
265
266 $notification_id = self::storeNotification($notification);
267 $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
268
269 $query = 'INSERT INTO ' . ilNotificationSetupHelper::$tbl_notification_queue . ' (notification_id, usr_id, valid_until, visible_for) '
270 . ' (SELECT %s, usr_id, %s, %s FROM ' . ilNotificationSetupHelper::$tbl_userlistener . ' WHERE disabled = 0 AND module = %s AND sender_id = %s)';
271
272 $types = [
278 ];
279
280 $values = [
281 $notification_id,
282 $valid_until,
283 $notification->getVisibleForSeconds(),
284 $notification->getType(),
285 $ref_id
286 ];
287
288 $ilDB->manipulateF($query, $types, $values);
289 }
290
291 public static function storeNotification(ilNotificationConfig $notification): int
292 {
293 global $DIC;
294
295 $ilDB = $DIC->database();
296
298
299 $ilDB->insert(
301 [
302 'notification_id' => [ilDBConstants::T_INTEGER, $id],
303 'serialized' => [ilDBConstants::T_TEXT, serialize($notification)],
304 ]
305 );
306
307 return $id;
308 }
309
310 public static function removeNotification(int $id): void
311 {
312 global $DIC;
313
314 $ilDB = $DIC->database();
315
316 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_data . ' WHERE notification_id = %s';
317 $types = [ilDBConstants::T_INTEGER];
318 $values = [$id];
319
320 $ilDB->manipulateF($query, $types, $values);
321 }
322
326 public static function getUsersByListener(string $module, int $sender_id): array
327 {
328 global $DIC;
329
330 $ilDB = $DIC->database();
331
332 $query = 'SELECT usr_id FROM ' . ilNotificationSetupHelper::$tbl_userlistener . ' WHERE disabled = 0 AND module = %s AND sender_id = %s';
334 $values = [$module, $sender_id];
335
336 $users = [];
337
338 $rset = $ilDB->queryF($query, $types, $values);
339 while ($row = $ilDB->fetchAssoc($rset)) {
340 $users[] = (int) $row['usr_id'];
341 }
342
343 return $users;
344 }
345
346 public static function disableListeners(string $module, int $sender_id): void
347 {
348 global $DIC;
349
350 $ilDB = $DIC->database();
351
352 $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_userlistener . ' SET disabled = 1 WHERE module = %s AND sender_id = %s';
354 $values = [$module, $sender_id];
355
356 $ilDB->manipulateF($query, $types, $values);
357 }
358
362 public static function enableListeners(string $module, int $sender_id, array $users = []): void
363 {
364 global $DIC;
365
366 $ilDB = $DIC->database();
367
368 $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_userlistener . ' SET disabled = 0 WHERE module = %s AND sender_id = %s';
369
370 if ($users) {
371 $query .= ' ' . $ilDB->in('usr_id', $users, false, ilDBConstants::T_INTEGER);
372 }
373
375 $values = [$module, $sender_id];
376
377 $ilDB->manipulateF($query, $types, $values);
378 }
379
380 public static function registerChannel(
382 string $name,
383 string $title,
384 string $description,
385 string $class,
386 string $classfile,
387 string $config_type
388 ): void {
389 $db->insert(
390 ilNotificationSetupHelper::$tbl_notification_channels,
391 [
392 'channel_name' => [ilDBConstants::T_TEXT, $name],
393 'title' => [ilDBConstants::T_TEXT, $title],
394 'description' => [ilDBConstants::T_TEXT, $description],
395 'class' => [ilDBConstants::T_TEXT, $class],
396 'include' => [ilDBConstants::T_TEXT, $classfile],
397 'config_type' => [ilDBConstants::T_TEXT, $config_type],
398 ]
399 );
400 }
401
402 public static function registerType(
403 ilDBInterface $db,
404 string $name,
405 string $title,
406 string $description,
407 string $notification_group,
408 string $config_type
409 ): void {
410 $db->insert(
411 ilNotificationSetupHelper::$tbl_notification_types,
412 [
413 'type_name' => [ilDBConstants::T_TEXT, $name],
414 'title' => [ilDBConstants::T_TEXT, $title],
415 'description' => [ilDBConstants::T_TEXT, $description],
416 'notification_group' => [ilDBConstants::T_TEXT, $notification_group],
417 'config_type' => [ilDBConstants::T_TEXT, $config_type],
418 ]
419 );
420 }
421
426 public static function getAvailableChannels(array $config_types = [], bool $include_disabled = false): array
427 {
428 global $DIC;
429
430 $ilDB = $DIC->database();
431
432 $query = 'SELECT channel_name, title, description, class, include, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_channels;
433 if ($config_types) {
434 $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, ilDBConstants::T_TEXT);
435 }
436
437 $rset = $ilDB->query($query);
438
439 $result = [];
440
441 $settings = new ilSetting('notifications');
442
443 while ($row = $ilDB->fetchAssoc($rset)) {
444 if (!$include_disabled && !$settings->get('enable_' . $row['channel_name'])) {
445 continue;
446 }
447
448 $result[$row['channel_name']] = [
449 'name' => $row['channel_name'],
450 'title' => $row['title'],
451 'description' => $row['description'],
452 'handler' => $row['class'],
453 'include' => $row['include'],
454 'config_type' => $row['config_type'],
455 ];
456 }
457
458 return $result;
459 }
460
465 public static function getAvailableTypes(array $config_types = []): array
466 {
467 global $DIC;
468
469 $ilDB = $DIC->database();
470
471 $query = 'SELECT type_name, title, description, notification_group, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_types;
472 if ($config_types) {
473 $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, ilDBConstants::T_TEXT);
474 }
475
476 $rset = $ilDB->query($query);
477
478 $result = [];
479
480 while ($row = $ilDB->fetchAssoc($rset)) {
481 $result[$row['type_name']] = [
482 'name' => $row['type_name'],
483 'title' => $row['title'],
484 'description' => $row['description'],
485 'group' => $row['notification_group'],
486 'config_type' => $row['config_type'],
487 ];
488 }
489
490 return $result;
491 }
492
493 public static function setConfigTypeForType(string $type_name, string $config_name): void
494 {
495 global $DIC;
496
497 $ilDB = $DIC->database();
498
499 $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_types . ' SET config_type = %s WHERE type_name = %s';
501 $values = [$config_name, $type_name];
502 $ilDB->manipulateF($query, $types, $values);
503 }
504
505 public static function setConfigTypeForChannel(string $channel_name, string $config_name): void
506 {
507 global $DIC;
508
509 $ilDB = $DIC->database();
510
511 $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_channels . ' SET config_type = %s WHERE channel_name = %s';
513 $values = [$config_name, $channel_name];
514 $ilDB->manipulateF($query, $types, $values);
515 }
516
521 public static function getUsersWithCustomConfig(array $usr_ids): array
522 {
523 global $DIC;
524
525 $ilDB = $DIC->database();
526
527 $query = 'SELECT usr_id, value FROM usr_pref WHERE ' . $ilDB->in(
528 'usr_id',
529 $usr_ids,
530 false,
532 ) . ' AND keyword = ' . $ilDB->quote(
533 'use_custom_notification_setting',
535 ) . ' AND value = ' . $ilDB->quote(
536 '1',
538 );
539 $rset = $ilDB->query($query);
540 $result = [];
541 while ($row = $ilDB->fetchAssoc($rset)) {
542 $result[(int) $row['usr_id']] = (bool) $row['value'];
543 }
544
545 $missing_usr_ids = array_diff(
546 $usr_ids,
547 array_keys($result)
548 );
549
550 $result = $result + array_combine(
551 $missing_usr_ids,
552 array_fill(0, count($missing_usr_ids), false)
553 );
554
555 return $result;
556 }
557}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
description of a localized parameter this information is used locate translations while processing no...
static registerChannel(ilDBInterface $db, string $name, string $title, string $description, string $class, string $classfile, string $config_type)
static getAvailableChannels(array $config_types=[], bool $include_disabled=false)
static setConfigTypeForChannel(string $channel_name, string $config_name)
static registerType(ilDBInterface $db, string $name, string $title, string $description, string $notification_group, string $config_type)
static fillPlaceholders(array $results, array $vars, array $lang_var_to_type_dict)
static enableListeners(string $module, int $sender_id, array $users=[])
static storeNotification(ilNotificationConfig $notification)
static findPlaceholders(string $pattern, string $translation)
static enqueueByListener(ilNotificationConfig $notification, int $ref_id)
static enqueueByUsers(ilNotificationConfig $notification, array $usr_ids)
static setConfigTypeForType(string $type_name, string $config_name)
static replaceFields(string $string, array $foundPlaceholders, array $params, string $startTag, string $endTage)
Class ilDBConstants.
ILIAS Setting Class.
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
$res
Definition: ltiservices.php:69
$results
global $DIC
Definition: shib_login.php:26