ILIAS  Release_4_2_x_branch Revision 61807
 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 
134  public static function setUserConfig($userid, array $configArray) {
135  global $ilDB;
136 
137  if ($userid != -1) {
138  $channels = self::getAvailableChannels(array('set_by_user'));
139  $types = self::getAvailableTypes(array('set_by_user'));
140  $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');
141  }
142  else {
143  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id=%s';
144  }
145 
146  $types = array('integer');
147  $values = array($userid);
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  $ilDB->insert(
156  array(
157  'usr_id' => array('integer', $userid),
158  'module' => array('text', $type),
159  'channel' => array('text', $channel),
160  )
161  );
162  }
163  }
164  }
165 
166  public static function loadUserConfig($userid) {
167  global $ilDB;
168 
169  $query = 'SELECT module, channel FROM ' . ilNotificationSetupHelper::$tbl_userconfig . ' WHERE usr_id = %s';
170  $types = array('integer');
171  $values = array($userid);
172 
173  $res = $ilDB->queryF($query, $types, $values);
174 
175  $result = array();
176 
177  while ($row = $ilDB->fetchAssoc($res)) {
178  if (!$result[$row['module']])
179  $result[$row['module']] = array();
180 
181  $result[$row['module']][] = $row['channel'];
182  }
183 
184  return $result;
185  }
186 
187  public static function enqueueByUsers(ilNotificationConfig $notification, array $userids) {
188  if (!$userids)
189  return;
190 
191  global $ilDB;
192 
193  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
194  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
195 
196  foreach($userids as $userid) {
197  $ilDB->insert(
199  array(
200  'notification_id' => array('integer', $notification_id),
201  'usr_id' => array('integer', $userid),
202  'valid_until' => array('integer', $valid_until),
203  )
204  );
205  }
206 
207  }
208 
209  public static function enqueueByListener(ilNotificationConfig $notification, $ref_id) {
210  global $ilDB;
211 
212  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
213  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
214 
215  $query = 'INSERT INTO ' . ilNotificationSetupHelper::$tbl_notification_queue . ' (notification_id, usr_id, valid_until) '
216  .' (SELECT %s, usr_id, %s FROM '. ilNotificationSetupHelper::$tbl_userlistener .' WHERE disabled = 0 AND module = %s AND sender_id = %s)';
217 
218  $types = array('integer', 'integer', 'text', 'integer');
219 
220  $values = array($notification_id, $valid_until, $notification->getType(), $ref_id);
221 
222  $ilDB->manipulateF($query, $types, $values);
223  }
224 /*
225  public static function enqueueByRoles(ilNotificationConfig $notification, array $roles) {
226  if (!$roles)
227  return;
228 
229  global $ilDB;
230 
231  $notification_id = ilNotificationDatabaseHandler::storeNotification($notification);
232  $valid_until = $notification->getValidForSeconds() ? (time() + $notification->getValidForSeconds()) : 0;
233 
234  foreach($userids as $userid) {
235  $ilDB->insert(
236  ilNotificationSetupHelper::$tbl_notification_queue,
237  array(
238  'notification_id' => array('integer', $notification_id),
239  'usr_id' => array('integer', $userid),
240  'valid_until' => array('integer', $valid_until),
241  )
242  );
243  }
244 
245  }
246 */
247  public static function storeNotification(ilNotificationConfig $notification) {
248  global $ilDB;
249 
251 
252  $ilDB->insert(
254  array(
255  'notification_id' => array('integer', $id),
256  'serialized' => array('text', serialize($notification)),
257  )
258  );
259 
260  return $id;
261  }
262 
263  public static function removeNotification($id) {
264  global $ilDB;
265 
266  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_data . ' WHERE notification_id = ?';
267  $types = array('integer');
268  $values = array($id);
269 
270  $ilDB->manipulateF($query, $types, $values);
271  }
272 
273  public static function getUsersByListener($module, $sender_id) {
274  global $ilDB;
275 
276  $query = 'SELECT usr_id FROM '. ilNotificationSetupHelper::$tbl_userlistener .' WHERE disabled = 0 AND module = %s AND sender_id = %s';
277  $types = array('text', 'integer');
278  $values = array($module, $sender_id);
279 
280  $users = array();
281 
282  $rset = $ilDB->queryF($query, $types, $values);
283  while($row = $ilDB->fetchAssoc($rset)) {
284  $users[] = $row['usr_id'];
285  }
286  return $users;
287  }
288 
289  public static function disableListeners($module, $sender_id) {
290  global $ilDB;
291 
292  $query = 'UPDATE '. ilNotificationSetupHelper::$tbl_userlistener .' SET disabled = 1 WHERE module = %s AND sender_id = %s';
293  $types = array('text', 'integer');
294  $values = array($module, $sender_id);
295 
296  $ilDB->manipulateF($query, $types, $values);
297  }
298 
299  public static function enableListeners($module, $sender_id, array $users = array()) {
300  global $ilDB;
301 
302  $query = 'UPDATE '. ilNotificationSetupHelper::$tbl_userlistener .' SET disabled = 0 WHERE module = %s AND sender_id = %s';
303 
304  if ($users) {
305  $query .= ' ' . $ilDB->in('usr_id', $users);
306  }
307 
308  $types = array('text', 'integer');
309  $values = array($module, $sender_id);
310 
311  $ilDB->manipulateF($query, $types, $values);
312  }
313 
314  public static function registerChannel($name, $title, $description, $class, $classfile, $config_type) {
315  global $ilDB;
316 
317  $ilDB->insert(
319  array(
320  'channel_name' => array('text', $name),
321  'title' => array('text', $title),
322  'description' => array('text', $description),
323  'class' => array('text', $class),
324  'include' => array('text', $classfile),
325  'config_type' => array('text', $config_type),
326  )
327  );
328  }
329 
330  public static function registerType($name, $title, $description, $notification_group, $config_type) {
331  global $ilDB;
332 
333  $ilDB->insert(
335  array(
336  'type_name' => array('text', $name),
337  'title' => array('text', $title),
338  'description' => array('text', $description),
339  'notification_group' => array('text', $notification_group),
340  'config_type' => array('text', $config_type),
341  )
342  );
343  }
344 
345  public static function getAvailableChannels($config_types = array(), $includeDisabled = false) {
346  global $ilDB;
347 
348  $query = 'SELECT channel_name, title, description, class, include, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_channels;
349  if ($config_types)
350  $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
351 
352  $rset = $ilDB->query($query);
353 
354  $result = array();
355 
356  $settings = new ilSetting('notifications');
357 
358  while ($row = $ilDB->fetchAssoc($rset)) {
359  if (!$includeDisabled && !$settings->get('enable_' . $row['channel_name']))
360  continue;
361 
362  $result[$row['channel_name']] = array (
363  'name' => $row['channel_name'],
364  'title' => $row['title'],
365  'description' => $row['description'],
366  'handler' => $row['class'],
367  'include' => $row['include'],
368  'config_type' => $row['config_type'],
369  );
370  }
371 
372  return $result;
373  }
374 
375  public static function getAvailableTypes($config_types = array()) {
376  global $ilDB;
377 
378  $query = 'SELECT type_name, title, description, notification_group, config_type FROM ' . ilNotificationSetupHelper::$tbl_notification_types;
379  if ($config_types)
380  $query .= ' WHERE ' . $ilDB->in('config_type', $config_types, false, 'text');
381 
382 
383  $rset = $ilDB->query($query);
384 
385  $result = array();
386 
387  while ($row = $ilDB->fetchAssoc($rset)) {
388  $result[$row['type_name']] = array (
389  'name' => $row['type_name'],
390  'title' => $row['title'],
391  'description' => $row['description'],
392  'group' => $row['notification_group'],
393  'config_type' => $row['config_type'],
394  );
395  }
396 
397  return $result;
398  }
399 
400  public static function setConfigTypeForType($type_name, $config_name) {
401  global $ilDB;
402  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_types . ' SET config_type = %s WHERE type_name = %s';
403  $types = array('text', 'text');
404  $values = array($config_name, $type_name);
405  $ilDB->manipulateF($query, $types, $values);
406  }
407 
408  public static function setConfigTypeForChannel($channel_name, $config_name) {
409  global $ilDB;
410  $query = 'UPDATE ' . ilNotificationSetupHelper::$tbl_notification_channels . ' SET config_type = %s WHERE channel_name = %s';
411  $types = array('text', 'text');
412  $values = array($config_name, $channel_name);
413  $ilDB->manipulateF($query, $types, $values);
414  }
415 
416 
417  public static function getUsersWithCustomConfig(array $userid) {
418  global $ilDB;
419  $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"';
420  $rset = $ilDB->query($query);
421  $result = array();
422  while($row = $ilDB->fetchAssoc($rset)) {
423  $result[$row['usr_id']] = (bool)$row['value'];
424  }
425  return $result;
426  }
427 }