ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
470 
471 
472  $res = array();
473 
474  if(self::isActive())
475  {
476  $objects = array();
477 
478  // user-preference data (MODE_SELF)
479  $log->debug("read usr_pref");
480  $set = $ilDB->query("SELECT DISTINCT(keyword) keyword".
481  " FROM usr_pref".
482  " WHERE ".$ilDB->like("keyword", "text", "grpcrs_ntf_%").
483  " AND value = ".$ilDB->quote("1", "text"));
484  while($row = $ilDB->fetchAssoc($set))
485  {
486  $ref_id = substr($row["keyword"], 11);
487  $objects[(int)$ref_id] = (int)$ref_id;
488  }
489 
490  // all other modes
491  $log->debug("read member_noti");
492  $set = $ilDB->query("SELECT ref_id".
493  " FROM member_noti");
494  while($row = $ilDB->fetchAssoc($set))
495  {
496  $objects[(int)$row["ref_id"]] = (int)$row["ref_id"];
497  }
498 
499  // this might be slow but it is to be used in CRON JOB ONLY!
500  foreach(array_unique($objects) as $ref_id)
501  {
502  // :TODO: enough checking?
503  if(!$tree->isDeleted($ref_id))
504  {
505  $log->debug("get active users");
506  $noti = new self($ref_id);
507  $active = $noti->getActiveUsers();
508  if(sizeof($active))
509  {
510  $res[$ref_id] = $active;
511  }
512  }
513  }
514  }
515 
516  return $res;
517  }
518 
519 
520  //
521  // (OBJECT SETTINGS) FORM
522  //
523 
531  public static function addToSettingsForm($a_ref_id, ilPropertyFormGUI $a_form = null, ilFormPropertyGUI $a_input = null)
532  {
533  global $lng;
534 
535  if(self::isActive() &&
536  $a_ref_id)
537  {
538  $lng->loadLanguageModule("membership");
539  $noti = new self($a_ref_id);
540 
541  $force_noti = new ilRadioGroupInputGUI($lng->txt("mem_force_notification"), "force_noti");
542  $force_noti->setRequired(true);
543  if($a_form)
544  {
545  $a_form->addItem($force_noti);
546  }
547  else
548  {
549  $a_input->addSubItem($force_noti);
550  }
551 
552  if($noti->isValidMode(self::MODE_SELF))
553  {
554  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_self"), self::MODE_SELF);
555  $force_noti->addOption($option);
556  }
557  if($noti->isValidMode(self::MODE_ALL_BLOCKED))
558  {
559  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_blocked"), self::MODE_ALL_BLOCKED);
560  $force_noti->addOption($option);
561 
562  if($noti->isValidMode(self::MODE_ALL))
563  {
564  $changeable = new ilCheckboxInputGUI($lng->txt("mem_force_notification_mode_all_sub_blocked"), "force_noti_allblk");
565  $option->addSubItem($changeable);
566  }
567  }
568  else if($noti->isValidMode(self::MODE_ALL))
569  {
570  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_all"), self::MODE_ALL);
571  $force_noti->addOption($option);
572  }
573  /* not supported in GUI
574  if($noti->isValidMode(self::MODE_CUSTOM))
575  {
576  $option = new ilRadioOption($lng->txt("mem_force_notification_mode_custom"), self::MODE_CUSTOM);
577  $option->setInfo($lng->txt("mem_force_notification_mode_custom_info"));
578  $force_noti->addOption($option);
579  }
580  */
581 
582  // set current mode
583  $current_mode = $noti->getMode();
584  $has_changeable_cb = ($noti->isValidMode(self::MODE_ALL_BLOCKED) &&
585  $noti->isValidMode(self::MODE_ALL));
586  if(!$has_changeable_cb)
587  {
588  $force_noti->setValue($current_mode);
589  }
590  else
591  {
592  switch($current_mode)
593  {
594  case self::MODE_SELF:
595  $force_noti->setValue($current_mode);
596  $changeable->setChecked(true); // checked as "default" on selection of parent
597  break;
598 
599  case self::MODE_ALL_BLOCKED:
600  $force_noti->setValue($current_mode);
601  break;
602 
603  case self::MODE_ALL:
604  $force_noti->setValue(self::MODE_ALL_BLOCKED);
605  $changeable->setChecked(true);
606  break;
607  }
608  }
609  }
610  }
611 
618  public static function importFromForm($a_ref_id, ilPropertyFormGUI $a_form = null)
619  {
620  if(self::isActive() &&
621  $a_ref_id)
622  {
623  $noti = new self($a_ref_id);
624  $has_changeable_cb = ($noti->isValidMode(self::MODE_ALL_BLOCKED) &&
625  $noti->isValidMode(self::MODE_ALL));
626  $changeable = null;
627  if(!$a_form)
628  {
629  $mode = (int)$_POST["force_noti"];
630  if($has_changeable_cb)
631  {
632  $changeable = (int)$_POST["force_noti_allblk"];
633  }
634  }
635  else
636  {
637  $mode = $a_form->getInput("force_noti");
638  if($has_changeable_cb)
639  {
640  $changeable = $a_form->getInput("force_noti_allblk");
641  }
642  }
643  // checkbox (all) is subitem of all_blocked
644  if($changeable &&
645  $mode == self::MODE_ALL_BLOCKED)
646  {
647  $mode = self::MODE_ALL;
648  }
649  $noti->switchMode($mode);
650  }
651  }
652 }
This class represents an option in a radio group.
static isActive()
Is feature active?
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.
$ilUser
Definition: imgupload.php:18
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
Create styles array
The data for the language used.
This class represents a property in a property form.
global $ilSetting
Definition: privfeed.php:17
getActiveUsers()
Get active notifications for current object.
global $lng
Definition: privfeed.php:17
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.
activateUser($a_user_id=null)
Activate notification for user.