ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilNotificationSystem.php
Go to the documentation of this file.
1 <?php
2 
4 
5  private static $instance;
6 
7  private $handler = array();
8 
9  private $defaultLanguage = 'en';
10 
11  private function __construct() {
12  require_once 'Services/Notifications/classes/class.ilNotificationEchoHandler.php';
13  require_once 'Services/Notifications/classes/class.ilNotificationOSDHandler.php';
14  require_once 'Services/Notifications/classes/class.ilNotificationMailHandler.php';
15 
16  $this->addHandler('echo', new ilNotificationEchoHandler());
17  $this->addHandler('osd', new ilNotificationOSDHandler());
18  $this->addHandler('mail', new ilNotificationMailHandler());
19 
20  }
21 
22  private static function getInstance() {
23  if (!self::$instance) {
24  self::$instance = new self();
25  }
26  return self::$instance;
27  }
28 
29  private function addHandler($channel, ilNotificationHandler $handler) {
30  if (!array_key_exists($channel, $this->handler) || !is_array($this->handler[$channel]))
31  $this->handler[$channel] = array();
32 
33  $this->handler[$channel][] = $handler;
34  }
35 
36  private function toUsers(ilNotificationConfig $notification, $users, $processAsync = false) {
37 
38  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
39  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
40 
41 
42  if ($processAsync == false) {
43 
45  $usersWithCustomConfig = ilNotificationDatabaseHandler::getUsersWithCustomConfig($users);
46 
47  foreach($users as $user_id) {
48  if ($usersWithCustomConfig[$user_id]) {
50  }
51  }
52 
55 
56  $lang = ilNotificationDatabaseHandler::getTranslatedLanguageVariablesOfNotificationParameters($notification->getLanguageParameters());
57 
58  $user_by_handler = array();
59 
60  if ($types[$notification->getType()]['config_type'] == 'set_by_user') {
61  $it = new ilNotificationUserIterator($notification->getType(), $users);
62 
63  $channelsByAdmin = false;
64 
65  foreach($it as $usr_id => $data) {
66  if (!$channels[$data['channel']])
67  continue;
68 
69  if (!$user_by_handler[$data['channel']])
70  $user_by_handler[$data['channel']] = array();
71 
72  $user_by_handler[$data['channel']][] = $usr_id;
73  }
74  }
75  else if ($types[$notification->getType()]['config_type'] != 'disabled') {
76  $channelsByAdmin = true;
77  //$user_by_handler = array();
78 
79  if (isset($adminConfig[$notification->getType()])) {
80 
81  foreach($adminConfig[$notification->getType()] as $channel) {
82  if (!$channels[$channel])
83  continue;
84  $user_by_handler[$channel] = $users;
85 
86  }
87  }
88  }
89 
90 
91  $userCache = array();
92 
93  foreach($user_by_handler as $handler => $users) {
94  $handler = $this->handler[$handler];
95  foreach($users as $userId) {
96  if (!$userCache[$userId]) {
97  $userCache[$userId] = new ilObjUser($userId);
98  }
99  $user = $userCache[$userId];
100 
101  $instance = $notification->getUserInstance($user, $lang, $this->defaultLanguage);
102  foreach($handler as $h) {
103  $h->notify($instance);
104  }
105  }
106  }
107  }
108  else {
109  ilNotificationDatabaseHandler::enqueueByUsers($notification, $users);
110  }
111  }
112 
113  private function toListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false) {
114  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
115  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
116 
117  if ($processAsync == false) {
119  self::toUsers($notification, $users, false);
120  if ($notification->hasDisableAfterDeliverySet()) {
122  }
123  }
124  else {
126  }
127  }
128 
129  private function toRoles(ilNotificationConfig $notification, array $roles, $processAsync = false) {
130  require_once 'Services/Notifications/classes/class.ilNotificationUserIterator.php';
131  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
132 
133  global $rbacreview;
134 
135  $users = array();
136  foreach($roles as $role) {
137  $users[] = $rbacreview->assignedUsers($role);
138  }
139  $users = array_unique(call_user_func_array('array_merge', $users));
140 
141  self::toUsers($notification, $users, $processAsync);
142  }
143 
144 
145  public static function sendNotificationToUsers(ilNotificationConfig $notification, $users, $processAsync = false) {
146  self::getInstance()->toUsers($notification, $users, $processAsync);
147  }
148 
149  public static function sendNotificationToListeners(ilNotificationConfig $notification, $ref_id, $processAsync = false) {
150  self::getInstance()->toListeners($notification, $ref_id, $processAsync);
151  }
152 
153  public static function sendNotificationToRoles(ilNotificationConfig $notification, array $roles, $processAsync = false) {
154  self::getInstance()->toRoles($notification, $roles, $processAsync);
155  }
156 
157  public static function enableListeners($module, $ref_id) {
158  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
160  }
161 
162  public static function enableUserListeners($module, $ref_id, array $users) {
163  if (!$users)
164  return;
165  require_once 'Services/Notifications/classes/class.ilNotificationDatabaseHelper.php';
167  }
168 }
169 ?>