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
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
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, $ilBench;
00444
00445 $ilBench->start("ilConditionHandler", "getConditionsOfTarget");
00446
00447 if ($a_target_type == "")
00448 {
00449 $a_target_type = ilObject::_lookupType($a_target_obj_id);
00450 }
00451
00452 $query = "SELECT * FROM conditions ".
00453 "WHERE target_obj_id = '".$a_target_obj_id."'".
00454 " AND target_type = '".$a_target_type."'";
00455
00456 $res = $ilDB->query($query);
00457 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00458 {
00459 $tmp_array['id'] = $row->id;
00460 $tmp_array['target_ref_id'] = $row->target_ref_id;
00461 $tmp_array['target_obj_id'] = $row->target_obj_id;
00462 $tmp_array['target_type'] = $row->target_type;
00463 $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00464 $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00465 $tmp_array['trigger_type'] = $row->trigger_type;
00466 $tmp_array['operator'] = $row->operator;
00467 $tmp_array['value'] = $row->value;
00468
00469 $conditions[] = $tmp_array;
00470 unset($tmp_array);
00471 }
00472
00473 $ilBench->stop("ilConditionHandler", "getConditionsOfTarget");
00474
00475 return $conditions ? $conditions : array();
00476 }
00477
00478 function _getCondition($a_id)
00479 {
00480 global $ilDB;
00481
00482 $query = "SELECT * FROM conditions ".
00483 "WHERE id = '".$a_id."'";
00484
00485 $res = $ilDB->query($query);
00486 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00487 {
00488 $tmp_array['id'] = $row->id;
00489 $tmp_array['target_ref_id'] = $row->target_ref_id;
00490 $tmp_array['target_obj_id'] = $row->target_obj_id;
00491 $tmp_array['target_type'] = $row->target_type;
00492 $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00493 $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00494 $tmp_array['trigger_type'] = $row->trigger_type;
00495 $tmp_array['operator'] = $row->operator;
00496 $tmp_array['value'] = $row->value;
00497
00498 return $tmp_array;
00499 }
00500 return false;
00501 }
00502
00503
00504
00510 function _checkCondition($a_id)
00511 {
00512 $condition = ilConditionHandler::_getCondition($a_id);
00513
00514
00515 switch($condition['trigger_type'])
00516 {
00517 case "tst":
00518 include_once './assessment/classes/class.ilObjTestAccess.php';
00519 return ilObjTestAccess::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00520
00521 case "crs":
00522 include_once './course/classes/class.ilObjCourse.php';
00523 return ilObjCourse::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00524
00525 case 'exc':
00526 include_once './classes/class.ilObjExercise.php';
00527 return ilObjExercise::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00528
00529 case 'crsg':
00530 include_once './course/classes/class.ilObjCourseGrouping.php';
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 global $ilBench;
00546
00547 foreach(ilConditionHandler::_getConditionsOfTarget($a_target_id, $a_target_type) as $condition)
00548 {
00549 $ilBench->start("ilConditionHandler", "checkCondition");
00550 $check = ilConditionHandler::_checkCondition($condition['id']);
00551 $ilBench->stop("ilConditionHandler", "checkCondition");
00552
00553 if(!$check)
00554 {
00555 return false;
00556 }
00557 }
00558 return true;
00559 }
00560
00561
00562 function validate()
00563 {
00564
00565 $trigger_obj =& ilObjectFactory::getInstanceByRefId($this->getTriggerRefId());
00566 $target_obj =& ilObjectFactory::getInstanceByRefId($this->getTargetRefId());
00567
00568
00569 $query = "SELECT * FROM conditions WHERE ".
00570 "trigger_obj_id = '".$trigger_obj->getId()."' ".
00571 "AND target_obj_id = '".$target_obj->getId()."'";
00572
00573 $res = $this->db->query($query);
00574 if($res->numRows() > 1)
00575 {
00576 $this->setErrorMessage($this->lng->txt('condition_already_assigned'));
00577
00578 unset($trigger_obj);
00579 unset($target_obj);
00580 return false;
00581 }
00582
00583
00584 $this->target_obj_id = $target_obj->getId();
00585 if($this->checkCircle($target_obj->getId()))
00586 {
00587 $this->setErrorMessage($this->lng->txt('condition_circle_created'));
00588
00589 unset($trigger_obj);
00590 unset($target_obj);
00591 return false;
00592 }
00593 return true;
00594 }
00595
00596 function checkCircle($a_obj_id)
00597 {
00598
00599 return false;
00600
00601 foreach(ilConditionHandler::_getConditionsOfTarget($a_obj_id) as $condition)
00602 {
00603 if($condition['trigger_obj_id'] == $this->target_obj_id and $condition['operator'] == $this->getOperator())
00604 {
00605 $this->circle = true;
00606 break;
00607 }
00608 else
00609 {
00610 $this->checkCircle($condition['trigger_obj_id']);
00611 }
00612 }
00613 return $this->circle;
00614 }
00615 }
00616
00617 ?>