ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilForumNotification.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/Forum/classes/class.ilObjForum.php';
5require_once 'Modules/Forum/classes/class.ilForum.php';
6
16{
17 protected static $node_data_cache = array();
18
20 private $user_id;
21 private $forum_id;
22 private $thread_id;
23 private $admin_force;
24 private $user_toggle;
25
26 private $ref_id;
27 private $db;
28 private $user;
29
30
35 public function __construct($ref_id)
36 {
37 global $DIC;
38
39 $this->lng = $DIC->language();
40 $this->db = $DIC->database();
41 $this->user = $DIC->user();
42 $this->ref_id = $ref_id;
43 $this->forum_id = $DIC['ilObjDataCache']->lookupObjId($ref_id);
44 }
45
46 public function setNotificationId($a_notification_id)
47 {
48 $this->notification_id = $a_notification_id;
49 }
50 public function getNotificationId()
51 {
53 }
54 public function setUserId($a_user_id)
55 {
56 $this->user_id = $a_user_id;
57 }
58 public function getUserId()
59 {
60 return $this->user_id;
61 }
62
63 public function setForumId($a_forum_id)
64 {
65 $this->forum_id = $a_forum_id;
66 }
67 public function getForumId()
68 {
69 return $this->forum_id;
70 }
71
72 public function setThreadId($a_thread_id)
73 {
74 $this->thread_id = $a_thread_id;
75 }
76 public function getThreadId()
77 {
78 return $this->thread_id;
79 }
80
81
82 public function setAdminForce($a_admin_force)
83 {
84 $this->admin_force = $a_admin_force;
85 }
86 public function getAdminForce()
87 {
88 return $this->admin_force;
89 }
90
91
92 public function setUserToggle($a_user_toggle)
93 {
94 $this->user_toggle = $a_user_toggle;
95 }
96 public function getUserToggle()
97 {
98 return $this->user_toggle;
99 }
100
101 public function setForumRefId($a_ref_id)
102 {
103 $this->ref_id = $a_ref_id;
104 }
105 public function getForumRefId()
106 {
107 return $this->ref_id;
108 }
109
110 //user_id of who sets the setting to notify members
111 public function setUserIdNoti($a_user_id_noti)
112 {
113 $this->user_id_noti = $a_user_id_noti;
114 }
115 //user_id of who sets the setting to notify members
116 public function getUserIdNoti()
117 {
118 return $this->user_id_noti;
119 }
120
121 public function isAdminForceNotification()
122 {
123 $res = $this->db->queryF(
124 '
125 SELECT admin_force_noti FROM frm_notification
126 WHERE user_id = %s
127 AND frm_id = %s
128 AND user_id_noti > %s ',
129 array('integer','integer', 'integer'),
130 array($this->getUserId(), $this->getForumId(), 0)
131 );
132
133 while ($row = $this->db->fetchAssoc($res)) {
134 return $row['admin_force_noti'];
135 }
136 }
137
138 public function isUserToggleNotification()
139 {
140 $res = $this->db->queryF(
141 '
142 SELECT user_toggle_noti FROM frm_notification
143 WHERE user_id = %s
144 AND frm_id = %s
145 AND user_id_noti > %s',
146 array('integer', 'integer', 'integer'),
147 array($this->getUserId(), $this->getForumId(), 0 )
148 );
149
150 while ($row = $this->db->fetchAssoc($res)) {
151 return $row['user_toggle_noti'];
152 }
153 }
154
155 public function insertAdminForce()
156 {
157 $next_id = $this->db->nextId('frm_notification');
158 $this->db->manipulateF(
159 '
160 INSERT INTO frm_notification
161 (notification_id, user_id, frm_id, admin_force_noti, user_toggle_noti, user_id_noti)
162 VALUES(%s,%s,%s,%s,%s,%s)',
163 array('integer', 'integer', 'integer', 'integer', 'integer', 'integer'),
164 array($next_id, $this->getUserId(), $this->getForumId(), $this->getAdminForce(), $this->getUserToggle(), $this->user->getId())
165 );
166 }
167
168 public function deleteAdminForce()
169 {
170 $this->db->manipulateF(
171 '
172 DELETE FROM frm_notification
173 WHERE user_id = %s
174 AND frm_id = %s
175 AND admin_force_noti = %s
176 AND user_id_noti > %s' ,
177 array('integer', 'integer','integer', 'integer'),
178 array($this->getUserId(), $this->getForumId(), 1, 0)
179 );
180 }
181
182 public function deleteUserToggle()
183 {
184 $this->db->manipulateF(
185 '
186 DELETE FROM frm_notification
187 WHERE user_id = %s
188 AND frm_id = %s
189 AND admin_force_noti = %s
190 AND user_toggle_noti = %s
191 AND user_id_noti > %s' ,
192 array('integer', 'integer','integer','integer', 'integer'),
193 array($this->getUserId(),$this->getForumId(),1,1, 0 )
194 );
195 }
196
197 public function updateUserToggle()
198 {
199 $this->db->manipulateF(
200 '
201 UPDATE frm_notification
202 SET user_toggle_noti = %s
203 WHERE user_id = %s
204 AND frm_id = %s
205 AND admin_force_noti = %s',
206 array('integer','integer','integer','integer'),
207 array($this->getUserToggle(), $this->getUserId(),$this->getForumId(), 1)
208 );
209 }
210
211 /* If a new member enters a Course or a Group, this function checks
212 * if this CRS/GRP contains a forum and a notification setting set by admin or moderator
213 * and inserts the new member into frm_notification
214 * */
215 public static function checkForumsExistsInsert($ref_id, $user_id = 0)
216 {
217 global $DIC;
218 $ilUser = $DIC->user();
219
220 include_once 'Modules/Forum/classes/class.ilForumProperties.php';
221
222 $node_data = self::getCachedNodeData($ref_id);
223
224 foreach ($node_data as $data) {
225 //check frm_properties if frm_noti is enabled
226 $frm_noti = new ilForumNotification($data['ref_id']);
227 if ($user_id != 0) {
228 $frm_noti->setUserId($user_id);
229 } else {
230 $frm_noti->setUserId($ilUser->getId());
231 }
232
234 $frm_noti->setAdminForce($admin_force);
235
237 if ($user_toggle) {
238 $frm_noti->setAdminForce(1);
239 }
240
241 if ($admin_force == 1 || $user_toggle == 1) {
242 $frm_noti->setUserToggle($user_toggle);
243 $frm_noti->setForumId($data['obj_id']);
244 if ($frm_noti->existsNotification() == false) {
245 $frm_noti->insertAdminForce();
246 }
247 }
248 }
249 }
250
251 public static function checkForumsExistsDelete($ref_id, $user_id = 0)
252 {
253 global $DIC;
254 $ilUser = $DIC->user();
255
256 $node_data = self::getCachedNodeData($ref_id);
257
258 include_once 'Modules/Forum/classes/class.ilForumModerators.php';
259
260 foreach ($node_data as $data) {
261 //check frm_properties if frm_noti is enabled
262 $frm_noti = new ilForumNotification($data['ref_id']);
263 $objFrmMods = new ilForumModerators($data['ref_id']);
264 $moderator_ids = $objFrmMods->getCurrentModerators();
265
266 if ($user_id != 0) {
267 $frm_noti->setUserId($user_id);
268 } else {
269 $frm_noti->setUserId($ilUser->getId());
270 }
271
272 $frm_noti->setForumId($data['obj_id']);
273 if (!in_array($frm_noti->getUserId(), $moderator_ids)) {
274 $frm_noti->deleteAdminForce();
275 }
276 }
277 }
278
283 public static function getCachedNodeData($ref_id)
284 {
285 if (!array_key_exists($ref_id, self::$node_data_cache)) {
286 global $DIC;
287 self::$node_data_cache[$ref_id] = $DIC->repositoryTree()->getChildsByType($ref_id, 'frm');
288 }
289
290 return self::$node_data_cache[$ref_id];
291 }
292
297 public static function _isParentNodeGrpCrs($a_ref_id)
298 {
299 global $DIC;
300
301 $parent_ref_id = $DIC->repositoryTree()->getParentId($a_ref_id);
302 $parent_obj = ilObjectFactory::getInstanceByRefId($parent_ref_id);
303
304 if ($parent_obj->getType() == 'crs' || $parent_obj->getType() == 'grp') {
305 return $parent_obj->getType();
306 } else {
307 return false;
308 }
309 }
310
314 public static function _clearForcedForumNotifications($a_parameter)
315 {
316 global $DIC;
317 $ilDB = $DIC->database();
318 $ilObjDataCache = $DIC['ilObjDataCache'];
319
320 if (!$a_parameter['tree'] == 'tree') {
321 return;
322 }
323
324 $ref_id = $a_parameter['source_id'];
326
327 if ($is_parent) {
328 $forum_id = $ilObjDataCache->lookupObjId($ref_id);
329
330 $ilDB->manipulateF(
331 '
332 DELETE FROM frm_notification
333 WHERE frm_id = %s
334 AND admin_force_noti = %s',
335 array('integer','integer'),
336 array($forum_id, 1)
337 );
338 }
339 }
340
345 public static function checkParentNodeTree($ref_id)
346 {
347 global $DIC;
348 $tree = $DIC->repositoryTree();
349
350 $parent_ref_id = $tree->getParentId($ref_id);
351 $parent_obj = ilObjectFactory::getInstanceByRefId($parent_ref_id);
352
353 if ($parent_obj->getType() == 'crs') {
354 include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
355 $oParticipants = ilCourseParticipants::_getInstanceByObjId($parent_obj->getId());
356 } elseif ($parent_obj->getType() == 'grp') {
357 include_once 'Modules/Group/classes/class.ilGroupParticipants.php';
358 $oParticipants = ilGroupParticipants::_getInstanceByObjId($parent_obj->getId());
359 }
360
361 $result = array();
362 if ($parent_obj->getType() == 'crs' || $parent_obj->getType() == 'grp') {
363 $moderator_ids = ilForum::_getModerators($ref_id);
364 $admin_ids = $oParticipants->getAdmins();
365 $tutor_ids = $oParticipants->getTutors();
366
367 $result = array_unique(array_merge($moderator_ids, $admin_ids, $tutor_ids));
368 }
369 return $result;
370 }
371
372 public function update()
373 {
374 $this->db->manipulateF(
375 '
376 UPDATE frm_notification
377 SET admin_force_noti = %s,
378 user_toggle_noti = %s
379 WHERE user_id = %s
380 AND frm_id = %s',
381 array('integer','integer','integer','integer'),
382 array($this->getAdminForce(), $this->getUserToggle(), $this->getUserId(), $this->getForumId())
383 );
384 }
385
387 {
388 $this->db->manipulateF(
389 '
390 DELETE FROM frm_notification
391 WHERE frm_id = %s
392 AND user_id_noti > %s',
393 array('integer', 'integer'),
394 array($this->getForumId(), 0)
395 );
396 }
397
398 public function read()
399 {
400 $result = array();
401
402 $query = $this->db->queryF(
403 '
404 SELECT * FROM frm_notification WHERE
405 frm_id = %s',
406 array('integer'),
407 array($this->getForumId())
408 );
409
410 while ($row = $this->db->fetchAssoc($query)) {
411 $result[$row['user_id']] = $row;
412 }
413 return $result;
414 }
415
420 public static function mergeThreadNotificiations($merge_source_thread_id, $merge_target_thread_id)
421 {
422 // check notifications etc..
423 global $DIC;
424 $ilDB = $DIC->database();
425
426 $res = $ilDB->queryF(
427 'SELECT notification_id, user_id FROM frm_notification
428 WHERE frm_id = %s
429 AND thread_id = %s
430 ORDER BY user_id ASC',
431 array('integer', 'integer'),
432 array(0, $merge_source_thread_id)
433 );
434
435 $res_2 = $ilDB->queryF(
436 'SELECT user_id FROM frm_notification
437 WHERE frm_id = %s
438 AND thread_id = %s
439 ORDER BY user_id ASC',
440 array('integer', 'integer'),
441 array(0, $merge_target_thread_id)
442 );
443
444 $users_already_notified = $ilDB->fetchAssoc($res_2);
445 while ($row = $ilDB->fetchAssoc($res)) {
446 if (in_array($row['user_id'], $users_already_notified)) {
447 // delete source notification because already exists for target_id
448 $ilDB->manipulatef(
449 'DELETE FROM frm_notification WHERE notification_id = %s',
450 array('integer'),
451 array($row['notification_id'])
452 );
453 } else {
454 // update source notification
455 $ilDB->update(
456 'frm_notification',
457 array('thread_id' => array('integer', $merge_target_thread_id)),
458 array('thread_id' => array('integer', $merge_source_thread_id)
459 )
460 );
461 }
462 }
463 }
464
468 public function existsNotification()
469 {
470 $res = $this->db->queryF(
471 '
472 SELECT * FROM frm_notification
473 WHERE user_id = %s
474 AND frm_id = %s
475 AND admin_force_noti = %s',
476 array('integer', 'integer', 'integer'),
477 array($this->getUserId(), $this->getForumId(), $this->getAdminForce())
478 );
479
480 if ($row = $this->db->numRows($res) > 0) {
481 return true;
482 }
483 return false;
484 }
485
489 public function cloneFromSource($sourceRefId)
490 {
491 $sourceNotificationSettings = new self($sourceRefId);
492 $records = $sourceNotificationSettings->read();
493
494 foreach ($records as $usrId => $row) {
495 $this->setUserId($usrId);
496 $this->setAdminForce($row['admin_force_noti']);
497 $this->setUserToggle($row['user_toggle_noti']);
498 $this->setUserIdNoti($row['user_id_noti']);
499
500 $this->insertAdminForce();
501 }
502 }
503}
$result
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
Class ilForumModerators.
Class ilForumNotification.
__construct($ref_id)
Constructor @access public.
setNotificationId($a_notification_id)
static checkForumsExistsDelete($ref_id, $user_id=0)
static _clearForcedForumNotifications($a_parameter)
static checkForumsExistsInsert($ref_id, $user_id=0)
static mergeThreadNotificiations($merge_source_thread_id, $merge_target_thread_id)
static _isParentNodeGrpCrs($a_ref_id)
static _isUserToggleNoti($a_obj_id)
static _isAdminForceNoti($a_obj_id)
static _getModerators($a_ref_id)
get all users assigned to local role il_frm_moderator_<frm_ref_id> (static)
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
$query
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
$records
Definition: simple_test.php:22
global $ilDB
$ilUser
Definition: imgupload.php:18