ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilNotificationDatabaseHelper.php
Go to the documentation of this file.
1 <?php
2 
3 require_once 'Services/Notifications/classes/class.ilNotificationSetupHelper.php';
4 
6 {
12  public static function getTranslatedLanguageVariablesOfNotificationParameters($vars = array())
13  {
14  global $DIC;
15 
16  $ilDB = $DIC->database();
17 
18  $where = array();
19  $langVarToTypeDict = array();
20 
21  foreach ($vars as $type => $var) {
26  if (!$var) {
27  continue;
28  }
29  $where[] = sprintf('module = %s AND identifier = %s', $ilDB->quote($var->getLanguageModule()), $ilDB->quote($var->getName()));
30  $langVarToTypeDict[$var->getName()] = $type;
31  }
32 
33  if (!$where) {
34  return array();
35  }
36 
37  $query = 'SELECT identifier, lang_key, value FROM lng_data WHERE (' . join(') OR (', $where) . ')';
38  $res = $ilDB->query($query);
39  $results = array();
40 
41  while ($row = $ilDB->fetchAssoc($res)) {
42  if (!$results[$row['identifier']]) {
43  $results[$row['identifier']] = new stdClass();
44  $results[$row['identifier']]->lang_untouched = array();
45  $results[$row['identifier']]->params = array();
46  }
47  $results[$row['identifier']]->lang_untouched[$row['lang_key']] = $row['value'];
48  }
49 
50  return self::fillPlaceholders($results, $vars, $langVarToTypeDict);
51  }
52 
60  protected static function fillPlaceholders($results, $vars, $langVarToTypeDict)
61  {
62  $pattern_old = '/##(.+?)##/im';
63  $pattern = '/\[(.+?)\]/im';
64 
65  foreach ($results as $langVar => $res) {
66  $placeholdersStack = array();
67  $res->lang = array();
68 
69  foreach ($res->lang_untouched as $iso2shorthandle => $translation) {
70  $translation = str_replace("\\n", "\n", $translation);
71  $placeholdersStack[] = self::findPlaceholders($pattern, $translation);
72  $translation = self::replaceFields($translation, $placeholdersStack[count($placeholdersStack) - 1], $vars[$langVarToTypeDict[$langVar]]->getParameters(), '[', ']');
73  $placeholdersStack[] = self::findPlaceholders($pattern_old, $translation);
74  $res->lang[$iso2shorthandle] = self::replaceFields($translation, $placeholdersStack[count($placeholdersStack) - 1], $vars[$langVarToTypeDict[$langVar]]->getParameters(), '##', '##');
75  }
76 
77  $res->params = array_diff(
78  array_unique(
79  call_user_func_array('array_merge', $placeholdersStack)
80  ),
81  array_keys($vars[$langVarToTypeDict[$langVar]]->getParameters())
82  );
83  }
84 
85  return $results;
86  }
87 
94  protected static function findPlaceholders($pattern, $translation)
95  {
96  $foundPlaceholders = array();
97  preg_match_all($pattern, $translation, $foundPlaceholders);
98  return (array) $foundPlaceholders[1];
99  }
100 
110  private static function replaceFields($string, $foundPlaceholders, $params, $startTag, $endTage)
111  {
112  foreach ($foundPlaceholders as $placeholder) {
113  if (array_key_exists(strtoupper($placeholder), $params)) {
114  $string = str_ireplace($startTag . $placeholder . $endTage, $params[strtoupper($placeholder)], $string);
115  }
116  if (array_key_exists(strtolower($placeholder), $params)) {
117  $string = str_ireplace($startTag . $placeholder . $endTage, $params[strtolower($placeholder)], $string);
118  }
119  }
120  return $string;
121  }
122 
147  public static function setUserConfig($userid, array $configArray)
148  {
149  global $DIC;
150 
151  $ilDB = $DIC->database();
152 
153  if ($userid != -1) {
154  $channels = self::getAvailableChannels(array('set_by_user'));
155  $types = self::getAvailableTypes(array('set_by_user'));
156  $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');
157  } else {
158  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id=%s';
159  }
160 
161  $types = array('integer');
162  $values = array($userid);
163 
164  // delete old settings
165  $ilDB->manipulateF($query, $types, $values);
166 
167  foreach ($configArray as $type => $channels) {
168  foreach ($channels as $channel => $value) {
169  if (!$value) {
170  continue;
171  }
172  $ilDB->insert(
174  array(
175  'usr_id' => array('integer', $userid),
176  'module' => array('text', $type),
177  'channel' => array('text', $channel),
178  )
179  );
180  }
181  }
182  }
183 
184  public static function loadUserConfig($userid)
185  {
186  global $DIC;
187 
188  $ilDB = $DIC->database();
189 
190  $query = 'SELECT module, channel FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id = %s';
191  $types = array('integer');
192  $values = array($userid);
193 
194  $res = $ilDB->queryF($query, $types, $values);
195 
196  $result = array();
197 
198  while ($row = $ilDB->fetchAssoc($res)) {
199  if (!$result[$row['module']]) {
200  $result[$row['module']] = array();
201  }
202 
203  $result[$row['module']][] = $row['channel'];
204  }
205 
206  return $result;
207  }
208 
209  public static function enqueueByUsers(ilNotificationConfig $notification, array $userids)
210  {
211  if (!$userids) {
212  return;
213  }
214 
215  global $DIC;
216 
217  $ilDB = $DIC->database();
218 
219  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
220  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
221 
222  foreach ($userids as $userid) {
223  $ilDB->insert(
225  array(
226  'notification_id' => array('integer', $notification_id),
227  'usr_id' => array('integer', $userid),
228  'valid_until' => array('integer', $valid_until),
229  'visible_for' => array('integer', $notification->getVisibleForSeconds())
230  )
231  );
232  }
233  }
234 
235  public static function enqueueByListener(ilNotificationConfig $notification, $ref_id)
236  {
237  global $DIC;
238 
239  $ilDB = $DIC->database();
240 
241  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
242  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
243 
244  $query = 'INSERT INTO ' . ilNotificationSetupHelper::$tbl_notification_queue . ' (notification_id, usr_id, valid_until, visible_for) '
245  . ' (SELECT %s, usr_id, %s, %s FROM ' . ilNotificationSetupHelper::$tbl_userlistener . ' WHERE disabled = 0 AND module = %s AND sender_id = %s)';
246 
247  $types = array('integer', 'integer', 'integer', 'text', 'integer');
248 
249  $values = array($notification_id, $valid_until, $notification->getVisibleForSeconds(), $notification->getType(), $ref_id);
250 
251  $ilDB->manipulateF($query, $types, $values);
252  }
253 
254  public static function storeNotification(ilNotificationConfig $notification)
255  {
256  global $DIC;
257 
258  $ilDB = $DIC->database();
259 
261 
262  $ilDB->insert(
264  array(
265  'notification_id' => array('integer', $id),
266  'serialized' => array('text', serialize($notification)),
267  )
268  );
269 
270  return $id;
271  }
272 
273  public static function removeNotification($id)
274  {
275  global $DIC;
276 
277  $ilDB = $DIC->database();
278 
279  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_data . ' WHERE notification_id = ?';
280  $types = array('integer');
281  $values = array($id);
282 
283  $ilDB->manipulateF($query, $types, $values);
284  }
285 
286  public static function getUsersByListener($module, $sender_id)
287  {
288  global $DIC;
289 
290  $ilDB = $DIC->database();
291 
292  $query = 'SELECT usr_id FROM ' . ilNotificationSetupHelper::$tbl_userlistener . ' WHERE disabled = 0 AND module = %s AND sender_id = %s';
293  $types = array('text', 'integer');
294  $values = array($module, $sender_id);
295 
296  $users = array();
297 
298  $rset = $ilDB->queryF($query, $types, $values);
299  while ($row = $ilDB->fetchAssoc($rset)) {
300  $users[] = $row['usr_id'];
301  }
302  return $users;
303  }
304 
305  public static function disableListeners($module, $sender_id)
306  {
307  global $DIC;
308 
309  $ilDB = $DIC->database();
310 
311  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_userlistener . ' SET disabled = 1 WHERE module = %s AND sender_id = %s';
312  $types = array('text', 'integer');
313  $values = array($module, $sender_id);
314 
315  $ilDB->manipulateF($query, $types, $values);
316  }
317 
318  public static function enableListeners($module, $sender_id, array $users = array())
319  {
320  global $DIC;
321 
322  $ilDB = $DIC->database();
323 
324  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_userlistener . ' SET disabled = 0 WHERE module = %s AND sender_id = %s';
325 
326  if ($users) {
327  $query .= ' ' . $ilDB->in('usr_id', $users);
328  }
329 
330  $types = array('text', 'integer');
331  $values = array($module, $sender_id);
332 
333  $ilDB->manipulateF($query, $types, $values);
334  }
335 
348  public static function registerChannel($name, $title, $description, $class, $classfile, $config_type)
349  {
350  global $DIC;
351 
352  $ilDB = $DIC->database();
353 
354  $ilDB->insert(
356  array(
357  'channel_name' => array('text', $name),
358  'title' => array('text', $title),
359  'description' => array('text', $description),
360  'class' => array('text', $class),
361  'include' => array('text', $classfile),
362  'config_type' => array('text', $config_type),
363  )
364  );
365  }
366 
378  public static function registerType($name, $title, $description, $notification_group, $config_type)
379  {
380  global $DIC;
381 
382  $ilDB = $DIC->database();
383 
384  $ilDB->insert(
386  array(
387  'type_name' => array('text', $name),
388  'title' => array('text', $title),
389  'description' => array('text', $description),
390  'notification_group' => array('text', $notification_group),
391  'config_type' => array('text', $config_type),
392  )
393  );
394  }
395 
396  public static function getAvailableChannels($config_types = array(), $includeDisabled = false)
397  {
398  global $DIC;
399 
400  $ilDB = $DIC->database();
401 
402  $query = 'SELECT channel_name, title, description, class, include, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_channels;
403  if ($config_types) {
404  $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
405  }
406 
407  $rset = $ilDB->query($query);
408 
409  $result = array();
410 
411  $settings = new ilSetting('notifications');
412 
413  while ($row = $ilDB->fetchAssoc($rset)) {
414  if (!$includeDisabled && !$settings->get('enable_' . $row['channel_name'])) {
415  continue;
416  }
417 
418  $result[$row['channel_name']] = array(
419  'name' => $row['channel_name'],
420  'title' => $row['title'],
421  'description' => $row['description'],
422  'handler' => $row['class'],
423  'include' => $row['include'],
424  'config_type' => $row['config_type'],
425  );
426  }
427 
428  return $result;
429  }
430 
431  public static function getAvailableTypes($config_types = array())
432  {
433  global $DIC;
434 
435  $ilDB = $DIC->database();
436 
437  $query = 'SELECT type_name, title, description, notification_group, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_types;
438  if ($config_types) {
439  $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
440  }
441 
442 
443  $rset = $ilDB->query($query);
444 
445  $result = array();
446 
447  while ($row = $ilDB->fetchAssoc($rset)) {
448  $result[$row['type_name']] = array(
449  'name' => $row['type_name'],
450  'title' => $row['title'],
451  'description' => $row['description'],
452  'group' => $row['notification_group'],
453  'config_type' => $row['config_type'],
454  );
455  }
456 
457  return $result;
458  }
459 
460  public static function setConfigTypeForType($type_name, $config_name)
461  {
462  global $DIC;
463 
464  $ilDB = $DIC->database();
465 
466  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_types . ' SET config_type = %s WHERE type_name = %s';
467  $types = array('text', 'text');
468  $values = array($config_name, $type_name);
469  $ilDB->manipulateF($query, $types, $values);
470  }
471 
472  public static function setConfigTypeForChannel($channel_name, $config_name)
473  {
474  global $DIC;
475 
476  $ilDB = $DIC->database();
477 
478  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_channels . ' SET config_type = %s WHERE channel_name = %s';
479  $types = array('text', 'text');
480  $values = array($config_name, $channel_name);
481  $ilDB->manipulateF($query, $types, $values);
482  }
483 
484 
485  public static function getUsersWithCustomConfig(array $userid)
486  {
487  global $DIC;
488 
489  $ilDB = $DIC->database();
490 
491  $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"';
492  $rset = $ilDB->query($query);
493  $result = array();
494  while ($row = $ilDB->fetchAssoc($rset)) {
495  $result[$row['usr_id']] = (bool) $row['value'];
496  }
497  return $result;
498  }
499 }
static enqueueByUsers(ilNotificationConfig $notification, array $userids)
$result
$type
global $DIC
Definition: saml.php:7
if(empty($userids)) $userid
$userids
if(!array_key_exists('StateId', $_REQUEST)) $id
static registerChannel($name, $title, $description, $class, $classfile, $config_type)
Registers a new notification channel for distributing notifications.
static storeNotification(ilNotificationConfig $notification)
if($modEnd===false) $module
Definition: module.php:59
static registerType($name, $title, $description, $notification_group, $config_type)
Registers a new notification type.
Describes a notification and provides methods for publishing this notification.
static setConfigTypeForChannel($channel_name, $config_name)
static setConfigTypeForType($type_name, $config_name)
foreach($_POST as $key=> $value) $res
static enqueueByListener(ilNotificationConfig $notification, $ref_id)
$values
static findPlaceholders($pattern, $translation)
$query
$users
Definition: authpage.php:44
static replaceFields($string, $foundPlaceholders, $params, $startTag, $endTage)
$row
static fillPlaceholders($results, $vars, $langVarToTypeDict)
$results
Definition: svg-scanner.php:47
global $ilDB
static setUserConfig($userid, array $configArray)
Sets the configuration for all given configurations.
static getAvailableChannels($config_types=array(), $includeDisabled=false)
static enableListeners($module, $sender_id, array $users=array())