ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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)
23  {
28  if(!$var)
29  {
30  continue;
31  }
32  $where[] = sprintf('module = %s AND identifier = %s', $ilDB->quote($var->getLanguageModule()), $ilDB->quote($var->getName()));
33  $langVarToTypeDict[$var->getName()] = $type;
34  }
35 
36  if(!$where)
37  {
38  return array();
39  }
40 
41  $query = 'SELECT identifier, lang_key, value FROM lng_data WHERE (' . join(') OR (', $where) . ')';
42  $res = $ilDB->query($query);
43  $results = array();
44 
45  while($row = $ilDB->fetchAssoc($res))
46  {
47  if(!$results[$row['identifier']])
48  {
49  $results[$row['identifier']] = new stdClass();
50  $results[$row['identifier']]->lang_untouched = array();
51  $results[$row['identifier']]->params = array();
52  }
53  $results[$row['identifier']]->lang_untouched[$row['lang_key']] = $row['value'];
54  }
55 
56  return self::fillPlaceholders($results, $vars, $langVarToTypeDict);
57  }
58 
66  protected static function fillPlaceholders($results, $vars, $langVarToTypeDict)
67  {
68  $pattern_old = '/##(.+?)##/im';
69  $pattern = '/\[(.+?)\]/im';
70 
71  foreach($results as $langVar => $res)
72  {
73  $placeholdersStack = array();
74  $res->lang = array();
75 
76  foreach($res->lang_untouched as $iso2shorthandle => $translation)
77  {
78  $translation = str_replace("\\n", "\n", $translation);
79  $placeholdersStack[] = self::findPlaceholders($pattern, $translation);
80  $translation = self::replaceFields($translation, $placeholdersStack[count($placeholdersStack) - 1], $vars[$langVarToTypeDict[$langVar]]->getParameters(), '[', ']');
81  $placeholdersStack[] = self::findPlaceholders($pattern_old, $translation);
82  $res->lang[$iso2shorthandle] = self::replaceFields($translation, $placeholdersStack[count($placeholdersStack) - 1], $vars[$langVarToTypeDict[$langVar]]->getParameters(), '##', '##');
83  }
84 
85  $res->params = array_diff(
86  array_unique(
87  call_user_func_array('array_merge', $placeholdersStack)
88  ),
89  array_keys($vars[$langVarToTypeDict[$langVar]]->getParameters())
90  );
91  }
92 
93  return $results;
94  }
95 
102  protected static function findPlaceholders($pattern, $translation)
103  {
104  $foundPlaceholders = array();
105  preg_match_all($pattern, $translation, $foundPlaceholders);
106  return (array)$foundPlaceholders[1];
107  }
108 
118  private static function replaceFields($string, $foundPlaceholders, $params, $startTag, $endTage)
119  {
120  foreach($foundPlaceholders as $placeholder)
121  {
122  if(array_key_exists(strtoupper($placeholder), $params))
123  {
124  $string = str_ireplace($startTag . $placeholder . $endTage, $params[strtoupper($placeholder)], $string);
125  }
126  if(array_key_exists(strtolower($placeholder), $params))
127  {
128  $string = str_ireplace($startTag . $placeholder . $endTage, $params[strtolower($placeholder)], $string);
129  }
130  }
131  return $string;
132  }
133 
158  public static function setUserConfig($userid, array $configArray) {
159  global $ilDB;
160 
161  if ($userid != -1) {
162  $channels = self::getAvailableChannels(array('set_by_user'));
163  $types = self::getAvailableTypes(array('set_by_user'));
164  $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');
165  }
166  else {
167  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id=%s';
168  }
169 
170  $types = array('integer');
171  $values = array($userid);
172 
173  // delete old settings
174  $ilDB->manipulateF($query, $types, $values);
175 
176  foreach ($configArray as $type => $channels) {
177  foreach ($channels as $channel => $value) {
178  if (!$value)
179  continue;
180  $ilDB->insert(
182  array(
183  'usr_id' => array('integer', $userid),
184  'module' => array('text', $type),
185  'channel' => array('text', $channel),
186  )
187  );
188  }
189  }
190  }
191 
192  public static function loadUserConfig($userid) {
193  global $ilDB;
194 
195  $query = 'SELECT module, channel FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id = %s';
196  $types = array('integer');
197  $values = array($userid);
198 
199  $res = $ilDB->queryF($query, $types, $values);
200 
201  $result = array();
202 
203  while ($row = $ilDB->fetchAssoc($res)) {
204  if (!$result[$row['module']])
205  $result[$row['module']] = array();
206 
207  $result[$row['module']][] = $row['channel'];
208  }
209 
210  return $result;
211  }
212 
213  public static function enqueueByUsers(ilNotificationConfig $notification, array $userids)
214  {
215  if(!$userids)
216  return;
217 
218  global $ilDB;
219 
220  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
221  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
222 
223  foreach($userids as $userid)
224  {
225  $ilDB->insert(
227  array(
228  'notification_id' => array('integer', $notification_id),
229  'usr_id' => array('integer', $userid),
230  'valid_until' => array('integer', $valid_until),
231  'visible_for' => array('integer', $notification->getVisibleForSeconds())
232  )
233  );
234  }
235  }
236 
237  public static function enqueueByListener(ilNotificationConfig $notification, $ref_id) {
238  global $ilDB;
239 
240  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
241  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
242 
243  $query = 'INSERT INTO ' . ilNotificationSetupHelper::$tbl_notification_queue . ' (notification_id, usr_id, valid_until, visible_for) '
244  .' (SELECT %s, usr_id, %s, %s FROM '. ilNotificationSetupHelper::$tbl_userlistener .' WHERE disabled = 0 AND module = %s AND sender_id = %s)';
245 
246  $types = array('integer', 'integer', 'integer', 'text', 'integer');
247 
248  $values = array($notification_id, $valid_until, $notification->getVisibleForSeconds(), $notification->getType(), $ref_id);
249 
250  $ilDB->manipulateF($query, $types, $values);
251  }
252 /*
253  public static function enqueueByRoles(ilNotificationConfig $notification, array $roles) {
254  if (!$roles)
255  return;
256 
257  global $ilDB;
258 
259  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
260  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
261 
262  foreach($userids as $userid) {
263  $ilDB->insert(
264  ilNotificationSetupHelper::$tbl_notification_queue,
265  array(
266  'notification_id' => array('integer', $notification_id),
267  'usr_id' => array('integer', $userid),
268  'valid_until' => array('integer', $valid_until),
269  )
270  );
271  }
272 
273  }
274 */
275  public static function storeNotification(ilNotificationConfig $notification) {
276  global $ilDB;
277 
279 
280  $ilDB->insert(
282  array(
283  'notification_id' => array('integer', $id),
284  'serialized' => array('text', serialize($notification)),
285  )
286  );
287 
288  return $id;
289  }
290 
291  public static function removeNotification($id) {
292  global $ilDB;
293 
294  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_data . ' WHERE notification_id = ?';
295  $types = array('integer');
296  $values = array($id);
297 
298  $ilDB->manipulateF($query, $types, $values);
299  }
300 
301  public static function getUsersByListener($module, $sender_id) {
302  global $ilDB;
303 
304  $query = 'SELECT usr_id FROM '. ilNotificationSetupHelper::$tbl_userlistener .' WHERE disabled = 0 AND module = %s AND sender_id = %s';
305  $types = array('text', 'integer');
306  $values = array($module, $sender_id);
307 
308  $users = array();
309 
310  $rset = $ilDB->queryF($query, $types, $values);
311  while($row = $ilDB->fetchAssoc($rset)) {
312  $users[] = $row['usr_id'];
313  }
314  return $users;
315  }
316 
317  public static function disableListeners($module, $sender_id) {
318  global $ilDB;
319 
320  $query = 'UPDATE '. ilNotificationSetupHelper::$tbl_userlistener .' SET disabled = 1 WHERE module = %s AND sender_id = %s';
321  $types = array('text', 'integer');
322  $values = array($module, $sender_id);
323 
324  $ilDB->manipulateF($query, $types, $values);
325  }
326 
327  public static function enableListeners($module, $sender_id, array $users = array()) {
328  global $ilDB;
329 
330  $query = 'UPDATE '. ilNotificationSetupHelper::$tbl_userlistener .' SET disabled = 0 WHERE module = %s AND sender_id = %s';
331 
332  if ($users) {
333  $query .= ' ' . $ilDB->in('usr_id', $users);
334  }
335 
336  $types = array('text', 'integer');
337  $values = array($module, $sender_id);
338 
339  $ilDB->manipulateF($query, $types, $values);
340  }
341 
354  public static function registerChannel($name, $title, $description, $class, $classfile, $config_type) {
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  global $ilDB;
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  global $ilDB;
398 
399  $query = 'SELECT channel_name, title, description, class, include, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_channels;
400  if ($config_types)
401  $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
402 
403  $rset = $ilDB->query($query);
404 
405  $result = array();
406 
407  $settings = new ilSetting('notifications');
408 
409  while ($row = $ilDB->fetchAssoc($rset)) {
410  if (!$includeDisabled && !$settings->get('enable_' . $row['channel_name']))
411  continue;
412 
413  $result[$row['channel_name']] = array (
414  'name' => $row['channel_name'],
415  'title' => $row['title'],
416  'description' => $row['description'],
417  'handler' => $row['class'],
418  'include' => $row['include'],
419  'config_type' => $row['config_type'],
420  );
421  }
422 
423  return $result;
424  }
425 
426  public static function getAvailableTypes($config_types = array()) {
427  global $ilDB;
428 
429  $query = 'SELECT type_name, title, description, notification_group, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_types;
430  if ($config_types)
431  $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
432 
433 
434  $rset = $ilDB->query($query);
435 
436  $result = array();
437 
438  while ($row = $ilDB->fetchAssoc($rset)) {
439  $result[$row['type_name']] = array (
440  'name' => $row['type_name'],
441  'title' => $row['title'],
442  'description' => $row['description'],
443  'group' => $row['notification_group'],
444  'config_type' => $row['config_type'],
445  );
446  }
447 
448  return $result;
449  }
450 
451  public static function setConfigTypeForType($type_name, $config_name) {
452  global $ilDB;
453  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_types . ' SET config_type = %s WHERE type_name = %s';
454  $types = array('text', 'text');
455  $values = array($config_name, $type_name);
456  $ilDB->manipulateF($query, $types, $values);
457  }
458 
459  public static function setConfigTypeForChannel($channel_name, $config_name) {
460  global $ilDB;
461  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_channels . ' SET config_type = %s WHERE channel_name = %s';
462  $types = array('text', 'text');
463  $values = array($config_name, $channel_name);
464  $ilDB->manipulateF($query, $types, $values);
465  }
466 
467 
468  public static function getUsersWithCustomConfig(array $userid) {
469  global $ilDB;
470  $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"';
471  $rset = $ilDB->query($query);
472  $result = array();
473  while($row = $ilDB->fetchAssoc($rset)) {
474  $result[$row['usr_id']] = (bool)$row['value'];
475  }
476  return $result;
477  }
478 }
static enqueueByUsers(ilNotificationConfig $notification, array $userids)
ILIAS Setting Class.
$result
static registerChannel($name, $title, $description, $class, $classfile, $config_type)
Registers a new notification channel for distributing notifications.
static storeNotification(ilNotificationConfig $notification)
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)
static enqueueByListener(ilNotificationConfig $notification, $ref_id)
static findPlaceholders($pattern, $translation)
$results
Create styles array
The data for the language used.
static replaceFields($string, $foundPlaceholders, $params, $startTag, $endTage)
static fillPlaceholders($results, $vars, $langVarToTypeDict)
$ref_id
Definition: sahs_server.php:39
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.
$params
Definition: example_049.php:96
static enableListeners($module, $sender_id, array $users=array())