ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMembershipNotifications.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
12  protected $ref_id; // [int]
13  protected $mode; // [int]
14  protected $custom; // [array]
15  protected $participants; // [ilParticipants]
16 
17  const VALUE_OFF = 0;
18  const VALUE_ON = 1;
19  const VALUE_BLOCKED = 2;
20 
21  const MODE_SELF = 1;
22  const MODE_ALL = 2;
23  const MODE_ALL_BLOCKED = 3;
24  const MODE_CUSTOM = 4;
25 
32  public function __construct($a_ref_id)
33  {
34  $this->ref_id = (int) $a_ref_id;
35  $this->custom = array();
36  $this->setMode(self::MODE_SELF);
37 
38  if ($this->ref_id) {
39  $this->read();
40  }
41  }
42 
48  public static function isActive()
49  {
50  global $DIC;
51 
52  $ilSetting = $DIC['ilSetting'];
53 
54  return ($ilSetting->get("block_activated_news") &&
55  $ilSetting->get("crsgrp_ntf"));
56  }
57 
61  protected function read()
62  {
63  global $DIC;
64 
65  $ilDB = $DIC['ilDB'];
66 
67  $set = $ilDB->query("SELECT nmode mode" .
68  " FROM member_noti" .
69  " WHERE ref_id = " . $ilDB->quote($this->ref_id, "integer"));
70  if ($ilDB->numRows($set)) {
71  $row = $ilDB->fetchAssoc($set);
72  $this->setMode($row["mode"]);
73 
74  if ($row["mode"] == self::MODE_CUSTOM) {
75  $set = $ilDB->query("SELECT *" .
76  " FROM member_noti_user" .
77  " WHERE ref_id = " . $ilDB->quote($this->ref_id, "integer"));
78  while ($row = $ilDB->fetchAssoc($set)) {
79  $this->custom[$row["user_id"]] = $row["status"];
80  }
81  }
82  }
83  }
84 
85 
86  //
87  // MODE
88  //
89 
95  public function getMode()
96  {
97  return $this->mode;
98  }
99 
105  protected function setMode($a_value)
106  {
107  if ($this->isValidMode($a_value)) {
108  $this->mode = $a_value;
109  }
110  }
111 
118  protected function isValidMode($a_value)
119  {
120  $valid = array(
121  self::MODE_SELF
122  ,self::MODE_ALL
123  ,self::MODE_ALL_BLOCKED
124  // ,self::MODE_CUSTOM currently used in forum
125  );
126  return in_array($a_value, $valid);
127  }
128 
135  public function switchMode($a_new_mode)
136  {
137  global $DIC;
138 
139  $ilDB = $DIC['ilDB'];
140 
141  if (!$this->ref_id) {
142  return;
143  }
144 
145  if ($this->mode &&
146  $this->mode != $a_new_mode &&
147  $this->isValidMode($a_new_mode)) {
148  $ilDB->manipulate("DELETE FROM member_noti" .
149  " WHERE ref_id = " . $ilDB->quote($this->ref_id, "integer"));
150 
151  // no custom data
152  if ($a_new_mode != self::MODE_CUSTOM) {
153  $ilDB->manipulate("DELETE FROM member_noti_user" .
154  " WHERE ref_id = " . $ilDB->quote($this->ref_id, "integer"));
155  }
156 
157  // mode self is default
158  if ($a_new_mode != self::MODE_SELF) {
159  $ilDB->insert("member_noti", array(
160  "ref_id" => array("integer", $this->ref_id),
161  "nmode" => array("integer", $a_new_mode)
162  ));
163  }
164 
165  // remove all user settings (all active is preset, optional opt out)
166  if ($a_new_mode == self::MODE_ALL) {
167  $ilDB->manipulate("DELETE FROM usr_pref" .
168  " WHERE " . $ilDB->like("keyword", "text", "grpcrs_ntf_" . $this->ref_id));
169  }
170  }
171 
172  $this->setMode($a_new_mode);
173  }
174 
175 
176  //
177  // ACTIVE USERS
178  //
179 
185  protected function getParticipants()
186  {
187  global $DIC;
188 
189  $tree = $DIC['tree'];
190 
191  if ($this->participants === null) {
192  $this->participants = false;
193 
194  $grp_ref_id = $tree->checkForParentType($this->ref_id, "grp");
195  if ($grp_ref_id) {
196  include_once "Modules/Group/classes/class.ilGroupParticipants.php";
197  $this->participants = ilGroupParticipants::_getInstanceByObjId(ilObject::_lookupObjId($grp_ref_id));
198  }
199 
200  if (!$this->participants) {
201  $crs_ref_id = $tree->checkForParentType($this->ref_id, "crs");
202  if ($crs_ref_id) {
203  include_once "Modules/Course/classes/class.ilCourseParticipants.php";
204  $this->participants = ilCourseParticipants::_getInstanceByObjId(ilObject::_lookupObjId($crs_ref_id));
205  }
206  }
207  }
208 
209  return $this->participants;
210  }
211 
217  public function getActiveUsers()
218  {
219  global $DIC;
220 
221  $ilDB = $DIC->database();
222 
223  $users = $all = array();
224 
225  $part_obj = $this->getParticipants();
226  if ($part_obj) {
227  $all = $part_obj->getParticipants();
228  }
229  if (!sizeof($all)) {
230  return array();
231  }
232 
233  switch ($this->getMode()) {
234  // users decide themselves
235  case self::MODE_SELF:
236  $set = $ilDB->query("SELECT usr_id" .
237  " FROM usr_pref" .
238  " WHERE keyword = " . $ilDB->quote("grpcrs_ntf_".$this->ref_id, "text") .
239  " AND value = " . $ilDB->quote(self::VALUE_ON, "text"));
240  while ($row = $ilDB->fetchAssoc($set)) {
241  $users[] = $row["usr_id"];
242  }
243  break;
244 
245  // all members, mind opt-out
246  case self::MODE_ALL:
247  // users who did opt-out
248  $inactive = array();
249  $set = $ilDB->query("SELECT usr_id" .
250  " FROM usr_pref" .
251  " WHERE keyword = " . $ilDB->quote("grpcrs_ntf_".$this->ref_id, "text") .
252  " AND value = " . $ilDB->quote(self::VALUE_OFF, "text"));
253  while ($row = $ilDB->fetchAssoc($set)) {
254  $inactive[] = $row["usr_id"];
255  }
256  $users = array_diff($all, $inactive);
257  break;
258 
259  // all members, no opt-out
260  case self::MODE_ALL_BLOCKED:
261  $users = $all;
262  break;
263 
264  // custom settings
265  case self::MODE_CUSTOM:
266  foreach ($this->custom as $user_id => $status) {
267  if ($status != self::VALUE_OFF) {
268  $users[] = $user_id;
269  }
270  }
271  break;
272  }
273 
274  // only valid participants
275  return array_intersect($all, $users);
276  }
277 
278 
279  //
280  // USER STATUS
281  //
282 
289  public function activateUser($a_user_id = null)
290  {
291  return $this->toggleUser(true, $a_user_id);
292  }
293 
300  public function deactivateUser($a_user_id = null)
301  {
302  return $this->toggleUser(false, $a_user_id);
303  }
304 
311  protected function getUser($a_user_id = null)
312  {
313  global $DIC;
314 
315  $ilUser = $DIC['ilUser'];
316 
317  if ($a_user_id === null ||
318  $a_user_id == $ilUser->getId()) {
319  $user = $ilUser;
320  } else {
321  $user = new ilUser($a_user_id);
322  }
323 
324  if ($user->getId() &&
325  $user->getId() != ANONYMOUS_USER_ID) {
326  return $user;
327  }
328  }
329 
337  protected function toggleUser($a_status, $a_user_id = null)
338  {
339  global $DIC;
340 
341  $ilDB = $DIC['ilDB'];
342 
343  if (!self::isActive()) {
344  return;
345  }
346 
347  switch ($this->getMode()) {
348  case self::MODE_ALL:
349  case self::MODE_SELF:
350  // current user!
351  $user = $this->getUser();
352  if ($user) {
353  // blocked value not supported in user pref!
354  $user->setPref("grpcrs_ntf_" . $this->ref_id, (int) (bool) $a_status);
355  $user->writePrefs();
356  return true;
357  }
358  break;
359 
360  case self::MODE_CUSTOM:
361  $user = $this->getUser($a_user_id);
362  if ($user) {
363  $user_id = $user->getId();
364 
365  // did status change at all?
366  if (!array_key_exists($user_id, $this->custom) ||
367  $this->custom[$user_id != $a_status]) {
368  $this->custom[$user_id] = $a_status;
369 
370  $ilDB->replace(
371  "member_noti_user",
372  array(
373  "ref_id" => array("integer", $this->ref_id),
374  "user_id" => array("integer", $user_id),
375  ),
376  array(
377  "status" => array("integer", $a_status)
378  )
379  );
380  }
381  return true;
382  }
383  break;
384 
385  case self::MODE_ALL_BLOCKED:
386  // no individual settings
387  break;
388  }
389 
390  return false;
391  }
392 
393 
394  //
395  // CURRENT USER
396  //
397 
403  public function isCurrentUserActive()
404  {
405  global $DIC;
406 
407  $ilUser = $DIC['ilUser'];
408 
409  return in_array($ilUser->getId(), $this->getActiveUsers());
410  }
411 
417  public function canCurrentUserEdit()
418  {
419  global $DIC;
420 
421  $ilUser = $DIC['ilUser'];
422 
423  $user_id = $ilUser->getId();
424  if ($user_id == ANONYMOUS_USER_ID) {
425  return false;
426  }
427 
428  switch ($this->getMode()) {
429  case self::MODE_SELF:
430  case self::MODE_ALL:
431  return true;
432 
433  case self::MODE_ALL_BLOCKED:
434  return false;
435 
436  case self::MODE_CUSTOM:
437  return !(array_key_exists($user_id, $this->custom) &&
438  $this->custom[$user_id] == self::VALUE_BLOCKED);
439  }
440  }
441 
442 
443  //
444  // CRON
445  //
446 
452  public static function getActiveUsersforAllObjects()
453  {
454  global $DIC;
455 
456  $ilDB = $DIC['ilDB'];
457  $tree = $DIC['tree'];
458 
460 
461 
462  $res = array();
463 
464  if (self::isActive()) {
465  $objects = array();
466 
467  // user-preference data (MODE_SELF)
468  $log->debug("read usr_pref");
469  $set = $ilDB->query("SELECT DISTINCT(keyword) keyword" .
470  " FROM usr_pref" .
471  " WHERE " . $ilDB->like("keyword", "text", "grpcrs_ntf_%") .
472  " AND value = " . $ilDB->quote("1", "text"));
473  while ($row = $ilDB->fetchAssoc($set)) {
474  $ref_id = substr($row["keyword"], 11);
475  $objects[(int) $ref_id] = (int) $ref_id;
476  }
477 
478  // all other modes
479  $log->debug("read member_noti");
480  $set = $ilDB->query("SELECT ref_id" .
481  " FROM member_noti");
482  while ($row = $ilDB->fetchAssoc($set)) {
483  $objects[(int) $row["ref_id"]] = (int) $row["ref_id"];
484  }
485 
486  // this might be slow but it is to be used in CRON JOB ONLY!
487  foreach (array_unique($objects) as $ref_id) {
488  // :TODO: enough checking?
489  if (!$tree->isDeleted($ref_id)) {
490  $log->debug("get active users");
491  $noti = new self($ref_id);
492  $active = $noti->getActiveUsers();
493  if (sizeof($active)) {
494  $res[$ref_id] = $active;
495  }
496  }
497  }
498  }
499 
500  return $res;
501  }
502 
503 
504  //
505  // (OBJECT SETTINGS) FORM
506  //
507 
515  public static function addToSettingsForm($a_ref_id, ilPropertyFormGUI $a_form = null, ilFormPropertyGUI $a_input = null)
516  {
517  global $DIC;
518 
519  $lng = $DIC['lng'];
520 
521  if (self::isActive() &&
522  $a_ref_id) {
523  $lng->loadLanguageModule("membership");
524  $noti = new self($a_ref_id);
525 
526  $force_noti = new ilRadioGroupInputGUI($lng->txt("mem_force_notification"), "force_noti");
527  $force_noti->setRequired(true);
528  if ($a_form) {
529  $a_form->addItem($force_noti);
530  } else {
531  $a_input->addSubItem($force_noti);
532  }
533 
534  if ($noti->isValidMode(self::MODE_SELF)) {
535  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_self"), self::MODE_SELF);
536  $force_noti->addOption($option);
537  }
538  if ($noti->isValidMode(self::MODE_ALL_BLOCKED)) {
539  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_blocked"), self::MODE_ALL_BLOCKED);
540  $force_noti->addOption($option);
541 
542  if ($noti->isValidMode(self::MODE_ALL)) {
543  $changeable = new ilCheckboxInputGUI($lng->txt("mem_force_notification_mode_all_sub_blocked"), "force_noti_allblk");
544  $option->addSubItem($changeable);
545  }
546  } elseif ($noti->isValidMode(self::MODE_ALL)) {
547  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_all"), self::MODE_ALL);
548  $force_noti->addOption($option);
549  }
550  /* not supported in GUI
551  if($noti->isValidMode(self::MODE_CUSTOM))
552  {
553  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_custom"), self::MODE_CUSTOM);
554  $option->setInfo($lng->txt("mem_force_notification_mode_custom_info"));
555  $force_noti->addOption($option);
556  }
557  */
558 
559  // set current mode
560  $current_mode = $noti->getMode();
561  $has_changeable_cb = ($noti->isValidMode(self::MODE_ALL_BLOCKED) &&
562  $noti->isValidMode(self::MODE_ALL));
563  if (!$has_changeable_cb) {
564  $force_noti->setValue($current_mode);
565  } else {
566  switch ($current_mode) {
567  case self::MODE_SELF:
568  $force_noti->setValue($current_mode);
569  $changeable->setChecked(true); // checked as "default" on selection of parent
570  break;
571 
572  case self::MODE_ALL_BLOCKED:
573  $force_noti->setValue($current_mode);
574  break;
575 
576  case self::MODE_ALL:
577  $force_noti->setValue(self::MODE_ALL_BLOCKED);
578  $changeable->setChecked(true);
579  break;
580  }
581  }
582  }
583  }
584 
591  public static function importFromForm($a_ref_id, ilPropertyFormGUI $a_form = null)
592  {
593  if (self::isActive() &&
594  $a_ref_id) {
595  $noti = new self($a_ref_id);
596  $has_changeable_cb = ($noti->isValidMode(self::MODE_ALL_BLOCKED) &&
597  $noti->isValidMode(self::MODE_ALL));
598  $changeable = null;
599  if (!$a_form) {
600  $mode = (int) $_POST["force_noti"];
601  if ($has_changeable_cb) {
602  $changeable = (int) $_POST["force_noti_allblk"];
603  }
604  } else {
605  $mode = $a_form->getInput("force_noti");
606  if ($has_changeable_cb) {
607  $changeable = $a_form->getInput("force_noti_allblk");
608  }
609  }
610  // checkbox (all) is subitem of all_blocked
611  if ($changeable &&
612  $mode == self::MODE_ALL_BLOCKED) {
613  $mode = self::MODE_ALL;
614  }
615  $noti->switchMode($mode);
616  }
617  }
618 
624  public function cloneSettings($new_ref_id)
625  {
626  global $ilDB;
627 
628  $set = $ilDB->queryF(
629  "SELECT * FROM member_noti " .
630  " WHERE ref_id = %s ",
631  array("integer"),
632  array($this->ref_id)
633  );
634  while ($rec = $ilDB->fetchAssoc($set)) {
635  $ilDB->insert("member_noti", array(
636  "ref_id" => array("integer", $new_ref_id),
637  "nmode" => array("integer", $rec["nmode"])
638  ));
639  }
640  }
641 }
This class represents an option in a radio group.
static isActive()
Is feature active?
This class represents a property form user interface.
global $DIC
Definition: saml.php:7
getUser($a_user_id=null)
Init user instance.
$valid
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
This class represents a checkbox property in a property form.
deactivateUser($a_user_id=null)
Deactivate notification for user.
$log
Definition: sabredav.php:21
isCurrentUserActive()
Get user notification status.
static addToSettingsForm($a_ref_id, ilPropertyFormGUI $a_form=null, ilFormPropertyGUI $a_input=null)
Add notification settings to form.
This class represents a property in a property form.
setValue($a_value)
Set Value.
foreach($_POST as $key=> $value) $res
$lng
static _lookupObjId($a_id)
isValidMode($a_value)
Is given mode valid?
switchMode($a_new_mode)
Switch mode for object.
$ilUser
Definition: imgupload.php:18
$user
Definition: migrateto20.php:57
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
$users
Definition: authpage.php:44
$row
This class represents a property in a property form.
global $ilSetting
Definition: privfeed.php:17
getActiveUsers()
Get active notifications for current object.
global $ilDB
getParticipants()
Init participants for current object.
static getLogger($a_component_id)
Get component logger.
static getActiveUsersforAllObjects()
Get active notifications for all objects.
toggleUser($a_status, $a_user_id=null)
Toggle user notification status.
Membership notification settings.
canCurrentUserEdit()
Can user change notification status?
static importFromForm($a_ref_id, ilPropertyFormGUI $a_form=null)
Import notification settings from form.
$_POST["username"]
setRequired($a_required)
Set Required.
cloneSettings($new_ref_id)
Clone notification object settings.
activateUser($a_user_id=null)
Activate notification for user.