ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjectActivation.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
14  protected $timing_type;
15  protected $timing_start;
16  protected $timing_end;
17  protected $suggestion_start;
18  protected $suggestion_end;
19  protected $earliest_start;
20  protected $latest_end;
21  protected $visible;
22  protected $changeable;
23 
24  protected static $preloaded_data = array();
25 
26  const TIMINGS_ACTIVATION = 0;
28  const TIMINGS_PRESETTING = 2;
29  const TIMINGS_FIXED = 3; // session only => obsolete?
30 
31  function __construct()
32  {
33 
34  }
35 
42  function setTimingType($a_type)
43  {
44  $this->timing_type = $a_type;
45  }
46 
53  function getTimingType()
54  {
55  return $this->timing_type;
56  }
57 
63  function setTimingStart($a_start)
64  {
65  $this->timing_start = $a_start;
66  }
67 
73  function getTimingStart()
74  {
75  return $this->timing_start;
76  }
77 
83  function setTimingEnd($a_end)
84  {
85  $this->timing_end = $a_end;
86  }
87 
93  function getTimingEnd()
94  {
95  return $this->timing_end;
96  }
97 
103  function setSuggestionStart($a_start)
104  {
105  $this->suggestion_start = $a_start;
106  }
107 
114  {
115  return $this->suggestion_start ? $this->suggestion_start : mktime(0,0,1,date('n',time()),date('j',time()),date('Y',time()));
116  }
117 
123  function setSuggestionEnd($a_end)
124  {
125  $this->suggestion_end = $a_end;
126  }
127 
133  function getSuggestionEnd()
134  {
135  return $this->suggestion_end ? $this->suggestion_end : mktime(23,55,00,date('n',time()),date('j',time()),date('Y',time()));
136  }
137 
143  function setEarliestStart($a_start)
144  {
145  $this->earliest_start = $a_start;
146  }
147 
153  function getEarliestStart()
154  {
155  return $this->earliest_start ? $this->earliest_start : mktime(0,0,1,date('n',time()),date('j',time()),date('Y',time()));
156  }
157 
163  function setLatestEnd($a_end)
164  {
165  $this->latest_end = $a_end;
166  }
167 
173  function getLatestEnd()
174  {
175  return $this->latest_end ? $this->latest_end : mktime(23,55,00,date('n',time()),date('j',time()),date('Y',time()));
176  }
177 
183  function toggleVisible($a_status)
184  {
185  $this->visible = (int) $a_status;
186  }
187 
193  function enabledVisible()
194  {
195  return (bool) $this->visible;
196  }
197 
203  function toggleChangeable($a_status)
204  {
205  $this->changeable = (int) $a_status;
206  }
207 
213  function enabledChangeable()
214  {
215  return (bool) $this->changeable;
216  }
217 
224  {
225  global $ilErr, $lng;
226 
227  $ilErr->setMessage('');
228 
229  if($this->getTimingType() == self::TIMINGS_ACTIVATION)
230  {
231  if($this->getTimingStart() > $this->getTimingEnd())
232  {
233  $ilErr->appendMessage($lng->txt("crs_activation_start_invalid"));
234  }
235  }
236  else if($this->getTimingType() == self::TIMINGS_PRESETTING)
237  {
238  if($this->getSuggestionStart() > $this->getSuggestionEnd())
239  {
240  $ilErr->appendMessage($lng->txt('crs_latest_end_not_valid'));
241  }
242  }
243 
244  if($ilErr->getMessage())
245  {
246  return false;
247  }
248  return true;
249  }
250 
257  function update($a_ref_id, $a_parent_id = null)
258  {
259  global $ilDB;
260 
261  // #10110
262  $query = "UPDATE crs_items SET ".
263  "timing_type = ".$ilDB->quote($this->getTimingType(),'integer').", ".
264  "timing_start = ".$ilDB->quote((int)$this->getTimingStart(),'integer').", ".
265  "timing_end = ".$ilDB->quote((int)$this->getTimingEnd(),'integer').", ".
266  "suggestion_start = ".$ilDB->quote($this->getSuggestionStart(),'integer').", ".
267  "suggestion_end = ".$ilDB->quote($this->getSuggestionEnd(),'integer').", ".
268  "changeable = ".$ilDB->quote($this->enabledChangeable(),'integer').", ".
269  "earliest_start = ".$ilDB->quote($this->getEarliestStart(),'integer').", ".
270  "latest_end = ".$ilDB->quote($this->getLatestEnd(),'integer').", ";
271 
272  if($a_parent_id)
273  {
274  $query .= "parent_id = ".$ilDB->quote($a_parent_id,'integer').", ";
275  }
276 
277  $query .= "visible = ".$ilDB->quote($this->enabledVisible(),'integer')." ".
278  "WHERE obj_id = ".$ilDB->quote($a_ref_id,'integer');
279  $ilDB->manipulate($query);
280 
281  unset(self::$preloaded_data[$a_ref_id]);
282 
283  return true;
284  }
285 
291  public static function preloadData(array $a_ref_ids)
292  {
293  global $ilDB;
294 
295  $sql = "SELECT * FROM crs_items".
296  " WHERE ".$ilDB->in("obj_id", $a_ref_ids, "", "integer");
297  $set = $ilDB->query($sql);
298  while($row = $ilDB->fetchAssoc($set))
299  {
300  self::$preloaded_data[$row["obj_id"]] = $row;
301  }
302  }
303 
310  public static function getItem($a_ref_id)
311  {
312  global $ilDB;
313 
314  if(isset(self::$preloaded_data[$a_ref_id]))
315  {
316  return self::$preloaded_data[$a_ref_id];
317  }
318 
319  $sql = "SELECT * FROM crs_items".
320  " WHERE obj_id = ".$ilDB->quote($a_ref_id, "integer");
321  $set = $ilDB->query($sql);
322  $row = $ilDB->fetchAssoc($set);
323 
324  if(!isset($row["obj_id"]))
325  {
326  $row = self::createDefaultEntry($a_ref_id);
327  }
328  if($row["obj_id"])
329  {
330  self::$preloaded_data[$row["obj_id"]] = $row;
331  }
332  return $row;
333  }
334 
340  public static function addAdditionalSubItemInformation(array &$a_item)
341  {
342  global $ilUser;
343 
344  $item = self::getItem($a_item['ref_id']);
345 
346  $a_item['obj_id'] = ($a_item['obj_id'] > 0)
347  ? $a_item['obj_id']
348  : ilObject::_lookupObjId($a_item['ref_id']);
349  $a_item['type'] = ($a_item['type'] != '')
350  ? $a_item['type']
351  : ilObject::_lookupType($a_item['obj_id']);
352 
353  $a_item['timing_type'] = $item['timing_type'];
354 
355  if($item['changeable'] &&
356  $item['timing_type'] == self::TIMINGS_PRESETTING)
357  {
358  include_once 'Modules/Course/classes/Timings/class.ilTimingPlaned.php';
359  $user_data = ilTimingPlaned::_getPlanedTimings($ilUser->getId(), $a_item['ref_id']);
360  if($user_data['planed_start'])
361  {
362  $a_item['start'] = $user_data['planed_start'];
363  $a_item['end'] = $user_data['planed_end'];
364  $a_item['activation_info'] = 'crs_timings_planed_info';
365  }
366  else
367  {
368  $a_item['start'] = $item['suggestion_start'];
369  $a_item['end'] = $item['suggestion_end'];
370  $a_item['activation_info'] = 'crs_timings_suggested_info';
371  }
372  }
373  elseif($item['timing_type'] == self::TIMINGS_PRESETTING)
374  {
375  $a_item['start'] = $item['suggestion_start'];
376  $a_item['end'] = $item['suggestion_end'];
377  $a_item['activation_info'] = 'crs_timings_suggested_info';
378  }
379  elseif($item['timing_type'] == self::TIMINGS_ACTIVATION)
380  {
381  $a_item['start'] = $item['timing_start'];
382  $a_item['end'] = $item['timing_end'];
383  $a_item['activation_info'] = 'activation';
384  }
385  else
386  {
387  $a_item['start'] = 'abc';
388  }
389 
390  // #7359 - session sorting should always base on appointment date
391  if($a_item['type'] == 'sess')
392  {
393  include_once('./Modules/Session/classes/class.ilSessionAppointment.php');
394  $info = ilSessionAppointment::_lookupAppointment($a_item['obj_id']);
395 
396  // #11987
397  $a_item['masked_start'] = $a_item['start'];
398  $a_item['masked_end'] = $a_item['end'];
399  $a_item['start'] = $info['start'];
400  $a_item['end'] = $info['end'];
401  }
402  }
403 
411  public static function addListGUIActivationProperty(ilObjectListGUI $a_list_gui, array &$a_item)
412  {
413  global $lng;
414 
416  if(isset($a_item['timing_type']))
417  {
418  if(!isset($a_item['masked_start']))
419  {
420  $start = $a_item['start'];
421  $end = $a_item['end'];
422  }
423  else
424  {
425  $start = $a_item['masked_start'];
426  $end = $a_item['masked_end'];
427  }
428  $activation = '';
429  switch($a_item['timing_type'])
430  {
432  $activation = ilDatePresentation::formatPeriod(
433  new ilDateTime($start,IL_CAL_UNIX),
434  new ilDateTime($end,IL_CAL_UNIX));
435  break;
436 
438  $activation = ilDatePresentation::formatPeriod(
439  new ilDate($start,IL_CAL_UNIX),
440  new ilDate($end,IL_CAL_UNIX));
441  break;
442  }
443  if ($activation != "")
444  {
445  global $lng;
446  $lng->loadLanguageModule('crs');
447 
448  $a_list_gui->addCustomProperty($lng->txt($a_item['activation_info']),
449  $activation, false, true);
450  }
451  }
452  }
453 
460  protected static function createDefaultEntry($a_ref_id)
461  {
462  global $ilDB, $tree;
463 
464  $parent_id = $tree->getParentId($a_ref_id);
465  if(!$parent_id)
466  {
467  return;
468  }
469 
470  // #10077
471  $ilDB->lockTables(array(
472  array("name" => "crs_items",
473  "type" => ilDB::LOCK_WRITE,
474  "alias" => "")
475  ));
476 
477  $sql = "SELECT * FROM crs_items".
478  " WHERE obj_id = ".$ilDB->quote($a_ref_id, "integer");
479  $set = $ilDB->query($sql);
480  if(!$ilDB->numRows($set))
481  {
482  $now = time();
483  $now_parts = getdate($now);
484 
485  $a_item = array();
486  $a_item["timing_type"] = self::TIMINGS_DEACTIVATED;
487  $a_item["timing_start"] = $now;
488  $a_item["timing_end"] = $now;
489  $a_item["suggestion_start"] = $now;
490  $a_item["suggestion_end"] = $now;
491  $a_item['visible'] = 0;
492  $a_item['changeable'] = 0;
493  $a_item['earliest_start'] = $now;
494  $a_item['latest_end'] = mktime(23,55,00,$now_parts["mon"],$now_parts["mday"],$now_parts["year"]);
495  $a_item['visible'] = 0;
496  $a_item['changeable'] = 0;
497 
498  $query = "INSERT INTO crs_items (parent_id,obj_id,timing_type,timing_start,timing_end," .
499  "suggestion_start,suggestion_end, ".
500  "changeable,earliest_start,latest_end,visible,position) ".
501  "VALUES( ".
502  $ilDB->quote($parent_id,'integer').",".
503  $ilDB->quote($a_ref_id,'integer').",".
504  $ilDB->quote($a_item["timing_type"],'integer').",".
505  $ilDB->quote($a_item["timing_start"],'integer').",".
506  $ilDB->quote($a_item["timing_end"],'integer').",".
507  $ilDB->quote($a_item["suggestion_start"],'integer').",".
508  $ilDB->quote($a_item["suggestion_end"],'integer').",".
509  $ilDB->quote($a_item["changeable"],'integer').",".
510  $ilDB->quote($a_item['earliest_start'],'integer').", ".
511  $ilDB->quote($a_item['latest_end'],'integer').", ".
512  $ilDB->quote($a_item["visible"],'integer').", ".
513  $ilDB->quote(0,'integer').")";
514  $ilDB->manipulate($query);
515  }
516 
517  $ilDB->unlockTables();
518 
519  // #9982 - to make getItem()-cache work
520  $a_item["obj_id"] = $a_ref_id;
521  $a_item["parent_id"] = $parent_id;
522 
523  return $a_item;
524  }
525 
531  public static function deleteAllEntries($a_ref_id)
532  {
533  global $ilDB;
534 
535  if(!$a_ref_id)
536  {
537  return;
538  }
539 
540  $query = "DELETE FROM crs_items ".
541  "WHERE obj_id = ".$ilDB->quote($a_ref_id,'integer');
542  $ilDB->manipulate($query);
543 
544  $query = "DELETE FROM crs_items ".
545  "WHERE parent_id = ".$ilDB->quote($a_ref_id,'integer');
546  $ilDB->manipulate($query);
547 
548  return true;
549  }
550 
558  public static function cloneDependencies($a_ref_id,$a_target_id,$a_copy_id)
559  {
560  global $ilLog;
561 
562  $ilLog->write(__METHOD__.': Begin course items...');
563 
564  $items = self::getItems($a_ref_id);
565  if(!$items)
566  {
567  $ilLog->write(__METHOD__.': No course items found.');
568  return true;
569  }
570 
571  // new course item object
572  if(!is_object($new_container = ilObjectFactory::getInstanceByRefId($a_target_id,false)))
573  {
574  $ilLog->write(__METHOD__.': Cannot create target object.');
575  return false;
576  }
577 
578  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
579  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
580  $mappings = $cp_options->getMappings();
581 
582  foreach($items as $item)
583  {
584  if(!isset($mappings[$item['parent_id']]) or !$mappings[$item['parent_id']])
585  {
586  $ilLog->write(__METHOD__.': No mapping for parent nr. '.$item['parent_id']);
587  continue;
588  }
589  if(!isset($mappings[$item['obj_id']]) or !$mappings[$item['obj_id']])
590  {
591  $ilLog->write(__METHOD__.': No mapping for item nr. '.$item['obj_id']);
592  continue;
593  }
594  $new_item_id = $mappings[$item['obj_id']];
595  $new_parent = $mappings[$item['parent_id']];
596 
597  $new_item = new self();
598  $new_item->setTimingType($item['timing_type']);
599  $new_item->setTimingStart($item['timing_start']);
600  $new_item->setTimingEnd($item['timing_end']);
601  $new_item->setSuggestionStart($item['suggestion_start']);
602  $new_item->setSuggestionEnd($item['suggestion_end']);
603  $new_item->toggleChangeable($item['changeable']);
604  $new_item->setEarliestStart($item['earliest_start']);
605  $new_item->setLatestEnd($item['latest_end']);
606  $new_item->toggleVisible($item['visible']);
607  $new_item->update($new_item_id, $new_parent);
608 
609  $ilLog->write(__METHOD__.': Added new entry for item nr. '.$item['obj_id']);
610  }
611  $ilLog->write(__METHOD__.': Finished course items.');
612  }
613 
614 
615  //
616  // TIMINGS VIEW RELATED (COURSE ONLY)
617  //
618 
625  public static function hasTimings($a_ref_id)
626  {
627  global $tree, $ilDB;
628 
629  $subtree = $tree->getSubTree($tree->getNodeData($a_ref_id));
630  $ref_ids = array();
631  foreach($subtree as $node)
632  {
633  $ref_ids[] = $node['ref_id'];
634  }
635 
636  $query = "SELECT * FROM crs_items ".
637  "WHERE timing_type = ".$ilDB->quote(self::TIMINGS_PRESETTING,'integer')." ".
638  "AND ".$ilDB->in('obj_id',$ref_ids,false,'integer');
639  $res = $ilDB->query($query);
640  return $res->numRows() ? true :false;
641  }
642 
649  public static function hasChangeableTimings($a_ref_id)
650  {
651  global $tree, $ilDB;
652 
653  $subtree = $tree->getSubTree($tree->getNodeData($a_ref_id));
654  $ref_ids = array();
655  foreach($subtree as $node)
656  {
657  $ref_ids[] = $node['ref_id'];
658  }
659 
660  $query = "SELECT * FROM crs_items ".
661  "WHERE timing_type = ".$ilDB->quote(self::TIMINGS_PRESETTING,'integer')." ".
662  "AND changeable = ".$ilDB->quote(1,'integer')." ".
663  "AND ".$ilDB->in('obj_id',$ref_ids,false,'integer');
664  $res = $ilDB->query($query);
665  return $res->numRows() ? true : false;
666  }
667 
674  protected static function processListItems(array $a_ref_ids)
675  {
676  global $tree;
677 
678  $res = array();
679 
680  foreach($a_ref_ids as $item_ref_id)
681  {
682  if($tree->isDeleted($item_ref_id))
683  {
684  continue;
685  }
686  // #7571: when node is removed from system, e.g. inactive trashcan, an empty array is returned
687  $node = $tree->getNodeData($item_ref_id);
688  if($node["ref_id"] != $item_ref_id)
689  {
690  continue;
691  }
692  $res[$item_ref_id] = $node;
693  }
694 
695  if(sizeof($res))
696  {
697  self::preloadData(array_keys($res));
698  foreach($res as $idx => $item)
699  {
701  $res[$idx] = $item;
702  }
703  }
704 
705  return array_values($res);
706  }
707 
714  public static function getItemsByEvent($a_event_id)
715  {
716  include_once 'Modules/Session/classes/class.ilEventItems.php';
717  $event_items = new ilEventItems($a_event_id);
718  return self::processListItems($event_items->getItems());
719  }
720 
727  public static function getItemsByItemGroup($a_item_group_ref_id)
728  {
729  include_once 'Modules/ItemGroup/classes/class.ilItemGroupItems.php';
730  $ig_items = new ilItemGroupItems($a_item_group_ref_id);
731  $items = $ig_items->getValidItems();
732  return self::processListItems($items);
733  }
734 
741  public static function getItemsByObjective($a_objective_id)
742  {
743  include_once('./Modules/Course/classes/class.ilCourseObjectiveMaterials.php');
744  $item_ids = ilCourseObjectiveMaterials::_getAssignedMaterials($a_objective_id);
745  return self::processListItems($item_ids);
746  }
747 
755  public static function getItems($a_parent_id, $a_with_list_data = true)
756  {
757  global $tree;
758 
759  $items = array();
760 
761  $ref_ids = array();
762  foreach($tree->getChilds($a_parent_id) as $item)
763  {
764  if($item['type'] != 'rolf')
765  {
766  $items[] = $item;
767  $ref_ids[] = $item['ref_id'];
768  }
769  }
770 
771  if($ref_ids)
772  {
773  self::preloadData($ref_ids);
774 
775  foreach($items as $idx => $item)
776  {
777  if(!$a_with_list_data)
778  {
779  $items[$idx] = array_merge($item, self::getItem($item['ref_id']));
780  }
781  else
782  {
784  $items[$idx] = $item;
785  }
786  }
787  }
788 
789  return $items;
790  }
791 
798  public static function getTimingsAdministrationItems($a_parent_id)
799  {
800  $items = self::getItems($a_parent_id, false);
801 
802  if($items)
803  {
804  $active = $inactive = array();
805  foreach($items as $item)
806  {
807  // active should be first in order
808  if($item['timing_type'] == self::TIMINGS_DEACTIVATED)
809  {
810  $inactive[] = $item;
811  }
812  else
813  {
814  $active[] = $item;
815  }
816  }
817 
818  $active = ilUtil::sortArray($active,'start','asc');
819  $inactive = ilUtil::sortArray($inactive,'title','asc');
820  $items = array_merge($active,$inactive);
821  }
822 
823  return $items;
824  }
825 
832  public static function getTimingsItems($a_container_ref_id)
833  {
834  global $objDefinition;
835 
836  $filtered = array();
837 
838  include_once 'Modules/Session/classes/class.ilEventItems.php';
839  $event_items = ilEventItems::_getItemsOfContainer($a_container_ref_id);
840  foreach(self::getTimingsAdministrationItems($a_container_ref_id) as $item)
841  {
842  if(!in_array($item['ref_id'],$event_items) &&
843  !$objDefinition->isSideBlock($item['type']))
844  {
845  $filtered[] = $item;
846  }
847  }
848 
849  return $filtered;
850  }
851 }
852 
853 ?>