ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
7
17{
18 protected static $node_data_cache = array();
19
21 private $user_id;
22 private $forum_id;
23 private $thread_id;
24 private $admin_force;
25 private $user_toggle;
26
27 private $ref_id;
28
29
30
35 public function __construct($ref_id)
36 {
37 global $ilObjDataCache,$lng,$ilias;
38
39 $this->lng = $lng;
40 $this->ilias = $ilias;
41 $this->ref_id = $ref_id;
42 $this->forum_id = $ilObjDataCache->lookupObjId($ref_id);
43
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 global $ilDB;
124
125 $res = $ilDB->queryF('
126 SELECT admin_force_noti FROM frm_notification
127 WHERE user_id = %s
128 AND frm_id = %s
129 AND user_id_noti > %s ',
130 array('integer','integer', 'integer'),
131 array($this->getUserId(), $this->getForumId(), 0));
132
133 while($row = $ilDB->fetchAssoc($res))
134 {
135 return $row['admin_force_noti'];
136 }
137 }
138 public function isUserToggleNotification()
139 {
140 global $ilDB;
141
142 $res = $ilDB->queryF('
143 SELECT user_toggle_noti FROM frm_notification
144 WHERE user_id = %s
145 AND frm_id = %s
146 AND user_id_noti > %s',
147 array('integer', 'integer', 'integer'),
148 array($this->getUserId(), $this->getForumId(), 0 ));
149
150 while($row = $ilDB->fetchAssoc($res))
151 {
152 return $row['user_toggle_noti'];
153 }
154
155 }
156
157 public function insertAdminForce()
158 {
159 global $ilDB, $ilUser;
160
161 $next_id = $ilDB->nextId('frm_notification');
162 $res = $ilDB->manipulateF('
163 INSERT INTO frm_notification
164 (notification_id, user_id, frm_id, admin_force_noti, user_toggle_noti, user_id_noti)
165 VALUES(%s,%s,%s,%s,%s,%s)',
166 array('integer', 'integer', 'integer', 'integer', 'integer', 'integer'),
167 array($next_id, $this->getUserId(), $this->getForumId(), $this->getAdminForce(), $this->getUserToggle(), $ilUser->getId()));
168
169 }
170 public function deleteAdminForce()
171 {
172 global $ilDB;
173
174 $res = $ilDB->manipulateF('
175 DELETE FROM frm_notification
176 WHERE user_id = %s
177 AND frm_id = %s
178 AND admin_force_noti = %s
179 AND user_id_noti > %s' ,
180 array('integer', 'integer','integer', 'integer'),
181 array($this->getUserId(), $this->getForumId(), 1, 0));
182 }
183
184 public function deleteUserToggle()
185 {
186 global $ilDB, $ilUser;
187
188 $res = $ilDB->manipulateF('
189 DELETE FROM frm_notification
190 WHERE user_id = %s
191 AND frm_id = %s
192 AND admin_force_noti = %s
193 AND user_toggle_noti = %s
194 AND user_id_noti > %s' ,
195 array('integer', 'integer','integer','integer', 'integer'),
196 array($this->getUserId(),$this->getForumId(),1,1, 0 ));
197
198 }
199
200 public function updateUserToggle()
201 {
202 global $ilDB;
203
204 $res = $ilDB->manipulateF('
205 UPDATE frm_notification
206 SET user_toggle_noti = %s
207 WHERE user_id = %s
208 AND frm_id = %s
209 AND admin_force_noti = %s',
210 array('integer','integer','integer','integer'),
211 array($this->getUserToggle(), $this->getUserId(),$this->getForumId(), 1));
212 }
213
214 /* If a new member enters a Course or a Group, this function checks
215 * if this CRS/GRP contains a forum and a notification setting set by admin or moderator
216 * and inserts the new member into frm_notification
217 * */
218 public static function checkForumsExistsInsert($ref_id, $user_id = 0)
219 {
220 global $ilUser;
221
222 include_once 'Modules/Forum/classes/class.ilForumProperties.php';
223
224 $node_data = self::getCachedNodeData($ref_id);
225
226 foreach($node_data as $data)
227 {
228 //check frm_properties if frm_noti is enabled
229 $frm_noti = new ilForumNotification($data['ref_id']);
230 if($user_id != 0)
231 {
232 $frm_noti->setUserId($user_id);
233 }
234 else $frm_noti->setUserId($ilUser->getId());
235
237 $frm_noti->setAdminForce($admin_force);
238
240 if($user_toggle) $frm_noti->setAdminForce(1);
241
242 if($admin_force == 1 || $user_toggle == 1)
243 {
244 $frm_noti->setUserToggle($user_toggle);
245 $frm_noti->setForumId($data['obj_id']);
246 if($frm_noti->existsNotification() == false)
247 {
248 $frm_noti->insertAdminForce();
249 }
250 }
251 }
252 }
253
254 public static function checkForumsExistsDelete($ref_id, $user_id = 0)
255 {
256 global $ilUser;
257
258 $node_data = self::getCachedNodeData($ref_id);
259
260 include_once 'Modules/Forum/classes/class.ilForumModerators.php';
261
262 foreach($node_data as $data)
263 {
264 //check frm_properties if frm_noti is enabled
265 $frm_noti = new ilForumNotification($data['ref_id']);
266 $objFrmMods = new ilForumModerators($data['ref_id']);
267 $moderator_ids = $objFrmMods->getCurrentModerators();
268
269 if($user_id != 0)
270 {
271 $frm_noti->setUserId($user_id);
272 }
273 else $frm_noti->setUserId($ilUser->getId());
274
275 $frm_noti->setForumId($data['obj_id']);
276 if(!in_array($frm_noti->getUserId(), $moderator_ids))
277 {
278 $frm_noti->deleteAdminForce();
279 }
280 }
281 }
282
286 public static function getCachedNodeData($ref_id)
287 {
288 if(!array_key_exists($ref_id, self::$node_data_cache))
289 {
290 global $tree;
291 self::$node_data_cache[$ref_id] = $tree->getChildsByType($ref_id, 'frm');
292 }
293
294 return self::$node_data_cache[$ref_id];
295 }
296
297 public static function _isParentNodeGrpCrs($a_ref_id)
298 {
299 global $tree;
300
301 $parent_ref_id = $tree->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 return false;
307 }
308
309
310 public static function _clearForcedForumNotifications($a_parameter)
311 {
312 global $ilDB, $ilObjDataCache;
313
314 if(!$a_parameter['tree'] == 'tree')
315 {
316 return;
317 }
318
319 $ref_id = $a_parameter['source_id'];
321
322 if($is_parent)
323 {
324 $forum_id = $ilObjDataCache->lookupObjId($ref_id);
325
326 $ilDB->manipulateF('
327 DELETE FROM frm_notification
328 WHERE frm_id = %s
329 AND admin_force_noti = %s',
330 array('integer','integer'),
331 array($forum_id, 1));
332 }
333 }
334
335 public static function checkParentNodeTree($ref_id)
336 {
337 global $tree;
338
339 $parent_ref_id = $tree->getParentId($ref_id);
340 $parent_obj = ilObjectFactory::getInstanceByRefId($parent_ref_id);
341
342 if($parent_obj->getType() == 'crs')
343 {
344 include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
345 $oParticipants = ilCourseParticipants::_getInstanceByObjId($parent_obj->getId());
346 }
347 else if($parent_obj->getType() == 'grp')
348 {
349 include_once 'Modules/Group/classes/class.ilGroupParticipants.php';
350 $oParticipants = ilGroupParticipants::_getInstanceByObjId($parent_obj->getId());
351 }
352
353 $result = array();
354 if($parent_obj->getType() == 'crs' || $parent_obj->getType() == 'grp')
355 {
356 $moderator_ids = ilForum::_getModerators($ref_id);
357 $admin_ids = $oParticipants->getAdmins();
358 $tutor_ids = $oParticipants->getTutors();
359
360 $result = array_unique(array_merge($moderator_ids,$admin_ids,$tutor_ids));
361 }
362 return $result;
363 }
364
365 public function update()
366 {
367 global $ilDB;
368
369 $res = $ilDB->manipulateF('
370 UPDATE frm_notification
371 SET admin_force_noti = %s,
372 user_toggle_noti = %s
373 WHERE user_id = %s
374 AND frm_id = %s',
375 array('integer','integer','integer','integer'),
376 array($this->getAdminForce(), $this->getUserToggle(), $this->getUserId(), $this->getForumId()));
377 }
378
380 {
381 global $ilDB;
382
383 $res = $ilDB->manipulateF('
384 DELETE FROM frm_notification
385 WHERE frm_id = %s
386 AND user_id_noti > %s',
387 array('integer', 'integer'),
388 array($this->getForumId(), 0));
389 }
390
391 public function read()
392 {
393 global $ilDB;
394 $result = array();
395
396 $query = $ilDB->queryF('
397 SELECT * FROM frm_notification WHERE
398 frm_id = %s',
399 array('integer'),
400 array($this->getForumId()));
401
402 while($row = $ilDB->fetchAssoc($query))
403 {
404 $result[$row['user_id']] = $row;
405 }
406 return $result;
407 }
408
409 public static function mergeThreadNotificiations($merge_source_thread_id, $merge_target_thread_id)
410 {
411 // check notifications etc..
412 global $ilDB;
413
414 $res = $ilDB->queryF('SELECT notification_id, user_id FROM frm_notification
415 WHERE frm_id = %s
416 AND thread_id = %s
417 ORDER BY user_id ASC',
418 array('integer', 'integer'), array(0, $merge_source_thread_id));
419
420 $res_2 = $ilDB->queryF('SELECT user_id FROM frm_notification
421 WHERE frm_id = %s
422 AND thread_id = %s
423 ORDER BY user_id ASC',
424 array('integer', 'integer'), array(0, $merge_target_thread_id));
425
426 $users_already_notified = $ilDB->fetchAssoc($res_2);
427 while($row = $ilDB->fetchAssoc($res))
428 {
429 if(in_array($row['user_id'], $users_already_notified))
430 {
431 // delete source notification because already exists for target_id
432 $ilDB->manipulatef('DELETE FROM frm_notification WHERE notification_id = %s',
433 array('integer'), array($row['notification_id']));
434 }
435 else
436 {
437 // update source notification
438 $ilDB->update('frm_notification',
439 array('thread_id' => array('integer', $merge_target_thread_id)),
440 array('thread_id' => array('integer', $merge_source_thread_id)
441 ));
442 }
443 }
444 }
445
449 public function existsNotification()
450 {
451 global $ilDB;
452
453 $res = $ilDB->queryF('
454 SELECT * FROM frm_notification
455 WHERE user_id = %s
456 AND frm_id = %s
457 AND admin_force_noti = %s',
458 array('integer', 'integer', 'integer'),
459 array($this->getUserId(), $this->getForumId(), $this->getAdminForce()));
460
461 if($row = $ilDB->numRows($res) > 0)
462 {
463 return true;
464 }
465 return false;
466 }
467
468} // END class.ilForumNotification
$result
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
redirection script todo: (a better solution should control the processing via a xml file)
global $lng
Definition: privfeed.php:17
global $ilDB
$ilUser
Definition: imgupload.php:18