ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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;
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 $ilSetting;
51
52 return ($ilSetting->get("block_activated_news") &&
53 $ilSetting->get("crsgrp_ntf"));
54 }
55
59 protected function read()
60 {
61 global $ilDB;
62
63 $set = $ilDB->query("SELECT nmode mode" .
64 " FROM member_noti" .
65 " WHERE ref_id = " . $ilDB->quote($this->ref_id, "integer"));
66 if ($ilDB->numRows($set)) {
67 $row = $ilDB->fetchAssoc($set);
68 $this->setMode($row["mode"]);
69
70 if ($row["mode"] == self::MODE_CUSTOM) {
71 $set = $ilDB->query("SELECT *" .
72 " FROM member_noti_user" .
73 " WHERE ref_id = " . $ilDB->quote($this->ref_id, "integer"));
74 while ($row = $ilDB->fetchAssoc($set)) {
75 $this->custom[$row["user_id"]] = $row["status"];
76 }
77 }
78 }
79 }
80
81
82 //
83 // MODE
84 //
85
91 public function getMode()
92 {
93 return $this->mode;
94 }
95
101 protected function setMode($a_value)
102 {
103 if ($this->isValidMode($a_value)) {
104 $this->mode = $a_value;
105 }
106 }
107
114 protected function isValidMode($a_value)
115 {
116 $valid = array(
117 self::MODE_SELF
118 ,self::MODE_ALL
119 ,self::MODE_ALL_BLOCKED
120 // ,self::MODE_CUSTOM currently used in forum
121 );
122 return in_array($a_value, $valid);
123 }
124
131 public function switchMode($a_new_mode)
132 {
133 global $ilDB;
134
135 if (!$this->ref_id) {
136 return;
137 }
138
139 if ($this->mode &&
140 $this->mode != $a_new_mode &&
141 $this->isValidMode($a_new_mode)) {
142 $ilDB->manipulate("DELETE FROM member_noti" .
143 " WHERE ref_id = " . $ilDB->quote($this->ref_id, "integer"));
144
145 // no custom data
146 if ($a_new_mode != self::MODE_CUSTOM) {
147 $ilDB->manipulate("DELETE FROM member_noti_user" .
148 " WHERE ref_id = " . $ilDB->quote($this->ref_id, "integer"));
149 }
150
151 // mode self is default
152 if ($a_new_mode != self::MODE_SELF) {
153 $ilDB->insert("member_noti", array(
154 "ref_id" => array("integer", $this->ref_id),
155 "nmode" => array("integer", $a_new_mode)
156 ));
157 }
158
159 // remove all user settings (all active is preset, optional opt out)
160 if ($a_new_mode == self::MODE_ALL) {
161 $ilDB->manipulate("DELETE FROM usr_pref" .
162 " WHERE " . $ilDB->like("keyword", "text", "grpcrs_ntf_" . $this->ref_id));
163 }
164 }
165
166 $this->setMode($a_new_mode);
167 }
168
169
170 //
171 // ACTIVE USERS
172 //
173
179 protected function getParticipants()
180 {
181 global $tree;
182
183 if ($this->participants === null) {
184 $this->participants = false;
185
186 $grp_ref_id = $tree->checkForParentType($this->ref_id, "grp");
187 if ($grp_ref_id) {
188 include_once "Modules/Group/classes/class.ilGroupParticipants.php";
189 $this->participants = ilGroupParticipants::_getInstanceByObjId(ilObject::_lookupObjId($grp_ref_id));
190 }
191
192 if (!$this->participants) {
193 $crs_ref_id = $tree->checkForParentType($this->ref_id, "crs");
194 if ($crs_ref_id) {
195 include_once "Modules/Course/classes/class.ilCourseParticipants.php";
196 $this->participants = ilCourseParticipants::_getInstanceByObjId(ilObject::_lookupObjId($crs_ref_id));
197 }
198 }
199 }
200
201 return $this->participants;
202 }
203
209 public function getActiveUsers()
210 {
211 global $ilDB;
212
213 $users = $all = array();
214
215 $part_obj = $this->getParticipants();
216 if ($part_obj) {
217 $all = $part_obj->getParticipants();
218 }
219 if (!sizeof($all)) {
220 return array();
221 }
222
223 switch ($this->getMode()) {
224 // users decide themselves
225 case self::MODE_SELF:
226 $set = $ilDB->query("SELECT usr_id" .
227 " FROM usr_pref" .
228 " WHERE " . $ilDB->like("keyword", "text", "grpcrs_ntf_" . $this->ref_id) .
229 " AND value = " . $ilDB->quote(self::VALUE_ON, "text"));
230 while ($row = $ilDB->fetchAssoc($set)) {
231 $users[] = $row["usr_id"];
232 }
233 break;
234
235 // all members, mind opt-out
236 case self::MODE_ALL:
237 // users who did opt-out
238 $inactive = array();
239 $set = $ilDB->query("SELECT usr_id" .
240 " FROM usr_pref" .
241 " WHERE " . $ilDB->like("keyword", "text", "grpcrs_ntf_" . $this->ref_id) .
242 " AND value = " . $ilDB->quote(self::VALUE_OFF, "text"));
243 while ($row = $ilDB->fetchAssoc($set)) {
244 $inactive[] = $row["usr_id"];
245 }
246 $users = array_diff($all, $inactive);
247 break;
248
249 // all members, no opt-out
251 $users = $all;
252 break;
253
254 // custom settings
256 foreach ($this->custom as $user_id => $status) {
257 if ($status != self::VALUE_OFF) {
258 $users[] = $user_id;
259 }
260 }
261 break;
262 }
263
264 // only valid participants
265 return array_intersect($all, $users);
266 }
267
268
269 //
270 // USER STATUS
271 //
272
279 public function activateUser($a_user_id = null)
280 {
281 return $this->toggleUser(true, $a_user_id);
282 }
283
290 public function deactivateUser($a_user_id = null)
291 {
292 return $this->toggleUser(false, $a_user_id);
293 }
294
301 protected function getUser($a_user_id = null)
302 {
303 global $ilUser;
304
305 if ($a_user_id === null ||
306 $a_user_id == $ilUser->getId()) {
307 $user = $ilUser;
308 } else {
309 $user = new ilUser($a_user_id);
310 }
311
312 if ($user->getId() &&
313 $user->getId() != ANONYMOUS_USER_ID) {
314 return $user;
315 }
316 }
317
325 protected function toggleUser($a_status, $a_user_id = null)
326 {
327 global $ilDB;
328
329 if (!self::isActive()) {
330 return;
331 }
332
333 switch ($this->getMode()) {
334 case self::MODE_ALL:
335 case self::MODE_SELF:
336 // current user!
337 $user = $this->getUser();
338 if ($user) {
339 // blocked value not supported in user pref!
340 $user->setPref("grpcrs_ntf_" . $this->ref_id, (int) (bool) $a_status);
341 $user->writePrefs();
342 return true;
343 }
344 break;
345
347 $user = $this->getUser($a_user_id);
348 if ($user) {
349 $user_id = $user->getId();
350
351 // did status change at all?
352 if (!array_key_exists($user_id, $this->custom) ||
353 $this->custom[$user_id != $a_status]) {
354 $this->custom[$user_id] = $a_status;
355
356 $ilDB->replace(
357 "member_noti_user",
358 array(
359 "ref_id" => array("integer", $this->ref_id),
360 "user_id" => array("integer", $user_id),
361 ),
362 array(
363 "status" => array("integer", $a_status)
364 )
365 );
366 }
367 return true;
368 }
369 break;
370
372 // no individual settings
373 break;
374 }
375
376 return false;
377 }
378
379
380 //
381 // CURRENT USER
382 //
383
389 public function isCurrentUserActive()
390 {
391 global $ilUser;
392
393 return in_array($ilUser->getId(), $this->getActiveUsers());
394 }
395
401 public function canCurrentUserEdit()
402 {
403 global $ilUser;
404
405 $user_id = $ilUser->getId();
406 if ($user_id == ANONYMOUS_USER_ID) {
407 return false;
408 }
409
410 switch ($this->getMode()) {
411 case self::MODE_SELF:
412 case self::MODE_ALL:
413 return true;
414
416 return false;
417
419 return !(array_key_exists($user_id, $this->custom) &&
420 $this->custom[$user_id] == self::VALUE_BLOCKED);
421 }
422 }
423
424
425 //
426 // CRON
427 //
428
434 public static function getActiveUsersforAllObjects()
435 {
436 global $ilDB, $tree;
437
439
440
441 $res = array();
442
443 if (self::isActive()) {
444 $objects = array();
445
446 // user-preference data (MODE_SELF)
447 $log->debug("read usr_pref");
448 $set = $ilDB->query("SELECT DISTINCT(keyword) keyword" .
449 " FROM usr_pref" .
450 " WHERE " . $ilDB->like("keyword", "text", "grpcrs_ntf_%") .
451 " AND value = " . $ilDB->quote("1", "text"));
452 while ($row = $ilDB->fetchAssoc($set)) {
453 $ref_id = substr($row["keyword"], 11);
454 $objects[(int) $ref_id] = (int) $ref_id;
455 }
456
457 // all other modes
458 $log->debug("read member_noti");
459 $set = $ilDB->query("SELECT ref_id" .
460 " FROM member_noti");
461 while ($row = $ilDB->fetchAssoc($set)) {
462 $objects[(int) $row["ref_id"]] = (int) $row["ref_id"];
463 }
464
465 // this might be slow but it is to be used in CRON JOB ONLY!
466 foreach (array_unique($objects) as $ref_id) {
467 // :TODO: enough checking?
468 if (!$tree->isDeleted($ref_id)) {
469 $log->debug("get active users");
470 $noti = new self($ref_id);
471 $active = $noti->getActiveUsers();
472 if (sizeof($active)) {
473 $res[$ref_id] = $active;
474 }
475 }
476 }
477 }
478
479 return $res;
480 }
481
482
483 //
484 // (OBJECT SETTINGS) FORM
485 //
486
494 public static function addToSettingsForm($a_ref_id, ilPropertyFormGUI $a_form = null, ilFormPropertyGUI $a_input = null)
495 {
496 global $lng;
497
498 if (self::isActive() &&
499 $a_ref_id) {
500 $lng->loadLanguageModule("membership");
501 $noti = new self($a_ref_id);
502
503 $force_noti = new ilRadioGroupInputGUI($lng->txt("mem_force_notification"), "force_noti");
504 $force_noti->setRequired(true);
505 if ($a_form) {
506 $a_form->addItem($force_noti);
507 } else {
508 $a_input->addSubItem($force_noti);
509 }
510
511 if ($noti->isValidMode(self::MODE_SELF)) {
512 $option = new ilRadioOption($lng->txt("mem_force_notification_mode_self"), self::MODE_SELF);
513 $force_noti->addOption($option);
514 }
515 if ($noti->isValidMode(self::MODE_ALL_BLOCKED)) {
516 $option = new ilRadioOption($lng->txt("mem_force_notification_mode_blocked"), self::MODE_ALL_BLOCKED);
517 $force_noti->addOption($option);
518
519 if ($noti->isValidMode(self::MODE_ALL)) {
520 $changeable = new ilCheckboxInputGUI($lng->txt("mem_force_notification_mode_all_sub_blocked"), "force_noti_allblk");
521 $option->addSubItem($changeable);
522 }
523 } elseif ($noti->isValidMode(self::MODE_ALL)) {
524 $option = new ilRadioOption($lng->txt("mem_force_notification_mode_all"), self::MODE_ALL);
525 $force_noti->addOption($option);
526 }
527 /* not supported in GUI
528 if($noti->isValidMode(self::MODE_CUSTOM))
529 {
530 $option = new ilRadioOption($lng->txt("mem_force_notification_mode_custom"), self::MODE_CUSTOM);
531 $option->setInfo($lng->txt("mem_force_notification_mode_custom_info"));
532 $force_noti->addOption($option);
533 }
534 */
535
536 // set current mode
537 $current_mode = $noti->getMode();
538 $has_changeable_cb = ($noti->isValidMode(self::MODE_ALL_BLOCKED) &&
539 $noti->isValidMode(self::MODE_ALL));
540 if (!$has_changeable_cb) {
541 $force_noti->setValue($current_mode);
542 } else {
543 switch ($current_mode) {
544 case self::MODE_SELF:
545 $force_noti->setValue($current_mode);
546 $changeable->setChecked(true); // checked as "default" on selection of parent
547 break;
548
550 $force_noti->setValue($current_mode);
551 break;
552
553 case self::MODE_ALL:
554 $force_noti->setValue(self::MODE_ALL_BLOCKED);
555 $changeable->setChecked(true);
556 break;
557 }
558 }
559 }
560 }
561
568 public static function importFromForm($a_ref_id, ilPropertyFormGUI $a_form = null)
569 {
570 if (self::isActive() &&
571 $a_ref_id) {
572 $noti = new self($a_ref_id);
573 $has_changeable_cb = ($noti->isValidMode(self::MODE_ALL_BLOCKED) &&
574 $noti->isValidMode(self::MODE_ALL));
575 $changeable = null;
576 if (!$a_form) {
577 $mode = (int) $_POST["force_noti"];
578 if ($has_changeable_cb) {
579 $changeable = (int) $_POST["force_noti_allblk"];
580 }
581 } else {
582 $mode = $a_form->getInput("force_noti");
583 if ($has_changeable_cb) {
584 $changeable = $a_form->getInput("force_noti_allblk");
585 }
586 }
587 // checkbox (all) is subitem of all_blocked
588 if ($changeable &&
589 $mode == self::MODE_ALL_BLOCKED) {
591 }
592 $noti->switchMode($mode);
593 }
594 }
595
601 public function cloneSettings($new_ref_id)
602 {
603 global $ilDB;
604
605 $set = $ilDB->queryF(
606 "SELECT * FROM member_noti " .
607 " WHERE ref_id = %s ",
608 array("integer"),
609 array($this->ref_id)
610 );
611 while ($rec = $ilDB->fetchAssoc($set)) {
612 $ilDB->insert("member_noti", array(
613 "ref_id" => array("integer", $new_ref_id),
614 "nmode" => array("integer", $rec["nmode"])
615 ));
616 }
617 }
618}
$users
Definition: authpage.php:44
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
This class represents a checkbox property in a property form.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
This class represents a property in a property form.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
static getLogger($a_component_id)
Get component logger.
Membership notification settings.
isCurrentUserActive()
Get user notification status.
canCurrentUserEdit()
Can user change notification status?
getParticipants()
Init participants for current object.
getUser($a_user_id=null)
Init user instance.
deactivateUser($a_user_id=null)
Deactivate notification for user.
getActiveUsers()
Get active notifications for current object.
static importFromForm($a_ref_id, ilPropertyFormGUI $a_form=null)
Import notification settings from form.
activateUser($a_user_id=null)
Activate notification for user.
cloneSettings($new_ref_id)
Clone notification object settings.
toggleUser($a_status, $a_user_id=null)
Toggle user notification status.
switchMode($a_new_mode)
Switch mode for object.
static addToSettingsForm($a_ref_id, ilPropertyFormGUI $a_form=null, ilFormPropertyGUI $a_input=null)
Add notification settings to form.
static getActiveUsersforAllObjects()
Get active notifications for all objects.
isValidMode($a_value)
Is given mode valid?
static _lookupObjId($a_id)
This class represents a property form user interface.
This class represents a property in a property form.
This class represents an option in a radio group.
$valid
global $lng
Definition: privfeed.php:17
global $ilSetting
Definition: privfeed.php:17
foreach($_POST as $key=> $value) $res
global $ilDB
$ilUser
Definition: imgupload.php:18