Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00281 function storeCondition()
00282 {
00283
00284 $query = 'INSERT INTO conditions '.
00285 "VALUES('0','".$this->getTargetRefId()."','".$this->getTargetObjId()."','".$this->getTargetType()."','".
00286 $this->getTriggerRefId()."','".$this->getTriggerObjId()."','".$this->getTriggerType()."','".
00287 $this->getOperator()."','".$this->getValue()."')";
00288
00289 $res = $this->db->query($query);
00290
00291 $query = "SELECT LAST_INSERT_ID() AS last FROM conditions";
00292 $res = $this->db->query($query);
00293 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00294 {
00295 $last_id = $row->last;
00296 }
00297
00298 if ($this->validation && !$this->validate())
00299 {
00300 $this->deleteCondition($last_id);
00301 return false;
00302 }
00303 return true;
00304 }
00305
00309 function updateCondition($a_id)
00310 {
00311 $query = "UPDATE conditions SET ".
00312 "operator = '".$this->getOperator()."', ".
00313 "value = '".$this->getValue()."' ".
00314 "WHERE id = '".$a_id."'";
00315
00316 $res = $this->db->query($query);
00317
00318 return true;
00319 }
00320
00321
00326 function delete($a_ref_id)
00327 {
00328 $query = "DELETE FROM conditions WHERE ".
00329 "target_ref_id = '".$a_ref_id."' ".
00330 "OR trigger_ref_id = '".$a_ref_id."'";
00331
00332 $res = $this->db->query($query);
00333
00334 return true;
00335 }
00336
00340 function deleteCondition($a_id)
00341 {
00342 global $ilDB;
00343
00344 $query = "DELETE FROM conditions ".
00345 "WHERE id = '".$a_id."'";
00346
00347 $res = $ilDB->query($query);
00348
00349 return true;
00350 }
00351
00356 function _getConditionsOfTrigger($a_trigger_obj_type, $a_trigger_id)
00357 {
00358
00359
00360 return $conditions ? $conditions : array();
00361 }
00362
00373 function _getConditionsOfTarget($a_target_obj_id, $a_target_type = "")
00374 {
00375 global $ilDB;
00376
00377 if ($a_target_type == "")
00378 {
00379 $a_target_type = ilObject::_lookupType($a_target_obj_id);
00380 }
00381
00382 $query = "SELECT * FROM conditions ".
00383 "WHERE target_obj_id = '".$a_target_obj_id."'".
00384 " AND target_type = '".$a_target_type."'";
00385
00386 $res = $ilDB->query($query);
00387 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00388 {
00389 $tmp_array['id'] = $row->id;
00390 $tmp_array['target_ref_id'] = $row->target_ref_id;
00391 $tmp_array['target_obj_id'] = $row->target_obj_id;
00392 $tmp_array['target_type'] = $row->target_type;
00393 $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00394 $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00395 $tmp_array['trigger_type'] = $row->trigger_type;
00396 $tmp_array['operator'] = $row->operator;
00397 $tmp_array['value'] = $row->value;
00398
00399 $conditions[] = $tmp_array;
00400 unset($tmp_array);
00401 }
00402
00403 return $conditions ? $conditions : array();
00404 }
00405
00406 function _getCondition($a_id)
00407 {
00408 global $ilDB;
00409
00410 $query = "SELECT * FROM conditions ".
00411 "WHERE id = '".$a_id."'";
00412
00413 $res = $ilDB->query($query);
00414 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00415 {
00416 $tmp_array['id'] = $row->id;
00417 $tmp_array['target_ref_id'] = $row->target_ref_id;
00418 $tmp_array['target_obj_id'] = $row->target_obj_id;
00419 $tmp_array['target_type'] = $row->target_type;
00420 $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00421 $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00422 $tmp_array['trigger_type'] = $row->trigger_type;
00423 $tmp_array['operator'] = $row->operator;
00424 $tmp_array['value'] = $row->value;
00425
00426 return $tmp_array;
00427 }
00428 return false;
00429 }
00430
00431
00432
00438 function _checkCondition($a_id)
00439 {
00440 $condition = ilConditionHandler::_getCondition($a_id);
00441
00442 switch($condition['trigger_type'])
00443 {
00444 case "tst":
00445 include_once './assessment/classes/class.ilObjTest.php';
00446 return ilObjTest::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00447
00448 case "qst":
00449 return ilObjCourse::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00450
00451 case "crs":
00452 include_once './course/classes/class.ilObjCourse.php';
00453 return ilObjCourse::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00454
00455 case 'exc':
00456 include_once './classes/class.ilObjExercise.php';
00457
00458 return ilObjExercise::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00459
00460 default:
00461 return false;
00462
00463 }
00464
00465 }
00466
00470 function _checkAllConditionsOfTarget($a_target_id, $a_target_type = "")
00471 {
00472 foreach(ilConditionHandler::_getConditionsOfTarget($a_target_id, $a_target_type) as $condition)
00473 {
00474 if(!ilConditionHandler::_checkCondition($condition['id']))
00475 {
00476 return false;
00477 }
00478 }
00479 return true;
00480 }
00481
00482
00483 function validate()
00484 {
00485
00486 $trigger_obj =& ilObjectFactory::getInstanceByRefId($this->getTriggerRefId());
00487 $target_obj =& ilObjectFactory::getInstanceByRefId($this->getTargetRefId());
00488
00489
00490 $query = "SELECT * FROM conditions WHERE ".
00491 "trigger_obj_id = '".$trigger_obj->getId()."' ".
00492 "AND target_obj_id = '".$target_obj->getId()."'";
00493
00494 $res = $this->db->query($query);
00495 if($res->numRows() > 1)
00496 {
00497 $this->setErrorMessage($this->lng->txt('condition_already_assigned'));
00498
00499 unset($trigger_obj);
00500 unset($target_obj);
00501 return false;
00502 }
00503
00504
00505 $this->target_obj_id = $target_obj->getId();
00506 if($this->checkCircle($target_obj->getId()))
00507 {
00508 $this->setErrorMessage($this->lng->txt('condition_circle_created'));
00509
00510 unset($trigger_obj);
00511 unset($target_obj);
00512 return false;
00513 }
00514 return true;
00515 }
00516
00517 function checkCircle($a_obj_id)
00518 {
00519 foreach(ilConditionHandler::_getConditionsOfTarget($a_obj_id) as $condition)
00520 {
00521 if($condition['trigger_obj_id'] == $this->target_obj_id)
00522 {
00523 $this->circle = true;
00524 break;
00525 }
00526 else
00527 {
00528 $this->checkCircle($condition['trigger_obj_id']);
00529 }
00530 }
00531 return $this->circle;
00532 }
00533 }
00534
00535 ?>