ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCalendarCategories.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 {
30  public const MODE_UNDEFINED = 0;
31  public const MODE_REPOSITORY = 2; // course/group full calendar view (allows to select other calendars)
32  public const MODE_REMOTE_ACCESS = 3;
34  public const MODE_PERSONAL_DESKTOP_ITEMS = 5;
35  public const MODE_MANAGE = 6;
36  public const MODE_CONSULTATION = 7;
37  public const MODE_PORTFOLIO_CONSULTATION = 8;
38  public const MODE_REMOTE_SELECTED = 9;
39  public const MODE_REPOSITORY_CONTAINER_ONLY = 10; // course/group content view (side block, focus on course/group appointments only)
40  public const MODE_SINGLE_CALENDAR = 11;
41 
42  protected static ?ilCalendarCategories $instance = null;
43  protected ilDBInterface $db;
44  protected ilLogger $logger;
46  protected ilObjUser $user;
47  protected ilLanguage $lng;
50  protected ilTree $tree;
51 
52  protected int $user_id = 0;
53  protected int $mode = self::MODE_UNDEFINED;
54 
55  protected array $categories = array();
56  protected array $categories_info = array();
57  protected array $subitem_categories = array();
58 
59  protected int $root_ref_id = 0;
60  protected int $root_obj_id = 0;
61 
62  protected int $ch_user_id = 0;
63  protected int $target_ref_id = 0;
64 
68  public function __construct(int $a_usr_id = 0)
69  {
70  global $DIC;
71 
72  $this->user = $DIC->user();
73  $this->db = $DIC->database();
74  $this->logger = $DIC->logger()->cal();
75  $this->lng = $DIC->language();
76  $this->rbacsystem = $DIC->rbac()->system();
77  $this->access = $DIC->access();
78  $this->user_id = $a_usr_id;
79  $this->tree = $DIC->repositoryTree();
80  if (!$this->user_id) {
81  $this->user_id = $this->user->getId();
82  }
83  $this->fav_rep = new ilFavouritesDBRepository();
84  }
85 
89  public static function _getInstance($a_usr_id = 0): ilCalendarCategories
90  {
91  if (self::$instance) {
92  return self::$instance;
93  }
94  return self::$instance = new ilCalendarCategories($a_usr_id);
95  }
96 
100  public static function lookupRemoteCalendars(): array
101  {
102  global $DIC;
103 
104  $db = $DIC->database();
105 
106  $query = 'SELECT cat_id FROM cal_categories ' .
107  'WHERE loc_type = ' . $db->quote(ilCalendarCategory::LTYPE_REMOTE);
108  $res = $db->query($query);
109  $remote = [];
110  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
111  $remote[] = new ilCalendarCategory((int) $row->cat_id);
112  }
113  return $remote;
114  }
115 
119  public static function _lookupCategoryIdByObjId(int $a_obj_id): int
120  {
121  global $DIC;
122 
123  $ilDB = $DIC->database();
124  $query = "SELECT cat_id FROM cal_categories " .
125  "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
126  "AND type = " . $ilDB->quote(ilCalendarCategory::TYPE_OBJ, 'integer') . " ";
127 
128  $res = $ilDB->query($query);
129  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
130  return (int) $row->cat_id;
131  }
132  return 0;
133  }
134 
138  public static function _isOwner(int $a_usr_id, int $a_cal_id): bool
139  {
140  global $DIC;
141 
142  $ilDB = $DIC['ilDB'];
143 
144  $query = "SELECT * FROM cal_categories " .
145  "WHERE cat_id = " . $ilDB->quote($a_cal_id, 'integer') . " " .
146  "AND obj_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
147  "AND type = " . $ilDB->quote(ilCalendarCategory::TYPE_USR, 'integer') . " ";
148  $res = $ilDB->query($query);
149  return (bool) $res->numRows();
150  }
151 
155  public static function deletePDItemsCache(int $a_usr_id): void
156  {
157  ilCalendarCache::getInstance()->deleteByAdditionalKeys(
158  $a_usr_id,
159  self::MODE_PERSONAL_DESKTOP_ITEMS,
160  'categories'
161  );
162  }
163 
167  public static function deleteRepositoryCache(int $a_usr_id): void
168  {
169  ilCalendarCache::getInstance()->deleteByAdditionalKeys(
170  $a_usr_id,
171  self::MODE_REPOSITORY,
172  'categories'
173  );
174  }
175 
179  final protected function sleep(): string
180  {
181  return serialize(
182  array(
183  'categories' => $this->categories,
184  'categories_info' => $this->categories_info,
185  'subitem_categories' => $this->subitem_categories
186  )
187  );
188  }
189 
193  protected function wakeup(string $a_ser): void
194  {
195  $info = unserialize($a_ser);
196 
197  $this->categories = $info['categories'];
198  $this->categories_info = $info['categories_info'];
199  $this->subitem_categories = $info['subitem_categories'];
200  }
201 
205  public function setCHUserId(int $a_user_id): void
206  {
207  $this->ch_user_id = $a_user_id;
208  }
209 
210  public function getCHUserId(): int
211  {
212  return $this->ch_user_id;
213  }
214 
215  protected function setMode(int $a_mode): void
216  {
217  $this->mode = $a_mode;
218  }
219 
220  public function getMode(): int
221  {
222  return $this->mode;
223  }
224 
225  protected function setTargetRefId(int $a_ref_id): void
226  {
227  $this->target_ref_id = $a_ref_id;
228  }
229 
230  public function getTargetRefId(): int
231  {
232  return $this->target_ref_id;
233  }
234 
235  public function setSourceRefId(int $a_val): void
236  {
237  $this->root_ref_id = $a_val;
238  }
239 
242  public function getsourcerefid(): int
243  {
244  return $this->root_ref_id;
245  }
246 
250  public function initialize(
251  int $a_mode,
252  int $a_source_ref_id = 0,
253  bool $a_use_cache = false,
254  int $a_cat_id = 0
255  ): void {
256  if ($this->getMode() != 0) {
257  throw new ilCalCategoriesInitializedMultipleException("ilCalendarCategories is initialized multiple times for user " . $this->user_id . ".");
258  }
259 
260  $this->setMode($a_mode);
261 
262  // see comments in https://mantis.ilias.de/view.php?id=25254
263  if ($a_use_cache && $this->getMode() != self::MODE_REPOSITORY_CONTAINER_ONLY) {
264  // Read categories from cache
265  if ($cats = ilCalendarCache::getInstance()->getEntry($this->user_id . ':' . $a_mode . ':categories:' . $a_source_ref_id)) {
266  if ($this->getMode() != self::MODE_REPOSITORY &&
267  $this->getMode() != self::MODE_CONSULTATION &&
268  $this->getMode() != self::MODE_PORTFOLIO_CONSULTATION) {
269  $this->wakeup($cats);
270  return;
271  }
272  }
273  }
274 
275  switch ($this->getMode()) {
276  case self::MODE_REMOTE_ACCESS:
278  $this->readPDCalendars();
279  } else {
280  $this->readSelectedItemCalendars();
281  }
282  break;
283 
284  case self::MODE_REMOTE_SELECTED:
285  $this->readSelectedCalendar($a_source_ref_id);
286  break;
287 
288  case self::MODE_PERSONAL_DESKTOP_MEMBERSHIP:
289  $this->readPDCalendars();
290  break;
291 
292  case self::MODE_PERSONAL_DESKTOP_ITEMS:
293  $this->readSelectedItemCalendars();
294  break;
295 
296  case self::MODE_REPOSITORY:
297  $this->root_ref_id = $a_source_ref_id;
298  $this->root_obj_id = ilObject::_lookupObjId($this->root_ref_id);
299  $this->readReposCalendars();
300  break;
301 
302  case self::MODE_REPOSITORY_CONTAINER_ONLY:
303  $this->root_ref_id = $a_source_ref_id;
304  $this->root_obj_id = ilObject::_lookupObjId($this->root_ref_id);
305  $this->readReposCalendars(true);
306  break;
307 
308  case self::MODE_MANAGE:
309  $this->readPDCalendars();
310  $this->readSelectedItemCalendars();
311  break;
312 
313  case self::MODE_CONSULTATION:
314  #$this->readPrivateCalendars();
315  $this->setTargetRefId($a_source_ref_id);
316  $this->readConsultationHoursCalendar($a_source_ref_id);
317  break;
318 
319  case self::MODE_PORTFOLIO_CONSULTATION:
321  break;
322 
323  case self::MODE_SINGLE_CALENDAR:
324  $this->readSingleCalendar($a_cat_id);
325  break;
326  }
327 
328  if ($a_use_cache) {
329  // Store in cache
330  ilCalendarCache::getInstance()->storeEntry(
331  $this->user_id . ':' . $a_mode . ':categories:' . $a_source_ref_id,
332  $this->sleep(),
333  $this->user_id,
334  $a_mode,
335  'categories'
336  );
337  }
338  }
339 
340  public function getCategoryInfo(int $a_cat_id): array
341  {
342  if (isset($this->categories_info[$a_cat_id])) {
343  return $this->categories_info[$a_cat_id];
344  }
345 
346  if (in_array($a_cat_id, $this->subitem_categories)) {
347  foreach ($this->categories as $cat_id) {
348  if (in_array($a_cat_id, $this->categories_info[$cat_id]['subitem_ids'])) {
349  return $this->categories_info[$cat_id];
350  }
351  }
352  }
353  return [];
354  }
355 
356  public function getCategoriesInfo(): array
357  {
358  return $this->categories_info ?: array();
359  }
360 
361  public function getCategories(bool $a_include_subitem_calendars = false): array
362  {
363  if ($a_include_subitem_calendars) {
364  return array_merge($this->categories, $this->subitem_categories);
365  }
366 
367  return $this->categories ?: array();
368  }
369 
370  public function getSubitemCategories(int $a_cat_id): array
371  {
372  if (!isset($this->categories_info[$a_cat_id]['subitem_ids'])) {
373  return array($a_cat_id);
374  }
375  return array_merge((array) $this->categories_info[$a_cat_id]['subitem_ids'], array($a_cat_id));
376  }
377 
378  public function prepareCategoriesOfUserForSelection(): array
379  {
380  $has_personal_calendar = false;
381  $cats = [];
382  foreach ($this->categories_info as $info) {
383  if ($info['obj_type'] == 'sess' || $info['obj_type'] == 'exc') {
384  continue;
385  }
386 
387  if (($info['remote'] ?? 0) == ilCalendarCategory::LTYPE_REMOTE) {
388  continue;
389  }
390 
391  if ($info['type'] == ilCalendarCategory::TYPE_USR and $info['editable']) {
392  $has_personal_calendar = true;
393  }
394 
395  if ($info['editable']) {
396  $cats[$info['cat_id']] = $info['title'];
397  }
398  }
399  // If there
400  if (!$has_personal_calendar) {
401  $cats[0] = $this->lng->txt('cal_default_calendar');
402  }
403  return $cats;
404  }
405 
410  public function getNotificationCalendars(): array
411  {
412  $not = array();
413  foreach ($this->categories_info as $info) {
414  if ($info['type'] == ilCalendarCategory::TYPE_OBJ and $info['editable']) {
415  if (ilObject::_lookupType($info['obj_id']) == 'crs' or ilObject::_lookupType($info['obj_id']) == 'grp') {
416  $not[] = (int) $info['cat_id'];
417  }
418  }
419  }
420  return $not;
421  }
422 
426  public function isEditable(int $a_cat_id): bool
427  {
428  return isset($this->categories_info[$a_cat_id]['editable']) && $this->categories_info[$a_cat_id]['editable'];
429  }
430 
434  public function isVisible($a_cat_id): bool
435  {
436  return in_array($a_cat_id, $this->categories) || in_array($a_cat_id, $this->subitem_categories);
437  }
438 
442  protected function readPDCalendars(): void
443  {
444  $this->readPublicCalendars();
445  $this->readPrivateCalendars();
447  $this->readBookingCalendar();
448 
449  $this->readSelectedCategories(ilParticipants::_getMembershipByType($this->user_id, ['crs']));
450  $this->readSelectedCategories(ilParticipants::_getMembershipByType($this->user_id, ['grp']));
451  $this->readSelectedCategories(
453  0,
454  false
455  );
456 
457  $this->addSubitemCalendars();
458  }
459 
463  protected function readSelectedCalendar(int $a_cal_id): void
464  {
465  $cat = new ilCalendarCategory($a_cal_id);
466  if ($cat->getType() == ilCalendarCategory::TYPE_OBJ) {
467  $this->readSelectedCategories(array($cat->getObjId()));
468  $this->addSubitemCalendars();
469  } else {
470  $this->categories[] = $a_cal_id;
471  }
472  }
473 
474  protected function readSelectedItemCalendars(): void
475  {
476  $this->readPublicCalendars();
477  $this->readPrivateCalendars();
479  $this->readBookingCalendar();
480 
481  $obj_ids = array();
482 
483  $courses = array();
484  $groups = array();
485  $sessions = array();
486  $exercises = array();
487  foreach ($this->fav_rep->getFavouritesOfUser(
488  $this->user->getId(),
489  array('crs', 'grp', 'sess', 'exc')
490  ) as $item) {
491  if ($this->access->checkAccess('read', '', $item['ref_id'])) {
492  switch ($item['type']) {
493  case 'crs':
494  $courses[] = $item['obj_id'];
495  break;
496 
497  case 'sess':
498  $sessions[] = $item['obj_id'];
499  break;
500 
501  case 'grp':
502  $groups[] = $item['obj_id'];
503  break;
504 
505  case 'exc':
506  $exercises[] = $item['obj_id'];
507  break;
508  }
509  }
510  }
511  $this->readSelectedCategories($courses);
512  $this->readSelectedCategories($sessions);
513  $this->readSelectedCategories($groups);
514  $this->readSelectedCategories($exercises);
515  $this->readSelectedCategories(
517  0,
518  false
519  );
520 
521  $this->addSubitemCalendars();
522  }
523 
527  protected function readReposCalendars($a_container_only = false): void
528  {
529  if (!$a_container_only) {
530  $this->readPublicCalendars();
531  $this->readPrivateCalendars();
532  //$this->readConsultationHoursCalendar($this->root_ref_id);
533  $this->readAllConsultationHoursCalendarOfContainer($this->root_ref_id);
534  }
535 
536  #$query = "SELECT ref_id,obd.obj_id obj_id FROM tree t1 ".
537  # "JOIN object_reference obr ON t1.child = obr.ref_id ".
538  # "JOIN object_data obd ON obd.obj_id = obr.obj_id ".
539  # "WHERE t1.lft >= (SELECT lft FROM tree WHERE child = ".$this->db->quote($this->root_ref_id,'integer')." ) ".
540  # "AND t1.lft <= (SELECT rgt FROM tree WHERE child = ".$this->db->quote($this->root_ref_id,'integer')." ) ".
541  # "AND ".$ilDB->in('type',array('crs','grp','sess'),false,'text')." ".
542  # "AND tree = 1";
543 
544  // alternative 1: do not aggregate items of current course
545  if (false) { //
546  $subtree_query = $GLOBALS['DIC']['tree']->getSubTreeQuery(
547  $this->root_ref_id,
548  array('object_reference.ref_id', 'object_data.obj_id'),
549  array('crs', 'grp', 'sess', 'exc')
550  );
551 
552  $res = $this->db->query($subtree_query);
553  $obj_ids = array();
554  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
555  if ($this->tree->isDeleted((int) $row->ref_id)) {
556  continue;
557  }
558 
559  $obj_type = ilObject::_lookupType((int) $row->obj_id);
560  if ($obj_type == 'crs' or $obj_type == 'grp') {
561  //Added for calendar revision --> https://goo.gl/CXGTRF
562  //In 5.2-trunk, the booking pools did not appear in the marginal calendar.
563  $this->readBookingCalendar();
564  // Check for global/local activation
565  if (!ilCalendarSettings::_getInstance()->lookupCalendarActivated((int) $row->obj_id)) {
566  continue;
567  }
568  }
569  if ($this->access->checkAccess('read', '', (int) $row->ref_id)) {
570  $obj_ids[] = (int) $row->obj_id;
571  }
572  }
573  $this->readSelectedCategories($obj_ids, $this->root_ref_id);
574  } else { // alternative 2: aggregate items of current course (discussion with timon 3.8.3017: this is the current preference)
575  $this->readSelectedCategories(array($this->root_obj_id), $this->root_ref_id);
576  }
577 
578  if (!$a_container_only) {
579  $this->readSelectedCategories(ilParticipants::_getMembershipByType($this->user_id, ['crs']));
580  $this->readSelectedCategories(ilParticipants::_getMembershipByType($this->user_id, ['grp']));
581  $this->readSelectedCategories(
583  0,
584  false
585  );
586  }
587 
588  $this->addSubitemCalendars();
589  }
590 
591  public function readSingleCalendar(int $a_cat_id): void
592  {
593  $cat = new ilCalendarCategory($a_cat_id);
594  switch ($cat->getType()) {
596  $this->readSelectedCalendar($a_cat_id);
597  break;
598 
600  $this->readPublicCalendars(array($a_cat_id));
601  break;
602 
604  $this->readPrivateCalendars(array($a_cat_id));
605  break;
606 
608  $this->readConsultationHoursCalendar($this->root_ref_id, $a_cat_id);
609  break;
610 
612  $this->readBookingCalendar();
613  break;
614  }
615  }
616 
620  protected function readPublicCalendars($cat_ids = null): void
621  {
622  $in = "";
623  if (is_array($cat_ids)) {
624  $in = " AND " . $this->db->in('cat_id', $cat_ids, false, 'integer') . " ";
625  }
626 
627  // global categories
628  $query = "SELECT * FROM cal_categories " .
629  "WHERE type = " . $this->db->quote(ilCalendarCategory::TYPE_GLOBAL, 'integer') . " " . $in .
630  "ORDER BY title ";
631 
632  $res = $this->db->query($query);
633  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
634  $this->categories[] = (int) $row->cat_id;
635  $this->categories_info[(int) $row->cat_id]['obj_type'] = '';
636  $this->categories_info[(int) $row->cat_id]['source_ref_id'] = 0;
637  $this->categories_info[(int) $row->cat_id]['obj_id'] = (int) $row->obj_id;
638  $this->categories_info[(int) $row->cat_id]['cat_id'] = (int) $row->cat_id;
639  $this->categories_info[(int) $row->cat_id]['title'] = $row->title;
640  $this->categories_info[(int) $row->cat_id]['color'] = $row->color;
641  $this->categories_info[(int) $row->cat_id]['type'] = (int) $row->type;
642  $this->categories_info[(int) $row->cat_id]['editable'] = $this->rbacsystem->checkAccess(
643  'edit_event',
644  ilCalendarSettings::_getInstance()->getCalendarSettingsId()
645  );
646  $this->categories_info[(int) $row->cat_id]['settings'] = $this->rbacsystem->checkAccess(
647  'write',
648  ilCalendarSettings::_getInstance()->getCalendarSettingsId()
649  );
650  $this->categories_info[(int) $row->cat_id]['accepted'] = false;
651  $this->categories_info[(int) $row->cat_id]['remote'] = (int) $row->loc_type == ilCalendarCategory::LTYPE_REMOTE;
652  }
653  }
654 
658  protected function readPrivateCalendars(?array $only_cat_ids = null): void
659  {
660  $cat_ids = [];
661  $in = "";
662  if (is_array($only_cat_ids)) {
663  $in = " AND " . $this->db->in('cat_id', $only_cat_ids, false, 'integer') . " ";
664  }
665 
666  // First read private calendars of user
667  $query = "SELECT cat_id FROM cal_categories " .
668  "WHERE type = " . $this->db->quote(ilCalendarCategory::TYPE_USR, 'integer') . " " .
669  "AND obj_id = " . $this->db->quote($this->user->getId(), 'integer') . " " . $in;
670  $res = $this->db->query($query);
671  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
672  $cat_ids[] = (int) $row->cat_id;
673  }
674 
675  // Read shared calendars
676  $accepted_ids = ilCalendarSharedStatus::getAcceptedCalendars($this->user->getId());
677  if (!$cat_ids = array_merge($cat_ids, $accepted_ids)) {
678  return;
679  }
680  if (is_array($only_cat_ids)) {
681  $cat_ids = array_filter($cat_ids, function ($id) use ($only_cat_ids) {
682  return in_array($id, $only_cat_ids);
683  });
684  }
685  // user categories
686  $query = "SELECT * FROM cal_categories " .
687  "WHERE type = " . $this->db->quote(ilCalendarCategory::TYPE_USR, 'integer') . " " .
688  "AND " . $this->db->in('cat_id', $cat_ids, false, 'integer') . " " .
689  "ORDER BY title ";
690 
691  $res = $this->db->query($query);
692  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
693  $this->categories[] = (int) $row->cat_id;
694  $this->categories_info[(int) $row->cat_id]['obj_type'] = '';
695  $this->categories_info[(int) $row->cat_id]['source_ref_id'] = 0;
696  $this->categories_info[(int) $row->cat_id]['obj_id'] = (int) $row->obj_id;
697  $this->categories_info[(int) $row->cat_id]['cat_id'] = (int) $row->cat_id;
698  $this->categories_info[(int) $row->cat_id]['title'] = $row->title;
699  $this->categories_info[(int) $row->cat_id]['color'] = $row->color;
700  $this->categories_info[(int) $row->cat_id]['type'] = (int) $row->type;
701 
702  if (in_array((int) $row->cat_id, $accepted_ids)) {
703  $shared = new ilCalendarShared((int) $row->cat_id);
704  if ($shared->isEditableForUser($this->user->getId())) {
705  $this->categories_info[(int) $row->cat_id]['editable'] = true;
706  } else {
707  $this->categories_info[(int) $row->cat_id]['editable'] = false;
708  }
709  } else {
710  $this->categories_info[(int) $row->cat_id]['editable'] = true;
711  }
712  if ($this->user->getId() == (int) $row->obj_id) {
713  $this->categories_info[(int) $row->cat_id]['settings'] = true;
714  } else {
715  $this->categories_info[(int) $row->cat_id]['settings'] = false;
716  }
717 
718  $this->categories_info[(int) $row->cat_id]['accepted'] = in_array((int) $row->cat_id, $accepted_ids);
719  $this->categories_info[(int) $row->cat_id]['remote'] = ((int) $row->loc_type == ilCalendarCategory::LTYPE_REMOTE);
720  }
721  }
722 
726  public function readAllConsultationHoursCalendarOfContainer(int $a_container_ref_id): void
727  {
728  $obj_id = ilObject::_lookupObjId($a_container_ref_id);
729  $participants = ilCourseParticipants::_getInstanceByObjId($obj_id);
730  $users = array_unique(array_merge($participants->getTutors(), $participants->getAdmins()));
731  $users = ilBookingEntry::lookupBookableUsersForObject([$obj_id], $users);
732  $old_ch = $this->getCHUserId();
733  foreach ($users as $user) {
734  $this->setCHUserId($user);
735  $this->readConsultationHoursCalendar($a_container_ref_id);
736  }
737  $this->setCHUserId($old_ch);
738  }
739 
743  public function readConsultationHoursCalendar(?int $a_target_ref_id = null, int $a_cat_id = 0): void
744  {
745  if (!$this->getCHUserId()) {
746  $this->setCHUserId($this->user_id);
747  }
748 
749  if ($a_target_ref_id) {
750  $target_obj_id = ilObject::_lookupObjId($a_target_ref_id);
751 
752  $query = 'SELECT DISTINCT(cc.cat_id) FROM booking_entry be ' .
753  'LEFT JOIN booking_obj_assignment bo ON be.booking_id = bo.booking_id ' .
754  'JOIN cal_entries ce ON be.booking_id = ce.context_id ' .
755  'JOIN cal_cat_assignments ca ON ce.cal_id = ca.cal_id ' .
756  'JOIN cal_categories cc ON ca.cat_id = cc.cat_id ' .
757  'WHERE ((bo.target_obj_id IS NULL) OR bo.target_obj_id = ' . $this->db->quote(
758  $target_obj_id,
759  'integer'
760  ) . ' ) ';
761 
762  // limit only to user if no cat id is given
763  if ($a_cat_id == 0) {
764  $query .= 'AND cc.obj_id = ' . $this->db->quote($this->getCHUserId(), 'integer');
765  }
766 
767  $res = $this->db->query($query);
768  $categories = array();
769  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
770  if ($a_cat_id == 0 || (int) $row->cat_id == $a_cat_id) {
771  $categories[] = (int) $row->cat_id;
772  }
773  }
774 
775  if ($categories) {
776  $query = 'SELECT * FROM cal_categories ' .
777  'WHERE ' . $this->db->in('cat_id', $categories, false, 'integer');
778  $res = $this->db->query($query);
779  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
780  $this->categories[] = (int) $row->cat_id;
781  $this->categories_info[(int) $row->cat_id]['obj_type'] = '';
782  $this->categories_info[(int) $row->cat_id]['source_ref_id'] = 0;
783  $this->categories_info[(int) $row->cat_id]['obj_id'] = (int) $row->obj_id;
784  $this->categories_info[(int) $row->cat_id]['cat_id'] = (int) $row->cat_id;
785  $this->categories_info[(int) $row->cat_id]['title'] = ilObjUser::_lookupFullname((int) $row->obj_id);
786  $this->categories_info[(int) $row->cat_id]['color'] = $row->color;
787  $this->categories_info[(int) $row->cat_id]['type'] = (int) $row->type;
788  $this->categories_info[(int) $row->cat_id]['editable'] = false;
789  $this->categories_info[(int) $row->cat_id]['settings'] = false;
790  $this->categories_info[(int) $row->cat_id]['accepted'] = false;
791  $this->categories_info[(int) $row->cat_id]['remote'] = false;
792  }
793  }
794  } else { // no category given
795  $filter = ($a_cat_id > 0)
796  ? " AND cat_id = " . $this->db->quote($a_cat_id, "integer")
797  : " AND obj_id = " . $this->db->quote($this->getCHUserId(), 'integer');
798 
799  $query = "SELECT * FROM cal_categories cc " .
800  "WHERE type = " . $this->db->quote(ilCalendarCategory::TYPE_CH, 'integer') . ' ' . $filter;
801  $res = $this->db->query($query);
802  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
803  $this->categories[] = (int) $row->cat_id;
804  $this->categories_info[(int) $row->cat_id]['obj_type'] = '';
805  $this->categories_info[(int) $row->cat_id]['source_ref_id'] = 0;
806  $this->categories_info[(int) $row->cat_id]['obj_id'] = (int) $row->obj_id;
807  $this->categories_info[(int) $row->cat_id]['cat_id'] = (int) $row->cat_id;
808  $this->categories_info[(int) $row->cat_id]['title'] = $row->title;
809  $this->categories_info[(int) $row->cat_id]['color'] = $row->color;
810  $this->categories_info[(int) $row->cat_id]['type'] = (int) $row->type;
811  $this->categories_info[(int) $row->cat_id]['editable'] = false;
812  $this->categories_info[(int) $row->cat_id]['settings'] = false;
813  $this->categories_info[(int) $row->cat_id]['accepted'] = false;
814  $this->categories_info[(int) $row->cat_id]['remote'] = false;
815  }
816  }
817  }
818 
822  public function readBookingCalendar(?int $user_id = null): void
823  {
824  if (!$user_id) {
825  $user_id = $this->user_id;
826  }
827 
828  $query = "SELECT * FROM cal_categories " .
829  "WHERE type = " . $this->db->quote(ilCalendarCategory::TYPE_BOOK, 'integer') . ' ' .
830  "AND obj_id = " . $this->db->quote($user_id, 'integer');
831  $res = $this->db->query($query);
832  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
833  $this->categories[] = (int) $row->cat_id;
834  $this->categories_info[(int) $row->cat_id]['source_ref_id'] = 0;
835  $this->categories_info[(int) $row->cat_id]['obj_type'] = '';
836  $this->categories_info[(int) $row->cat_id]['obj_id'] = (int) $row->obj_id;
837  $this->categories_info[(int) $row->cat_id]['cat_id'] = (int) $row->cat_id;
838  $this->categories_info[(int) $row->cat_id]['title'] = $row->title;
839  $this->categories_info[(int) $row->cat_id]['color'] = $row->color;
840  $this->categories_info[(int) $row->cat_id]['type'] = (int) $row->type;
841  $this->categories_info[(int) $row->cat_id]['editable'] = false;
842  $this->categories_info[(int) $row->cat_id]['settings'] = false;
843  $this->categories_info[(int) $row->cat_id]['accepted'] = false;
844  $this->categories_info[(int) $row->cat_id]['remote'] = false;
845  }
846  }
847 
848  protected function readSelectedCategories(array $a_obj_ids, int $a_source_ref_id = 0, bool $check_permissions = true): void
849  {
850  if (!count($a_obj_ids)) {
851  return;
852  }
853 
854  $query = "SELECT * FROM cal_categories " .
855  "WHERE type = " . $this->db->quote(ilCalendarCategory::TYPE_OBJ, 'integer') . " " .
856  "AND " . $this->db->in('obj_id', $a_obj_ids, false, 'integer') . " " .
857  "ORDER BY title ";
858 
859  $res = $this->db->query($query);
860  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
861  // check activation/deactivation
862  $obj_type = ilObject::_lookupType((int) $row->obj_id);
863  if ($obj_type == 'crs' or $obj_type == 'grp') {
864  if (!ilCalendarSettings::_getInstance()->lookupCalendarActivated((int) $row->obj_id)) {
865  continue;
866  }
867  }
868 
869  $editable = false;
870  $exists = false;
871  $settings = false;
872  foreach (ilObject::_getAllReferences((int) $row->obj_id) as $ref_id) {
873  if ($this->access->checkAccess('edit_event', '', $ref_id)) {
874  $settings = true;
875  }
876  if ($this->access->checkAccess('edit_event', '', $ref_id)) {
877  $exists = true;
878  $editable = true;
879  break;
880  } elseif ($this->access->checkAccess('read', '', $ref_id)) {
881  $exists = true;
882  }
883  }
884  if (!$exists && $check_permissions) {
885  continue;
886  }
887  $this->categories_info[(int) $row->cat_id]['editable'] = $editable;
888  $this->categories_info[(int) $row->cat_id]['accepted'] = false;
889  $this->categories_info[(int) $row->cat_id]['settings'] = $settings;
890 
891  $this->categories[] = (int) $row->cat_id;
892  $this->categories_info[(int) $row->cat_id]['obj_type'] = ilObject::_lookupType((int) $row->obj_id);
893  $this->categories_info[(int) $row->cat_id]['obj_id'] = (int) $row->obj_id;
894  $this->categories_info[(int) $row->cat_id]['cat_id'] = (int) $row->cat_id;
895  $this->categories_info[(int) $row->cat_id]['color'] = $row->color;
896  #$this->categories_info[$row->cat_id]['title'] = ilObject::_lookupTitle($row->obj_id);
897  $this->categories_info[(int) $row->cat_id]['title'] = $row->title;
898  $this->categories_info[(int) $row->cat_id]['type'] = (int) $row->type;
899  $this->categories_info[(int) $row->cat_id]['remote'] = false;
900  $this->categories_info[(int) $row->cat_id]['source_ref_id'] = $a_source_ref_id;
901  }
902  }
903 
908  protected function addSubitemCalendars(): void
909  {
910  $course_ids = array();
911  foreach ($this->categories as $cat_id) {
912  if (isset($this->categories_info[$cat_id]['obj_type']) &&
913  in_array($this->categories_info[$cat_id]['obj_type'], ['crs', 'grp', 'tals'])) {
914  $course_ids[] = $this->categories_info[$cat_id]['obj_id'];
915  }
916  }
917 
918  $query = "SELECT od2.obj_id sess_id, od1.obj_id crs_id,cat_id, or2.ref_id sess_ref_id, od2.type FROM object_data od1 " .
919  "JOIN object_reference or1 ON od1.obj_id = or1.obj_id " .
920  "JOIN tree t ON or1.ref_id = t.parent " .
921  "JOIN object_reference or2 ON t.child = or2.ref_id " .
922  "JOIN object_data od2 ON or2.obj_id = od2.obj_id " .
923  "JOIN cal_categories cc ON od2.obj_id = cc.obj_id " .
924  "WHERE " . $this->db->in('od2.type', array('sess', 'exc', 'etal'), false, 'text') .
925  "AND (od1.type = 'crs' OR od1.type = 'grp' OR od1.type = 'tals') " .
926  "AND " . $this->db->in('od1.obj_id', $course_ids, false, 'integer') . ' ' .
927  "AND or2.deleted IS NULL";
928 
929  $res = $this->db->query($query);
930  $cat_ids = array();
931  $course_sessions = array();
932  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
933  if ($row->type !== 'etal') {
934  if (
935  !$this->access->checkAccessOfUser($this->user_id, 'read', '', (int) $row->sess_ref_id) ||
936  !$this->access->checkAccessOfUser($this->user_id, 'visible', '', (int) $row->sess_ref_id)
937  ) {
938  continue;
939  }
940  }
941  $cat_ids[] = (int) $row->cat_id;
942  $course_sessions[(int) $row->crs_id][(int) $row->sess_id] = (int) $row->cat_id;
943  $this->subitem_categories[] = (int) $row->cat_id;
944  }
945 
946  foreach ($this->categories as $cat_id) {
947  if (
948  (isset($this->categories_info[$cat_id]['obj_type']) &&
949  in_array($this->categories_info[$cat_id]['obj_type'], ['crs', 'grp', 'tals'])) &&
950  isset($this->categories_info[$cat_id]['obj_id']) &&
951  isset($course_sessions[$this->categories_info[$cat_id]['obj_id']]) &&
952  is_array($course_sessions[$this->categories_info[$cat_id]['obj_id']])) {
953  foreach ($course_sessions[$this->categories_info[$cat_id]['obj_id']] as $sess_id => $sess_cat_id) {
954  $this->categories_info[$cat_id]['subitem_ids'][$sess_id] = $sess_cat_id;
955  $this->categories_info[$cat_id]['subitem_obj_ids'][$sess_cat_id] = $sess_id;
956  }
957  } else {
958  $this->categories_info[$cat_id]['subitem_ids'] = array();
959  $this->categories_info[$cat_id]['subitem_obj_ids'] = array();
960  }
961  }
962  }
963 
967  protected function lookupRelevantTalkSeriesIds(): array
968  {
969  $repository = new IliasDBEmployeeTalkSeriesRepository($this->user, $this->db);
970  $talks = $repository->findByOwnerAndEmployee();
971  return array_map(function (ilObjEmployeeTalkSeries $item) {
972  return $item->getId();
973  }, $talks);
974  }
975 
980  public static function lookupPrivateCategories(int $a_user_id): array
981  {
982  global $DIC;
983 
984  $ilDB = $DIC['ilDB'];
985 
986  // First read private calendars of user
987  $set = $ilDB->query("SELECT * FROM cal_categories " .
988  "WHERE type = " . $ilDB->quote(ilCalendarCategory::TYPE_USR, 'integer') . " " .
989  "AND obj_id = " . $ilDB->quote($a_user_id, 'integer'));
990  $cats = array();
991 
992  while ($rec = $ilDB->fetchAssoc($set)) {
993  $rec['cat_id'] = (int) $rec['cat_id'];
994  $rec['obj_id'] = (int) $rec['obj_id'];
995  $rec['type'] = (int) $rec['type'];
996  $rec['loc_type'] = (int) $rec['loc_type'];
997  $cats[] = $rec;
998  }
999  return $cats;
1000  }
1001 }
class for calendar categories
static lookupPrivateCategories(int $a_user_id)
Lookup private categories of user.
initialize(int $a_mode, int $a_source_ref_id=0, bool $a_use_cache=false, int $a_cat_id=0)
initialize visible categories
isEditable(int $a_cat_id)
check if category is editable
$res
Definition: ltiservices.php:66
addSubitemCalendars()
Add subitem calendars E.g.
setCHUserId(int $a_user_id)
Set ch user id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(int $a_usr_id=0)
Singleton instance.
static _lookupFullname(int $a_user_id)
static getAcceptedCalendars(int $a_usr_id)
static _getAllReferences(int $id)
get all reference ids for object ID
if(!file_exists('../ilias.ini.php'))
readReposCalendars($a_container_only=false)
Read available repository calendars.
ilFavouritesDBRepository $fav_rep
readBookingCalendar(?int $user_id=null)
Read booking manager calendar.
sleep()
Serialize categories.
isVisible($a_cat_id)
check if category is visible
quote($value, string $type)
Stores calendar categories.
Handles shared calendars.
static _lookupObjId(int $ref_id)
static _lookupCategoryIdByObjId(int $a_obj_id)
lookup category by obj_id
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getCategories(bool $a_include_subitem_calendars=false)
$ref_id
Definition: ltiauth.php:65
static _isOwner(int $a_usr_id, int $a_cal_id)
check if user is owner of a category
static _lookupTitle(int $obj_id)
static _getInstanceByObjId(int $a_obj_id)
$GLOBALS["DIC"]
Definition: wac.php:53
readSelectedCategories(array $a_obj_ids, int $a_source_ref_id=0, bool $check_permissions=true)
global $DIC
Definition: shib_login.php:22
readAllConsultationHoursCalendarOfContainer(int $a_container_ref_id)
Read personal consultation hours calendar of all tutors for a container.
static deleteRepositoryCache(int $a_usr_id)
Delete cache.
query(string $query)
Run a (read-only) Query on the database.
static _getInstance($a_usr_id=0)
get singleton instance
readPrivateCalendars(?array $only_cat_ids=null)
Read private calendars.
wakeup(string $a_ser)
Load from serialize string.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static ilCalendarCategories $instance
static lookupBookableUsersForObject(array $a_obj_id, array $a_user_ids)
Consultation hours are offered if 1) consultation hour owner is admin or tutor and no object assignme...
getNotificationCalendars()
Get all calendars that allow send of notifications (Editable and course group calendars) ...
static _getMembershipByType(int $a_usr_id, array $a_type, bool $a_only_member_role=false)
get membership by type Get course or group membership
static _lookupType(int $id, bool $reference=false)
readPublicCalendars($cat_ids=null)
Read public calendars.
readSelectedCalendar(int $a_cal_id)
Read info about selected calendar.
readConsultationHoursCalendar(?int $a_target_ref_id=null, int $a_cat_id=0)
Read personal consultation hours calendar.
static deletePDItemsCache(int $a_usr_id)
Delete cache (add remove desktop item)
readPDCalendars()
Read categories of user.