ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $rbacReview;
22 
23  private function __construct(\ilRbacReview $rbacReview = null)
24  {
25  require_once 'Services/Notifications/classes/class.ilNotificationEchoHandler.php';
26  require_once 'Services/Notifications/classes/class.ilNotificationOSDHandler.php';
27  require_once 'Services/Notifications/classes/class.ilNotificationMailHandler.php';
28 
29  // add default handlers
30  $this->addHandler('echo', new ilNotificationEchoHandler());
31  $this->addHandler('osd', new ilNotificationOSDHandler());
32  $this->addHandler('mail', new ilNotificationMailHandler());
33 
34  if ($rbacReview === null) {
35  global $DIC;
36  $rbacReview = $DIC->rbac()->review();
37  }
38  $this->rbacReview = $rbacReview;
39  }
40 
41  private static function getInstance()
42  {
43  if (!self::$instance) {
44  self::$instance = new self();
45  }
46  return self::$instance;
47  }
48 
55  private function addHandler($channel, ilNotificationHandler $handler)
56  {
57  if (!array_key_exists($channel, $this->handler) || !is_array($this->handler[$channel])) {
58  $this->handler[$channel] = array();
59  }
60 
61  $this->handler[$channel][] = $handler;
62  }
63 
72  private function toUsers(ilNotificationConfig $notification, $users, $processAsync = false)
73  {
74  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
75  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
76 
77 
78  // if async processing is disabled send them immediately
79  if ($processAsync == false) {
80 
81  // loading the default configuration
84 
85  // @todo this loop might be obsolet :)
86  foreach ($users as $user_id) {
87  if ($usersWithCustomConfig[$user_id]) {
89  }
90  }
91 
92  // load all available channels
94  // load all available types
96  // preload translation vars
97  $lang = ilNotificationDatabaseHandler::getTranslatedLanguageVariablesOfNotificationParameters($notification->getLanguageParameters());
98 
99  $user_by_handler = array();
100 
101  // check if the type allows custom user configurations for determining
102  // the output channel (e.g. send chat notifications only via osd)
103  if ($types[$notification->getType()]['config_type'] == 'set_by_user') {
104  $it = new ilNotificationUserIterator($notification->getType(), $users);
105 
106  $channelsByAdmin = false;
107 
108  // add the user to each channel he configured in his own configuration
109  foreach ($it as $usr_id => $data) {
110  // the configured user channel is (currently) not known
111  if (!$channels[$data['channel']]) {
112  continue;
113  }
114 
115  if (!$user_by_handler[$data['channel']]) {
116  $user_by_handler[$data['channel']] = array();
117  }
118 
119  $user_by_handler[$data['channel']][] = $usr_id;
120  }
121  }
122  // if type is configured to allow settings only applied by admin
123  elseif ($types[$notification->getType()]['config_type'] != 'disabled') {
124  $channelsByAdmin = true;
125  //$user_by_handler = array();
126 
127  if (isset($adminConfig[$notification->getType()])) {
128  foreach ($adminConfig[$notification->getType()] as $channel) {
129  if (!$channels[$channel]) {
130  continue;
131  }
132  $user_by_handler[$channel] = $users;
133  }
134  }
135  }
136 
137 
138  $userCache = array();
139 
140  // process the notifications for each output channel
141  foreach ($user_by_handler as $handler => $users) {
142  $handler = $this->handler[$handler];
143  // and process each user for the current output channel
144  foreach ($users as $userId) {
145  if (!$userCache[$userId]) {
146  $user = ilObjectFactory::getInstanceByObjId($userId, false);
147  if (!$user || !($user instanceof \ilObjUser)) {
148  continue;
149  }
150  $userCache[$userId] = $user;
151  }
152  $user = $userCache[$userId];
153 
154  // optain the message instance for the user
155  // @todo this step could be cached on a per user basis
156  // as it is independed from the output handler
157  $instance = $notification->getUserInstance($user, $lang, $this->defaultLanguage);
158  foreach ($handler as $h) {
159  // fire the notification
160  $h->notify($instance);
161  }
162  }
163  }
164  }
165  // use async processing
166  else {
167  // just enque the current configuration
169  }
170  }
171 
180  private function toListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false)
181  {
182  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
183  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
184 
185  if ($processAsync == false) {
187  self::toUsers($notification, $users, false);
188  if ($notification->hasDisableAfterDeliverySet()) {
189  ilNotificationDatabaseHandler::disableListeners($notification->getType(), $ref_id);
190  }
191  } else {
192  ilNotificationDatabaseHandler::enqueueByListener($notification, $ref_id);
193  }
194  }
195 
204  private function toRoles(ilNotificationConfig $notification, array $roles, $processAsync = false)
205  {
206  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
207  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
208 
209  $users = array();
210  foreach ($roles as $role) {
211  $users[] = $this->rbacReview->assignedUsers($role);
212  }
213  // make sure to handle every user only once
214  $users = array_unique(call_user_func_array('array_merge', $users));
215 
216  self::toUsers($notification, $users, $processAsync);
217  }
218 
226  public static function sendNotificationToUsers(ilNotificationConfig $notification, $users, $processAsync = false)
227  {
228  self::getInstance()->toUsers($notification, $users, $processAsync);
229  }
230 
238  public static function sendNotificationToListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false)
239  {
240  self::getInstance()->toListeners($notification, $ref_id, $processAsync);
241  }
242 
250  public static function sendNotificationToRoles(ilNotificationConfig $notification, array $roles, $processAsync = false)
251  {
252  self::getInstance()->toRoles($notification, $roles, $processAsync);
253  }
254 
255  public static function enableListeners($module, $ref_id)
256  {
257  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
259  }
260 
261  public static function enableUserListeners($module, $ref_id, array $users)
262  {
263  if (!$users) {
264  return;
265  }
266  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
268  }
269 }
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.
global $DIC
Definition: saml.php:7
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.
$user
Definition: migrateto20.php:57
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
Main notification handling routines for sending notifications to recipients.
$users
Definition: authpage.php:44
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)
__construct(\ilRbacReview $rbacReview=null)
static enableListeners($module, $sender_id, array $users=array())
$data
Definition: bench.php:6
class ilRbacReview Contains Review functions of core Rbac.