ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilNotificationOSDHandler.php
Go to the documentation of this file.
1 <?php
2 
3 require_once 'Services/Notifications/classes/class.ilNotificationSetupHelper.php';
4 require_once 'Services/Notifications/classes/class.ilNotificationEchoHandler.php';
5 
11  public function notify(ilNotificationObject $notification) {
12  global $ilDB;
13 
15 
16  $ilDB->insert(
18  array(
19  'notification_osd_id' => array('integer', $id),
20  'usr_id' => array('integer', $notification->user->getId()),
21  'serialized' => array('text', serialize($notification)),
22  'valid_until' => array('integer', $notification->baseNotification->getValidForSeconds() ? ($notification->baseNotification->getValidForSeconds() + time()) : 0),
23  'type' => array('text', $notification->baseNotification->getType()),
24  'time_added' => array('integer', time()),
25  )
26  );
27  }
28 
29  public function showSettings($item) {
30  global $lng;
31  $txt = new ilTextInputGUI($lng->txt('polling_intervall'), 'osd_polling_intervall');
32  $txt->setRequired(true);
33  $txt->setInfo($lng->txt('polling_in_seconds'));
34  $txt->setValue('300');
35 
36  $item->addSubItem($txt);
37 
38  return array('osd_polling_intervall');
39  }
40 
41  public static function getNotificationsForUser($user_id, $append_osd_id_to_link = true, $max_age_seconds = 0) {
42  global $ilDB;
43 
44  $query = 'SELECT notification_osd_id, serialized, valid_until, type FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler
45  . ' WHERE usr_id = %s AND (valid_until = 0 OR valid_until > ' . $ilDB->quote( time() ,'integer') . ') AND time_added > %s';
46 
47  $types = array('integer', 'integer');
48  $values = array($user_id, $max_age_seconds ? (time() - $max_age_seconds) : 0);
49 
50  $rset = $ilDB->queryF($query, $types, $values);
51  $notifications = array();
52 
53  while($row = $ilDB->fetchAssoc($rset)) {
54  $row['data'] = unserialize($row['serialized']);
55  unset($row['serialized']);
56 
57  $row['data']->handlerParams = array('general' => $row['data']->handlerParams[''], 'osd' => $row['data']->handlerParams['osd']);
58 
59  if ($append_osd_id_to_link) {
60 
61  if ($row['data']->link) {
62  $row['data']->link = self::appendParamToLink($row['data']->link, 'osd_id', $row['notification_osd_id']);
63  }
64 
65  $row['data']->shortDescription = self::appendOsdIdToLinks($row['data']->shortDescription, $row['notification_osd_id']);
66  $row['data']->longDescription = self::appendOsdIdToLinks($row['data']->longDescription, $row['notification_osd_id']);
67 
68  }
69  $notifications[] = $row;
70  }
71 
73 
74  return $notifications;
75  }
76 
77  private static function appendOsdIdToLinks($subject, $osd_id) {
78  $matches = array();
79  preg_match_all('/href="(.*?)"/', $subject, $matches);
80  if($matches[1]) {
81  foreach($matches[1] as $match) {
82  $match_appended = self::appendParamToLink($match, 'osd_id', $osd_id);
83  $subject = str_replace($match, $match_appended, $subject);
84  }
85  }
86  return $subject;
87  }
88 
96  public static function removeNotification($notification_osd_id) {
97  global $ilDB;
98 
99  $query = 'SELECT usr_id FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
100  $types = array('integer');
101  $values = array($notification_osd_id);
102 
103  $rset = $ilDB->queryF($query, $types, $values);
104 
105  if ($row = $ilDB->fetchAssoc($rset)) {
106 
107  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
108  $types = array('integer');
109  $values = array($notification_osd_id);
110 
111  $ilDB->manipulateF($query, $types, $values);
112 
113  // sends a "delete the given notification" notification using the
114  // osd_maint channel
115  $deletedNotification = new ilNotificationConfig('osd_maint');
116  $deletedNotification->setValidForSeconds(120);
117  $deletedNotification->setTitleVar('deleted');
118  $deletedNotification->setShortDescriptionVar($notification_osd_id);
119  $deletedNotification->setLongDescriptionVar('dummy');
120 
121  require_once 'Services/Notifications/classes/class.ilNotificationSystem.php';
122  ilNotificationSystem::sendNotificationToUsers($deletedNotification, array($row['usr_id']));
123  }
124  }
125 
131  public static function cleanup() {
132  global $ilDB;
133  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE valid_until < ' . $ilDB->quote( time() ,'integer');
134  $ilDB->manipulate($query);
135  }
136 
140  public static function cleanupOnRandom() {
141  $rnd = rand(0, 10000);
142  if ($rnd == 500) {
143  self::cleanup();
144  }
145  }
146 
155  private static function appendParamToLink($link, $param, $value) {
156  if (strpos($link, '?') !== false) {
157  $link .= '&' . $param . '=' . $value;
158  }
159  else {
160  $link .= '?' . $param . '=' . $value;
161  }
162  return $link;
163  }
164 }