ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 {
12 
14  private $database;
15 
17  private $language;
18 
25  public function __construct(
26  \ilDBInterface $database = null,
27  \ilLanguage $language = null,
28  \ILIAS\DI\Container $dic = null
29  ) {
30  if ($dic === null) {
31  global $DIC;
32  $dic = $DIC;
33  }
34 
35  if ($database === null) {
36  $database = $dic->database();
37  }
38  $this->database = $database;
39 
40  if ($language === null) {
41  $language = $dic->language();
42  }
43  $this->language = $language;
44  }
45 
46  public function notify(ilNotificationObject $notification)
47  {
49 
50  $this->database->insert(
52  array(
53  'notification_osd_id' => array('integer', $id),
54  'usr_id' => array('integer', $notification->user->getId()),
55  'serialized' => array('text', serialize($notification)),
56  'valid_until' => array('integer', $notification->baseNotification->getValidForSeconds() ? ($notification->baseNotification->getValidForSeconds() + time()) : 0),
57  'visible_for' => array('integer', $notification->baseNotification->getVisibleForSeconds() ? $notification->baseNotification->getVisibleForSeconds() : 0),
58  'type' => array('text', $notification->baseNotification->getType()),
59  'time_added' => array('integer', time()),
60  )
61  );
62  }
63 
64  public function showSettings($item)
65  {
66  $txt = new ilTextInputGUI($this->language->txt('polling_intervall'), 'osd_polling_intervall');
67  $txt->setRequired(true);
68  $txt->setInfo($this->language->txt('polling_in_seconds'));
69  $txt->setValue('300');
70 
71  $item->addSubItem($txt);
72 
73  return array('osd_polling_intervall');
74  }
75 
76  public static function getNotificationsForUser($user_id, $append_osd_id_to_link = true, $max_age_seconds = 0)
77  {
78  global $DIC;
79 
80  $ilDB = $DIC->database();
81 
82  $query = 'SELECT notification_osd_id, serialized, valid_until, visible_for, type FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler
83  . ' WHERE usr_id = %s AND (valid_until = 0 OR valid_until > ' . $ilDB->quote(time(), 'integer') . ') AND time_added > %s';
84 
85  $types = array('integer', 'integer');
86  $values = array($user_id, $max_age_seconds ? (time() - $max_age_seconds) : 0);
87 
88  $rset = $ilDB->queryF($query, $types, $values);
89  $notifications = array();
90 
91  while ($row = $ilDB->fetchAssoc($rset)) {
92  $row['data'] = unserialize($row['serialized']);
93  unset($row['serialized']);
94 
95  $row['data']->handlerParams = array('general' => $row['data']->handlerParams[''], 'osd' => $row['data']->handlerParams['osd']);
96 
97  if ($append_osd_id_to_link) {
98  if ($row['data']->link) {
99  $row['data']->link = self::appendParamToLink($row['data']->link, 'osd_id', $row['notification_osd_id']);
100  }
101 
102  $row['data']->shortDescription = self::appendOsdIdToLinks($row['data']->shortDescription, $row['notification_osd_id']);
103  $row['data']->longDescription = self::appendOsdIdToLinks($row['data']->longDescription, $row['notification_osd_id']);
104  }
105  $notifications[] = $row;
106  }
107 
108  self::cleanupOnRandom();
109 
110  return $notifications;
111  }
112 
113  private static function appendOsdIdToLinks($subject, $osd_id)
114  {
115  $matches = array();
116  preg_match_all('/href="(.*?)"/', $subject, $matches);
117  if ($matches[1]) {
118  foreach ($matches[1] as $match) {
119  $match_appended = self::appendParamToLink($match, 'osd_id', $osd_id);
120  $subject = str_replace($match, $match_appended, $subject);
121  }
122  }
123  return $subject;
124  }
125 
133  public static function removeNotification($notification_osd_id)
134  {
135  global $DIC;
136 
137  $ilDB = $DIC->database();
138 
139  $query = 'SELECT usr_id FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
140  $types = array('integer');
141  $values = array($notification_osd_id);
142 
143  $rset = $ilDB->queryF($query, $types, $values);
144 
145  if ($row = $ilDB->fetchAssoc($rset)) {
146  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
147  $types = array('integer');
148  $values = array($notification_osd_id);
149 
150  $ilDB->manipulateF($query, $types, $values);
151  }
152  }
153 
159  public static function cleanup()
160  {
161  global $DIC;
162 
163  $ilDB = $DIC->database();
164 
165  $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE valid_until < ' . $ilDB->quote(time(), 'integer');
166  $ilDB->manipulate($query);
167  }
168 
172  public static function cleanupOnRandom()
173  {
174  $rnd = rand(0, 10000);
175  if ($rnd == 500) {
176  self::cleanup();
177  }
178  }
179 
188  private static function appendParamToLink($link, $param, $value)
189  {
190  if (strpos($link, '?') !== false) {
191  $link .= '&' . $param . '=' . $value;
192  } else {
193  $link .= '?' . $param . '=' . $value;
194  }
195  return $link;
196  }
197 }
static removeNotification($notification_osd_id)
Removes a notifcation and triggers a follow up notification to remove the notification from the brows...
global $DIC
Definition: saml.php:7
A concrete notification based on the ilNotificationConfiguration and returned by ilNotificationConfig...
Class BaseForm.
if(!array_key_exists('StateId', $_REQUEST)) $id
static appendParamToLink($link, $param, $value)
Helper to append an additional parameter to an existing url.
$values
Class HTTPServicesTest.
__construct(\ilDBInterface $database=null, \ilLanguage $language=null, \ILIAS\DI\Container $dic=null)
ilNotificationOSDHandler constructor.
This class represents a text property in a property form.
$query
Basic notification handler that dumps basic notification information to stdout.
$txt
Definition: error.php:11
$row
static cleanup()
Remove orphaned notifications.
static getNotificationsForUser($user_id, $append_osd_id_to_link=true, $max_age_seconds=0)
notify(ilNotificationObject $notification)
global $ilDB
language handling
static appendOsdIdToLinks($subject, $osd_id)
static cleanupOnRandom()
Exec self::clean with a probability of 1%.