• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilConditionHandler.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004     +-----------------------------------------------------------------------------+
00005     | ILIAS open source                                                           |
00006         +-----------------------------------------------------------------------------+
00007     | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00008     |                                                                             |
00009     | This program is free software; you can redistribute it and/or               |
00010     | modify it under the terms of the GNU General Public License                 |
00011     | as published by the Free Software Foundation; either version 2              |
00012     | of the License, or (at your option) any later version.                      |
00013     |                                                                             |
00014     | This program is distributed in the hope that it will be useful,             |
00015     | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00016     | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00017     | GNU General Public License for more details.                                |
00018     |                                                                             |
00019     | You should have received a copy of the GNU General Public License           |
00020     | along with this program; if not, write to the Free Software                 |
00021     | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00022     +-----------------------------------------------------------------------------+
00023 */
00024 
00086 class ilConditionHandler
00087 {
00088         const UNIQUE_CONDITIONS = 1;
00089         const SHARED_CONDITIONS = 0;
00090         
00091         var $db;
00092         var $lng;
00093         
00094 
00095         var $error_message;
00096 
00097         var $target_obj_id;
00098         var $target_ref_id;
00099         var $target_type;
00100         var $trigger_obj_id;
00101         var $trigger_ref_id;
00102         var $trigger_type;
00103         var $operator;
00104         var $value;
00105         var $validation;
00106 
00107         var $conditions;
00108 
00109 
00114         function ilConditionHandler()
00115         {
00116                 global $ilDB,$lng;
00117 
00118                 $this->db =& $ilDB;
00119                 $this->lng =& $lng;
00120                 $this->validation = true;
00121         }
00122 
00131         public static function _isReferenceHandlingOptional($a_type)
00132         {
00133                 switch($a_type)
00134                 {
00135                         case 'st':
00136                                 return true;
00137                         
00138                         default:
00139                                 return false;
00140                 }
00141         }
00142         
00153         public static function _adjustMovedObjectConditions($a_ref_id)
00154         {
00155                 global $tree;
00156                 
00157                 if($tree->checkForParentType($a_ref_id,'crs'))
00158                 {
00159                         // Nothing to do
00160                         return true;
00161                 }
00162                 
00163                 // Need another implementation that has better performance
00164                 $childs = $tree->getSubTree($tree->getNodeData($a_ref_id),false);
00165                 $conditions = self::_getDistinctTargetRefIds();
00166                 
00167                 foreach(array_intersect($conditions,$childs) as $target_ref)
00168                 {
00169                         if(!$tree->checkForParentType($target_ref,'crs'))
00170                         {
00171                                 self::_deleteTargetConditionsByRefId($target_ref);
00172                         }
00173                 }
00174                 return true;
00175         }
00176         
00184         public static function _getDistinctTargetRefIds()
00185         {
00186                 global $ilDB;
00187                 
00188                 $query = "SELECT DISTINCT target_ref_id AS ref FROM conditions ";
00189                 $res = $ilDB->query($query);
00190                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00191                 {
00192                         $ref_ids[] = $row->ref;
00193                 }
00194                 return $ref_ids ? $ref_ids : array();
00195         }
00196         
00207         public static function _deleteTargetConditionsByRefId($a_target_ref_id)
00208         {
00209                 global $ilDB;
00210                 
00211                 $query = "DELETE FROM conditions ".
00212                         "WHERE target_ref_id = ".$ilDB->quote($a_target_ref_id)." ".
00213                         "AND target_type != 'st' ";
00214                 $ilDB->query($query);
00215                 return true;
00216         }
00217 
00225         public function setReferenceHandlingType($a_type)
00226         {
00227                 return $this->condition_reference_type = $a_type;               
00228         }
00229         
00236         public function getReferenceHandlingType()
00237         {
00238                 return $this->condition_reference_type;
00239         }
00240 
00241         // SET GET
00242         function setErrorMessage($a_msg)
00243         {
00244                 $this->error_message = $a_msg;
00245         }
00246         function getErrorMessage()
00247         {
00248                 return $this->error_message;
00249         }
00250 
00254         function setTargetRefId($a_target_ref_id)
00255         {
00256                 return $this->target_ref_id = $a_target_ref_id;
00257         }
00258         
00262         function getTargetRefId()
00263         {
00264                 return $this->target_ref_id;
00265         }
00266         
00270         function setTargetObjId($a_target_obj_id)
00271         {
00272                 return $this->target_obj_id = $a_target_obj_id;
00273         }
00274         
00278         function getTargetObjId()
00279         {
00280                 return $this->target_obj_id;
00281         }
00282 
00286         function setTargetType($a_target_type)
00287         {
00288                 return $this->target_type = $a_target_type;
00289         }
00290         
00294         function getTargetType()
00295         {
00296                 return $this->target_type;
00297         }
00298         
00302         function setTriggerRefId($a_trigger_ref_id)
00303         {
00304                 return $this->trigger_ref_id = $a_trigger_ref_id;
00305         }
00306         
00310         function getTriggerRefId()
00311         {
00312                 return $this->trigger_ref_id;
00313         }
00314 
00318         function setTriggerObjId($a_trigger_obj_id)
00319         {
00320                 return $this->trigger_obj_id = $a_trigger_obj_id;
00321         }
00322         
00326         function getTriggerObjId()
00327         {
00328                 return $this->trigger_obj_id;
00329         }
00330 
00334         function setTriggerType($a_trigger_type)
00335         {
00336                 return $this->trigger_type = $a_trigger_type;
00337         }
00338         
00342         function getTriggerType()
00343         {
00344                 return $this->trigger_type;
00345         }
00346         
00350         function setOperator($a_operator)
00351         {
00352                 return $this->operator = $a_operator;
00353         }
00354         
00358         function getOperator()
00359         {
00360                 return $this->operator;
00361         }
00362         
00366         function setValue($a_value)
00367         {
00368                 return $this->value = $a_value;
00369         }
00370         
00374         function getValue()
00375         {
00376                 return $this->value;
00377         }
00378         
00382         function enableAutomaticValidation($a_validate = true)
00383         {
00384                 $this->validation = $a_validate;
00385         }
00386 
00392         function getTriggerTypes()
00393         {
00394                 return array('crs','exc','tst','sahs', 'svy');
00395         }
00396 
00397 
00398         function getOperatorsByTargetType($a_type)
00399         {
00400                 switch($a_type)
00401                 {
00402                         case 'crs':
00403                         case 'exc':
00404                                 return array('passed');
00405 
00406                         case 'tst':
00407                                 return array('passed','finished','not_finished');
00408 
00409                         case 'crsg':
00410                                 return array('not_member');
00411 
00412                         case 'sahs':
00413                                 return array('finished');
00414 
00415                         case 'svy':
00416                                 return array('finished');
00417 
00418                         default:
00419                                 return array();
00420                 }
00421         }
00422 
00428         function storeCondition()
00429         {
00430                 global $ilDB;
00431                 
00432                 // first insert, then validate: it's easier to check for circles if the new condition is in the db table
00433                 $query = 'INSERT INTO conditions '.
00434                         "VALUES('0',".$ilDB->quote($this->getTargetRefId()).",".
00435                         $ilDB->quote($this->getTargetObjId()).",".$ilDB->quote($this->getTargetType()).",".
00436                         $ilDB->quote($this->getTriggerRefId()).",".$ilDB->quote($this->getTriggerObjId()).",".
00437                         $ilDB->quote($this->getTriggerType()).",".
00438                         $ilDB->quote($this->getOperator()).",".$ilDB->quote($this->getValue()).", ".
00439                         $ilDB->quote($this->getReferenceHandlingType()).')';
00440 
00441                 $res = $this->db->query($query);
00442 
00443                 $query = "SELECT LAST_INSERT_ID() AS last FROM conditions";
00444                 $res = $this->db->query($query);
00445                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00446                 {
00447                         $last_id = $row->last;
00448                 }
00449                 
00450                 if ($this->validation && !$this->validate())
00451                 {
00452                         $this->deleteCondition($last_id);
00453                         return false;
00454                 }
00455                 return true;
00456         }
00457 
00458         function checkExists()
00459         {
00460                 global $ilDB;
00461                 
00462                 $query = "SELECT * FROM conditions ".
00463                         "WHERE target_ref_id = ".$ilDB->quote($this->getTargetRefId())." ".
00464                         "AND target_obj_id = ".$ilDB->quote($this->getTargetObjId())." ".
00465                         "AND trigger_ref_id = ".$ilDB->quote($this->getTriggerRefId())." ".
00466                         "AND trigger_obj_id = ".$ilDB->quote($this->getTriggerObjId())." ".
00467                         "AND operator = ".$ilDB->quote($this->getOperator());
00468 
00469                 $res = $this->db->query($query);
00470 
00471                 return $res->numRows() ? true : false;
00472         }
00476         function updateCondition($a_id)
00477         {
00478                 global $ilDB;
00479                 
00480                 $query = "UPDATE conditions SET ".
00481                         "target_ref_id = ".$ilDB->quote($this->getTargetRefId()).", ".
00482                         "operator = ".$ilDB->quote($this->getOperator()).", ".
00483                         "value = ".$ilDB->quote($this->getValue()).", ".
00484                         "ref_handling = ".$this->db->quote($this->getReferenceHandlingType())." ".
00485                         "WHERE id = ".$ilDB->quote($a_id);
00486 
00487                 $res = $this->db->query($query);
00488 
00489                 return true;
00490         }
00491 
00492 
00497         function delete($a_ref_id)
00498         {
00499                 global $ilDB;
00500                 
00501                 $query = "DELETE FROM conditions WHERE ".
00502                         "target_ref_id = ".$ilDB->quote($a_ref_id)." ".
00503                         "OR trigger_ref_id = ".$ilDB->quote($a_ref_id);
00504 
00505                 $res = $this->db->query($query);
00506 
00507                 return true;
00508         }
00513         function deleteByObjId($a_obj_id)
00514         {
00515                 global $ilDB;
00516                 
00517                 $query = "DELETE FROM conditions WHERE ".
00518                         "target_obj_id = ".$ilDB->quote($a_obj_id)." ".
00519                         "OR trigger_obj_id = ".$ilDB->quote($a_obj_id);
00520 
00521                 $res = $this->db->query($query);
00522 
00523                 return true;
00524         }
00525 
00529         function deleteCondition($a_id)
00530         {
00531                 global $ilDB;
00532 
00533                 $query = "DELETE FROM conditions ".
00534                         "WHERE id = ".$ilDB->quote($a_id);
00535 
00536                 $res = $ilDB->query($query);
00537 
00538                 return true;
00539         }
00540         
00545         function _getConditionsOfTrigger($a_trigger_obj_type, $a_trigger_id)
00546         {
00547                 global $ilDB;
00548 
00549                 $query = "SELECT * FROM conditions ".
00550                         "WHERE trigger_obj_id = ".$ilDB->quote($a_trigger_id)." ".
00551                         " AND trigger_type = ".$ilDB->quote($a_trigger_obj_type)." ";
00552 
00553                 $res = $ilDB->query($query);
00554                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00555                 {
00556                         $tmp_array['id']                        = $row->id;
00557                         $tmp_array['target_ref_id'] = $row->target_ref_id;
00558                         $tmp_array['target_obj_id'] = $row->target_obj_id;
00559                         $tmp_array['target_type']       = $row->target_type;
00560                         $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00561                         $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00562                         $tmp_array['trigger_type']      = $row->trigger_type;
00563                         $tmp_array['operator']          = $row->operator;
00564                         $tmp_array['value']                     = $row->value;
00565                         $tmp_array['ref_handling']  = $row->ref_handling;
00566 
00567                         $conditions[] = $tmp_array;
00568                         unset($tmp_array);
00569                 }
00570 
00571                 return $conditions ? $conditions : array();
00572         }
00573 
00584         function _getConditionsOfTarget($a_target_ref_id,$a_target_obj_id, $a_target_type = "")
00585         {
00586                 global $ilDB, $ilBench;
00587 
00588                 $ilBench->start("ilConditionHandler", "getConditionsOfTarget");
00589 
00590                 if ($a_target_type == "")
00591                 {
00592                         $a_target_type = ilObject::_lookupType($a_target_obj_id);
00593                 }
00594 
00595                 $query = "SELECT * FROM conditions ".
00596                         "WHERE target_obj_id = ".$ilDB->quote($a_target_obj_id)." ".
00597                         " AND target_type = ".$ilDB->quote($a_target_type);
00598 
00599                 $res = $ilDB->query($query);
00600                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00601                 {
00602                         if($row->ref_handling == self::UNIQUE_CONDITIONS)
00603                         {
00604                                 if($row->target_ref_id != $a_target_ref_id)
00605                                 {
00606                                         continue;
00607                                 }
00608                         }
00609                         
00610                         $tmp_array['id']                        = $row->id;
00611                         $tmp_array['target_ref_id'] = $row->target_ref_id;
00612                         $tmp_array['target_obj_id'] = $row->target_obj_id;
00613                         $tmp_array['target_type']       = $row->target_type;
00614                         $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00615                         $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00616                         $tmp_array['trigger_type']      = $row->trigger_type;
00617                         $tmp_array['operator']          = $row->operator;
00618                         $tmp_array['value']                     = $row->value;
00619                         $tmp_array['ref_handling']  = $row->ref_handling;
00620 
00621                         $conditions[] = $tmp_array;
00622                         unset($tmp_array);
00623                 }
00624 
00625                 $ilBench->stop("ilConditionHandler", "getConditionsOfTarget");
00626 
00627                 return $conditions ? $conditions : array();
00628         }
00629 
00630         function _getCondition($a_id)
00631         {
00632                 global $ilDB;
00633 
00634                 $query = "SELECT * FROM conditions ".
00635                         "WHERE id = ".$ilDB->quote($a_id);
00636 
00637                 $res = $ilDB->query($query);
00638                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00639                 {
00640                         $tmp_array['id']                        = $row->id;
00641                         $tmp_array['target_ref_id'] = $row->target_ref_id;
00642                         $tmp_array['target_obj_id'] = $row->target_obj_id;
00643                         $tmp_array['target_type']       = $row->target_type;
00644                         $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00645                         $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00646                         $tmp_array['trigger_type']      = $row->trigger_type;
00647                         $tmp_array['operator']          = $row->operator;
00648                         $tmp_array['value']                     = $row->value;
00649                         $tmp_array['ref_handling']  = $row->ref_handling;
00650 
00651                         return $tmp_array;
00652                 }
00653                 return false;
00654         }
00655 
00656 
00657 
00663         function _checkCondition($a_id)
00664         {
00665                 $condition = ilConditionHandler::_getCondition($a_id);
00666 
00667                 switch($condition['trigger_type'])
00668                 {
00669                         case "tst":
00670                                 include_once './Modules/Test/classes/class.ilObjTestAccess.php';
00671                                 return ilObjTestAccess::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00672 
00673                         case "crs":
00674                                 include_once './Modules/Course/classes/class.ilObjCourse.php';
00675                                 return ilObjCourse::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00676 
00677                         case 'exc':
00678                                 include_once './Modules/Exercise/classes/class.ilObjExercise.php';
00679                                 return ilObjExercise::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00680 
00681                         case 'crsg':
00682                                 include_once './Modules/Course/classes/class.ilObjCourseGrouping.php';
00683                                 return ilObjCourseGrouping::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00684 
00685                         case 'sahs':
00686                                 global $ilUser;
00687 
00688                                 include_once './Services/Tracking/classes/class.ilLPStatusWrapper.php';
00689                                 return in_array($ilUser->getId(),$completed = ilLPStatusWrapper::_getCompleted($condition['trigger_obj_id']));
00690 
00691                         case 'svy':
00692                                 include_once './Modules/Survey/classes/class.ilObjSurvey.php';
00693                                 return ilObjSurvey::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00694 
00695                         default:
00696                                 return false;
00697 
00698                 }
00699 
00700         }
00701 
00705         function _checkAllConditionsOfTarget($a_target_ref_id,$a_target_id, $a_target_type = "")
00706         {
00707                 global $ilBench;
00708 
00709                 foreach(ilConditionHandler::_getConditionsOfTarget($a_target_ref_id,$a_target_id, $a_target_type) as $condition)
00710                 {
00711                         $ilBench->start("ilConditionHandler", "checkCondition");
00712                         $check = ilConditionHandler::_checkCondition($condition['id']);
00713                         $ilBench->stop("ilConditionHandler", "checkCondition");
00714 
00715                         if(!$check)
00716                         {
00717                                 return false;
00718                         }
00719                 }
00720                 return true;
00721         }
00722 
00723         // PRIVATE
00724         function validate()
00725         {
00726                 global $ilDB;
00727                 
00728                 // check if obj_id is already assigned
00729                 $trigger_obj =& ilObjectFactory::getInstanceByRefId($this->getTriggerRefId());
00730                 $target_obj =& ilObjectFactory::getInstanceByRefId($this->getTargetRefId());
00731 
00732 
00733                 $query = "SELECT * FROM conditions WHERE ".
00734                         "trigger_ref_id = ".$ilDB->quote($trigger_obj->getId())." ".
00735                         "AND target_ref_id = ".$ilDB->quote($target_obj->getId());
00736 
00737                 $res = $this->db->query($query);
00738                 if($res->numRows() > 1)
00739                 {
00740                         $this->setErrorMessage($this->lng->txt('condition_already_assigned'));
00741 
00742                         unset($trigger_obj);
00743                         unset($target_obj);
00744                         return false;
00745                 }
00746 
00747                 // check for circle
00748                 $this->target_obj_id = $target_obj->getId();
00749                 if($this->checkCircle($this->getTargetRefId(),$target_obj->getId()))
00750                 {
00751                         $this->setErrorMessage($this->lng->txt('condition_circle_created'));
00752                         
00753                         unset($trigger_obj);
00754                         unset($target_obj);
00755                         return false;
00756                 }                       
00757                 return true;
00758         }
00759 
00760         function checkCircle($a_ref_id,$a_obj_id)
00761         {
00762                 foreach(ilConditionHandler::_getConditionsOfTarget($a_ref_id,$a_obj_id) as $condition)
00763                 {
00764                         if($condition['trigger_obj_id'] == $this->target_obj_id and $condition['operator'] == $this->getOperator())
00765                         {
00766                                 $this->circle = true;
00767                                 break;
00768                         }
00769                         else
00770                         {
00771                                 $this->checkCircle($condition['trigger_ref_id'],$condition['trigger_obj_id']);
00772                         }
00773                 }
00774                 return $this->circle;
00775         }
00776 }
00777 
00778 ?>

Generated on Fri Dec 13 2013 17:56:47 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1