ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilConditionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
67 {
68  const UNIQUE_CONDITIONS = 1;
69  const SHARED_CONDITIONS = 0;
70 
71  var $db;
72  var $lng;
73 
74 
76 
83  var $operator;
84  var $value;
86 
88 
89 
94  function ilConditionHandler()
95  {
96  global $ilDB,$lng;
97 
98  $this->db =& $ilDB;
99  $this->lng =& $lng;
100  $this->validation = true;
101  }
102 
111  public static function _isReferenceHandlingOptional($a_type)
112  {
113  switch($a_type)
114  {
115  case 'st':
116  return true;
117 
118  default:
119  return false;
120  }
121  }
122 
133  public static function _adjustMovedObjectConditions($a_ref_id)
134  {
135  global $tree;
136 
137  if($tree->checkForParentType($a_ref_id,'crs'))
138  {
139  // Nothing to do
140  return true;
141  }
142 
143  // Need another implementation that has better performance
144  $childs = $tree->getSubTree($tree->getNodeData($a_ref_id),false);
146 
147  foreach(array_intersect($conditions,$childs) as $target_ref)
148  {
149  if(!$tree->checkForParentType($target_ref,'crs'))
150  {
152  }
153  }
154  return true;
155  }
156 
164  public static function _getDistinctTargetRefIds()
165  {
166  global $ilDB;
167 
168  $query = "SELECT DISTINCT target_ref_id ref FROM conditions ";
169  $res = $ilDB->query($query);
170  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
171  {
172  $ref_ids[] = $row->ref;
173  }
174  return $ref_ids ? $ref_ids : array();
175  }
176 
187  public static function _deleteTargetConditionsByRefId($a_target_ref_id)
188  {
189  global $ilDB;
190 
191  $query = "DELETE FROM conditions ".
192  "WHERE target_ref_id = ".$ilDB->quote($a_target_ref_id,'integer')." ".
193  "AND target_type != 'st' ";
194  $res = $ilDB->manipulate($query);
195  return true;
196  }
197 
205  public function setReferenceHandlingType($a_type)
206  {
207  return $this->condition_reference_type = $a_type;
208  }
209 
216  public function getReferenceHandlingType()
217  {
218  return (int) $this->condition_reference_type;
219  }
220 
221  // SET GET
222  function setErrorMessage($a_msg)
223  {
224  $this->error_message = $a_msg;
225  }
226  function getErrorMessage()
227  {
228  return $this->error_message;
229  }
230 
234  function setTargetRefId($a_target_ref_id)
235  {
236  return $this->target_ref_id = $a_target_ref_id;
237  }
238 
242  function getTargetRefId()
243  {
244  return $this->target_ref_id;
245  }
246 
250  function setTargetObjId($a_target_obj_id)
251  {
252  return $this->target_obj_id = $a_target_obj_id;
253  }
254 
258  function getTargetObjId()
259  {
260  return $this->target_obj_id;
261  }
262 
266  function setTargetType($a_target_type)
267  {
268  return $this->target_type = $a_target_type;
269  }
270 
274  function getTargetType()
275  {
276  return $this->target_type;
277  }
278 
282  function setTriggerRefId($a_trigger_ref_id)
283  {
284  return $this->trigger_ref_id = $a_trigger_ref_id;
285  }
286 
290  function getTriggerRefId()
291  {
292  return $this->trigger_ref_id;
293  }
294 
298  function setTriggerObjId($a_trigger_obj_id)
299  {
300  return $this->trigger_obj_id = $a_trigger_obj_id;
301  }
302 
306  function getTriggerObjId()
307  {
308  return $this->trigger_obj_id;
309  }
310 
314  function setTriggerType($a_trigger_type)
315  {
316  return $this->trigger_type = $a_trigger_type;
317  }
318 
322  function getTriggerType()
323  {
324  return $this->trigger_type;
325  }
326 
330  function setOperator($a_operator)
331  {
332  return $this->operator = $a_operator;
333  }
334 
338  function getOperator()
339  {
340  return $this->operator;
341  }
342 
346  function setValue($a_value)
347  {
348  return $this->value = $a_value;
349  }
350 
354  function getValue()
355  {
356  return $this->value;
357  }
358 
362  function enableAutomaticValidation($a_validate = true)
363  {
364  $this->validation = $a_validate;
365  }
366 
372  function getTriggerTypes()
373  {
374  return array('crs','exc','tst','sahs', 'svy');
375  }
376 
377 
378  function getOperatorsByTargetType($a_type)
379  {
380  switch($a_type)
381  {
382  case 'crs':
383  case 'exc':
384  return array('passed');
385 
386  case 'tst':
387  return array('passed','finished','not_finished');
388 
389  case 'crsg':
390  return array('not_member');
391 
392  case 'sahs':
393  return array('finished');
394 
395  case 'svy':
396  return array('finished');
397 
398  default:
399  return array();
400  }
401  }
402 
408  function storeCondition()
409  {
410  global $ilDB;
411 
412  // first insert, then validate: it's easier to check for circles if the new condition is in the db table
413  $next_id = $ilDB->nextId('conditions');
414  $query = 'INSERT INTO conditions (condition_id,target_ref_id,target_obj_id,target_type,'.
415  'trigger_ref_id,trigger_obj_id,trigger_type,operator,value,ref_handling) '.
416  'VALUES ('.
417  $ilDB->quote($next_id,'integer').','.
418  $ilDB->quote($this->getTargetRefId(),'integer').",".
419  $ilDB->quote($this->getTargetObjId(),'integer').",".
420  $ilDB->quote($this->getTargetType(),'text').",".
421  $ilDB->quote($this->getTriggerRefId(),'integer').",".
422  $ilDB->quote($this->getTriggerObjId(),'integer').",".
423  $ilDB->quote($this->getTriggerType(),'text').",".
424  $ilDB->quote($this->getOperator(),'text').",".
425  $ilDB->quote($this->getValue(),'text').", ".
426  $ilDB->quote($this->getReferenceHandlingType(),'integer').
427  ')';
428 
429  $res = $ilDB->manipulate($query);
430 
431  if ($this->validation && !$this->validate())
432  {
433  $this->deleteCondition($next_id);
434  return false;
435  }
436  return true;
437  }
438 
439  function checkExists()
440  {
441  global $ilDB;
442 
443  $query = "SELECT * FROM conditions ".
444  "WHERE target_ref_id = ".$ilDB->quote($this->getTargetRefId(),'integer')." ".
445  "AND target_obj_id = ".$ilDB->quote($this->getTargetObjId(),'integer')." ".
446  "AND trigger_ref_id = ".$ilDB->quote($this->getTriggerRefId(),'integer')." ".
447  "AND trigger_obj_id = ".$ilDB->quote($this->getTriggerObjId(),'integer')." ".
448  "AND operator = ".$ilDB->quote($this->getOperator(),'text');
449  $res = $ilDB->query($query);
450 
451  return $res->numRows() ? true : false;
452  }
456  function updateCondition($a_id)
457  {
458  global $ilDB;
459 
460  $query = "UPDATE conditions SET ".
461  "target_ref_id = ".$ilDB->quote($this->getTargetRefId(),'integer').", ".
462  "operator = ".$ilDB->quote($this->getOperator(),'text').", ".
463  "value = ".$ilDB->quote($this->getValue(),'text').", ".
464  "ref_handling = ".$this->db->quote($this->getReferenceHandlingType(),'integer')." ".
465  "WHERE condition_id = ".$ilDB->quote($a_id,'integer');
466  $res = $ilDB->manipulate($query);
467 
468  return true;
469  }
470 
471 
476  function delete($a_ref_id)
477  {
478  global $ilDB;
479 
480  $query = "DELETE FROM conditions WHERE ".
481  "target_ref_id = ".$ilDB->quote($a_ref_id,'integer')." ".
482  "OR trigger_ref_id = ".$ilDB->quote($a_ref_id,'integer');
483  $res = $ilDB->manipulate($query);
484 
485  return true;
486  }
491  function deleteByObjId($a_obj_id)
492  {
493  global $ilDB;
494 
495  $query = "DELETE FROM conditions WHERE ".
496  "target_obj_id = ".$ilDB->quote($a_obj_id,'integer')." ".
497  "OR trigger_obj_id = ".$ilDB->quote($a_obj_id,'integer');
498  $res = $ilDB->manipulate($query);
499 
500  return true;
501  }
502 
506  function deleteCondition($a_id)
507  {
508  global $ilDB;
509 
510  $query = "DELETE FROM conditions ".
511  "WHERE condition_id = ".$ilDB->quote($a_id,'integer');
512  $res = $ilDB->manipulate($query);
513 
514  return true;
515  }
516 
521  function _getConditionsOfTrigger($a_trigger_obj_type, $a_trigger_id)
522  {
523  global $ilDB;
524 
525  $query = "SELECT * FROM conditions ".
526  "WHERE trigger_obj_id = ".$ilDB->quote($a_trigger_id,'integer')." ".
527  " AND trigger_type = ".$ilDB->quote($a_trigger_obj_type,'text');
528 
529  $res = $ilDB->query($query);
530  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
531  {
532  $tmp_array['id'] = $row->condition_id;
533  $tmp_array['target_ref_id'] = $row->target_ref_id;
534  $tmp_array['target_obj_id'] = $row->target_obj_id;
535  $tmp_array['target_type'] = $row->target_type;
536  $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
537  $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
538  $tmp_array['trigger_type'] = $row->trigger_type;
539  $tmp_array['operator'] = $row->operator;
540  $tmp_array['value'] = $row->value;
541  $tmp_array['ref_handling'] = $row->ref_handling;
542 
543  $conditions[] = $tmp_array;
544  unset($tmp_array);
545  }
546 
547  return $conditions ? $conditions : array();
548  }
549 
560  function _getConditionsOfTarget($a_target_ref_id,$a_target_obj_id, $a_target_type = "")
561  {
562  global $ilDB, $ilBench;
563 
564  $ilBench->start("ilConditionHandler", "getConditionsOfTarget");
565 
566  if ($a_target_type == "")
567  {
568  $a_target_type = ilObject::_lookupType($a_target_obj_id);
569  }
570 
571  $query = "SELECT * FROM conditions ".
572  "WHERE target_obj_id = ".$ilDB->quote($a_target_obj_id,'integer')." ".
573  " AND target_type = ".$ilDB->quote($a_target_type,'text');
574 
575  $res = $ilDB->query($query);
576  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
577  {
578  if($row->ref_handling == self::UNIQUE_CONDITIONS)
579  {
580  if($row->target_ref_id != $a_target_ref_id)
581  {
582  continue;
583  }
584  }
585 
586  $tmp_array['id'] = $row->condition_id;
587  $tmp_array['target_ref_id'] = $row->target_ref_id;
588  $tmp_array['target_obj_id'] = $row->target_obj_id;
589  $tmp_array['target_type'] = $row->target_type;
590  $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
591  $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
592  $tmp_array['trigger_type'] = $row->trigger_type;
593  $tmp_array['operator'] = $row->operator;
594  $tmp_array['value'] = $row->value;
595  $tmp_array['ref_handling'] = $row->ref_handling;
596 
597  $conditions[] = $tmp_array;
598  unset($tmp_array);
599  }
600 
601  $ilBench->stop("ilConditionHandler", "getConditionsOfTarget");
602 
603  return $conditions ? $conditions : array();
604  }
605 
606  function _getCondition($a_id)
607  {
608  global $ilDB;
609 
610  $query = "SELECT * FROM conditions ".
611  "WHERE condition_id = ".$ilDB->quote($a_id,'integer');
612 
613  $res = $ilDB->query($query);
614  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
615  {
616  $tmp_array['id'] = $row->condition_id;
617  $tmp_array['target_ref_id'] = $row->target_ref_id;
618  $tmp_array['target_obj_id'] = $row->target_obj_id;
619  $tmp_array['target_type'] = $row->target_type;
620  $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
621  $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
622  $tmp_array['trigger_type'] = $row->trigger_type;
623  $tmp_array['operator'] = $row->operator;
624  $tmp_array['value'] = $row->value;
625  $tmp_array['ref_handling'] = $row->ref_handling;
626 
627  return $tmp_array;
628  }
629  return false;
630  }
631 
632 
633 
639  function _checkCondition($a_id,$a_usr_id = 0)
640  {
641  global $ilUser;
642 
643  $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
644 
645  $condition = ilConditionHandler::_getCondition($a_id);
646 
647  switch($condition['trigger_type'])
648  {
649  case "tst":
650  include_once './Modules/Test/classes/class.ilObjTestAccess.php';
651  return ilObjTestAccess::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
652 
653  case "crs":
654  include_once './Modules/Course/classes/class.ilObjCourse.php';
655  return ilObjCourse::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
656 
657  case 'exc':
658  include_once './Modules/Exercise/classes/class.ilObjExercise.php';
659  return ilObjExercise::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
660 
661  case 'crsg':
662  include_once './Modules/Course/classes/class.ilObjCourseGrouping.php';
663  return ilObjCourseGrouping::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
664 
665  case 'sahs':
666  include_once './Services/Tracking/classes/class.ilLPStatusWrapper.php';
667  return in_array($a_usr_id,$completed = ilLPStatusWrapper::_getCompleted($condition['trigger_obj_id']));
668 
669  case 'svy':
670  include_once './Modules/Survey/classes/class.ilObjSurvey.php';
671  return ilObjSurvey::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
672 
673  default:
674  return false;
675 
676  }
677 
678  }
679 
683  function _checkAllConditionsOfTarget($a_target_ref_id,$a_target_id, $a_target_type = "",$a_usr_id = 0)
684  {
685  global $ilBench,$ilUser,$tree;
686 
687  $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
688 
689  foreach(ilConditionHandler::_getConditionsOfTarget($a_target_ref_id,$a_target_id, $a_target_type) as $condition)
690  {
691  if($tree->isDeleted($condition['trigger_ref_id']))
692  {
693  continue;
694  }
695 
696  $ilBench->start("ilConditionHandler", "checkCondition");
697  $check = ilConditionHandler::_checkCondition($condition['id'],$a_usr_id);
698  $ilBench->stop("ilConditionHandler", "checkCondition");
699 
700  include_once './Services/Container/classes/class.ilMemberViewSettings.php';
701  if(!$check and !ilMemberViewSettings::getInstance()->isActive())
702  {
703  return false;
704  }
705  }
706  return true;
707  }
708 
709  // PRIVATE
710  function validate()
711  {
712  global $ilDB;
713 
714  // check if obj_id is already assigned
715  $trigger_obj =& ilObjectFactory::getInstanceByRefId($this->getTriggerRefId());
716  $target_obj =& ilObjectFactory::getInstanceByRefId($this->getTargetRefId());
717 
718 
719  $query = "SELECT * FROM conditions WHERE ".
720  "trigger_ref_id = ".$ilDB->quote($trigger_obj->getId(),'integer')." ".
721  "AND target_ref_id = ".$ilDB->quote($target_obj->getId(),'integer');
722 
723  $res = $this->db->query($query);
724  if($res->numRows() > 1)
725  {
726  $this->setErrorMessage($this->lng->txt('condition_already_assigned'));
727 
728  unset($trigger_obj);
729  unset($target_obj);
730  return false;
731  }
732 
733  // check for circle
734  $this->target_obj_id = $target_obj->getId();
735  if($this->checkCircle($this->getTargetRefId(),$target_obj->getId()))
736  {
737  $this->setErrorMessage($this->lng->txt('condition_circle_created'));
738 
739  unset($trigger_obj);
740  unset($target_obj);
741  return false;
742  }
743  return true;
744  }
745 
746  function checkCircle($a_ref_id,$a_obj_id)
747  {
748  foreach(ilConditionHandler::_getConditionsOfTarget($a_ref_id,$a_obj_id) as $condition)
749  {
750  if($condition['trigger_obj_id'] == $this->target_obj_id and $condition['operator'] == $this->getOperator())
751  {
752  $this->circle = true;
753  break;
754  }
755  else
756  {
757  $this->checkCircle($condition['trigger_ref_id'],$condition['trigger_obj_id']);
758  }
759  }
760  return $this->circle;
761  }
762 }
763 
764 ?>