ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilNotificationOSDHandler.php
Go to the documentation of this file.
1<?php
2
3require_once 'Services/Notifications/classes/class.ilNotificationSetupHelper.php';
4require_once 'Services/Notifications/classes/class.ilNotificationEchoHandler.php';
5
11{
12
14 private $database;
15
17 private $language;
18
25 public function __construct(
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
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) {
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}
An exception for terminatinating execution or to throw for unit testing.
language handling
Basic notification handler that dumps basic notification information to stdout.
Notification handler for senden a notification popup to the recipients browser.
static appendOsdIdToLinks($subject, $osd_id)
static removeNotification($notification_osd_id)
Removes a notifcation and triggers a follow up notification to remove the notification from the brows...
static cleanup()
Remove orphaned notifications.
static cleanupOnRandom()
Exec self::clean with a probability of 1%.
notify(ilNotificationObject $notification)
__construct(\ilDBInterface $database=null, \ilLanguage $language=null, \ILIAS\DI\Container $dic=null)
ilNotificationOSDHandler constructor.
static getNotificationsForUser($user_id, $append_osd_id_to_link=true, $max_age_seconds=0)
static appendParamToLink($link, $param, $value)
Helper to append an additional parameter to an existing url.
A concrete notification based on the ilNotificationConfiguration and returned by ilNotificationConfig...
This class represents a text property in a property form.
$txt
Definition: error.php:13
Interface ilDBInterface.
language()
Definition: language.php:2
Class HTTPServicesTest.
Class ChatMainBarProvider \MainMenu\Provider.
$query
$dic
Definition: result.php:13
global $ilDB
$DIC
Definition: xapitoken.php:46
$param
Definition: xapitoken.php:31