ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  const TYPE_BOOK = 8;
25 
26  const THRESHOLD = 180; // time between mails in minutes
27 
36  public static function hasNotification($type, $user_id, $id)
37  {
38  global $DIC;
39 
40  $ilDB = $DIC->database();
41  $tree = $DIC->repositoryTree();
42 
43  $notification = false;
44 
45  include_once("./Services/Notification/classes/class.ilObjNotificationSettings.php");
46  $setting = new ilObjNotificationSettings($id);
48  // check membership, members should be notidifed...
49  foreach (ilObject::_getAllReferences($id) as $ref_id) {
50  $grp_ref_id = $tree->checkForParentType($ref_id, 'grp');
51  if ($grp_ref_id > 0) {
52  include_once("./Modules/Group/classes/class.ilGroupParticipants.php");
53  if (ilGroupParticipants::_isParticipant($grp_ref_id, $user_id)) {
54  $notification = true;
55  }
56  }
57  $crs_ref_id = $tree->checkForParentType($ref_id, 'crs');
58  if ($crs_ref_id > 0) {
59  include_once("./Modules/Course/classes/class.ilCourseParticipants.php");
60  if (ilCourseParticipants::_isParticipant($crs_ref_id, $user_id)) {
61  $notification = true;
62  }
63  }
64  }
65 
66  if ($notification && $setting->getMode() == ilObjNotificationSettings::MODE_DEF_ON_OPT_OUT) {
67  $set = $ilDB->query("SELECT user_id FROM notification" .
68  " WHERE type = " . $ilDB->quote($type, "integer") .
69  " AND user_id = " . $ilDB->quote($user_id, "integer") .
70  " AND id = " . $ilDB->quote($id, "integer") .
71  " AND activated = " . $ilDB->quote(0, "integer"));
72  $rec = $ilDB->fetchAssoc($set);
73  // ... except when the opted out
74  if ($rec["user_id"] == $user_id) {
75  return false;
76  }
77  return true;
78  }
79 
80  if ($notification && $setting->getMode() == ilObjNotificationSettings::MODE_DEF_ON_NO_OPT_OUT) {
81  return true;
82  }
83  }
84 
85 
86  $set = $ilDB->query("SELECT user_id FROM notification" .
87  " WHERE type = " . $ilDB->quote($type, "integer") .
88  " AND user_id = " . $ilDB->quote($user_id, "integer") .
89  " AND id = " . $ilDB->quote($id, "integer") .
90  " AND activated = " . $ilDB->quote(1, "integer"));
91 
92  return (bool) $ilDB->numRows($set);
93  }
94 
101  public static function hasOptOut($obj_id)
102  {
103  include_once("./Services/Notification/classes/class.ilObjNotificationSettings.php");
104  $setting = new ilObjNotificationSettings($obj_id);
105  if ($setting->getMode() == ilObjNotificationSettings::MODE_DEF_ON_NO_OPT_OUT) {
106  return false;
107  }
108  return true;
109  }
110 
120  public static function getNotificationsForObject($type, $id, $page_id = null, $ignore_threshold = false)
121  {
122  global $DIC;
123 
124  $ilDB = $DIC->database();
125  $tree = $DIC->repositoryTree();
126 
127  include_once("./Services/Notification/classes/class.ilObjNotificationSettings.php");
128 
129  // currently done for blog
130  $recipients = array();
131  $setting = new ilObjNotificationSettings($id);
132  if ($setting->getMode() != ilObjNotificationSettings::MODE_DEF_OFF_USER_ACTIVATION) {
133  foreach (ilObject::_getAllReferences($id) as $ref_id) {
134  $grp_ref_id = $tree->checkForParentType($ref_id, 'grp');
135  if ($grp_ref_id > 0) {
136  include_once("./Modules/Group/classes/class.ilGroupParticipants.php");
138  foreach ($p->getMembers() as $user_id) {
139  if (!in_array($user_id, $recipients)) {
140  $recipients[$user_id] = $user_id;
141  }
142  }
143  }
144  $crs_ref_id = $tree->checkForParentType($ref_id, 'crs');
145  if ($crs_ref_id > 0) {
146  include_once("./Modules/Course/classes/class.ilCourseParticipants.php");
148  foreach ($p->getMembers() as $user_id) {
149  if (!in_array($user_id, $recipients)) {
150  $recipients[$user_id] = $user_id;
151  }
152  }
153  }
154  }
155  }
156 
157  // remove all users that deactivated the feature
158  if ($setting->getMode() == ilObjNotificationSettings::MODE_DEF_ON_OPT_OUT) {
159  $sql = "SELECT user_id FROM notification" .
160  " WHERE type = " . $ilDB->quote($type, "integer") .
161  " AND id = " . $ilDB->quote($id, "integer") .
162  " AND activated = " . $ilDB->quote(0, "integer") .
163  " AND " . $ilDB->in("user_id", $recipients, false, "integer");
164  $set = $ilDB->query($sql);
165  while ($rec = $ilDB->fetchAssoc($set)) {
166  unset($recipients[$rec["user_id"]]);
167  }
168  }
169 
170  // remove all users that got a mail
171  if ($setting->getMode() != ilObjNotificationSettings::MODE_DEF_OFF_USER_ACTIVATION && !$ignore_threshold) {
172  $sql = "SELECT user_id FROM notification" .
173  " WHERE type = " . $ilDB->quote($type, "integer") .
174  " AND id = " . $ilDB->quote($id, "integer") .
175  " AND activated = " . $ilDB->quote(1, "integer") .
176  " AND " . $ilDB->in("user_id", $recipients, false, "integer");
177  $sql .= " AND (last_mail > " . $ilDB->quote(date(
178  "Y-m-d H:i:s",
179  strtotime("-" . self::THRESHOLD . "minutes")
180  ), "timestamp");
181  if ($page_id) {
182  $sql .= " AND page_id = " . $ilDB->quote($page_id, "integer");
183  }
184  $sql .= ")";
185 
186  $set = $ilDB->query($sql);
187  while ($rec = $ilDB->fetchAssoc($set)) {
188  unset($recipients[$rec["user_id"]]);
189  }
190  }
191 
192  // get single subscriptions
193  if ($setting->getMode() != ilObjNotificationSettings::MODE_DEF_ON_NO_OPT_OUT) {
194  $sql = "SELECT user_id FROM notification" .
195  " WHERE type = " . $ilDB->quote($type, "integer") .
196  " AND id = " . $ilDB->quote($id, "integer") .
197  " AND activated = " . $ilDB->quote(1, "integer");
198  if (!$ignore_threshold) {
199  $sql .= " AND (last_mail < " . $ilDB->quote(date(
200  "Y-m-d H:i:s",
201  strtotime("-" . self::THRESHOLD . "minutes")
202  ), "timestamp") .
203  " OR last_mail IS NULL";
204  if ($page_id) {
205  $sql .= " OR page_id <> " . $ilDB->quote($page_id, "integer");
206  }
207  $sql .= ")";
208  }
209  $set = $ilDB->query($sql);
210  while ($row = $ilDB->fetchAssoc($set)) {
211  $recipients[$row["user_id"]] = $row["user_id"];
212  }
213  }
214 
215  return $recipients;
216  }
217 
226  public static function setNotification($type, $user_id, $id, $status = true)
227  {
228  global $DIC;
229 
230  $ilDB = $DIC->database();
231 
232  $fields = array(
233  "type" => array("integer", $type),
234  "user_id" => array("integer", $user_id),
235  "id" => array("integer", $id)
236  );
237  $ilDB->replace("notification", $fields, array("activated" => array("integer", (int) $status)));
238  }
239 
248  public static function updateNotificationTime($type, $id, array $user_ids, $page_id = false)
249  {
250  global $DIC;
251 
252  $ilDB = $DIC->database();
253 
254  $sql = "UPDATE notification" .
255  " SET last_mail = " . $ilDB->quote(date("Y-m-d H:i:s"), "timestamp");
256 
257  if ($page_id) {
258  $sql .= ", page_id = " . $ilDB->quote($page_id, "integer");
259  }
260 
261  $sql .= " WHERE type = " . $ilDB->quote($type, "integer") .
262  " AND id = " . $ilDB->quote($id, "integer") .
263  " AND " . $ilDB->in("user_id", $user_ids, false, "integer");
264 
265  $ilDB->query($sql);
266  }
267 
274  public static function removeForObject($type, $id)
275  {
276  global $DIC;
277 
278  $ilDB = $DIC->database();
279 
280  $ilDB->query("DELETE FROM notification" .
281  " WHERE type = " . $ilDB->quote($type, "integer") .
282  " AND id = " . $ilDB->quote($id, "integer"));
283  }
284 
290  public static function removeForUser($user_id)
291  {
292  global $DIC;
293 
294  $ilDB = $DIC->database();
295 
296  $ilDB->query("DELETE FROM notification" .
297  " WHERE user_id = " . $ilDB->quote($user_id, "integer"));
298  }
299 }
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.
static setNotification($type, $user_id, $id, $status=true)
Set notification status for object and user.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
$row
global $ilDB