ILIAS  release_4-3 Revision
 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 
527  $res = $ilDB->query($query);
528  $obj_ids = array();
529  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
530  {
531  if($tree->isDeleted($row->ref_id))
532  {
533  continue;
534  }
535 
536  $obj_type = ilObject::_lookupType($row->obj_id);
537  if($obj_type == 'crs' or $obj_type == 'grp')
538  {
539  // Check for global/local activation
540  if(!ilCalendarSettings::_getInstance()->lookupCalendarActivated($row->obj_id))
541  {
542  continue;
543  }
544  }
545  if($ilAccess->checkAccess('read','',$row->ref_id))
546  {
547  $obj_ids[] = $row->obj_id;
548  }
549  }
550  $this->readSelectedCategories($obj_ids);
551  }
552 
559  protected function readPublicCalendars()
560  {
561  global $rbacsystem,$ilAccess;
562 
563  // global categories
564  $query = "SELECT * FROM cal_categories ".
565  "WHERE type = ".$this->db->quote(ilCalendarCategory::TYPE_GLOBAL ,'integer')." ".
566  "ORDER BY title ";
567 
568  $res = $this->db->query($query);
569  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
570  {
571  $this->categories[] = $row->cat_id;
572  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
573  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
574  $this->categories_info[$row->cat_id]['title'] = $row->title;
575  $this->categories_info[$row->cat_id]['color'] = $row->color;
576  $this->categories_info[$row->cat_id]['type'] = $row->type;
577  $this->categories_info[$row->cat_id]['editable'] = $rbacsystem->checkAccess('edit_event',ilCalendarSettings::_getInstance()->getCalendarSettingsId());
578  $this->categories_info[$row->cat_id]['accepted'] = false;
579  $this->categories_info[$row->cat_id]['remote'] = ($row->loc_type == ilCalendarCategory::LTYPE_REMOTE);
580  }
581 
582  return true;
583  }
584 
591  protected function readPrivateCalendars()
592  {
593  global $ilUser;
594  global $ilDB;
595 
596  // First read private calendars of user
597  $query = "SELECT cat_id FROM cal_categories ".
598  "WHERE type = ".$this->db->quote(ilCalendarCategory::TYPE_USR ,'integer')." ".
599  "AND obj_id = ".$this->db->quote($ilUser->getId(),'integer')." ";
600  $res = $this->db->query($query);
601  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
602  {
603  $cat_ids[] = $row->cat_id;
604  }
605 
606  // Read shared calendars
607  include_once('./Services/Calendar/classes/class.ilCalendarSharedStatus.php');
608  $accepted_ids = ilCalendarSharedStatus::getAcceptedCalendars($ilUser->getId());
609  if(!$cat_ids = array_merge((array) $cat_ids, $accepted_ids))
610  {
611  return true;
612  }
613 
614 
615  // user categories
616  $query = "SELECT * FROM cal_categories ".
617  "WHERE type = ".$this->db->quote(ilCalendarCategory::TYPE_USR ,'integer')." ".
618  "AND ".$ilDB->in('cat_id',$cat_ids,false,'integer')." ".
619  "ORDER BY title ";
620 
621  $res = $this->db->query($query);
622  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
623  {
624  $this->categories[] = $row->cat_id;
625  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
626  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
627  $this->categories_info[$row->cat_id]['title'] = $row->title;
628  $this->categories_info[$row->cat_id]['color'] = $row->color;
629  $this->categories_info[$row->cat_id]['type'] = $row->type;
630  $this->categories_info[$row->cat_id]['editable'] = $row->obj_id == $ilUser->getId();
631  $this->categories_info[$row->cat_id]['accepted'] = in_array($row->cat_id, $accepted_ids);
632  $this->categories_info[$row->cat_id]['remote'] = ($row->loc_type == ilCalendarCategory::LTYPE_REMOTE);
633  }
634  }
635 
641  public function readConsultationHoursCalendar($user_id = NULL)
642  {
643  global $ilDB;
644 
645  if(!$user_id)
646  {
648  }
649 
650  $query = "SELECT * FROM cal_categories ".
651  "WHERE type = ".$ilDB->quote(ilCalendarCategory::TYPE_CH,'integer').' '.
652  "AND obj_id = ".$ilDB->quote($user_id,'integer');
653  $res = $ilDB->query($query);
654  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
655  {
656  $this->categories[] = $row->cat_id;
657  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
658  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
659  $this->categories_info[$row->cat_id]['title'] = $row->title;
660  $this->categories_info[$row->cat_id]['color'] = $row->color;
661  $this->categories_info[$row->cat_id]['type'] = $row->type;
662  $this->categories_info[$row->cat_id]['editable'] = false;
663  $this->categories_info[$row->cat_id]['accepted'] = false;
664  $this->categories_info[$row->cat_id]['remote'] = false;
665  }
666  }
667 
673  public function readBookingCalendar($user_id = NULL)
674  {
675  global $ilDB;
676 
677  if(!$user_id)
678  {
680  }
681 
682  $query = "SELECT * FROM cal_categories ".
683  "WHERE type = ".$ilDB->quote(ilCalendarCategory::TYPE_BOOK,'integer').' '.
684  "AND obj_id = ".$ilDB->quote($user_id,'integer');
685  $res = $ilDB->query($query);
686  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
687  {
688  $this->categories[] = $row->cat_id;
689  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
690  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
691  $this->categories_info[$row->cat_id]['title'] = $row->title;
692  $this->categories_info[$row->cat_id]['color'] = $row->color;
693  $this->categories_info[$row->cat_id]['type'] = $row->type;
694  $this->categories_info[$row->cat_id]['editable'] = false;
695  $this->categories_info[$row->cat_id]['accepted'] = false;
696  $this->categories_info[$row->cat_id]['remote'] = false;
697  }
698  }
699 
706  protected function readSelectedCategories($a_obj_ids)
707  {
708  global $ilAccess,$tree;
709  global $ilDB;
710 
711  if(!count($a_obj_ids))
712  {
713  return true;
714  }
715 
716  $query = "SELECT * FROM cal_categories ".
717  "WHERE type = ".$this->db->quote(ilCalendarCategory::TYPE_OBJ ,'integer')." ".
718  "AND ".$ilDB->in('obj_id',$a_obj_ids,false,'integer')." ".
719  "ORDER BY title ";
720 
721  $res = $this->db->query($query);
722  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
723  {
724  // check activation/deactivation
725  $obj_type = ilObject::_lookupType($row->obj_id);
726  if($obj_type == 'crs' or $obj_type == 'grp')
727  {
728  if(!ilCalendarSettings::_getInstance()->lookupCalendarActivated($row->obj_id))
729  {
730  continue;
731  }
732  }
733 
734  $editable = false;
735  $exists = false;
736  foreach(ilObject::_getAllReferences($row->obj_id) as $ref_id)
737  {
738  if($ilAccess->checkAccess('edit_event','',$ref_id))
739  {
740  $exists = true;
741  $editable = true;
742  break;
743  }
744  elseif($ilAccess->checkAccess('read','',$ref_id))
745  {
746  $exists = true;
747  }
748  }
749  if(!$exists)
750  {
751  continue;
752  }
753  $this->categories_info[$row->cat_id]['editable'] = $editable;
754 
755  $this->categories[] = $row->cat_id;
756  $this->categories_info[$row->cat_id]['obj_id'] = $row->obj_id;
757  $this->categories_info[$row->cat_id]['cat_id'] = $row->cat_id;
758  $this->categories_info[$row->cat_id]['color'] = $row->color;
759  #$this->categories_info[$row->cat_id]['title'] = ilObject::_lookupTitle($row->obj_id);
760  $this->categories_info[$row->cat_id]['title'] = $row->title;
761  $this->categories_info[$row->cat_id]['obj_type'] = ilObject::_lookupType($row->obj_id);
762  $this->categories_info[$row->cat_id]['type'] = $row->type;
763  $this->categories_info[$row->cat_id]['remote'] = false;
764 
765  }
766  }
767 
774  protected function addSubitemCalendars()
775  {
776  global $ilDB;
777 
778  $course_ids = array();
779  foreach($this->categories as $cat_id)
780  {
781  if($this->categories_info[$cat_id]['obj_type'] == 'crs' or $this->categories_info[$cat_id]['obj_type'] == 'grp')
782  {
783  $course_ids[] = $this->categories_info[$cat_id]['obj_id'];
784  }
785  }
786 
787  $query = "SELECT od2.obj_id sess_id, od1.obj_id crs_id,cat_id FROM object_data od1 ".
788  "JOIN object_reference or1 ON od1.obj_id = or1.obj_id ".
789  "JOIN tree t ON or1.ref_id = t.parent ".
790  "JOIN object_reference or2 ON t.child = or2.ref_id ".
791  "JOIN object_data od2 ON or2.obj_id = od2.obj_id ".
792  "JOIN cal_categories cc ON od2.obj_id = cc.obj_id ".
793  "WHERE od2.type = 'sess' ".
794  "AND (od1.type = 'crs' OR od1.type = 'grp') ".
795  "AND ".$ilDB->in('od1.obj_id',$course_ids,false,'integer').' '.
796  "AND or2.deleted IS NULL";
797 
798  $res = $ilDB->query($query);
799  $cat_ids = array();
800  $course_sessions = array();
801  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
802  {
803  $cat_ids[] = $row->cat_id;
804  $course_sessions[$row->crs_id][$row->sess_id] = $row->cat_id;
805  $this->subitem_categories[] = $row->cat_id;
806  }
807 
808  foreach($this->categories as $cat_id)
809  {
810  if(
811  ($this->categories_info[$cat_id]['obj_type'] == 'crs' ||
812  $this->categories_info[$cat_id]['obj_type'] == 'grp' ) &&
813  isset($this->categories_info[$cat_id]['obj_id']) &&
814  isset($course_sessions[$this->categories_info[$cat_id]['obj_id']]) &&
815  is_array($course_sessions[$this->categories_info[$cat_id]['obj_id']]))
816  {
817  foreach($course_sessions[$this->categories_info[$cat_id]['obj_id']] as $sess_id => $sess_cat_id)
818  {
819  $this->categories_info[$cat_id]['subitem_ids'][$sess_id] = $sess_cat_id;
820  $this->categories_info[$cat_id]['subitem_obj_ids'][$sess_cat_id] = $sess_id;
821  }
822  }
823  else
824  {
825  $this->categories_info[$cat_id]['subitem_ids'] = array();
826  $this->categories_info[$cat_id]['subitem_obj_ids'] = array();
827  }
828  }
829  }
830 
837  static function lookupPrivateCategories($a_user_id)
838  {
839  global $ilDB;
840 
841  // First read private calendars of user
842  $set = $ilDB->query("SELECT * FROM cal_categories ".
843  "WHERE type = ".$ilDB->quote(ilCalendarCategory::TYPE_USR ,'integer')." ".
844  "AND obj_id = ".$ilDB->quote($a_user_id,'integer'));
845  $cats = array();
846  while ($rec = $ilDB->fetchAssoc($set))
847  {
848  $cats[] = $rec;
849  }
850  return $cats;
851  }
852 
853 }
854 ?>