• Main Page
  • Related Pages
  • 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         var $db;
00089         var $lng;
00090         
00091 
00092         var $error_message;
00093 
00094         var $target_obj_id;
00095         var $target_ref_id;
00096         var $target_type;
00097         var $trigger_obj_id;
00098         var $trigger_ref_id;
00099         var $trigger_type;
00100         var $operator;
00101         var $value;
00102         var $validation;
00103 
00104         var $conditions;
00105 
00106 
00111         function ilConditionHandler()
00112         {
00113                 global $ilDB,$lng;
00114 
00115                 $this->db =& $ilDB;
00116                 $this->lng =& $lng;
00117                 $this->validation = true;
00118         }
00119 
00120         // SET GET
00121         function setErrorMessage($a_msg)
00122         {
00123                 $this->error_message = $a_msg;
00124         }
00125         function getErrorMessage()
00126         {
00127                 return $this->error_message;
00128         }
00129 
00133         function setTargetRefId($a_target_ref_id)
00134         {
00135                 return $this->target_ref_id = $a_target_ref_id;
00136         }
00137         
00141         function getTargetRefId()
00142         {
00143                 return $this->target_ref_id;
00144         }
00145         
00149         function setTargetObjId($a_target_obj_id)
00150         {
00151                 return $this->target_obj_id = $a_target_obj_id;
00152         }
00153         
00157         function getTargetObjId()
00158         {
00159                 return $this->target_obj_id;
00160         }
00161 
00165         function setTargetType($a_target_type)
00166         {
00167                 return $this->target_type = $a_target_type;
00168         }
00169         
00173         function getTargetType()
00174         {
00175                 return $this->target_type;
00176         }
00177         
00181         function setTriggerRefId($a_trigger_ref_id)
00182         {
00183                 return $this->trigger_ref_id = $a_trigger_ref_id;
00184         }
00185         
00189         function getTriggerRefId()
00190         {
00191                 return $this->trigger_ref_id;
00192         }
00193 
00197         function setTriggerObjId($a_trigger_obj_id)
00198         {
00199                 return $this->trigger_obj_id = $a_trigger_obj_id;
00200         }
00201         
00205         function getTriggerObjId()
00206         {
00207                 return $this->trigger_obj_id;
00208         }
00209 
00213         function setTriggerType($a_trigger_type)
00214         {
00215                 return $this->trigger_type = $a_trigger_type;
00216         }
00217         
00221         function getTriggerType()
00222         {
00223                 return $this->trigger_type;
00224         }
00225         
00229         function setOperator($a_operator)
00230         {
00231                 return $this->operator = $a_operator;
00232         }
00233         
00237         function getOperator()
00238         {
00239                 return $this->operator;
00240         }
00241         
00245         function setValue($a_value)
00246         {
00247                 return $this->value = $a_value;
00248         }
00249         
00253         function getValue()
00254         {
00255                 return $this->value;
00256         }
00257         
00261         function enableAutomaticValidation($a_validate = true)
00262         {
00263                 $this->validation = $a_validate;
00264         }
00265 
00271         function getTriggerTypes()
00272         {
00273                 return array('crs','exc','tst');
00274         }
00275 
00276 
00277         function getOperatorsByTargetType($a_type)
00278         {
00279                 switch($a_type)
00280                 {
00281                         case 'crs':
00282                         case 'exc':
00283                                 return array('passed');
00284 
00285                         case 'tst':
00286                                 return array('passed','finished','not_finished');
00287 
00288                         case 'crsg':
00289                                 return array('not_member');
00290 
00291                         default:
00292                                 return array();
00293                 }
00294         }
00295 
00301         function storeCondition()
00302         {
00303                 // first insert, then validate: it's easier to check for circles if the new condition is in the db table
00304                 $query = 'INSERT INTO conditions '.
00305                         "VALUES('0','".$this->getTargetRefId()."','".$this->getTargetObjId()."','".$this->getTargetType()."','".
00306                         $this->getTriggerRefId()."','".$this->getTriggerObjId()."','".$this->getTriggerType()."','".
00307                         $this->getOperator()."','".$this->getValue()."')";
00308 
00309                 $res = $this->db->query($query);
00310 
00311                 $query = "SELECT LAST_INSERT_ID() AS last FROM conditions";
00312                 $res = $this->db->query($query);
00313                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00314                 {
00315                         $last_id = $row->last;
00316                 }
00317                 
00318                 if ($this->validation && !$this->validate())
00319                 {
00320                         $this->deleteCondition($last_id);
00321                         return false;
00322                 }
00323                 return true;
00324         }
00325 
00326         function checkExists()
00327         {
00328                 $query = "SELECT * FROM conditions ".
00329                         "WHERE target_ref_id = '".$this->getTargetRefId()."' ".
00330                         "AND target_obj_id = '".$this->getTargetObjId()."' ".
00331                         "AND trigger_ref_id = '".$this->getTriggerRefId()."' ".
00332                         "AND trigger_obj_id = '".$this->getTriggerObjId()."' ".
00333                         "AND operator = '".$this->getOperator()."'";
00334 
00335                 $res = $this->db->query($query);
00336 
00337                 return $res->numRows() ? true : false;
00338         }
00342         function updateCondition($a_id)
00343         {
00344                 $query = "UPDATE conditions SET ".
00345                         "operator = '".$this->getOperator()."', ".
00346                         "value = '".$this->getValue()."' ".
00347                         "WHERE id = '".$a_id."'";
00348 
00349                 $res = $this->db->query($query);
00350 
00351                 return true;
00352         }
00353 
00354 
00359         function delete($a_ref_id)
00360         {
00361                 $query = "DELETE FROM conditions WHERE ".
00362                         "target_ref_id = '".$a_ref_id."' ".
00363                         "OR trigger_ref_id = '".$a_ref_id."'";
00364 
00365                 $res = $this->db->query($query);
00366 
00367                 return true;
00368         }
00373         function deleteByObjId($a_obj_id)
00374         {
00375                 $query = "DELETE FROM conditions WHERE ".
00376                         "target_obj_id = '".$a_obj_id."' ".
00377                         "OR trigger_obj_id = '".$a_obj_id."'";
00378 
00379                 $res = $this->db->query($query);
00380 
00381                 return true;
00382         }
00383 
00387         function deleteCondition($a_id)
00388         {
00389                 global $ilDB;
00390 
00391                 $query = "DELETE FROM conditions ".
00392                         "WHERE id = '".$a_id."'";
00393 
00394                 $res = $ilDB->query($query);
00395 
00396                 return true;
00397         }
00398 
00403         function _getConditionsOfTrigger($a_trigger_obj_type, $a_trigger_id)
00404         {
00405                 global $ilDB;
00406         
00407                 $query = "SELECT * FROM conditions ".
00408                         "WHERE trigger_obj_id = '".$a_trigger_id."'".
00409                         " AND trigger_type = '".$a_trigger_obj_type."'";
00410 
00411                 $res = $ilDB->query($query);
00412                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00413                 {
00414                         $tmp_array['id']                        = $row->id;
00415                         $tmp_array['target_ref_id'] = $row->target_ref_id;
00416                         $tmp_array['target_obj_id'] = $row->target_obj_id;
00417                         $tmp_array['target_type']       = $row->target_type;
00418                         $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00419                         $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00420                         $tmp_array['trigger_type']      = $row->trigger_type;
00421                         $tmp_array['operator']          = $row->operator;
00422                         $tmp_array['value']                     = $row->value;
00423 
00424                         $conditions[] = $tmp_array;
00425                         unset($tmp_array);
00426                 }
00427                 
00428                 return $conditions ? $conditions : array();
00429         }
00430 
00441         function _getConditionsOfTarget($a_target_obj_id, $a_target_type = "")
00442         {
00443                 global $ilDB;
00444                 
00445                 if ($a_target_type == "")
00446                 {
00447                         $a_target_type = ilObject::_lookupType($a_target_obj_id);
00448                 }
00449 
00450                 $query = "SELECT * FROM conditions ".
00451                         "WHERE target_obj_id = '".$a_target_obj_id."'".
00452                         " AND target_type = '".$a_target_type."'";
00453 
00454                 $res = $ilDB->query($query);
00455                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00456                 {
00457                         $tmp_array['id']                        = $row->id;
00458                         $tmp_array['target_ref_id'] = $row->target_ref_id;
00459                         $tmp_array['target_obj_id'] = $row->target_obj_id;
00460                         $tmp_array['target_type']       = $row->target_type;
00461                         $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00462                         $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00463                         $tmp_array['trigger_type']      = $row->trigger_type;
00464                         $tmp_array['operator']          = $row->operator;
00465                         $tmp_array['value']                     = $row->value;
00466 
00467                         $conditions[] = $tmp_array;
00468                         unset($tmp_array);
00469                 }
00470                 
00471                 return $conditions ? $conditions : array();
00472         }
00473 
00474         function _getCondition($a_id)
00475         {
00476                 global $ilDB;
00477 
00478                 $query = "SELECT * FROM conditions ".
00479                         "WHERE id = '".$a_id."'";
00480 
00481                 $res = $ilDB->query($query);
00482                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00483                 {
00484                         $tmp_array['id']                        = $row->id;
00485                         $tmp_array['target_ref_id'] = $row->target_ref_id;
00486                         $tmp_array['target_obj_id'] = $row->target_obj_id;
00487                         $tmp_array['target_type']       = $row->target_type;
00488                         $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00489                         $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00490                         $tmp_array['trigger_type']      = $row->trigger_type;
00491                         $tmp_array['operator']          = $row->operator;
00492                         $tmp_array['value']                     = $row->value;
00493 
00494                         return $tmp_array;
00495                 }
00496                 return false;
00497         }
00498 
00499 
00500 
00506         function _checkCondition($a_id)
00507         {
00508                 $condition = ilConditionHandler::_getCondition($a_id);
00509 
00510                 switch($condition['trigger_type'])
00511                 {
00512                         case "tst":
00513                                 include_once './assessment/classes/class.ilObjTest.php';
00514                                 return ilObjTest::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00515 
00516                         case "qst":
00517                                 return ilObjCourse::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00518 
00519                         case "crs":
00520                                 include_once './course/classes/class.ilObjCourse.php';
00521                                 return ilObjCourse::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00522 
00523                         case 'exc':
00524                                 include_once './classes/class.ilObjExercise.php';
00525 
00526                                 return ilObjExercise::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00527 
00528                         case 'crsg':
00529                                 include_once './course/classes/class.ilObjCourseGrouping.php';
00530 
00531                                 return ilObjCourseGrouping::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00532 
00533                         default:
00534                                 return false;
00535 
00536                 }
00537 
00538         }
00539 
00543         function _checkAllConditionsOfTarget($a_target_id, $a_target_type = "")
00544         {
00545                 foreach(ilConditionHandler::_getConditionsOfTarget($a_target_id, $a_target_type) as $condition)
00546                 {
00547                         if(!ilConditionHandler::_checkCondition($condition['id']))
00548                         {
00549                                 return false;
00550                         }
00551                 }
00552                 return true;
00553         }
00554 
00555         // PRIVATE
00556         function validate()
00557         {
00558                 // check if obj_id is already assigned
00559                 $trigger_obj =& ilObjectFactory::getInstanceByRefId($this->getTriggerRefId());
00560                 $target_obj =& ilObjectFactory::getInstanceByRefId($this->getTargetRefId());
00561                 
00562 
00563                 $query = "SELECT * FROM conditions WHERE ".
00564                         "trigger_obj_id = '".$trigger_obj->getId()."' ".
00565                         "AND target_obj_id = '".$target_obj->getId()."'";
00566 
00567                 $res = $this->db->query($query);
00568                 if($res->numRows() > 1)
00569                 {
00570                         $this->setErrorMessage($this->lng->txt('condition_already_assigned'));
00571 
00572                         unset($trigger_obj);
00573                         unset($target_obj);
00574                         return false;
00575                 }
00576 
00577                 // check for circle
00578                 $this->target_obj_id = $target_obj->getId();
00579                 if($this->checkCircle($target_obj->getId()))
00580                 {
00581                         $this->setErrorMessage($this->lng->txt('condition_circle_created'));
00582                         
00583                         unset($trigger_obj);
00584                         unset($target_obj);
00585                         return false;
00586                 }                       
00587                 return true;
00588         }
00589 
00590         function checkCircle($a_obj_id)
00591         {
00592                 // Temporally disabled
00593                 return false;
00594 
00595                 foreach(ilConditionHandler::_getConditionsOfTarget($a_obj_id) as $condition)
00596                 {
00597                         if($condition['trigger_obj_id'] == $this->target_obj_id and $condition['operator'] == $this->getOperator())
00598                         {
00599                                 $this->circle = true;
00600                                 break;
00601                         }
00602                         else
00603                         {
00604                                 $this->checkCircle($condition['trigger_obj_id']);
00605                         }
00606                 }
00607                 return $this->circle;
00608         }
00609 }
00610 
00611 ?>

Generated on Fri Dec 13 2013 09:06:33 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1