ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCalendarCategories.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once('./Services/Calendar/classes/class.ilCalendarCategory.php');
25 include_once('./Services/Calendar/classes/class.ilCalendarSettings.php');
26 include_once './Services/Calendar/classes/class.ilCalendarCache.php';
27 
38 {
39  const MODE_REPOSITORY = 2;
40  const MODE_REMOTE_ACCESS = 3;
43  const MODE_MANAGE = 6;
44  const MODE_CONSULTATION = 7;
45 
46  protected static $instance = null;
47 
48  protected $db;
49 
50  protected $user_id;
51 
52  protected $categories = array();
53  protected $categories_info = array();
54  protected $subitem_categories = array();
55 
56  protected $root_ref_id = 0;
57  protected $root_obj_id = 0;
58 
59 
67  protected function __construct($a_usr_id = 0)
68  {
69  global $ilUser,$ilDB;
70 
71  $this->user_id = $a_usr_id;
72  if(!$this->user_id)
73  {
74  $this->user_id = $ilUser->getId();
75  }
76  $this->db = $ilDB;
77  }
78 
87  public static function _getInstance($a_usr_id = 0)
88  {
89  if(self::$instance)
90  {
91  return self::$instance;
92  }
93  return self::$instance = new ilCalendarCategories($a_usr_id);
94  }
95 
104  public static function _lookupCategoryIdByObjId($a_obj_id)
105  {
106  global $ilDB;
107 
108  $query = "SELECT cat_id FROM cal_categories ".
109  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
110  "AND type = ".$ilDB->quote(ilCalendarCategory::TYPE_OBJ,'integer')." ";
111 
112  $res = $ilDB->query($query);
113  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
114  {
115  return $row->cat_id;
116  }
117  return 0;
118  }
119 
120 
130  public static function _isOwner($a_usr_id,$a_cal_id)
131  {
132  global $ilDB;
133 
134  $query = "SELECT * FROM cal_categories ".
135  "WHERE cat_id = ".$ilDB->quote($a_cal_id ,'integer')." ".
136  "AND obj_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
137  "AND type = ".$ilDB->quote(ilCalendarCategory::TYPE_USR ,'integer')." ";
138  $res = $ilDB->query($query);
139  return $res->numRows() ? true : false;
140  }
141 
147  public static function deletePDItemsCache($a_usr_id)
148  {
149  ilCalendarCache::getInstance()->deleteByAdditionalKeys(
150  $a_usr_id,
151  self::MODE_PERSONAL_DESKTOP_ITEMS,
152  'categories'
153  );
154  }
155 
161  public static function deleteRepositoryCache($a_usr_id)
162  {
163  ilCalendarCache::getInstance()->deleteByAdditionalKeys(
164  $a_usr_id,
165  self::MODE_REPOSITORY,
166  'categories'
167  );
168 
169  }
170 
171 
176  protected function sleep()
177  {
178  return serialize(
179  array(
180  'categories' => $this->categories,
181  'categories_info' => $this->categories_info,
182  'subitem_categories'=> $this->subitem_categories
183  )
184  );
185  }
186 
192  protected function wakeup($a_ser)
193  {
194  $info = unserialize($a_ser);
195 
196  $this->categories = $info['categories'];
197  $this->categories_info = $info['categories_info'];
198  $this->subitem_categories = $info['subitem_categories'];
199  }
200 
209  public function initialize($a_mode,$a_source_ref_id = 0,$a_use_cache = false)
210  {
211  if($a_use_cache)
212  {
213  // Read categories from cache
214  if($cats = ilCalendarCache::getInstance()->getEntry($this->user_id.':'.$a_mode.':categories:'.(int) $a_source_ref_id))
215  {
216  $this->wakeup($cats);
217  return;
218  }
219  }
220 
221 
222  switch($a_mode)
223  {
224  case self::MODE_REMOTE_ACCESS:
225  include_once('./Services/Calendar/classes/class.ilCalendarUserSettings.php');
227  {
228  $this->readPDCalendars();
229  }
230  else
231  {
232  $this->readSelectedItemCalendars();
233  }
234  break;
235 
236  case self::MODE_PERSONAL_DESKTOP_MEMBERSHIP:
237  $this->readPDCalendars();
238  break;
239 
240  case self::MODE_PERSONAL_DESKTOP_ITEMS:
241  $this->readSelectedItemCalendars();
242  break;
243 
244  case self::MODE_REPOSITORY:
245  $this->root_ref_id = $a_source_ref_id;
246  $this->root_obj_id = ilObject::_lookupObjId($this->root_ref_id);
247  $this->readReposCalendars();
248  break;
249 
250  case self::MODE_MANAGE:
251  $this->readPDCalendars();
252  $this->readSelectedItemCalendars();
253  break;
254 
255  case self::MODE_CONSULTATION:
256  $this->readPrivateCalendars();
257  $this->readConsultationHoursCalendar($a_source_ref_id);
258  break;
259  }
260 
261  if($a_use_cache)
262  {
263  // Store in cache
264  ilCalendarCache::getInstance()->storeEntry(
265  $this->user_id.':'.$a_mode.':categories:'.(int) $a_source_ref_id,
266  $this->sleep(),
268  $a_mode,
269  'categories'
270  );
271  }
272  }
273 
281  public function getCategoryInfo($a_cat_id)
282  {
283  if(isset($this->categories_info[$a_cat_id]))
284  {
285  return $this->categories_info[$a_cat_id];
286  }
287 
288  if(in_array($a_cat_id,(array) $this->subitem_categories))
289  {
290  foreach($this->categories as $cat_id)
291  {
292  if(in_array($a_cat_id,$this->categories_info[$cat_id]['subitem_ids']))
293  {
294  return $this->categories_info[$cat_id];
295  }
296  }
297  }
298  }
299 
300 
308  public function getCategoriesInfo()
309  {
310  return $this->categories_info ? $this->categories_info : array();
311  }
312 
319  public function getCategories($a_include_subitem_calendars = false)
320  {
321  if($a_include_subitem_calendars)
322  {
323  return array_merge((array) $this->categories, (array) $this->subitem_categories);
324  }
325 
326  return $this->categories ? $this->categories : array();
327  }
328 
335  public function getSubitemCategories($a_cat_id)
336  {
337  if(!isset($this->categories_info[$a_cat_id]['subitem_ids']))
338  {
339  return array($a_cat_id);
340  }
341  return array_merge((array) $this->categories_info[$a_cat_id]['subitem_ids'],array($a_cat_id));
342  }
343 
344 
353  {
354  global $lng;
355 
356  $has_personal_calendar = false;
357  foreach($this->categories_info as $info)
358  {
359  if($info['obj_type'] == 'sess')
360  {
361  continue;
362  }
363  if($info['type'] == ilCalendarCategory::TYPE_USR and $info['editable'])
364  {
365  $has_personal_calendar = true;
366  }
367 
368  if($info['editable'])
369  {
370  $cats[$info['cat_id']] = $info['title'];
371  }
372  }
373  // If there
374  if(!$has_personal_calendar)
375  {
376  $cats[0] = $lng->txt('cal_default_calendar');
377  }
378  return $cats ? $cats : array();
379  }
380 
386  public function getNotificationCalendars()
387  {
388  $not = array();
389  foreach($this->categories_info as $info)
390  {
391  if($info['type'] == ilCalendarCategory::TYPE_OBJ and $info['editable'] == true)
392  {
393  if(ilObject::_lookupType($info['obj_id']) == 'crs' or ilObject::_lookupType($info['obj_id']) == 'grp')
394  {
395  $not[] = $info['cat_id'];
396  }
397  }
398  }
399  return $not;
400  }
401 
409  public function isEditable($a_cat_id)
410  {
411  return isset($this->categories_info[$a_cat_id]['editable']) and $this->categories_info[$a_cat_id]['editable'];
412  }
413 
421  public function isVisible($a_cat_id)
422  {
423  return in_array($a_cat_id,$this->categories) or
424  in_array($a_cat_id,(array) $this->subitem_categories);
425  }
426 
427 
428 
429 
437  protected function readPDCalendars()
438  {
439  global $rbacsystem;
440 
441 
442  $this->readPublicCalendars();
443  $this->readPrivateCalendars();
445  $this->readBookingCalendar();
446 
447  include_once('./Services/Membership/classes/class.ilParticipants.php');
448  $this->readSelectedCategories(ilParticipants::_getMembershipByType($this->user_id,'crs'));
449  $this->readSelectedCategories(ilParticipants::_getMembershipByType($this->user_id,'grp'));
450 
451  $this->addSubitemCalendars();
452  }
453 
460  protected function readSelectedItemCalendars()
461  {
462  global $ilUser,$ilAccess;
463 
464  $this->readPublicCalendars();
465  $this->readPrivateCalendars();
467  $this->readBookingCalendar();
468 
469  $obj_ids = array();
470 
471  $courses = array();
472  $groups = array();
473  $sessions = array();
474  foreach(ilObjUser::_lookupDesktopItems($ilUser->getId(),array('crs','grp','sess')) as $item)
475  {
476  if($ilAccess->checkAccess('read','',$item['ref_id']))
477  {
478  switch($item['type'])
479  {
480  case 'crs':
481  $courses[] = $item['obj_id'];
482  break;
483 
484  case 'sess':
485  $sessions[] = $item['obj_id'];
486  break;
487 
488  case 'grp':
489  $groups[] = $item['obj_id'];
490  break;
491 
492  }
493  }
494  }
495  $this->readSelectedCategories($courses);
496  $this->readSelectedCategories($sessions);
497  $this->readSelectedCategories($groups);
498 
499  $this->addSubitemCalendars();
500 
501  }
502 
510  protected function readReposCalendars()
511  {
512  global $ilAccess,$tree;
513  global $ilDB;
514 
515  $this->readPublicCalendars();
516  $this->readPrivateCalendars();
518 
519  $query = "SELECT ref_id,obd.obj_id obj_id FROM tree t1 ".
520  "JOIN object_reference obr ON t1.child = obr.ref_id ".
521  "JOIN object_data obd ON obd.obj_id = obr.obj_id ".
522  "WHERE t1.lft >= (SELECT lft FROM tree WHERE child = ".$this->db->quote($this->root_ref_id,'integer')." ) ".
523  "AND t1.lft <= (SELECT rgt FROM tree WHERE child = ".$this->db->quote($this->root_ref_id,'integer')." ) ".
524  "AND ".$ilDB->in('type',array('crs','grp','sess'),false,'text')." ".
525  "AND tree = 1";
526  $res = $ilDB->query($query);
527  $obj_ids = array();
528  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
529  {
530  if($tree->isDeleted($row->ref_id))
531  {
532  continue;
533  }
534 
535  if($ilAccess->checkAccess('read','',$row->ref_id))
536  {
537  $obj_ids[] = $row->obj_id;
538  }
539  }
540  $this->readSelectedCategories($obj_ids);
541  }
542 
549  protected function readPublicCalendars()
550  {
551  global $rbacsystem,$ilAccess;
552 
553  // global categories
554  $query = "SELECT * FROM cal_categories ".
555  "WHERE type = ".$this->db->quote(ilCalendarCategory::TYPE_GLOBAL ,'integer')." ".
556  "ORDER BY title ";
557 
558  $res = $this->db->query($query);
559  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
560  {
561  $this->categories[] = $row->cat_id;
562  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
563  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
564  $this->categories_info[$row->cat_id]['title'] = $row->title;
565  $this->categories_info[$row->cat_id]['color'] = $row->color;
566  $this->categories_info[$row->cat_id]['type'] = $row->type;
567  $this->categories_info[$row->cat_id]['editable'] = $rbacsystem->checkAccess('edit_event',ilCalendarSettings::_getInstance()->getCalendarSettingsId());
568  $this->categories_info[$row->cat_id]['accepted'] = false;
569  }
570 
571  return true;
572  }
573 
580  protected function readPrivateCalendars()
581  {
582  global $ilUser;
583  global $ilDB;
584 
585  // First read private calendars of user
586  $query = "SELECT cat_id FROM cal_categories ".
587  "WHERE type = ".$this->db->quote(ilCalendarCategory::TYPE_USR ,'integer')." ".
588  "AND obj_id = ".$this->db->quote($ilUser->getId(),'integer')." ";
589  $res = $this->db->query($query);
590  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
591  {
592  $cat_ids[] = $row->cat_id;
593  }
594 
595  // Read shared calendars
596  include_once('./Services/Calendar/classes/class.ilCalendarSharedStatus.php');
597  $accepted_ids = ilCalendarSharedStatus::getAcceptedCalendars($ilUser->getId());
598  if(!$cat_ids = array_merge((array) $cat_ids, $accepted_ids))
599  {
600  return true;
601  }
602 
603 
604  // user categories
605  $query = "SELECT * FROM cal_categories ".
606  "WHERE type = ".$this->db->quote(ilCalendarCategory::TYPE_USR ,'integer')." ".
607  "AND ".$ilDB->in('cat_id',$cat_ids,false,'integer')." ".
608  "ORDER BY title ";
609 
610  $res = $this->db->query($query);
611  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
612  {
613  $this->categories[] = $row->cat_id;
614  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
615  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
616  $this->categories_info[$row->cat_id]['title'] = $row->title;
617  $this->categories_info[$row->cat_id]['color'] = $row->color;
618  $this->categories_info[$row->cat_id]['type'] = $row->type;
619  $this->categories_info[$row->cat_id]['editable'] = $row->obj_id == $ilUser->getId();
620  $this->categories_info[$row->cat_id]['accepted'] = in_array($row->cat_id, $accepted_ids);
621  }
622  }
623 
629  public function readConsultationHoursCalendar($user_id = NULL)
630  {
631  global $ilDB;
632 
633  if(!$user_id)
634  {
636  }
637 
638  $query = "SELECT * FROM cal_categories ".
639  "WHERE type = ".$ilDB->quote(ilCalendarCategory::TYPE_CH,'integer').' '.
640  "AND obj_id = ".$ilDB->quote($user_id,'integer');
641  $res = $ilDB->query($query);
642  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
643  {
644  $this->categories[] = $row->cat_id;
645  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
646  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
647  $this->categories_info[$row->cat_id]['title'] = $row->title;
648  $this->categories_info[$row->cat_id]['color'] = $row->color;
649  $this->categories_info[$row->cat_id]['type'] = $row->type;
650  $this->categories_info[$row->cat_id]['editable'] = false;
651  $this->categories_info[$row->cat_id]['accepted'] = false;
652  }
653  }
654 
660  public function readBookingCalendar($user_id = NULL)
661  {
662  global $ilDB;
663 
664  if(!$user_id)
665  {
667  }
668 
669  $query = "SELECT * FROM cal_categories ".
670  "WHERE type = ".$ilDB->quote(ilCalendarCategory::TYPE_BOOK,'integer').' '.
671  "AND obj_id = ".$ilDB->quote($user_id,'integer');
672  $res = $ilDB->query($query);
673  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
674  {
675  $this->categories[] = $row->cat_id;
676  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
677  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
678  $this->categories_info[$row->cat_id]['title'] = $row->title;
679  $this->categories_info[$row->cat_id]['color'] = $row->color;
680  $this->categories_info[$row->cat_id]['type'] = $row->type;
681  $this->categories_info[$row->cat_id]['editable'] = false;
682  $this->categories_info[$row->cat_id]['accepted'] = false;
683  }
684  }
685 
692  protected function readSelectedCategories($a_obj_ids)
693  {
694  global $ilAccess,$tree;
695  global $ilDB;
696 
697  if(!count($a_obj_ids))
698  {
699  return true;
700  }
701 
702  $query = "SELECT * FROM cal_categories ".
703  "WHERE type = ".$this->db->quote(ilCalendarCategory::TYPE_OBJ ,'integer')." ".
704  "AND ".$ilDB->in('obj_id',$a_obj_ids,false,'integer')." ".
705  "ORDER BY title ";
706 
707  $res = $this->db->query($query);
708  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
709  {
710  $editable = false;
711  $exists = false;
712  foreach(ilObject::_getAllReferences($row->obj_id) as $ref_id)
713  {
714  if($ilAccess->checkAccess('edit_event','',$ref_id))
715  {
716  $exists = true;
717  $editable = true;
718  break;
719  }
720  elseif($ilAccess->checkAccess('read','',$ref_id))
721  {
722  $exists = true;
723  }
724  }
725  if(!$exists)
726  {
727  continue;
728  }
729  $this->categories_info[$row->cat_id]['editable'] = $editable;
730 
731  $this->categories[] = $row->cat_id;
732  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
733  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
734  $this->categories_info[$row->cat_id]['color'] = $row->color;
735  #$this->categories_info[$row->cat_id]['title'] = ilObject::_lookupTitle($row->obj_id);
736  $this->categories_info[$row->cat_id]['title'] = $row->title;
737  $this->categories_info[$row->cat_id]['obj_type'] = ilObject::_lookupType($row->obj_id);
738  $this->categories_info[$row->cat_id]['type'] = $row->type;
739  }
740  }
741 
748  protected function addSubitemCalendars()
749  {
750  global $ilDB;
751 
752  $course_ids = array();
753  foreach($this->categories as $cat_id)
754  {
755  if($this->categories_info[$cat_id]['obj_type'] == 'crs')
756  {
757  $course_ids[] = $this->categories_info[$cat_id]['obj_id'];
758  }
759  }
760 
761  $query = "SELECT od2.obj_id sess_id, od1.obj_id crs_id,cat_id FROM object_data od1 ".
762  "JOIN object_reference or1 ON od1.obj_id = or1.obj_id ".
763  "JOIN tree t ON or1.ref_id = t.parent ".
764  "JOIN object_reference or2 ON t.child = or2.ref_id ".
765  "JOIN object_data od2 ON or2.obj_id = od2.obj_id ".
766  "JOIN cal_categories cc ON od2.obj_id = cc.obj_id ".
767  "WHERE od2.type = 'sess' ".
768  "AND od1.type = 'crs' ".
769  "AND ".$ilDB->in('od1.obj_id',$course_ids,false,'integer').' '.
770  "AND or2.deleted IS NULL";
771 
772  $res = $ilDB->query($query);
773  $cat_ids = array();
774  $course_sessions = array();
775  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
776  {
777  $cat_ids[] = $row->cat_id;
778  $course_sessions[$row->crs_id][$row->sess_id] = $row->cat_id;
779  $this->subitem_categories[] = $row->cat_id;
780  }
781 
782  foreach($this->categories as $cat_id)
783  {
784  if($this->categories_info[$cat_id]['obj_type'] == 'crs' &&
785  isset($this->categories_info[$cat_id]['obj_id']) &&
786  isset($course_sessions[$this->categories_info[$cat_id]['obj_id']]) &&
787  is_array($course_sessions[$this->categories_info[$cat_id]['obj_id']]))
788  {
789  foreach($course_sessions[$this->categories_info[$cat_id]['obj_id']] as $sess_id => $sess_cat_id)
790  {
791  $this->categories_info[$cat_id]['subitem_ids'][$sess_id] = $sess_cat_id;
792  $this->categories_info[$cat_id]['subitem_obj_ids'][$sess_cat_id] = $sess_id;
793  }
794  }
795  else
796  {
797  $this->categories_info[$cat_id]['subitem_ids'] = array();
798  $this->categories_info[$cat_id]['subitem_obj_ids'] = array();
799  }
800  }
801  }
802 
803 }
804 ?>