ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  {
23  require_once 'Services/Notifications/classes/class.ilNotificationEchoHandler.php';
24  require_once 'Services/Notifications/classes/class.ilNotificationOSDHandler.php';
25  require_once 'Services/Notifications/classes/class.ilNotificationMailHandler.php';
26 
27  // add default handlers
28  $this->addHandler('echo', new ilNotificationEchoHandler());
29  $this->addHandler('osd', new ilNotificationOSDHandler());
30  $this->addHandler('mail', new ilNotificationMailHandler());
31  }
32 
33  private static function getInstance()
34  {
35  if (!self::$instance) {
36  self::$instance = new self();
37  }
38  return self::$instance;
39  }
40 
47  private function addHandler($channel, ilNotificationHandler $handler)
48  {
49  if (!array_key_exists($channel, $this->handler) || !is_array($this->handler[$channel])) {
50  $this->handler[$channel] = array();
51  }
52 
53  $this->handler[$channel][] = $handler;
54  }
55 
64  private function toUsers(ilNotificationConfig $notification, $users, $processAsync = false)
65  {
66  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
67  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
68 
69 
70  // if async processing is disabled send them immediately
71  if ($processAsync == false) {
72 
73  // loading the default configuration
76 
77  // @todo this loop might be obsolet :)
78  foreach ($users as $user_id) {
79  if ($usersWithCustomConfig[$user_id]) {
81  }
82  }
83 
84  // load all available channels
86  // load all available types
88  // preload translation vars
89  $lang = ilNotificationDatabaseHandler::getTranslatedLanguageVariablesOfNotificationParameters($notification->getLanguageParameters());
90 
91  $user_by_handler = array();
92 
93  // check if the type allows custom user configurations for determining
94  // the output channel (e.g. send chat notifications only via osd)
95  if ($types[$notification->getType()]['config_type'] == 'set_by_user') {
96  $it = new ilNotificationUserIterator($notification->getType(), $users);
97 
98  $channelsByAdmin = false;
99 
100  // add the user to each channel he configured in his own configuration
101  foreach ($it as $usr_id => $data) {
102  // the configured user channel is (currently) not known
103  if (!$channels[$data['channel']]) {
104  continue;
105  }
106 
107  if (!$user_by_handler[$data['channel']]) {
108  $user_by_handler[$data['channel']] = array();
109  }
110 
111  $user_by_handler[$data['channel']][] = $usr_id;
112  }
113  }
114  // if type is configured to allow settings only applied by admin
115  elseif ($types[$notification->getType()]['config_type'] != 'disabled') {
116  $channelsByAdmin = true;
117  //$user_by_handler = array();
118 
119  if (isset($adminConfig[$notification->getType()])) {
120  foreach ($adminConfig[$notification->getType()] as $channel) {
121  if (!$channels[$channel]) {
122  continue;
123  }
124  $user_by_handler[$channel] = $users;
125  }
126  }
127  }
128 
129 
130  $userCache = array();
131 
132  // process the notifications for each output channel
133  foreach ($user_by_handler as $handler => $users) {
134  $handler = $this->handler[$handler];
135  // and process each user for the current output channel
136  foreach ($users as $userId) {
137  if (!$userCache[$userId]) {
138  $user = ilObjectFactory::getInstanceByObjId($userId, false);
139  if (!$user || !($user instanceof \ilObjUser)) {
140  continue;
141  }
142  $userCache[$userId] = $user;
143  }
144  $user = $userCache[$userId];
145 
146  // optain the message instance for the user
147  // @todo this step could be cached on a per user basis
148  // as it is independed from the output handler
149  $instance = $notification->getUserInstance($user, $lang, $this->defaultLanguage);
150  foreach ($handler as $h) {
151  // fire the notification
152  $h->notify($instance);
153  }
154  }
155  }
156  }
157  // use async processing
158  else {
159  // just enque the current configuration
161  }
162  }
163 
172  private function toListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false)
173  {
174  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
175  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
176 
177  if ($processAsync == false) {
179  self::toUsers($notification, $users, false);
180  if ($notification->hasDisableAfterDeliverySet()) {
181  ilNotificationDatabaseHandler::disableListeners($notification->getType(), $ref_id);
182  }
183  } else {
184  ilNotificationDatabaseHandler::enqueueByListener($notification, $ref_id);
185  }
186  }
187 
197  private function toRoles(ilNotificationConfig $notification, array $roles, $processAsync = false)
198  {
199  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
200  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
201 
202  global $rbacreview;
203 
204  $users = array();
205  foreach ($roles as $role) {
206  $users[] = $rbacreview->assignedUsers($role);
207  }
208  // make sure to handle every user only once
209  $users = array_unique(call_user_func_array('array_merge', $users));
210 
211  self::toUsers($notification, $users, $processAsync);
212  }
213 
221  public static function sendNotificationToUsers(ilNotificationConfig $notification, $users, $processAsync = false)
222  {
223  self::getInstance()->toUsers($notification, $users, $processAsync);
224  }
225 
233  public static function sendNotificationToListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false)
234  {
235  self::getInstance()->toListeners($notification, $ref_id, $processAsync);
236  }
237 
245  public static function sendNotificationToRoles(ilNotificationConfig $notification, array $roles, $processAsync = false)
246  {
247  self::getInstance()->toRoles($notification, $roles, $processAsync);
248  }
249 
250  public static function enableListeners($module, $ref_id)
251  {
252  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
254  }
255 
256  public static function enableUserListeners($module, $ref_id, array $users)
257  {
258  if (!$users) {
259  return;
260  }
261  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
263  }
264 }
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.
if($modEnd===false) $module
Definition: module.php:59
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)
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.
$users
Definition: authpage.php:44
static enableUserListeners($module, $ref_id, array $users)
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())