ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
13 {
14  protected static $node_data_cache = array();
15 
17  private $user_id;
18  private $forum_id;
19  private $thread_id;
20  private $admin_force;
21  private $user_toggle;
22 
23  private $ref_id;
24  private $db;
25  private $user;
26 
27 
32  public function __construct($ref_id)
33  {
34  global $DIC;
35 
36  $this->lng = $DIC->language();
37  $this->db = $DIC->database();
38  $this->user = $DIC->user();
39  $this->ref_id = $ref_id;
40  $this->forum_id = $DIC['ilObjDataCache']->lookupObjId($ref_id);
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  $res = $this->db->queryF(
121  '
122  SELECT admin_force_noti FROM frm_notification
123  WHERE user_id = %s
124  AND frm_id = %s
125  AND user_id_noti > %s ',
126  array('integer','integer', 'integer'),
127  array($this->getUserId(), $this->getForumId(), 0)
128  );
129 
130  while ($row = $this->db->fetchAssoc($res)) {
131  return $row['admin_force_noti'];
132  }
133  }
134 
135  public function isUserToggleNotification()
136  {
137  $res = $this->db->queryF(
138  '
139  SELECT user_toggle_noti FROM frm_notification
140  WHERE user_id = %s
141  AND frm_id = %s
142  AND user_id_noti > %s',
143  array('integer', 'integer', 'integer'),
144  array($this->getUserId(), $this->getForumId(), 0 )
145  );
146 
147  while ($row = $this->db->fetchAssoc($res)) {
148  return $row['user_toggle_noti'];
149  }
150  }
151 
152  public function insertAdminForce()
153  {
154  $next_id = $this->db->nextId('frm_notification');
155  $this->db->manipulateF(
156  '
157  INSERT INTO frm_notification
158  (notification_id, user_id, frm_id, admin_force_noti, user_toggle_noti, user_id_noti)
159  VALUES(%s,%s,%s,%s,%s,%s)',
160  array('integer', 'integer', 'integer', 'integer', 'integer', 'integer'),
161  array($next_id, $this->getUserId(), $this->getForumId(), $this->getAdminForce(), $this->getUserToggle(), $this->user->getId())
162  );
163  }
164 
165  public function deleteAdminForce()
166  {
167  $this->db->manipulateF(
168  '
169  DELETE FROM frm_notification
170  WHERE user_id = %s
171  AND frm_id = %s
172  AND admin_force_noti = %s
173  AND user_id_noti > %s',
174  array('integer', 'integer','integer', 'integer'),
175  array($this->getUserId(), $this->getForumId(), 1, 0)
176  );
177  }
178 
179  public function deleteUserToggle()
180  {
181  $this->db->manipulateF(
182  '
183  DELETE FROM frm_notification
184  WHERE user_id = %s
185  AND frm_id = %s
186  AND admin_force_noti = %s
187  AND user_toggle_noti = %s
188  AND user_id_noti > %s',
189  array('integer', 'integer','integer','integer', 'integer'),
190  array($this->getUserId(),$this->getForumId(),1,1, 0 )
191  );
192  }
193 
194  public function updateUserToggle()
195  {
196  $this->db->manipulateF(
197  '
198  UPDATE frm_notification
199  SET user_toggle_noti = %s
200  WHERE user_id = %s
201  AND frm_id = %s
202  AND admin_force_noti = %s',
203  array('integer','integer','integer','integer'),
204  array($this->getUserToggle(), $this->getUserId(),$this->getForumId(), 1)
205  );
206  }
207 
208  /* If a new member enters a Course or a Group, this function checks
209  * if this CRS/GRP contains a forum and a notification setting set by admin or moderator
210  * and inserts the new member into frm_notification
211  * */
212  public static function checkForumsExistsInsert($ref_id, $user_id = 0)
213  {
214  global $DIC;
215  $ilUser = $DIC->user();
216 
217  $node_data = self::getCachedNodeData($ref_id);
218 
219  foreach ($node_data as $data) {
220  //check frm_properties if frm_noti is enabled
221  $frm_noti = new ilForumNotification($data['ref_id']);
222  if ($user_id != 0) {
223  $frm_noti->setUserId($user_id);
224  } else {
225  $frm_noti->setUserId($ilUser->getId());
226  }
227 
229  $frm_noti->setAdminForce($admin_force);
230 
232  if ($user_toggle) {
233  $frm_noti->setAdminForce(1);
234  }
235 
236  if ($admin_force == 1 || $user_toggle == 1) {
237  $frm_noti->setUserToggle($user_toggle);
238  $frm_noti->setForumId($data['obj_id']);
239  if ($frm_noti->existsNotification() == false) {
240  $frm_noti->insertAdminForce();
241  }
242  }
243  }
244  }
245 
246  public static function checkForumsExistsDelete($ref_id, $user_id = 0)
247  {
248  global $DIC;
249  $ilUser = $DIC->user();
250 
251  $node_data = self::getCachedNodeData($ref_id);
252 
253  foreach ($node_data as $data) {
254  //check frm_properties if frm_noti is enabled
255  $frm_noti = new ilForumNotification($data['ref_id']);
256  $objFrmMods = new ilForumModerators($data['ref_id']);
257  $moderator_ids = $objFrmMods->getCurrentModerators();
258 
259  if ($user_id != 0) {
260  $frm_noti->setUserId($user_id);
261  } else {
262  $frm_noti->setUserId($ilUser->getId());
263  }
264 
265  $frm_noti->setForumId($data['obj_id']);
266  if (!in_array($frm_noti->getUserId(), $moderator_ids)) {
267  $frm_noti->deleteAdminForce();
268  }
269  }
270  }
271 
276  public static function getCachedNodeData($ref_id)
277  {
278  if (!array_key_exists($ref_id, self::$node_data_cache)) {
279  global $DIC;
280  $node_data = $DIC->repositoryTree()->getSubTree(
281  $DIC->repositoryTree()->getNodeData($ref_id),
282  true,
283  'frm'
284  );
285  $node_data = array_filter($node_data, function ($forum_node) use ($DIC, $ref_id) {
286  // filter out forum if a grp lies in the path (#0027702)
287  foreach ($DIC->repositoryTree()->getNodePath($forum_node['child'], $ref_id) as $path_node) {
288  if ((int) $path_node['child'] !== (int) $ref_id && $path_node['type'] === 'grp') {
289  return false;
290  }
291  }
292  return true;
293  });
294  self::$node_data_cache[$ref_id] = $node_data;
295  }
296 
297  return self::$node_data_cache[$ref_id];
298  }
299 
304  public static function _isParentNodeGrpCrs($a_ref_id)
305  {
306  global $DIC;
307 
308  $parent_ref_id = $DIC->repositoryTree()->getParentId($a_ref_id);
309  $parent_obj = ilObjectFactory::getInstanceByRefId($parent_ref_id);
310 
311  if ($parent_obj->getType() == 'crs' || $parent_obj->getType() == 'grp') {
312  return $parent_obj->getType();
313  } else {
314  return false;
315  }
316  }
317 
321  public static function _clearForcedForumNotifications($a_parameter)
322  {
323  global $DIC;
324  $ilDB = $DIC->database();
325  $ilObjDataCache = $DIC['ilObjDataCache'];
326 
327  if (!$a_parameter['tree'] == 'tree') {
328  return;
329  }
330 
331  $ref_id = $a_parameter['source_id'];
332  $is_parent = self::_isParentNodeGrpCrs($ref_id);
333 
334  if ($is_parent) {
335  $forum_id = $ilObjDataCache->lookupObjId($ref_id);
336 
337  $ilDB->manipulateF(
338  '
339  DELETE FROM frm_notification
340  WHERE frm_id = %s
341  AND admin_force_noti = %s',
342  array('integer','integer'),
343  array($forum_id, 1)
344  );
345  }
346  }
347 
352  public static function checkParentNodeTree($ref_id)
353  {
354  global $DIC;
355  $tree = $DIC->repositoryTree();
356 
357  $parent_ref_id = $tree->getParentId($ref_id);
358  $parent_obj = ilObjectFactory::getInstanceByRefId($parent_ref_id);
359 
360  if ($parent_obj->getType() == 'crs') {
361  $oParticipants = ilCourseParticipants::_getInstanceByObjId($parent_obj->getId());
362  } elseif ($parent_obj->getType() == 'grp') {
363  $oParticipants = ilGroupParticipants::_getInstanceByObjId($parent_obj->getId());
364  }
365 
366  $result = array();
367  if ($parent_obj->getType() == 'crs' || $parent_obj->getType() == 'grp') {
368  $moderator_ids = ilForum::_getModerators($ref_id);
369  $admin_ids = $oParticipants->getAdmins();
370  $tutor_ids = $oParticipants->getTutors();
371 
372  $result = array_unique(array_merge($moderator_ids, $admin_ids, $tutor_ids));
373  }
374  return $result;
375  }
376 
377  public function update()
378  {
379  $this->db->manipulateF(
380  '
381  UPDATE frm_notification
382  SET admin_force_noti = %s,
383  user_toggle_noti = %s
384  WHERE user_id = %s
385  AND frm_id = %s',
386  array('integer','integer','integer','integer'),
387  array($this->getAdminForce(), $this->getUserToggle(), $this->getUserId(), $this->getForumId())
388  );
389  }
390 
391  public function deleteNotificationAllUsers()
392  {
393  $this->db->manipulateF(
394  '
395  DELETE FROM frm_notification
396  WHERE frm_id = %s
397  AND user_id_noti > %s',
398  array('integer', 'integer'),
399  array($this->getForumId(), 0)
400  );
401  }
402 
403  public function read()
404  {
405  $result = array();
406 
407  $query = $this->db->queryF(
408  '
409  SELECT * FROM frm_notification WHERE
410  frm_id = %s',
411  array('integer'),
412  array($this->getForumId())
413  );
414 
415  while ($row = $this->db->fetchAssoc($query)) {
416  $result[$row['user_id']] = $row;
417  }
418  return $result;
419  }
420 
425  public static function mergeThreadNotificiations($merge_source_thread_id, $merge_target_thread_id)
426  {
427  // check notifications etc..
428  global $DIC;
429  $ilDB = $DIC->database();
430 
431  $res = $ilDB->queryF(
432  'SELECT notification_id, user_id FROM frm_notification
433  WHERE frm_id = %s
434  AND thread_id = %s
435  ORDER BY user_id ASC',
436  array('integer', 'integer'),
437  array(0, $merge_source_thread_id)
438  );
439 
440  $res_2 = $ilDB->queryF(
441  'SELECT DISTINCT user_id FROM frm_notification
442  WHERE frm_id = %s
443  AND thread_id = %s
444  ORDER BY user_id ASC',
445  array('integer', 'integer'),
446  array(0, $merge_target_thread_id)
447  );
448 
449  $users_already_notified = array();
450  while ($users_row = $ilDB->fetchAssoc($res_2)) {
451  $users_already_notified[$users_row['user_id']] = $users_row['user_id'];
452  }
453 
454  while ($row = $ilDB->fetchAssoc($res)) {
455  if (isset($users_already_notified[$row['user_id']])) {
456  // delete source notification because already exists for target_id
457  $ilDB->manipulatef(
458  'DELETE FROM frm_notification WHERE notification_id = %s',
459  array('integer'),
460  array($row['notification_id'])
461  );
462  } else {
463  // update source notification
464  $ilDB->update(
465  'frm_notification',
466  array('thread_id' => array('integer', $merge_target_thread_id)),
467  array('thread_id' => array('integer', $merge_source_thread_id)
468  )
469  );
470  }
471  }
472  }
473 
477  public function existsNotification()
478  {
479  $res = $this->db->queryF(
480  '
481  SELECT * FROM frm_notification
482  WHERE user_id = %s
483  AND frm_id = %s
484  AND admin_force_noti = %s',
485  array('integer', 'integer', 'integer'),
486  array($this->getUserId(), $this->getForumId(), $this->getAdminForce())
487  );
488 
489  if ($row = $this->db->numRows($res) > 0) {
490  return true;
491  }
492  return false;
493  }
494 
498  public function cloneFromSource(int $sourceRefId)
499  {
500  $sourceNotificationSettings = new self($sourceRefId);
501  $records = $sourceNotificationSettings->read();
502 
503  foreach ($records as $usrId => $row) {
504  $this->setUserId($usrId);
505  $this->setAdminForce($row['admin_force_noti']);
506  $this->setUserToggle($row['user_toggle_noti']);
507  $this->setUserIdNoti($row['user_id_noti']);
508 
509  $this->insertAdminForce();
510  }
511  }
512 }
static _isParentNodeGrpCrs($a_ref_id)
$result
global $DIC
Definition: saml.php:7
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
Class ilForumNotification.
$records
Definition: simple_test.php:22
user()
Definition: user.php:4
static mergeThreadNotificiations($merge_source_thread_id, $merge_target_thread_id)
static _clearForcedForumNotifications($a_parameter)
foreach($_POST as $key=> $value) $res
static _isUserToggleNoti($a_obj_id)
static _isAdminForceNoti($a_obj_id)
$ilUser
Definition: imgupload.php:18
$query
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
static checkForumsExistsDelete($ref_id, $user_id=0)
$row
__construct($ref_id)
Constructor public.
global $ilDB
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
static _getModerators($a_ref_id)
get all users assigned to local role il_frm_moderator_<frm_ref_id> (static)
static checkForumsExistsInsert($ref_id, $user_id=0)
Class ilForumModerators.
$data
Definition: bench.php:6
setNotificationId($a_notification_id)