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