ILIAS  Release_4_2_x_branch Revision 61807
 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 
7  public function notify(ilNotificationObject $notification) {
8  global $ilDB;
9 
11 
12  $ilDB->insert(
14  array(
15  'notification_osd_id' => array('integer', $id),
16  'usr_id' => array('integer', $notification->user->getId()),
17  'serialized' => array('text', serialize($notification)),
18  'valid_until' => array('integer', $notification->baseNotification->getValidForSeconds() ? ($notification->baseNotification->getValidForSeconds() + time()) : 0),
19  'type' => array('text', $notification->baseNotification->getType()),
20  'time_added' => array('integer', time()),
21  )
22  );
23  }
24 
25  public function showSettings($item) {
26  global $lng;
27  $txt = new ilTextInputGUI($lng->txt('polling_intervall'), 'osd_polling_intervall');
28  $txt->setRequired(true);
29  $txt->setInfo($lng->txt('polling_in_seconds'));
30  $txt->setValue('300');
31 
32  $item->addSubItem($txt);
33 
34  return array('osd_polling_intervall');
35  }
36 
37  public static function getNotificationsForUser($user_id, $append_osd_id_to_link = true, $max_age_seconds = 0) {
38  global $ilDB;
39 
40  $query = 'SELECT notification_osd_id, serialized, valid_until, type FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler
41  . ' WHERE usr_id = %s AND (valid_until = 0 OR valid_until > ' . $ilDB->quote( time() ,'integer') . ') AND time_added > %s';
42 
43  $types = array('integer', 'integer');
44  $values = array($user_id, $max_age_seconds ? (time() - $max_age_seconds) : 0);
45 
46  $rset = $ilDB->queryF($query, $types, $values);
47  $notifications = array();
48 
49  while($row = $ilDB->fetchAssoc($rset)) {
50  $row['data'] = unserialize($row['serialized']);
51  unset($row['serialized']);
52 
53  $row['data']->handlerParams = array('general' => $row['data']->handlerParams[''], 'osd' => $row['data']->handlerParams['osd']);
54 
55  if ($append_osd_id_to_link) {
56 
57  if ($row['data']->link) {
58  $row['data']->link = self::appendParamToLink($row['data']->link, 'osd_id', $row['notification_osd_id']);
59  }
60 
61  $row['data']->shortDescription = self::appendOsdIdToLinks($row['data']->shortDescription, $row['notification_osd_id']);
62  $row['data']->longDescription = self::appendOsdIdToLinks($row['data']->longDescription, $row['notification_osd_id']);
63 
64  }
65  $notifications[] = $row;
66  }
67 
69 
70  return $notifications;
71  }
72 
73  private static function appendOsdIdToLinks($subject, $osd_id) {
74  $matches = array();
75  preg_match_all('/href="(.*?)"/', $subject, $matches);
76  if($matches[1]) {
77  foreach($matches[1] as $match) {
78  $match_appended = self::appendParamToLink($match, 'osd_id', $osd_id);
79  $subject = str_replace($match, $match_appended, $subject);
80  }
81  }
82  return $subject;
83  }
84 
85  public static function removeNotification($notification_osd_id) {
86  global $ilDB;
87 
88  $query = 'SELECT usr_id FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
89  $types = array('integer');
90  $values = array($notification_osd_id);
91 
92  $rset = $ilDB->queryF($query, $types, $values);
93 
94  if ($row = $ilDB->fetchAssoc($rset)) {
95 
96  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
97  $types = array('integer');
98  $values = array($notification_osd_id);
99 
100  $ilDB->manipulateF($query, $types, $values);
101 
102  $deletedNotification = new ilNotificationConfig('osd_maint');
103  $deletedNotification->setValidForSeconds(120);
104  $deletedNotification->setTitleVar('deleted');
105  $deletedNotification->setShortDescriptionVar($notification_osd_id);
106  $deletedNotification->setLongDescriptionVar('dummy');
107 
108  require_once 'Services/Notifications/classes/class.ilNotificationSystem.php';
109  ilNotificationSystem::sendNotificationToUsers($deletedNotification, array($row['usr_id']));
110  }
111  }
112 
113  public static function cleanup() {
114  global $ilDB;
115  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE valid_until < ' . $ilDB->quote( time() ,'integer');
116  $ilDB->manipulate($query);
117  }
118 
119  public static function cleanupOnRandom() {
120  $rnd = rand(0, 10000);
121  if ($rnd == 500) {
122  self::cleanup();
123  }
124  }
125 
126  private static function appendParamToLink($link, $param, $value) {
127  if (strpos($link, '?') !== false) {
128  $link .= '&' . $param . '=' . $value;
129  }
130  else {
131  $link .= '?' . $param . '=' . $value;
132  }
133  return $link;
134  }
135 }