ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups 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)
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  if (!$userids)
215  return;
216 
217  global $ilDB;
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  )
230  );
231  }
232 
233  }
234 
235  public static function enqueueByListener(ilNotificationConfig $notification, $ref_id) {
236  global $ilDB;
237 
238  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
239  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
240 
241  $query = 'INSERT INTO ' . ilNotificationSetupHelper::$tbl_notification_queue . ' (notification_id, usr_id, valid_until) '
242  .' (SELECT %s, usr_id, %s FROM '. ilNotificationSetupHelper::$tbl_userlistener .' WHERE disabled = 0 AND module = %s AND sender_id = %s)';
243 
244  $types = array('integer', 'integer', 'text', 'integer');
245 
246  $values = array($notification_id, $valid_until, $notification->getType(), $ref_id);
247 
248  $ilDB->manipulateF($query, $types, $values);
249  }
250 /*
251  public static function enqueueByRoles(ilNotificationConfig $notification, array $roles) {
252  if (!$roles)
253  return;
254 
255  global $ilDB;
256 
257  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
258  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
259 
260  foreach($userids as $userid) {
261  $ilDB->insert(
262  ilNotificationSetupHelper::$tbl_notification_queue,
263  array(
264  'notification_id' => array('integer', $notification_id),
265  'usr_id' => array('integer', $userid),
266  'valid_until' => array('integer', $valid_until),
267  )
268  );
269  }
270 
271  }
272 */
273  public static function storeNotification(ilNotificationConfig $notification) {
274  global $ilDB;
275 
277 
278  $ilDB->insert(
280  array(
281  'notification_id' => array('integer', $id),
282  'serialized' => array('text', serialize($notification)),
283  )
284  );
285 
286  return $id;
287  }
288 
289  public static function removeNotification($id) {
290  global $ilDB;
291 
292  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_data . ' WHERE notification_id = ?';
293  $types = array('integer');
294  $values = array($id);
295 
296  $ilDB->manipulateF($query, $types, $values);
297  }
298 
299  public static function getUsersByListener($module, $sender_id) {
300  global $ilDB;
301 
302  $query = 'SELECT usr_id FROM '. ilNotificationSetupHelper::$tbl_userlistener .' WHERE disabled = 0 AND module = %s AND sender_id = %s';
303  $types = array('text', 'integer');
304  $values = array($module, $sender_id);
305 
306  $users = array();
307 
308  $rset = $ilDB->queryF($query, $types, $values);
309  while($row = $ilDB->fetchAssoc($rset)) {
310  $users[] = $row['usr_id'];
311  }
312  return $users;
313  }
314 
315  public static function disableListeners($module, $sender_id) {
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  global $ilDB;
327 
328  $query = 'UPDATE '. ilNotificationSetupHelper::$tbl_userlistener .' SET disabled = 0 WHERE module = %s AND sender_id = %s';
329 
330  if ($users) {
331  $query .= ' ' . $ilDB->in('usr_id', $users);
332  }
333 
334  $types = array('text', 'integer');
335  $values = array($module, $sender_id);
336 
337  $ilDB->manipulateF($query, $types, $values);
338  }
339 
352  public static function registerChannel($name, $title, $description, $class, $classfile, $config_type) {
353  global $ilDB;
354 
355  $ilDB->insert(
357  array(
358  'channel_name' => array('text', $name),
359  'title' => array('text', $title),
360  'description' => array('text', $description),
361  'class' => array('text', $class),
362  'include' => array('text', $classfile),
363  'config_type' => array('text', $config_type),
364  )
365  );
366  }
367 
379  public static function registerType($name, $title, $description, $notification_group, $config_type) {
380  global $ilDB;
381 
382  $ilDB->insert(
384  array(
385  'type_name' => array('text', $name),
386  'title' => array('text', $title),
387  'description' => array('text', $description),
388  'notification_group' => array('text', $notification_group),
389  'config_type' => array('text', $config_type),
390  )
391  );
392  }
393 
394  public static function getAvailableChannels($config_types = array(), $includeDisabled = false) {
395  global $ilDB;
396 
397  $query = 'SELECT channel_name, title, description, class, include, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_channels;
398  if ($config_types)
399  $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
400 
401  $rset = $ilDB->query($query);
402 
403  $result = array();
404 
405  $settings = new ilSetting('notifications');
406 
407  while ($row = $ilDB->fetchAssoc($rset)) {
408  if (!$includeDisabled && !$settings->get('enable_' . $row['channel_name']))
409  continue;
410 
411  $result[$row['channel_name']] = array (
412  'name' => $row['channel_name'],
413  'title' => $row['title'],
414  'description' => $row['description'],
415  'handler' => $row['class'],
416  'include' => $row['include'],
417  'config_type' => $row['config_type'],
418  );
419  }
420 
421  return $result;
422  }
423 
424  public static function getAvailableTypes($config_types = array()) {
425  global $ilDB;
426 
427  $query = 'SELECT type_name, title, description, notification_group, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_types;
428  if ($config_types)
429  $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
430 
431 
432  $rset = $ilDB->query($query);
433 
434  $result = array();
435 
436  while ($row = $ilDB->fetchAssoc($rset)) {
437  $result[$row['type_name']] = array (
438  'name' => $row['type_name'],
439  'title' => $row['title'],
440  'description' => $row['description'],
441  'group' => $row['notification_group'],
442  'config_type' => $row['config_type'],
443  );
444  }
445 
446  return $result;
447  }
448 
449  public static function setConfigTypeForType($type_name, $config_name) {
450  global $ilDB;
451  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_types . ' SET config_type = %s WHERE type_name = %s';
452  $types = array('text', 'text');
453  $values = array($config_name, $type_name);
454  $ilDB->manipulateF($query, $types, $values);
455  }
456 
457  public static function setConfigTypeForChannel($channel_name, $config_name) {
458  global $ilDB;
459  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_channels . ' SET config_type = %s WHERE channel_name = %s';
460  $types = array('text', 'text');
461  $values = array($config_name, $channel_name);
462  $ilDB->manipulateF($query, $types, $values);
463  }
464 
465 
466  public static function getUsersWithCustomConfig(array $userid) {
467  global $ilDB;
468  $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"';
469  $rset = $ilDB->query($query);
470  $result = array();
471  while($row = $ilDB->fetchAssoc($rset)) {
472  $result[$row['usr_id']] = (bool)$row['value'];
473  }
474  return $result;
475  }
476 }