ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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  {
40  $this->read();
41  }
42  }
43 
49  public static function isActive()
50  {
51  global $ilSetting;
52 
53  return ($ilSetting->get("block_activated_news") &&
54  $ilSetting->get("crsgrp_ntf"));
55  }
56 
60  protected function read()
61  {
62  global $ilDB;
63 
64  $set = $ilDB->query("SELECT nmode mode".
65  " FROM member_noti".
66  " WHERE ref_id = ".$ilDB->quote($this->ref_id, "integer"));
67  if($ilDB->numRows($set))
68  {
69  $row = $ilDB->fetchAssoc($set);
70  $this->setMode($row["mode"]);
71 
72  if($row["mode"] == self::MODE_CUSTOM)
73  {
74  $set = $ilDB->query("SELECT *".
75  " FROM member_noti_user".
76  " WHERE ref_id = ".$ilDB->quote($this->ref_id, "integer"));
77  while($row = $ilDB->fetchAssoc($set))
78  {
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  {
109  $this->mode = $a_value;
110  }
111  }
112 
119  protected function isValidMode($a_value)
120  {
121  $valid = array(
122  self::MODE_SELF
123  ,self::MODE_ALL
124  ,self::MODE_ALL_BLOCKED
125  // ,self::MODE_CUSTOM currently used in forum
126  );
127  return in_array($a_value, $valid);
128  }
129 
136  public function switchMode($a_new_mode)
137  {
138  global $ilDB;
139 
140  if(!$this->ref_id)
141  {
142  return;
143  }
144 
145  if($this->mode &&
146  $this->mode != $a_new_mode &&
147  $this->isValidMode($a_new_mode))
148  {
149  $ilDB->manipulate("DELETE FROM member_noti".
150  " WHERE ref_id = ".$ilDB->quote($this->ref_id, "integer"));
151 
152  // no custom data
153  if($a_new_mode != self::MODE_CUSTOM)
154  {
155  $ilDB->manipulate("DELETE FROM member_noti_user".
156  " WHERE ref_id = ".$ilDB->quote($this->ref_id, "integer"));
157  }
158 
159  // mode self is default
160  if($a_new_mode != self::MODE_SELF)
161  {
162  $ilDB->insert("member_noti", array(
163  "ref_id" => array("integer", $this->ref_id),
164  "nmode" => array("integer", $a_new_mode)
165  ));
166  }
167 
168  // remove all user settings (all active is preset, optional opt out)
169  if($a_new_mode == self::MODE_ALL)
170  {
171  $ilDB->manipulate("DELETE FROM usr_pref".
172  " WHERE ".$ilDB->like("keyword", "text", "grpcrs_ntf_".$this->ref_id));
173  }
174  }
175 
176  $this->setMode($a_new_mode);
177  }
178 
179 
180  //
181  // ACTIVE USERS
182  //
183 
189  protected function getParticipants()
190  {
191  global $tree;
192 
193  if($this->participants === null)
194  {
195  $this->participants = false;
196 
197  $grp_ref_id = $tree->checkForParentType($this->ref_id, "grp");
198  if($grp_ref_id)
199  {
200  include_once "Modules/Group/classes/class.ilGroupParticipants.php";
201  $this->participants = ilGroupParticipants::_getInstanceByObjId(ilObject::_lookupObjId($grp_ref_id));
202  }
203 
204  if(!$this->participants)
205  {
206  $crs_ref_id = $tree->checkForParentType($this->ref_id, "crs");
207  if($crs_ref_id)
208  {
209  include_once "Modules/Course/classes/class.ilCourseParticipants.php";
210  $this->participants = ilCourseParticipants::_getInstanceByObjId(ilObject::_lookupObjId($crs_ref_id));
211  }
212  }
213  }
214 
215  return $this->participants;
216  }
217 
223  public function getActiveUsers()
224  {
225  global $ilDB;
226 
227  $users = $all = array();
228 
229  $part_obj = $this->getParticipants();
230  if($part_obj)
231  {
232  $all = $part_obj->getParticipants();
233  }
234  if(!sizeof($all))
235  {
236  return array();
237  }
238 
239  switch($this->getMode())
240  {
241  // users decide themselves
242  case self::MODE_SELF:
243  $set = $ilDB->query("SELECT usr_id".
244  " FROM usr_pref".
245  " WHERE ".$ilDB->like("keyword", "text", "grpcrs_ntf_".$this->ref_id).
246  " AND value = ".$ilDB->quote(self::VALUE_ON, "text"));
247  while($row = $ilDB->fetchAssoc($set))
248  {
249  $users[] = $row["usr_id"];
250  }
251  break;
252 
253  // all members, mind opt-out
254  case self::MODE_ALL:
255  // users who did opt-out
256  $inactive = array();
257  $set = $ilDB->query("SELECT usr_id".
258  " FROM usr_pref".
259  " WHERE ".$ilDB->like("keyword", "text", "grpcrs_ntf_".$this->ref_id).
260  " AND value = ".$ilDB->quote(self::VALUE_OFF, "text"));
261  while($row = $ilDB->fetchAssoc($set))
262  {
263  $inactive[] = $row["usr_id"];
264  }
265  $users = array_diff($all, $inactive);
266  break;
267 
268  // all members, no opt-out
269  case self::MODE_ALL_BLOCKED:
270  $users = $all;
271  break;
272 
273  // custom settings
274  case self::MODE_CUSTOM:
275  foreach($this->custom as $user_id => $status)
276  {
277  if($status != self::VALUE_OFF)
278  {
279  $users[] = $user_id;
280  }
281  }
282  break;
283  }
284 
285  // only valid participants
286  return array_intersect($all, $users);
287  }
288 
289 
290  //
291  // USER STATUS
292  //
293 
300  public function activateUser($a_user_id = null)
301  {
302  return $this->toggleUser(true, $a_user_id);
303  }
304 
311  public function deactivateUser($a_user_id = null)
312  {
313  return $this->toggleUser(false, $a_user_id);
314  }
315 
322  protected function getUser($a_user_id = null)
323  {
324  global $ilUser;
325 
326  if($a_user_id === null ||
327  $a_user_id == $ilUser->getId())
328  {
329  $user = $ilUser;
330  }
331  else
332  {
333  $user = new ilUser($a_user_id);
334  }
335 
336  if($user->getId() &&
337  $user->getId() != ANONYMOUS_USER_ID)
338  {
339  return $user;
340  }
341  }
342 
350  protected function toggleUser($a_status, $a_user_id = null)
351  {
352  global $ilDB;
353 
354  if(!self::isActive())
355  {
356  return;
357  }
358 
359  switch($this->getMode())
360  {
361  case self::MODE_ALL:
362  case self::MODE_SELF:
363  // current user!
364  $user = $this->getUser();
365  if($user)
366  {
367  // blocked value not supported in user pref!
368  $user->setPref("grpcrs_ntf_".$this->ref_id, (int)(bool)$a_status);
369  $user->writePrefs();
370  return true;
371  }
372  break;
373 
374  case self::MODE_CUSTOM:
375  $user = $this->getUser($a_user_id);
376  if($user)
377  {
378  $user_id = $user->getId();
379 
380  // did status change at all?
381  if(!array_key_exists($user_id, $this->custom) ||
382  $this->custom[$user_id != $a_status])
383  {
384  $this->custom[$user_id] = $a_status;
385 
386  $ilDB->replace("member_noti_user",
387  array(
388  "ref_id" => array("integer", $this->ref_id),
389  "user_id" => array("integer", $user_id),
390  ),
391  array(
392  "status" => array("integer", $a_status)
393  )
394  );
395  }
396  return true;
397  }
398  break;
399 
400  case self::MODE_ALL_BLOCKED:
401  // no individual settings
402  break;
403  }
404 
405  return false;
406  }
407 
408 
409  //
410  // CURRENT USER
411  //
412 
418  public function isCurrentUserActive()
419  {
420  global $ilUser;
421 
422  return in_array($ilUser->getId(), $this->getActiveUsers());
423  }
424 
430  public function canCurrentUserEdit()
431  {
432  global $ilUser;
433 
434  $user_id = $ilUser->getId();
435  if($user_id == ANONYMOUS_USER_ID)
436  {
437  return false;
438  }
439 
440  switch($this->getMode())
441  {
442  case self::MODE_SELF:
443  case self::MODE_ALL:
444  return true;
445 
446  case self::MODE_ALL_BLOCKED:
447  return false;
448 
449  case self::MODE_CUSTOM:
450  return !(array_key_exists($user_id, $this->custom) &&
451  $this->custom[$user_id] == self::VALUE_BLOCKED);
452  }
453  }
454 
455 
456  //
457  // CRON
458  //
459 
465  public static function getActiveUsersforAllObjects()
466  {
467  global $ilDB, $tree;
468 
469  $res = array();
470 
471  if(self::isActive())
472  {
473  $objects = array();
474 
475  // user-preference data (MODE_SELF)
476  $set = $ilDB->query("SELECT DISTINCT(keyword) keyword".
477  " FROM usr_pref".
478  " WHERE ".$ilDB->like("keyword", "text", "grpcrs_ntf_%").
479  " AND value = ".$ilDB->quote("1", "text"));
480  while($row = $ilDB->fetchAssoc($set))
481  {
482  $ref_id = substr($row["keyword"], 11);
483  $objects[] = (int)$ref_id;
484  }
485 
486  // all other modes
487  $set = $ilDB->query("SELECT ref_id".
488  " FROM member_noti");
489  while($row = $ilDB->fetchAssoc($set))
490  {
491  $objects[] = (int)$row["ref_id"];
492  }
493 
494  // this might be slow but it is to be used in CRON JOB ONLY!
495  foreach(array_unique($objects) as $ref_id)
496  {
497  // :TODO: enough checking?
498  if(!$tree->isDeleted($ref_id))
499  {
500  $noti = new self($ref_id);
501  $active = $noti->getActiveUsers();
502  if(sizeof($active))
503  {
504  $res[$ref_id] = $active;
505  }
506  }
507  }
508  }
509 
510  return $res;
511  }
512 
513 
514  //
515  // (OBJECT SETTINGS) FORM
516  //
517 
525  public static function addToSettingsForm($a_ref_id, ilPropertyFormGUI $a_form = null, ilFormPropertyGUI $a_input = null)
526  {
527  global $lng;
528 
529  if(self::isActive() &&
530  $a_ref_id)
531  {
532  $lng->loadLanguageModule("membership");
533  $noti = new self($a_ref_id);
534 
535  $force_noti = new ilRadioGroupInputGUI($lng->txt("mem_force_notification"), "force_noti");
536  $force_noti->setRequired(true);
537  if($a_form)
538  {
539  $a_form->addItem($force_noti);
540  }
541  else
542  {
543  $a_input->addSubItem($force_noti);
544  }
545 
546  if($noti->isValidMode(self::MODE_SELF))
547  {
548  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_self"), self::MODE_SELF);
549  $force_noti->addOption($option);
550  }
551  if($noti->isValidMode(self::MODE_ALL_BLOCKED))
552  {
553  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_blocked"), self::MODE_ALL_BLOCKED);
554  $force_noti->addOption($option);
555 
556  if($noti->isValidMode(self::MODE_ALL))
557  {
558  $changeable = new ilCheckboxInputGUI($lng->txt("mem_force_notification_mode_all_sub_blocked"), "force_noti_allblk");
559  $option->addSubItem($changeable);
560  }
561  }
562  else if($noti->isValidMode(self::MODE_ALL))
563  {
564  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_all"), self::MODE_ALL);
565  $force_noti->addOption($option);
566  }
567  /* not supported in GUI
568  if($noti->isValidMode(self::MODE_CUSTOM))
569  {
570  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_custom"), self::MODE_CUSTOM);
571  $option->setInfo($lng->txt("mem_force_notification_mode_custom_info"));
572  $force_noti->addOption($option);
573  }
574  */
575 
576  // set current mode
577  $current_mode = $noti->getMode();
578  $has_changeable_cb = ($noti->isValidMode(self::MODE_ALL_BLOCKED) &&
579  $noti->isValidMode(self::MODE_ALL));
580  if(!$has_changeable_cb)
581  {
582  $force_noti->setValue($current_mode);
583  }
584  else
585  {
586  switch($current_mode)
587  {
588  case self::MODE_SELF:
589  $force_noti->setValue($current_mode);
590  $changeable->setChecked(true); // checked as "default" on selection of parent
591  break;
592 
593  case self::MODE_ALL_BLOCKED:
594  $force_noti->setValue($current_mode);
595  break;
596 
597  case self::MODE_ALL:
598  $force_noti->setValue(self::MODE_ALL_BLOCKED);
599  $changeable->setChecked(true);
600  break;
601  }
602  }
603  }
604  }
605 
612  public static function importFromForm($a_ref_id, ilPropertyFormGUI $a_form = null)
613  {
614  if(self::isActive() &&
615  $a_ref_id)
616  {
617  $noti = new self($a_ref_id);
618  $has_changeable_cb = ($noti->isValidMode(self::MODE_ALL_BLOCKED) &&
619  $noti->isValidMode(self::MODE_ALL));
620  $changeable = null;
621  if(!$a_form)
622  {
623  $mode = (int)$_POST["force_noti"];
624  if($has_changeable_cb)
625  {
626  $changeable = (int)$_POST["force_noti_allblk"];
627  }
628  }
629  else
630  {
631  $mode = $a_form->getInput("force_noti");
632  if($has_changeable_cb)
633  {
634  $changeable = $a_form->getInput("force_noti_allblk");
635  }
636  }
637  // checkbox (all) is subitem of all_blocked
638  if($changeable &&
639  $mode == self::MODE_ALL_BLOCKED)
640  {
641  $mode = self::MODE_ALL;
642  }
643  $noti->switchMode($mode);
644  }
645  }
646 }
This class represents an option in a radio group.
static isActive()
Is feature active?
$_POST['username']
Definition: cron.php:12
This class represents a property form user interface.
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.
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.
static _lookupObjId($a_id)
isValidMode($a_value)
Is given mode valid?
switchMode($a_new_mode)
Switch mode for object.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
global $ilUser
Definition: imgupload.php:15
This class represents a property in a property form.
global $ilSetting
Definition: privfeed.php:40
getActiveUsers()
Get active notifications for current object.
global $lng
Definition: privfeed.php:40
global $ilDB
getParticipants()
Init participants for current object.
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.
setRequired($a_required)
Set Required.
activateUser($a_user_id=null)
Activate notification for user.