ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
4 require_once './Modules/Forum/classes/class.ilObjForum.php';
5 
6 
16 {
18  private $user_id;
19  private $forum_id;
20  private $thread_id;
21  private $admin_force;
22  private $user_toggle;
23 
24  private $ref_id;
25 
26 
27 
32  public function __construct($ref_id)
33  {
34  global $ilObjDataCache,$lng,$ilias;
35 
36  $this->lng = $lng;
37  $this->ilias = $ilias;
38  $this->ref_id = $ref_id;
39  $this->forum_id = $ilObjDataCache->lookupObjId($ref_id);
40 
41  }
42 
43  public function setNotificationId($a_notification_id)
44  {
45  $this->notification_id = $a_notification_id;
46  }
47  public function getNotificationId()
48  {
50  }
51  public function setUserId($a_user_id)
52  {
53  $this->user_id = $a_user_id;
54  }
55  public function getUserId()
56  {
57  return $this->user_id;
58  }
59 
60  public function setForumId($a_forum_id)
61  {
62  $this->forum_id = $a_forum_id;
63  }
64  public function getForumId()
65  {
66  return $this->forum_id;
67  }
68 
69  public function setThreadId($a_thread_id)
70  {
71  $this->thread_id = $a_thread_id;
72  }
73  public function getThreadId()
74  {
75  return $this->thread_id;
76  }
77 
78 
79  public function setAdminForce($a_admin_force)
80  {
81  $this->admin_force = $a_admin_force;
82  }
83  public function getAdminForce()
84  {
85  return $this->admin_force;
86  }
87 
88 
89  public function setUserToggle($a_user_toggle)
90  {
91  $this->user_toggle = $a_user_toggle;
92  }
93  public function getUserToggle()
94  {
95  return $this->user_toggle;
96  }
97 
98  public function setForumRefId($a_ref_id)
99  {
100  $this->ref_id = $a_ref_id;
101  }
102  public function getForumRefId()
103  {
104  return $this->ref_id;
105  }
106 
107  //user_id of who sets the setting to notify members
108  public function setUserIdNoti($a_user_id_noti)
109  {
110  $this->user_id_noti = $a_user_id_noti;
111  }
112  //user_id of who sets the setting to notify members
113  public function getUserIdNoti()
114  {
115  return $this->user_id_noti;
116  }
117 
118  public function isAdminForceNotification()
119  {
120  global $ilDB;
121 
122  $res = $ilDB->queryF('
123  SELECT admin_force_noti FROM frm_notification
124  WHERE user_id = %s
125  AND frm_id = %s
126  AND user_id_noti > %s ',
127  array('integer','integer', 'integer'),
128  array($this->getUserId(), $this->getForumId(), 0));
129 
130  while($row = $ilDB->fetchAssoc($res))
131  {
132  return $row['admin_force_noti'];
133  }
134  }
135  public function isUserToggleNotification()
136  {
137  global $ilDB;
138 
139  $res = $ilDB->queryF('
140  SELECT user_toggle_noti FROM frm_notification
141  WHERE user_id = %s
142  AND frm_id = %s
143  AND user_id_noti > %s',
144  array('integer', 'integer', 'integer'),
145  array($this->getUserId(), $this->getForumId(), 0 ));
146 
147  while($row = $ilDB->fetchAssoc($res))
148  {
149  return $row['user_toggle_noti'];
150  }
151 
152  }
153 
154  public function insertAdminForce()
155  {
156  global $ilDB, $ilUser;
157 
158  $next_id = $ilDB->nextId('frm_notification');
159  $res = $ilDB->manipulateF('
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(), $ilUser->getId()));
165 
166  }
167  public function deleteAdminForce()
168  {
169  global $ilDB;
170 
171  $res = $ilDB->manipulateF('
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  public function deleteUserToggle()
182  {
183  global $ilDB, $ilUser;
184 
185  $res = $ilDB->manipulateF('
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  global $ilDB;
200 
201  $res = $ilDB->manipulateF('
202  UPDATE frm_notification
203  SET user_toggle_noti = %s
204  WHERE user_id = %s
205  AND frm_id = %s
206  AND admin_force_noti = %s',
207  array('integer','integer','integer','integer'),
208  array($this->getUserToggle(), $this->getUserId(),$this->getForumId(), 1));
209  }
210 
211  public function getCrsGrpMemberToNotify()
212  {
213  global $ilDB;
214 
215  }
216 
217 
218  /* If a new member enters a Course or a Group, this function checks
219  * if this CRS/GRP contains a forum and a notification setting set by admin or moderator
220  * and inserts the new member into frm_notification
221  * */
222  public static function checkForumsExistsInsert($ref_id, $user_id = 0)
223  {
224  global $tree, $ilUser;
225 
226  include_once 'Modules/Forum/classes/class.ilForumProperties.php';
227 
228  $node_data = $tree->getChildsByType($ref_id, 'frm');
229  foreach($node_data as $data)
230  {
231  //check frm_properties if frm_noti is enabled
232  $frm_noti = new ilForumNotification($data['ref_id']);
233  if($user_id != 0)
234  {
235  $frm_noti->setUserId($user_id);
236  }
237  else $frm_noti->setUserId($ilUser->getId());
238 
240  $frm_noti->setAdminForce($admin_force);
241 
243  if($user_toggle) $frm_noti->setAdminForce(1);
244 
245  if($admin_force == 1 || $user_toggle == 1)
246  {
247  $frm_noti->setUserToggle($user_toggle);
248  $frm_noti->setForumId($data['obj_id']);
249  if($frm_noti->existsNotification() == false)
250  {
251  $frm_noti->insertAdminForce();
252  }
253  }
254  }
255  }
256 
257  public static function checkForumsExistsDelete($ref_id, $user_id = 0)
258  {
259  global $tree, $ilUser;
260 
261  $node_data = $tree->getChildsByType($ref_id, 'frm');
262 
263  include_once 'Modules/Forum/classes/class.ilForumModerators.php';
264 
265  foreach($node_data as $data)
266  {
267  //check frm_properties if frm_noti is enabled
268  $frm_noti = new ilForumNotification($data['ref_id']);
269  $objFrmMods = new ilForumModerators($data['ref_id']);
270  $moderator_ids = $objFrmMods->getCurrentModerators();
271 
272  if($user_id != 0)
273  {
274  $frm_noti->setUserId($user_id);
275  }
276  else $frm_noti->setUserId($ilUser->getId());
277 
278  $frm_noti->setForumId($data['obj_id']);
279  if(!in_array($frm_noti->getUserId(), $moderator_ids))
280  {
281  $frm_noti->deleteAdminForce();
282  }
283  }
284  }
285 
286  public static function _isParentNodeGrpCrs($a_ref_id)
287  {
288  global $tree;
289 
290  $parent_ref_id = $tree->getParentId($a_ref_id);
291  $parent_obj = ilObjectFactory::getInstanceByRefId($parent_ref_id);
292 
293  if($parent_obj->getType() == 'crs' || $parent_obj->getType() == 'grp')
294  return $parent_obj->getType();
295  else return false;
296  }
297 
298 
299  public static function _clearForcedForumNotifications($a_parameter)
300  {
301  global $ilDB, $ilObjDataCache;
302 
303  if(!$a_parameter['tree'] == 'tree')
304  {
305  return;
306  }
307 
308  $ref_id = $a_parameter['source_id'];
309  $is_parent = self::_isParentNodeGrpCrs($ref_id);
310 
311  if($is_parent)
312  {
313  $forum_id = $ilObjDataCache->lookupObjId($ref_id);
314 
315  $ilDB->manipulateF('
316  DELETE FROM frm_notification
317  WHERE frm_id = %s
318  AND admin_force_noti = %s',
319  array('integer','integer'),
320  array($forum_id, 1));
321  }
322  }
323 
324  public static function checkParentNodeTree($ref_id)
325  {
326  global $tree;
327 
328  $parent_ref_id = $tree->getParentId($ref_id);
329  $parent_obj = ilObjectFactory::getInstanceByRefId($parent_ref_id);
330 
331  if($parent_obj->getType() == 'crs')
332  {
333  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
334  $oParticipants = ilCourseParticipants::_getInstanceByObjId($parent_obj->getId());
335  }
336  else if($parent_obj->getType() == 'grp')
337  {
338  include_once 'Modules/Group/classes/class.ilGroupParticipants.php';
339  $oParticipants = ilGroupParticipants::_getInstanceByObjId($parent_obj->getId());
340  }
341 
342  $result = array();
343  if($parent_obj->getType() == 'crs' || $parent_obj->getType() == 'grp')
344  {
345  $moderator_ids = self::_getModerators($ref_id);
346  $admin_ids = $oParticipants->getAdmins();
347  $tutor_ids = $oParticipants->getTutors();
348 
349  $result = array_unique(array_merge($moderator_ids,$admin_ids,$tutor_ids));
350  }
351  return $result;
352  }
353 
361  public function _getModerators($a_ref_id)
362  {
363  global $rbacreview;
364 
365  $rolf = $rbacreview->getRoleFolderOfObject($a_ref_id);
366  $role_arr = $rbacreview->getRolesOfRoleFolder($rolf["ref_id"]);
367 
368  foreach ($role_arr as $role_id)
369  {
370  //$roleObj = $this->ilias->obj_factory->getInstanceByObjId($role_id);
371  $title = ilObject::_lookupTitle($role_id);
372  if ($title == "il_frm_moderator_".$a_ref_id)
373  {
374  #return $rbacreview->assignedUsers($roleObj->getId());
375  return $title = $rbacreview->assignedUsers($role_id);
376  }
377  }
378 
379  return array();
380  }
381 
382  public function update()
383  {
384  global $ilDB;
385 
386  $res = $ilDB->manipulateF('
387  UPDATE frm_notification
388  SET admin_force_noti = %s,
389  user_toggle_noti = %s
390  WHERE user_id = %s
391  AND frm_id = %s',
392  array('integer','integer','integer','integer'),
393  array($this->getAdminForce(), $this->getUserToggle(), $this->getUserId(), $this->getForumId()));
394  }
395 
396  public function deleteNotificationAllUsers()
397  {
398  global $ilDB;
399 
400  $res = $ilDB->manipulateF('
401  DELETE FROM frm_notification
402  WHERE frm_id = %s
403  AND user_id_noti > %s',
404  array('integer', 'integer'),
405  array($this->getForumId(), 0));
406  }
407 
408  public function read()
409  {
410  global $ilDB;
411  $result = array();
412 
413  $query = $ilDB->queryF('
414  SELECT * FROM frm_notification WHERE
415  frm_id = %s',
416  array('integer'),
417  array($this->getForumId()));
418 
419  while($row = $ilDB->fetchAssoc($query))
420  {
421  $result[$row['user_id']] = $row;
422  }
423  return $result;
424  }
425 
426 
430  public function existsNotification()
431  {
432  global $ilDB;
433 
434  $res = $ilDB->queryF('
435  SELECT * FROM frm_notification
436  WHERE user_id = %s
437  AND frm_id = %s
438  AND admin_force_noti = %s',
439  array('integer', 'integer', 'integer'),
440  array($this->getUserId(), $this->getForumId(), $this->getAdminForce()));
441 
442  if($row = $ilDB->numRows($res) > 0)
443  {
444  return true;
445  }
446  return false;
447  }
448 } // END class.ilForumNotification