ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilConditionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  +-----------------------------------------------------------------------------+
5  | ILIAS open source |
6  +-----------------------------------------------------------------------------+
7  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
8  | |
9  | This program is free software; you can redistribute it and/or |
10  | modify it under the terms of the GNU General Public License |
11  | as published by the Free Software Foundation; either version 2 |
12  | of the License, or (at your option) any later version. |
13  | |
14  | This program is distributed in the hope that it will be useful, |
15  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17  | GNU General Public License for more details. |
18  | |
19  | You should have received a copy of the GNU General Public License |
20  | along with this program; if not, write to the Free Software |
21  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22  +-----------------------------------------------------------------------------+
23 */
24 
87 {
88  const UNIQUE_CONDITIONS = 1;
89  const SHARED_CONDITIONS = 0;
90 
91  var $db;
92  var $lng;
93 
94 
96 
104  var $value;
106 
108 
109 
115  {
116  global $ilDB,$lng;
117 
118  $this->db =& $ilDB;
119  $this->lng =& $lng;
120  $this->validation = true;
121  }
122 
131  public static function _isReferenceHandlingOptional($a_type)
132  {
133  switch($a_type)
134  {
135  case 'st':
136  return true;
137 
138  default:
139  return false;
140  }
141  }
142 
153  public static function _adjustMovedObjectConditions($a_ref_id)
154  {
155  global $tree;
156 
157  if($tree->checkForParentType($a_ref_id,'crs'))
158  {
159  // Nothing to do
160  return true;
161  }
162 
163  // Need another implementation that has better performance
164  $childs = $tree->getSubTree($tree->getNodeData($a_ref_id),false);
166 
167  foreach(array_intersect($conditions,$childs) as $target_ref)
168  {
169  if(!$tree->checkForParentType($target_ref,'crs'))
170  {
172  }
173  }
174  return true;
175  }
176 
184  public static function _getDistinctTargetRefIds()
185  {
186  global $ilDB;
187 
188  $query = "SELECT DISTINCT target_ref_id AS ref FROM conditions ";
189  $res = $ilDB->query($query);
190  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
191  {
192  $ref_ids[] = $row->ref;
193  }
194  return $ref_ids ? $ref_ids : array();
195  }
196 
207  public static function _deleteTargetConditionsByRefId($a_target_ref_id)
208  {
209  global $ilDB;
210 
211  $query = "DELETE FROM conditions ".
212  "WHERE target_ref_id = ".$ilDB->quote($a_target_ref_id)." ".
213  "AND target_type != 'st' ";
214  $ilDB->query($query);
215  return true;
216  }
217 
225  public function setReferenceHandlingType($a_type)
226  {
227  return $this->condition_reference_type = $a_type;
228  }
229 
236  public function getReferenceHandlingType()
237  {
238  return $this->condition_reference_type;
239  }
240 
241  // SET GET
242  function setErrorMessage($a_msg)
243  {
244  $this->error_message = $a_msg;
245  }
246  function getErrorMessage()
247  {
248  return $this->error_message;
249  }
250 
254  function setTargetRefId($a_target_ref_id)
255  {
256  return $this->target_ref_id = $a_target_ref_id;
257  }
258 
262  function getTargetRefId()
263  {
264  return $this->target_ref_id;
265  }
266 
270  function setTargetObjId($a_target_obj_id)
271  {
272  return $this->target_obj_id = $a_target_obj_id;
273  }
274 
278  function getTargetObjId()
279  {
280  return $this->target_obj_id;
281  }
282 
286  function setTargetType($a_target_type)
287  {
288  return $this->target_type = $a_target_type;
289  }
290 
294  function getTargetType()
295  {
296  return $this->target_type;
297  }
298 
302  function setTriggerRefId($a_trigger_ref_id)
303  {
304  return $this->trigger_ref_id = $a_trigger_ref_id;
305  }
306 
310  function getTriggerRefId()
311  {
312  return $this->trigger_ref_id;
313  }
314 
318  function setTriggerObjId($a_trigger_obj_id)
319  {
320  return $this->trigger_obj_id = $a_trigger_obj_id;
321  }
322 
326  function getTriggerObjId()
327  {
328  return $this->trigger_obj_id;
329  }
330 
334  function setTriggerType($a_trigger_type)
335  {
336  return $this->trigger_type = $a_trigger_type;
337  }
338 
342  function getTriggerType()
343  {
344  return $this->trigger_type;
345  }
346 
350  function setOperator($a_operator)
351  {
352  return $this->operator = $a_operator;
353  }
354 
358  function getOperator()
359  {
360  return $this->operator;
361  }
362 
366  function setValue($a_value)
367  {
368  return $this->value = $a_value;
369  }
370 
374  function getValue()
375  {
376  return $this->value;
377  }
378 
382  function enableAutomaticValidation($a_validate = true)
383  {
384  $this->validation = $a_validate;
385  }
386 
392  function getTriggerTypes()
393  {
394  return array('crs','exc','tst','sahs', 'svy');
395  }
396 
397 
398  function getOperatorsByTargetType($a_type)
399  {
400  switch($a_type)
401  {
402  case 'crs':
403  case 'exc':
404  return array('passed');
405 
406  case 'tst':
407  return array('passed','finished','not_finished');
408 
409  case 'crsg':
410  return array('not_member');
411 
412  case 'sahs':
413  return array('finished');
414 
415  case 'svy':
416  return array('finished');
417 
418  default:
419  return array();
420  }
421  }
422 
428  function storeCondition()
429  {
430  global $ilDB;
431 
432  // first insert, then validate: it's easier to check for circles if the new condition is in the db table
433  $query = 'INSERT INTO conditions '.
434  "VALUES('0',".$ilDB->quote($this->getTargetRefId()).",".
435  $ilDB->quote($this->getTargetObjId()).",".$ilDB->quote($this->getTargetType()).",".
436  $ilDB->quote($this->getTriggerRefId()).",".$ilDB->quote($this->getTriggerObjId()).",".
437  $ilDB->quote($this->getTriggerType()).",".
438  $ilDB->quote($this->getOperator()).",".$ilDB->quote($this->getValue()).", ".
439  $ilDB->quote($this->getReferenceHandlingType()).')';
440 
441  $res = $this->db->query($query);
442 
443  $query = "SELECT LAST_INSERT_ID() AS last FROM conditions";
444  $res = $this->db->query($query);
445  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
446  {
447  $last_id = $row->last;
448  }
449 
450  if ($this->validation && !$this->validate())
451  {
452  $this->deleteCondition($last_id);
453  return false;
454  }
455  return true;
456  }
457 
458  function checkExists()
459  {
460  global $ilDB;
461 
462  $query = "SELECT * FROM conditions ".
463  "WHERE target_ref_id = ".$ilDB->quote($this->getTargetRefId())." ".
464  "AND target_obj_id = ".$ilDB->quote($this->getTargetObjId())." ".
465  "AND trigger_ref_id = ".$ilDB->quote($this->getTriggerRefId())." ".
466  "AND trigger_obj_id = ".$ilDB->quote($this->getTriggerObjId())." ".
467  "AND operator = ".$ilDB->quote($this->getOperator());
468 
469  $res = $this->db->query($query);
470 
471  return $res->numRows() ? true : false;
472  }
476  function updateCondition($a_id)
477  {
478  global $ilDB;
479 
480  $query = "UPDATE conditions SET ".
481  "target_ref_id = ".$ilDB->quote($this->getTargetRefId()).", ".
482  "operator = ".$ilDB->quote($this->getOperator()).", ".
483  "value = ".$ilDB->quote($this->getValue()).", ".
484  "ref_handling = ".$this->db->quote($this->getReferenceHandlingType())." ".
485  "WHERE id = ".$ilDB->quote($a_id);
486 
487  $res = $this->db->query($query);
488 
489  return true;
490  }
491 
492 
497  function delete($a_ref_id)
498  {
499  global $ilDB;
500 
501  $query = "DELETE FROM conditions WHERE ".
502  "target_ref_id = ".$ilDB->quote($a_ref_id)." ".
503  "OR trigger_ref_id = ".$ilDB->quote($a_ref_id);
504 
505  $res = $this->db->query($query);
506 
507  return true;
508  }
513  function deleteByObjId($a_obj_id)
514  {
515  global $ilDB;
516 
517  $query = "DELETE FROM conditions WHERE ".
518  "target_obj_id = ".$ilDB->quote($a_obj_id)." ".
519  "OR trigger_obj_id = ".$ilDB->quote($a_obj_id);
520 
521  $res = $this->db->query($query);
522 
523  return true;
524  }
525 
529  function deleteCondition($a_id)
530  {
531  global $ilDB;
532 
533  $query = "DELETE FROM conditions ".
534  "WHERE id = ".$ilDB->quote($a_id);
535 
536  $res = $ilDB->query($query);
537 
538  return true;
539  }
540 
545  function _getConditionsOfTrigger($a_trigger_obj_type, $a_trigger_id)
546  {
547  global $ilDB;
548 
549  $query = "SELECT * FROM conditions ".
550  "WHERE trigger_obj_id = ".$ilDB->quote($a_trigger_id)." ".
551  " AND trigger_type = ".$ilDB->quote($a_trigger_obj_type)." ";
552 
553  $res = $ilDB->query($query);
554  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
555  {
556  $tmp_array['id'] = $row->id;
557  $tmp_array['target_ref_id'] = $row->target_ref_id;
558  $tmp_array['target_obj_id'] = $row->target_obj_id;
559  $tmp_array['target_type'] = $row->target_type;
560  $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
561  $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
562  $tmp_array['trigger_type'] = $row->trigger_type;
563  $tmp_array['operator'] = $row->operator;
564  $tmp_array['value'] = $row->value;
565  $tmp_array['ref_handling'] = $row->ref_handling;
566 
567  $conditions[] = $tmp_array;
568  unset($tmp_array);
569  }
570 
571  return $conditions ? $conditions : array();
572  }
573 
584  function _getConditionsOfTarget($a_target_ref_id,$a_target_obj_id, $a_target_type = "")
585  {
586  global $ilDB, $ilBench;
587 
588  $ilBench->start("ilConditionHandler", "getConditionsOfTarget");
589 
590  if ($a_target_type == "")
591  {
592  $a_target_type = ilObject::_lookupType($a_target_obj_id);
593  }
594 
595  $query = "SELECT * FROM conditions ".
596  "WHERE target_obj_id = ".$ilDB->quote($a_target_obj_id)." ".
597  " AND target_type = ".$ilDB->quote($a_target_type);
598 
599  $res = $ilDB->query($query);
600  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
601  {
602  if($row->ref_handling == self::UNIQUE_CONDITIONS)
603  {
604  if($row->target_ref_id != $a_target_ref_id)
605  {
606  continue;
607  }
608  }
609 
610  $tmp_array['id'] = $row->id;
611  $tmp_array['target_ref_id'] = $row->target_ref_id;
612  $tmp_array['target_obj_id'] = $row->target_obj_id;
613  $tmp_array['target_type'] = $row->target_type;
614  $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
615  $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
616  $tmp_array['trigger_type'] = $row->trigger_type;
617  $tmp_array['operator'] = $row->operator;
618  $tmp_array['value'] = $row->value;
619  $tmp_array['ref_handling'] = $row->ref_handling;
620 
621  $conditions[] = $tmp_array;
622  unset($tmp_array);
623  }
624 
625  $ilBench->stop("ilConditionHandler", "getConditionsOfTarget");
626 
627  return $conditions ? $conditions : array();
628  }
629 
630  function _getCondition($a_id)
631  {
632  global $ilDB;
633 
634  $query = "SELECT * FROM conditions ".
635  "WHERE id = ".$ilDB->quote($a_id);
636 
637  $res = $ilDB->query($query);
638  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
639  {
640  $tmp_array['id'] = $row->id;
641  $tmp_array['target_ref_id'] = $row->target_ref_id;
642  $tmp_array['target_obj_id'] = $row->target_obj_id;
643  $tmp_array['target_type'] = $row->target_type;
644  $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
645  $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
646  $tmp_array['trigger_type'] = $row->trigger_type;
647  $tmp_array['operator'] = $row->operator;
648  $tmp_array['value'] = $row->value;
649  $tmp_array['ref_handling'] = $row->ref_handling;
650 
651  return $tmp_array;
652  }
653  return false;
654  }
655 
656 
657 
663  function _checkCondition($a_id,$a_usr_id = 0)
664  {
665  global $ilUser;
666 
667  $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
668 
669  $condition = ilConditionHandler::_getCondition($a_id);
670 
671  switch($condition['trigger_type'])
672  {
673  case "tst":
674  include_once './Modules/Test/classes/class.ilObjTestAccess.php';
675  return ilObjTestAccess::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
676 
677  case "crs":
678  include_once './Modules/Course/classes/class.ilObjCourse.php';
679  return ilObjCourse::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
680 
681  case 'exc':
682  include_once './Modules/Exercise/classes/class.ilObjExercise.php';
683  return ilObjExercise::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
684 
685  case 'crsg':
686  include_once './Modules/Course/classes/class.ilObjCourseGrouping.php';
687  return ilObjCourseGrouping::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
688 
689  case 'sahs':
690  include_once './Services/Tracking/classes/class.ilLPStatusWrapper.php';
691  return in_array($a_usr_id,$completed = ilLPStatusWrapper::_getCompleted($condition['trigger_obj_id']));
692 
693  case 'svy':
694  include_once './Modules/Survey/classes/class.ilObjSurvey.php';
695  return ilObjSurvey::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value'],$a_usr_id);
696 
697  default:
698  return false;
699 
700  }
701 
702  }
703 
707  function _checkAllConditionsOfTarget($a_target_ref_id,$a_target_id, $a_target_type = "",$a_usr_id = 0)
708  {
709  global $ilBench,$ilUser;
710 
711  $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
712 
713  foreach(ilConditionHandler::_getConditionsOfTarget($a_target_ref_id,$a_target_id, $a_target_type) as $condition)
714  {
715  $ilBench->start("ilConditionHandler", "checkCondition");
716  $check = ilConditionHandler::_checkCondition($condition['id'],$a_usr_id);
717  $ilBench->stop("ilConditionHandler", "checkCondition");
718 
719  if(!$check)
720  {
721  return false;
722  }
723  }
724  return true;
725  }
726 
727  // PRIVATE
728  function validate()
729  {
730  global $ilDB;
731 
732  // check if obj_id is already assigned
733  $trigger_obj =& ilObjectFactory::getInstanceByRefId($this->getTriggerRefId());
734  $target_obj =& ilObjectFactory::getInstanceByRefId($this->getTargetRefId());
735 
736 
737  $query = "SELECT * FROM conditions WHERE ".
738  "trigger_ref_id = ".$ilDB->quote($trigger_obj->getId())." ".
739  "AND target_ref_id = ".$ilDB->quote($target_obj->getId());
740 
741  $res = $this->db->query($query);
742  if($res->numRows() > 1)
743  {
744  $this->setErrorMessage($this->lng->txt('condition_already_assigned'));
745 
746  unset($trigger_obj);
747  unset($target_obj);
748  return false;
749  }
750 
751  // check for circle
752  $this->target_obj_id = $target_obj->getId();
753  if($this->checkCircle($this->getTargetRefId(),$target_obj->getId()))
754  {
755  $this->setErrorMessage($this->lng->txt('condition_circle_created'));
756 
757  unset($trigger_obj);
758  unset($target_obj);
759  return false;
760  }
761  return true;
762  }
763 
764  function checkCircle($a_ref_id,$a_obj_id)
765  {
766  foreach(ilConditionHandler::_getConditionsOfTarget($a_ref_id,$a_obj_id) as $condition)
767  {
768  if($condition['trigger_obj_id'] == $this->target_obj_id and $condition['operator'] == $this->getOperator())
769  {
770  $this->circle = true;
771  break;
772  }
773  else
774  {
775  $this->checkCircle($condition['trigger_ref_id'],$condition['trigger_obj_id']);
776  }
777  }
778  return $this->circle;
779  }
780 }
781 
782 ?>