ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  $userCache[$userId] = new ilObjUser($userId);
135  }
136  $user = $userCache[$userId];
137 
138  // optain the message instance for the user
139  // @todo this step could be cached on a per user basis
140  // as it is independed from the output handler
141  $instance = $notification->getUserInstance($user, $lang, $this->defaultLanguage);
142  foreach($handler as $h) {
143  // fire the notification
144  $h->notify($instance);
145  }
146  }
147  }
148  }
149  // use async processing
150  else {
151  // just enque the current configuration
152  ilNotificationDatabaseHandler::enqueueByUsers($notification, $users);
153  }
154  }
155 
164  private function toListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false) {
165  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
166  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
167 
168  if ($processAsync == false) {
170  self::toUsers($notification, $users, false);
171  if ($notification->hasDisableAfterDeliverySet()) {
173  }
174  }
175  else {
177  }
178  }
179 
189  private function toRoles(ilNotificationConfig $notification, array $roles, $processAsync = false) {
190  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
191  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
192 
193  global $rbacreview;
194 
195  $users = array();
196  foreach($roles as $role) {
197  $users[] = $rbacreview->assignedUsers($role);
198  }
199  // make sure to handle every user only once
200  $users = array_unique(call_user_func_array('array_merge', $users));
201 
202  self::toUsers($notification, $users, $processAsync);
203  }
204 
212  public static function sendNotificationToUsers(ilNotificationConfig $notification, $users, $processAsync = false) {
213  self::getInstance()->toUsers($notification, $users, $processAsync);
214  }
215 
223  public static function sendNotificationToListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false) {
224  self::getInstance()->toListeners($notification, $ref_id, $processAsync);
225  }
226 
234  public static function sendNotificationToRoles(ilNotificationConfig $notification, array $roles, $processAsync = false) {
235  self::getInstance()->toRoles($notification, $roles, $processAsync);
236  }
237 
238  public static function enableListeners($module, $ref_id) {
239  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
241  }
242 
243  public static function enableUserListeners($module, $ref_id, array $users) {
244  if (!$users)
245  return;
246  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
248  }
249 }
250 ?>