ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilNotificationSystem.php
Go to the documentation of this file.
1 <?php
14 
15  private static $instance;
16 
17  private $handler = array();
18 
19  private $defaultLanguage = 'en';
20 
21  private function __construct() {
22  require_once 'Services/Notifications/classes/class.ilNotificationEchoHandler.php';
23  require_once 'Services/Notifications/classes/class.ilNotificationOSDHandler.php';
24  require_once 'Services/Notifications/classes/class.ilNotificationMailHandler.php';
25 
26  // add default handlers
27  $this->addHandler('echo', new ilNotificationEchoHandler());
28  $this->addHandler('osd', new ilNotificationOSDHandler());
29  $this->addHandler('mail', new ilNotificationMailHandler());
30 
31  }
32 
33  private static function getInstance() {
34  if (!self::$instance) {
35  self::$instance = new self();
36  }
37  return self::$instance;
38  }
39 
46  private function addHandler($channel, ilNotificationHandler $handler) {
47  if (!array_key_exists($channel, $this->handler) || !is_array($this->handler[$channel]))
48  $this->handler[$channel] = array();
49 
50  $this->handler[$channel][] = $handler;
51  }
52 
61  private function toUsers(ilNotificationConfig $notification, $users, $processAsync = false) {
62 
63  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
64  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
65 
66 
67  // if async processing is disabled send them immediately
68  if ($processAsync == false) {
69 
70  // loading the default configuration
72  $usersWithCustomConfig = ilNotificationDatabaseHandler::getUsersWithCustomConfig($users);
73 
74  // @todo this loop might be obsolet :)
75  foreach($users as $user_id) {
76  if ($usersWithCustomConfig[$user_id]) {
78  }
79  }
80 
81  // load all available channels
83  // load all available types
85  // preload translation vars
86  $lang = ilNotificationDatabaseHandler::getTranslatedLanguageVariablesOfNotificationParameters($notification->getLanguageParameters());
87 
88  $user_by_handler = array();
89 
90  // check if the type allows custom user configurations for determining
91  // the output channel (e.g. send chat notifications only via osd)
92  if ($types[$notification->getType()]['config_type'] == 'set_by_user') {
93  $it = new ilNotificationUserIterator($notification->getType(), $users);
94 
95  $channelsByAdmin = false;
96 
97  // add the user to each channel he configured in his own configuration
98  foreach($it as $usr_id => $data) {
99  // the configured user channel is (currently) not known
100  if (!$channels[$data['channel']])
101  continue;
102 
103  if (!$user_by_handler[$data['channel']])
104  $user_by_handler[$data['channel']] = array();
105 
106  $user_by_handler[$data['channel']][] = $usr_id;
107  }
108  }
109  // if type is configured to allow settings only applied by admin
110  else if ($types[$notification->getType()]['config_type'] != 'disabled') {
111  $channelsByAdmin = true;
112  //$user_by_handler = array();
113 
114  if (isset($adminConfig[$notification->getType()])) {
115 
116  foreach($adminConfig[$notification->getType()] as $channel) {
117  if (!$channels[$channel])
118  continue;
119  $user_by_handler[$channel] = $users;
120 
121  }
122  }
123  }
124 
125 
126  $userCache = array();
127 
128  // process the notifications for each output channel
129  foreach ($user_by_handler as $handler => $users) {
130  $handler = $this->handler[$handler];
131  // and process each user for the current output channel
132  foreach ($users as $userId) {
133  if (!$userCache[$userId]) {
134  $user = ilObjectFactory::getInstanceByObjId($userId, false);
135  if (!$user || !($user instanceof \ilObjUser)) {
136  continue;
137  }
138  $userCache[$userId] = $user;
139  }
140  $user = $userCache[$userId];
141 
142  // optain the message instance for the user
143  // @todo this step could be cached on a per user basis
144  // as it is independed from the output handler
145  $instance = $notification->getUserInstance($user, $lang, $this->defaultLanguage);
146  foreach ($handler as $h) {
147  // fire the notification
148  $h->notify($instance);
149  }
150  }
151  }
152  }
153  // use async processing
154  else {
155  // just enque the current configuration
156  ilNotificationDatabaseHandler::enqueueByUsers($notification, $users);
157  }
158  }
159 
168  private function toListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false) {
169  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
170  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
171 
172  if ($processAsync == false) {
174  self::toUsers($notification, $users, false);
175  if ($notification->hasDisableAfterDeliverySet()) {
177  }
178  }
179  else {
181  }
182  }
183 
193  private function toRoles(ilNotificationConfig $notification, array $roles, $processAsync = false) {
194  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
195  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
196 
197  global $rbacreview;
198 
199  $users = array();
200  foreach($roles as $role) {
201  $users[] = $rbacreview->assignedUsers($role);
202  }
203  // make sure to handle every user only once
204  $users = array_unique(call_user_func_array('array_merge', $users));
205 
206  self::toUsers($notification, $users, $processAsync);
207  }
208 
216  public static function sendNotificationToUsers(ilNotificationConfig $notification, $users, $processAsync = false) {
217  self::getInstance()->toUsers($notification, $users, $processAsync);
218  }
219 
227  public static function sendNotificationToListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false) {
228  self::getInstance()->toListeners($notification, $ref_id, $processAsync);
229  }
230 
238  public static function sendNotificationToRoles(ilNotificationConfig $notification, array $roles, $processAsync = false) {
239  self::getInstance()->toRoles($notification, $roles, $processAsync);
240  }
241 
242  public static function enableListeners($module, $ref_id) {
243  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
245  }
246 
247  public static function enableUserListeners($module, $ref_id, array $users) {
248  if (!$users)
249  return;
250  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
252  }
253 }
254 ?>
static enqueueByUsers(ilNotificationConfig $notification, array $userids)
addHandler($channel, ilNotificationHandler $handler)
Registers a new handler for the given channel name.
Notification handler for sending notifications the to recipients email address.
toRoles(ilNotificationConfig $notification, array $roles, $processAsync=false)
Send a notification to a list of roles.
$h
wrapper for iterating a list of user settings by providing the user ids
static sendNotificationToUsers(ilNotificationConfig $notification, $users, $processAsync=false)
static enableListeners($module, $ref_id)
toUsers(ilNotificationConfig $notification, $users, $processAsync=false)
Creates the user notifications and send them.
Base class for notification handlers.
Describes a notification and provides methods for publishing this notification.
toListeners(ilNotificationConfig $notification, $ref_id, $processAsync=false)
Sends the notification to all listener which are subscribed to the given ref_id.
static enqueueByListener(ilNotificationConfig $notification, $ref_id)
Notification handler for senden a notification popup to the recipients browser.
Basic notification handler that dumps basic notification information to stdout.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
Create styles array
The data for the language used.
Main notification handling routines for sending notifications to recipients.
$ref_id
Definition: sahs_server.php:39
static enableUserListeners($module, $ref_id, array $users)
for($i=1; $i<=count($kw_cases_sel); $i+=1) $lang
Definition: langwiz.php:349
static sendNotificationToRoles(ilNotificationConfig $notification, array $roles, $processAsync=false)
static getAvailableChannels($config_types=array(), $includeDisabled=false)
static sendNotificationToListeners(ilNotificationConfig $notification, $ref_id, $processAsync=false)
getUserInstance(ilObjUser $user, $languageVars, $defaultLanguage)
static enableListeners($module, $sender_id, array $users=array())