ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  'visible_for' => array('integer', $notification->baseNotification->getVisibleForSeconds() ? $notification->baseNotification->getVisibleForSeconds() : 0),
24  'type' => array('text', $notification->baseNotification->getType()),
25  'time_added' => array('integer', time()),
26  )
27  );
28  }
29 
30  public function showSettings($item) {
31  global $lng;
32  $txt = new ilTextInputGUI($lng->txt('polling_intervall'), 'osd_polling_intervall');
33  $txt->setRequired(true);
34  $txt->setInfo($lng->txt('polling_in_seconds'));
35  $txt->setValue('300');
36 
37  $item->addSubItem($txt);
38 
39  return array('osd_polling_intervall');
40  }
41 
42  public static function getNotificationsForUser($user_id, $append_osd_id_to_link = true, $max_age_seconds = 0) {
43  global $ilDB;
44 
45  $query = 'SELECT notification_osd_id, serialized, valid_until, visible_for, type FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler
46  . ' WHERE usr_id = %s AND (valid_until = 0 OR valid_until > ' . $ilDB->quote( time() ,'integer') . ') AND time_added > %s';
47 
48  $types = array('integer', 'integer');
49  $values = array($user_id, $max_age_seconds ? (time() - $max_age_seconds) : 0);
50 
51  $rset = $ilDB->queryF($query, $types, $values);
52  $notifications = array();
53 
54  while($row = $ilDB->fetchAssoc($rset)) {
55  $row['data'] = unserialize($row['serialized']);
56  unset($row['serialized']);
57 
58  $row['data']->handlerParams = array('general' => $row['data']->handlerParams[''], 'osd' => $row['data']->handlerParams['osd']);
59 
60  if ($append_osd_id_to_link) {
61 
62  if ($row['data']->link) {
63  $row['data']->link = self::appendParamToLink($row['data']->link, 'osd_id', $row['notification_osd_id']);
64  }
65 
66  $row['data']->shortDescription = self::appendOsdIdToLinks($row['data']->shortDescription, $row['notification_osd_id']);
67  $row['data']->longDescription = self::appendOsdIdToLinks($row['data']->longDescription, $row['notification_osd_id']);
68 
69  }
70  $notifications[] = $row;
71  }
72 
73  self::cleanupOnRandom();
74 
75  return $notifications;
76  }
77 
78  private static function appendOsdIdToLinks($subject, $osd_id) {
79  $matches = array();
80  preg_match_all('/href="(.*?)"/', $subject, $matches);
81  if($matches[1]) {
82  foreach($matches[1] as $match) {
83  $match_appended = self::appendParamToLink($match, 'osd_id', $osd_id);
84  $subject = str_replace($match, $match_appended, $subject);
85  }
86  }
87  return $subject;
88  }
89 
97  public static function removeNotification($notification_osd_id) {
98  global $ilDB;
99 
100  $query = 'SELECT usr_id FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
101  $types = array('integer');
102  $values = array($notification_osd_id);
103 
104  $rset = $ilDB->queryF($query, $types, $values);
105 
106  if ($row = $ilDB->fetchAssoc($rset)) {
107 
108  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
109  $types = array('integer');
110  $values = array($notification_osd_id);
111 
112  $ilDB->manipulateF($query, $types, $values);
113 
114  // sends a "delete the given notification" notification using the
115  // osd_maint channel
116  /*$deletedNotification = new ilNotificationConfig('osd_maint');
117  $deletedNotification->setValidForSeconds(120);
118  $deletedNotification->setTitleVar('deleted');
119  $deletedNotification->setShortDescriptionVar($notification_osd_id);
120  $deletedNotification->setLongDescriptionVar('dummy');
121 
122  require_once 'Services/Notifications/classes/class.ilNotificationSystem.php';
123  ilNotificationSystem::sendNotificationToUsers($deletedNotification, array($row['usr_id']));*/
124  }
125  }
126 
132  public static function cleanup() {
133  global $ilDB;
134  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE valid_until < ' . $ilDB->quote( time() ,'integer');
135  $ilDB->manipulate($query);
136  }
137 
141  public static function cleanupOnRandom() {
142  $rnd = rand(0, 10000);
143  if ($rnd == 500) {
144  self::cleanup();
145  }
146  }
147 
156  private static function appendParamToLink($link, $param, $value) {
157  if (strpos($link, '?') !== false) {
158  $link .= '&' . $param . '=' . $value;
159  }
160  else {
161  $link .= '?' . $param . '=' . $value;
162  }
163  return $link;
164  }
165 }
static removeNotification($notification_osd_id)
Removes a notifcation and triggers a follow up notification to remove the notification from the brows...
A concrete notification based on the ilNotificationConfiguration and returned by ilNotificationConfig...
static appendParamToLink($link, $param, $value)
Helper to append an additional parameter to an existing url.
Notification handler for senden a notification popup to the recipients browser.
This class represents a text property in a property form.
Basic notification handler that dumps basic notification information to stdout.
$txt
Definition: error.php:12
Create styles array
The data for the language used.
static cleanup()
Remove orphaned notifications.
static getNotificationsForUser($user_id, $append_osd_id_to_link=true, $max_age_seconds=0)
global $lng
Definition: privfeed.php:17
notify(ilNotificationObject $notification)
global $ilDB
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static appendOsdIdToLinks($subject, $osd_id)
static cleanupOnRandom()
Exec self::clean with a probability of 1%.