ILIAS  release_8 Revision v8.24
ilNotificationDatabaseHandler.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21namespace ILIAS\Notifications;
22
26use ilSetting;
27use stdClass;
28
33{
37 public static function getTranslatedLanguageVariablesOfNotificationParameters(array $vars = []): array
38 {
39 global $DIC;
40
41 $ilDB = $DIC->database();
42
43 $where = [];
44 $langVarToTypeDict = [];
45
46 foreach ($vars as $type => $var) {
47 if (!$var) {
48 continue;
49 }
50 $where[] = sprintf('module = %s AND identifier = %s', $ilDB->quote($var->getLanguageModule()), $ilDB->quote($var->getName()));
51 $langVarToTypeDict[$var->getName()] = $type;
52 }
53
54 if (!$where) {
55 return [];
56 }
57
58 $query = 'SELECT identifier, lang_key, value FROM lng_data WHERE (' . implode(') OR (', $where) . ')';
59 $res = $ilDB->query($query);
60 $results = [];
61
62 while ($row = $ilDB->fetchAssoc($res)) {
63 if (!isset($results[$row['identifier']]) || !$results[$row['identifier']]) {
64 $results[$row['identifier']] = new stdClass();
65 $results[$row['identifier']]->lang_untouched = [];
66 $results[$row['identifier']]->params = [];
67 }
68 $results[$row['identifier']]->lang_untouched[$row['lang_key']] = $row['value'];
69 }
70
71 return self::fillPlaceholders($results, $vars, $langVarToTypeDict);
72 }
73
77 protected static function fillPlaceholders(array $results, array $vars, array $langVarToTypeDict): array
78 {
79 $pattern_old = '/##(.+?)##/im';
80 $pattern = '/\[(.+?)\]/im';
81
82 foreach ($results as $langVar => $res) {
83 $placeholdersStack = [];
84 $res->lang = [];
85
86 foreach ($res->lang_untouched as $iso2shorthandle => $translation) {
87 $translation = str_replace("\\n", "\n", $translation);
88 $placeholdersStack[] = self::findPlaceholders($pattern, $translation);
89 $translation = self::replaceFields($translation, $placeholdersStack[count($placeholdersStack) - 1], $vars[$langVarToTypeDict[$langVar]]->getParameters(), '[', ']');
90 $placeholdersStack[] = self::findPlaceholders($pattern_old, $translation);
91 $res->lang[$iso2shorthandle] = self::replaceFields($translation, $placeholdersStack[count($placeholdersStack) - 1], $vars[$langVarToTypeDict[$langVar]]->getParameters(), '##', '##');
92 }
93
94 $res->params = array_diff(
95 array_unique(
96 array_merge(...$placeholdersStack)
97 ),
98 array_keys($vars[$langVarToTypeDict[$langVar]]->getParameters())
99 );
100 }
101
102 return $results;
103 }
104
105 protected static function findPlaceholders(string $pattern, string $translation): array
106 {
107 $foundPlaceholders = [];
108 preg_match_all($pattern, $translation, $foundPlaceholders);
109 return (array) $foundPlaceholders[1];
110 }
111
116 private static function replaceFields(string $string, array $foundPlaceholders, array $params, string $startTag, string $endTage): string
117 {
118 $result = $string;
119 foreach ($foundPlaceholders as $placeholder) {
120 if (array_key_exists(strtoupper($placeholder), $params)) {
121 $result = str_ireplace($startTag . $placeholder . $endTage, $params[strtoupper($placeholder)], $result);
122 }
123 if (array_key_exists(strtolower($placeholder), $params)) {
124 $result = str_ireplace($startTag . $placeholder . $endTage, $params[strtolower($placeholder)], $result);
125 }
126 }
127 return $result;
128 }
129
130 public static function setUserConfig(int $userid, array $configArray): void
131 {
132 global $DIC;
133
134 $ilDB = $DIC->database();
135
136 if ($userid !== -1) {
137 $channels = self::getAvailableChannels(['set_by_user']);
138 $types = self::getAvailableTypes(['set_by_user']);
139 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id=%s AND ' . $ilDB->in('module', array_keys($types), false, 'text') . ' AND ' . $ilDB->in('channel', array_keys($channels), false, 'text');
140 } else {
141 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id=%s';
142 }
143
144 $types = ['integer'];
145 $values = [$userid];
146
147 // delete old settings
148 $ilDB->manipulateF($query, $types, $values);
149
150 foreach ($configArray as $type => $channels) {
151 foreach ($channels as $channel => $value) {
152 if (!$value) {
153 continue;
154 }
155 $ilDB->insert(
157 [
158 'usr_id' => ['integer', $userid],
159 'module' => ['text', $type],
160 'channel' => ['text', $channel],
161 ]
162 );
163 }
164 }
165 }
166
172 public static function loadUserConfig(int $userid): array
173 {
174 global $DIC;
175
176 $ilDB = $DIC->database();
177
178 $query = 'SELECT module, channel FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id = %s';
179 $types = ['integer'];
180 $values = [$userid];
181
182 $res = $ilDB->queryF($query, $types, $values);
183
184 $result = [];
185
186 while ($row = $ilDB->fetchAssoc($res)) {
187 if (!isset($result[$row['module']])) {
188 $result[$row['module']] = [];
189 }
190
191 $result[$row['module']][] = $row['channel'];
192 }
193
194 return $result;
195 }
196
197 public static function enqueueByUsers(ilNotificationConfig $notification, array $userids): void
198 {
199 if (!$userids) {
200 return;
201 }
202
203 global $DIC;
204
205 $ilDB = $DIC->database();
206
207 $notification_id = self::storeNotification($notification);
208 $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
209
210 foreach ($userids as $userid) {
211 $ilDB->insert(
213 [
214 'notification_id' => ['integer', $notification_id],
215 'usr_id' => ['integer', $userid],
216 'valid_until' => ['integer', $valid_until],
217 'visible_for' => ['integer', $notification->getVisibleForSeconds()]
218 ]
219 );
220 }
221 }
222
223 public static function enqueueByListener(ilNotificationConfig $notification, int $ref_id): void
224 {
225 global $DIC;
226
227 $ilDB = $DIC->database();
228
229 $notification_id = self::storeNotification($notification);
230 $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
231
232 $query = 'INSERT INTO ' . ilNotificationSetupHelper::$tbl_notification_queue . ' (notification_id, usr_id, valid_until, visible_for) '
233 . ' (SELECT %s, usr_id, %s, %s FROM ' . ilNotificationSetupHelper::$tbl_userlistener . ' WHERE disabled = 0 AND module = %s AND sender_id = %s)';
234
235 $types = ['integer', 'integer', 'integer', 'text', 'integer'];
236
237 $values = [$notification_id, $valid_until, $notification->getVisibleForSeconds(), $notification->getType(), $ref_id];
238
239 $ilDB->manipulateF($query, $types, $values);
240 }
241
242 public static function storeNotification(ilNotificationConfig $notification): int
243 {
244 global $DIC;
245
246 $ilDB = $DIC->database();
247
249
250 $ilDB->insert(
252 [
253 'notification_id' => ['integer', $id],
254 'serialized' => ['text', serialize($notification)],
255 ]
256 );
257
258 return $id;
259 }
260
261 public static function removeNotification(int $id): void
262 {
263 global $DIC;
264
265 $ilDB = $DIC->database();
266
267 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_data . ' WHERE notification_id = ?';
268 $types = ['integer'];
269 $values = [$id];
270
271 $ilDB->manipulateF($query, $types, $values);
272 }
273
277 public static function getUsersByListener(string $module, int $sender_id): array
278 {
279 global $DIC;
280
281 $ilDB = $DIC->database();
282
283 $query = 'SELECT usr_id FROM ' . ilNotificationSetupHelper::$tbl_userlistener . ' WHERE disabled = 0 AND module = %s AND sender_id = %s';
284 $types = ['text', 'integer'];
285 $values = [$module, $sender_id];
286
287 $users = [];
288
289 $rset = $ilDB->queryF($query, $types, $values);
290 while ($row = $ilDB->fetchAssoc($rset)) {
291 $users[] = (int) $row['usr_id'];
292 }
293 return $users;
294 }
295
296 public static function disableListeners(string $module, int $sender_id): void
297 {
298 global $DIC;
299
300 $ilDB = $DIC->database();
301
302 $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_userlistener . ' SET disabled = 1 WHERE module = %s AND sender_id = %s';
303 $types = ['text', 'integer'];
304 $values = [$module, $sender_id];
305
306 $ilDB->manipulateF($query, $types, $values);
307 }
308
309 public static function enableListeners(string $module, $sender_id, array $users = []): void
310 {
311 global $DIC;
312
313 $ilDB = $DIC->database();
314
315 $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_userlistener . ' SET disabled = 0 WHERE module = %s AND sender_id = %s';
316
317 if ($users) {
318 $query .= ' ' . $ilDB->in('usr_id', $users);
319 }
320
321 $types = ['text', 'integer'];
322 $values = [$module, $sender_id];
323
324 $ilDB->manipulateF($query, $types, $values);
325 }
326
327 public static function registerChannel(ilDBInterface $db, string $name, string $title, string $description, string $class, string $classfile, string $config_type): void
328 {
329 $db->insert(
331 [
332 'channel_name' => ['text', $name],
333 'title' => ['text', $title],
334 'description' => ['text', $description],
335 'class' => ['text', $class],
336 'include' => ['text', $classfile],
337 'config_type' => ['text', $config_type],
338 ]
339 );
340 }
341
342 public static function registerType(ilDBInterface $db, string $name, string $title, string $description, string $notification_group, string $config_type): void
343 {
344 $db->insert(
346 [
347 'type_name' => ['text', $name],
348 'title' => ['text', $title],
349 'description' => ['text', $description],
350 'notification_group' => ['text', $notification_group],
351 'config_type' => ['text', $config_type],
352 ]
353 );
354 }
355
359 public static function getAvailableChannels(array $config_types = [], bool $includeDisabled = false): array
360 {
361 global $DIC;
362
363 $ilDB = $DIC->database();
364
365 $query = 'SELECT channel_name, title, description, class, include, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_channels;
366 if ($config_types) {
367 $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
368 }
369
370 $rset = $ilDB->query($query);
371
372 $result = [];
373
374 $settings = new ilSetting('notifications');
375
376 while ($row = $ilDB->fetchAssoc($rset)) {
377 if (!$includeDisabled && !$settings->get('enable_' . $row['channel_name'])) {
378 continue;
379 }
380
381 $result[$row['channel_name']] = [
382 'name' => $row['channel_name'],
383 'title' => $row['title'],
384 'description' => $row['description'],
385 'handler' => $row['class'],
386 'include' => $row['include'],
387 'config_type' => $row['config_type'],
388 ];
389 }
390
391 return $result;
392 }
393
397 public static function getAvailableTypes(array $config_types = []): array
398 {
399 global $DIC;
400
401 $ilDB = $DIC->database();
402
403 $query = 'SELECT type_name, title, description, notification_group, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_types;
404 if ($config_types) {
405 $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
406 }
407
408
409 $rset = $ilDB->query($query);
410
411 $result = [];
412
413 while ($row = $ilDB->fetchAssoc($rset)) {
414 $result[$row['type_name']] = [
415 'name' => $row['type_name'],
416 'title' => $row['title'],
417 'description' => $row['description'],
418 'group' => $row['notification_group'],
419 'config_type' => $row['config_type'],
420 ];
421 }
422
423 return $result;
424 }
425
426 public static function setConfigTypeForType(string $type_name, string $config_name): void
427 {
428 global $DIC;
429
430 $ilDB = $DIC->database();
431
432 $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_types . ' SET config_type = %s WHERE type_name = %s';
433 $types = ['text', 'text'];
434 $values = [$config_name, $type_name];
435 $ilDB->manipulateF($query, $types, $values);
436 }
437
438 public static function setConfigTypeForChannel(string $channel_name, string $config_name): void
439 {
440 global $DIC;
441
442 $ilDB = $DIC->database();
443
444 $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_channels . ' SET config_type = %s WHERE channel_name = %s';
445 $types = ['text', 'text'];
446 $values = [$config_name, $channel_name];
447 $ilDB->manipulateF($query, $types, $values);
448 }
449
455 public static function getUsersWithCustomConfig(array $userid): array
456 {
457 global $DIC;
458
459 $ilDB = $DIC->database();
460
461 $query = 'SELECT usr_id, value FROM usr_pref WHERE ' . $ilDB->in('usr_id', $userid, false, 'integer') . ' AND keyword="use_custom_notification_setting" AND value="1"';
462 $rset = $ilDB->query($query);
463 $result = [];
464 while ($row = $ilDB->fetchAssoc($rset)) {
465 $result[$row['usr_id']] = (bool) $row['value'];
466 }
467 return $result;
468 }
469}
$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 fillPlaceholders(array $results, array $vars, array $langVarToTypeDict)
static enableListeners(string $module, $sender_id, array $users=[])
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 storeNotification(ilNotificationConfig $notification)
static getAvailableChannels(array $config_types=[], bool $includeDisabled=false)
static findPlaceholders(string $pattern, string $translation)
static enqueueByUsers(ilNotificationConfig $notification, array $userids)
static enqueueByListener(ilNotificationConfig $notification, int $ref_id)
static setConfigTypeForType(string $type_name, string $config_name)
static replaceFields(string $string, array $foundPlaceholders, array $params, string $startTag, string $endTage)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:67
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
$res
Definition: ltiservices.php:69
if($format !==null) $name
Definition: metadata.php:247
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$query
$type
$results