ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilNotification.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
16 {
18  const TYPE_WIKI = 2;
19  const TYPE_WIKI_PAGE = 3;
20  const TYPE_BLOG = 4;
22  const TYPE_POLL = 6;
24 
25  const THRESHOLD = 180; // time between mails in minutes
26 
35  public static function hasNotification($type, $user_id, $id)
36  {
37  global $DIC;
38 
39  $ilDB = $DIC->database();
40  $tree = $DIC->repositoryTree();
41 
42  $notification = false;
43 
44  include_once("./Services/Notification/classes/class.ilObjNotificationSettings.php");
45  $setting = new ilObjNotificationSettings($id);
47  // check membership, members should be notidifed...
48  foreach (ilObject::_getAllReferences($id) as $ref_id) {
49  $grp_ref_id = $tree->checkForParentType($ref_id, 'grp');
50  if ($grp_ref_id > 0) {
51  include_once("./Modules/Group/classes/class.ilGroupParticipants.php");
52  if (ilGroupParticipants::_isParticipant($grp_ref_id, $user_id)) {
53  $notification = true;
54  }
55  }
56  $crs_ref_id = $tree->checkForParentType($ref_id, 'crs');
57  if ($crs_ref_id > 0) {
58  include_once("./Modules/Course/classes/class.ilCourseParticipants.php");
59  if (ilCourseParticipants::_isParticipant($crs_ref_id, $user_id)) {
60  $notification = true;
61  }
62  }
63  }
64 
65  if ($notification && $setting->getMode() == ilObjNotificationSettings::MODE_DEF_ON_OPT_OUT) {
66  $set = $ilDB->query("SELECT user_id FROM notification" .
67  " WHERE type = " . $ilDB->quote($type, "integer") .
68  " AND user_id = " . $ilDB->quote($user_id, "integer") .
69  " AND id = " . $ilDB->quote($id, "integer") .
70  " AND activated = " . $ilDB->quote(0, "integer"));
71  $rec = $ilDB->fetchAssoc($set);
72  // ... except when the opted out
73  if ($rec["user_id"] == $user_id) {
74  return false;
75  }
76  return true;
77  }
78 
79  if ($notification && $setting->getMode() == ilObjNotificationSettings::MODE_DEF_ON_NO_OPT_OUT) {
80  return true;
81  }
82  }
83 
84 
85  $set = $ilDB->query("SELECT user_id FROM notification" .
86  " WHERE type = " . $ilDB->quote($type, "integer") .
87  " AND user_id = " . $ilDB->quote($user_id, "integer") .
88  " AND id = " . $ilDB->quote($id, "integer") .
89  " AND activated = " . $ilDB->quote(1, "integer"));
90 
91  return (bool) $ilDB->numRows($set);
92  }
93 
100  public static function hasOptOut($obj_id)
101  {
102  include_once("./Services/Notification/classes/class.ilObjNotificationSettings.php");
103  $setting = new ilObjNotificationSettings($obj_id);
104  if ($setting->getMode() == ilObjNotificationSettings::MODE_DEF_ON_NO_OPT_OUT) {
105  return false;
106  }
107  return true;
108  }
109 
119  public static function getNotificationsForObject($type, $id, $page_id = null, $ignore_threshold = false)
120  {
121  global $DIC;
122 
123  $ilDB = $DIC->database();
124  $tree = $DIC->repositoryTree();
125 
126  include_once("./Services/Notification/classes/class.ilObjNotificationSettings.php");
127 
128  // currently done for blog
129  $recipients = array();
130  $setting = new ilObjNotificationSettings($id);
131  if ($setting->getMode() != ilObjNotificationSettings::MODE_DEF_OFF_USER_ACTIVATION) {
132  foreach (ilObject::_getAllReferences($id) as $ref_id) {
133  $grp_ref_id = $tree->checkForParentType($ref_id, 'grp');
134  if ($grp_ref_id > 0) {
135  include_once("./Modules/Group/classes/class.ilGroupParticipants.php");
137  foreach ($p->getMembers() as $user_id) {
138  if (!in_array($user_id, $recipients)) {
139  $recipients[$user_id] = $user_id;
140  }
141  }
142  }
143  $crs_ref_id = $tree->checkForParentType($ref_id, 'crs');
144  if ($crs_ref_id > 0) {
145  include_once("./Modules/Course/classes/class.ilCourseParticipants.php");
147  foreach ($p->getMembers() as $user_id) {
148  if (!in_array($user_id, $recipients)) {
149  $recipients[$user_id] = $user_id;
150  }
151  }
152  }
153  }
154  }
155 
156  // remove all users that deactivated the feature
157  if ($setting->getMode() == ilObjNotificationSettings::MODE_DEF_ON_OPT_OUT) {
158  $sql = "SELECT user_id FROM notification" .
159  " WHERE type = " . $ilDB->quote($type, "integer") .
160  " AND id = " . $ilDB->quote($id, "integer") .
161  " AND activated = " . $ilDB->quote(0, "integer") .
162  " AND " . $ilDB->in("user_id", $recipients, false, "integer");
163  $set = $ilDB->query($sql);
164  while ($rec = $ilDB->fetchAssoc($set)) {
165  unset($recipients[$rec["user_id"]]);
166  }
167  }
168 
169  // remove all users that got a mail
170  if ($setting->getMode() != ilObjNotificationSettings::MODE_DEF_OFF_USER_ACTIVATION && !$ignore_threshold) {
171  $sql = "SELECT user_id FROM notification" .
172  " WHERE type = " . $ilDB->quote($type, "integer") .
173  " AND id = " . $ilDB->quote($id, "integer") .
174  " AND activated = " . $ilDB->quote(1, "integer") .
175  " AND " . $ilDB->in("user_id", $recipients, false, "integer");
176  $sql .= " AND (last_mail > " . $ilDB->quote(date(
177  "Y-m-d H:i:s",
178  strtotime("-" . self::THRESHOLD . "minutes")
179  ), "timestamp");
180  if ($page_id) {
181  $sql .= " AND page_id = " . $ilDB->quote($page_id, "integer");
182  }
183  $sql .= ")";
184 
185  $set = $ilDB->query($sql);
186  while ($rec = $ilDB->fetchAssoc($set)) {
187  unset($recipients[$rec["user_id"]]);
188  }
189  }
190 
191  // get single subscriptions
192  if ($setting->getMode() != ilObjNotificationSettings::MODE_DEF_ON_NO_OPT_OUT) {
193  $sql = "SELECT user_id FROM notification" .
194  " WHERE type = " . $ilDB->quote($type, "integer") .
195  " AND id = " . $ilDB->quote($id, "integer") .
196  " AND activated = " . $ilDB->quote(1, "integer");
197  if (!$ignore_threshold) {
198  $sql .= " AND (last_mail < " . $ilDB->quote(date(
199  "Y-m-d H:i:s",
200  strtotime("-" . self::THRESHOLD . "minutes")
201  ), "timestamp") .
202  " OR last_mail IS NULL";
203  if ($page_id) {
204  $sql .= " OR page_id <> " . $ilDB->quote($page_id, "integer");
205  }
206  $sql .= ")";
207  }
208  $set = $ilDB->query($sql);
209  while ($row = $ilDB->fetchAssoc($set)) {
210  $recipients[$row["user_id"]] = $row["user_id"];
211  }
212  }
213 
214  return $recipients;
215  }
216 
225  public static function setNotification($type, $user_id, $id, $status = true)
226  {
227  global $DIC;
228 
229  $ilDB = $DIC->database();
230 
231  $fields = array(
232  "type" => array("integer", $type),
233  "user_id" => array("integer", $user_id),
234  "id" => array("integer", $id)
235  );
236  $ilDB->replace("notification", $fields, array("activated" => array("integer", (int) $status)));
237  }
238 
247  public static function updateNotificationTime($type, $id, array $user_ids, $page_id = false)
248  {
249  global $DIC;
250 
251  $ilDB = $DIC->database();
252 
253  $sql = "UPDATE notification" .
254  " SET last_mail = " . $ilDB->quote(date("Y-m-d H:i:s"), "timestamp");
255 
256  if ($page_id) {
257  $sql .= ", page_id = " . $ilDB->quote($page_id, "integer");
258  }
259 
260  $sql .= " WHERE type = " . $ilDB->quote($type, "integer") .
261  " AND id = " . $ilDB->quote($id, "integer") .
262  " AND " . $ilDB->in("user_id", $user_ids, false, "integer");
263 
264  $ilDB->query($sql);
265  }
266 
273  public static function removeForObject($type, $id)
274  {
275  global $DIC;
276 
277  $ilDB = $DIC->database();
278 
279  $ilDB->query("DELETE FROM notification" .
280  " WHERE type = " . $ilDB->quote($type, "integer") .
281  " AND id = " . $ilDB->quote($id, "integer"));
282  }
283 
289  public static function removeForUser($user_id)
290  {
291  global $DIC;
292 
293  $ilDB = $DIC->database();
294 
295  $ilDB->query("DELETE FROM notification" .
296  " WHERE user_id = " . $ilDB->quote($user_id, "integer"));
297  }
298 }
static _isParticipant($a_ref_id, $a_usr_id)
Static function to check if a user is a participant of the container object.
static hasNotification($type, $user_id, $id)
Check notification status for object and user.
static _isParticipant($a_ref_id, $a_usr_id)
Static function to check if a user is a participant of the container object.
static removeForObject($type, $id)
Remove all notifications for given object.
$type
global $DIC
Definition: saml.php:7
Handles general object notification settings, see e.g.
Class ilNotification.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
if(!array_key_exists('StateId', $_REQUEST)) $id
static hasOptOut($obj_id)
Is opt out (disable notification) allowed?
static updateNotificationTime($type, $id, array $user_ids, $page_id=false)
Update the last mail timestamp for given object and users.
static _getAllReferences($a_id)
get all reference ids of object
static getNotificationsForObject($type, $id, $page_id=null, $ignore_threshold=false)
Get all users for given object.
static _lookupObjectId($a_ref_id)
lookup object id
static removeForUser($user_id)
Remove all notifications for given user.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
static setNotification($type, $user_id, $id, $status=true)
Set notification status for object and user.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
Create styles array
The data for the language used.
global $ilDB