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','sahs');
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 case 'sahs':
00292 return array('finished');
00293
00294 default:
00295 return array();
00296 }
00297 }
00298
00304 function storeCondition()
00305 {
00306
00307 $query = 'INSERT INTO conditions '.
00308 "VALUES('0','".$this->getTargetRefId()."','".$this->getTargetObjId()."','".$this->getTargetType()."','".
00309 $this->getTriggerRefId()."','".$this->getTriggerObjId()."','".$this->getTriggerType()."','".
00310 $this->getOperator()."','".$this->getValue()."')";
00311
00312 $res = $this->db->query($query);
00313
00314 $query = "SELECT LAST_INSERT_ID() AS last FROM conditions";
00315 $res = $this->db->query($query);
00316 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00317 {
00318 $last_id = $row->last;
00319 }
00320
00321 if ($this->validation && !$this->validate())
00322 {
00323 $this->deleteCondition($last_id);
00324 return false;
00325 }
00326 return true;
00327 }
00328
00329 function checkExists()
00330 {
00331 $query = "SELECT * FROM conditions ".
00332 "WHERE target_ref_id = '".$this->getTargetRefId()."' ".
00333 "AND target_obj_id = '".$this->getTargetObjId()."' ".
00334 "AND trigger_ref_id = '".$this->getTriggerRefId()."' ".
00335 "AND trigger_obj_id = '".$this->getTriggerObjId()."' ".
00336 "AND operator = '".$this->getOperator()."'";
00337
00338 $res = $this->db->query($query);
00339
00340 return $res->numRows() ? true : false;
00341 }
00345 function updateCondition($a_id)
00346 {
00347 $query = "UPDATE conditions SET ".
00348 "operator = '".$this->getOperator()."', ".
00349 "value = '".$this->getValue()."' ".
00350 "WHERE id = '".$a_id."'";
00351
00352 $res = $this->db->query($query);
00353
00354 return true;
00355 }
00356
00357
00362 function delete($a_ref_id)
00363 {
00364 $query = "DELETE FROM conditions WHERE ".
00365 "target_ref_id = '".$a_ref_id."' ".
00366 "OR trigger_ref_id = '".$a_ref_id."'";
00367
00368 $res = $this->db->query($query);
00369
00370 return true;
00371 }
00376 function deleteByObjId($a_obj_id)
00377 {
00378 $query = "DELETE FROM conditions WHERE ".
00379 "target_obj_id = '".$a_obj_id."' ".
00380 "OR trigger_obj_id = '".$a_obj_id."'";
00381
00382 $res = $this->db->query($query);
00383
00384 return true;
00385 }
00386
00390 function deleteCondition($a_id)
00391 {
00392 global $ilDB;
00393
00394 $query = "DELETE FROM conditions ".
00395 "WHERE id = '".$a_id."'";
00396
00397 $res = $ilDB->query($query);
00398
00399 return true;
00400 }
00401
00406 function _getConditionsOfTrigger($a_trigger_obj_type, $a_trigger_id)
00407 {
00408 global $ilDB;
00409
00410 $query = "SELECT * FROM conditions ".
00411 "WHERE trigger_obj_id = '".$a_trigger_id."'".
00412 " AND trigger_type = '".$a_trigger_obj_type."'";
00413
00414 $res = $ilDB->query($query);
00415 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00416 {
00417 $tmp_array['id'] = $row->id;
00418 $tmp_array['target_ref_id'] = $row->target_ref_id;
00419 $tmp_array['target_obj_id'] = $row->target_obj_id;
00420 $tmp_array['target_type'] = $row->target_type;
00421 $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00422 $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00423 $tmp_array['trigger_type'] = $row->trigger_type;
00424 $tmp_array['operator'] = $row->operator;
00425 $tmp_array['value'] = $row->value;
00426
00427 $conditions[] = $tmp_array;
00428 unset($tmp_array);
00429 }
00430
00431 return $conditions ? $conditions : array();
00432 }
00433
00444 function _getConditionsOfTarget($a_target_obj_id, $a_target_type = "")
00445 {
00446 global $ilDB, $ilBench;
00447
00448 $ilBench->start("ilConditionHandler", "getConditionsOfTarget");
00449
00450 if ($a_target_type == "")
00451 {
00452 $a_target_type = ilObject::_lookupType($a_target_obj_id);
00453 }
00454
00455 $query = "SELECT * FROM conditions ".
00456 "WHERE target_obj_id = '".$a_target_obj_id."'".
00457 " AND target_type = '".$a_target_type."'";
00458
00459 $res = $ilDB->query($query);
00460 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00461 {
00462 $tmp_array['id'] = $row->id;
00463 $tmp_array['target_ref_id'] = $row->target_ref_id;
00464 $tmp_array['target_obj_id'] = $row->target_obj_id;
00465 $tmp_array['target_type'] = $row->target_type;
00466 $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00467 $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00468 $tmp_array['trigger_type'] = $row->trigger_type;
00469 $tmp_array['operator'] = $row->operator;
00470 $tmp_array['value'] = $row->value;
00471
00472 $conditions[] = $tmp_array;
00473 unset($tmp_array);
00474 }
00475
00476 $ilBench->stop("ilConditionHandler", "getConditionsOfTarget");
00477
00478 return $conditions ? $conditions : array();
00479 }
00480
00481 function _getCondition($a_id)
00482 {
00483 global $ilDB;
00484
00485 $query = "SELECT * FROM conditions ".
00486 "WHERE id = '".$a_id."'";
00487
00488 $res = $ilDB->query($query);
00489 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00490 {
00491 $tmp_array['id'] = $row->id;
00492 $tmp_array['target_ref_id'] = $row->target_ref_id;
00493 $tmp_array['target_obj_id'] = $row->target_obj_id;
00494 $tmp_array['target_type'] = $row->target_type;
00495 $tmp_array['trigger_ref_id'] = $row->trigger_ref_id;
00496 $tmp_array['trigger_obj_id'] = $row->trigger_obj_id;
00497 $tmp_array['trigger_type'] = $row->trigger_type;
00498 $tmp_array['operator'] = $row->operator;
00499 $tmp_array['value'] = $row->value;
00500
00501 return $tmp_array;
00502 }
00503 return false;
00504 }
00505
00506
00507
00513 function _checkCondition($a_id)
00514 {
00515 $condition = ilConditionHandler::_getCondition($a_id);
00516
00517
00518 switch($condition['trigger_type'])
00519 {
00520 case "tst":
00521 include_once './assessment/classes/class.ilObjTestAccess.php';
00522 return ilObjTestAccess::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00523
00524 case "crs":
00525 include_once './course/classes/class.ilObjCourse.php';
00526 return ilObjCourse::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00527
00528 case 'exc':
00529 include_once './classes/class.ilObjExercise.php';
00530 return ilObjExercise::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00531
00532 case 'crsg':
00533 include_once './course/classes/class.ilObjCourseGrouping.php';
00534 return ilObjCourseGrouping::_checkCondition($condition['trigger_obj_id'],$condition['operator'],$condition['value']);
00535
00536 case 'sahs':
00537 global $ilUser;
00538
00539 include_once './Services/Tracking/classes/class.ilLPStatusWrapper.php';
00540 return in_array($ilUser->getId(),$completed = ilLPStatusWrapper::_getCompleted($condition['trigger_obj_id']));
00541
00542 default:
00543 return false;
00544
00545 }
00546
00547 }
00548
00552 function _checkAllConditionsOfTarget($a_target_id, $a_target_type = "")
00553 {
00554 global $ilBench;
00555
00556 foreach(ilConditionHandler::_getConditionsOfTarget($a_target_id, $a_target_type) as $condition)
00557 {
00558 $ilBench->start("ilConditionHandler", "checkCondition");
00559 $check = ilConditionHandler::_checkCondition($condition['id']);
00560 $ilBench->stop("ilConditionHandler", "checkCondition");
00561
00562 if(!$check)
00563 {
00564 return false;
00565 }
00566 }
00567 return true;
00568 }
00569
00570
00571 function validate()
00572 {
00573
00574 $trigger_obj =& ilObjectFactory::getInstanceByRefId($this->getTriggerRefId());
00575 $target_obj =& ilObjectFactory::getInstanceByRefId($this->getTargetRefId());
00576
00577
00578 $query = "SELECT * FROM conditions WHERE ".
00579 "trigger_obj_id = '".$trigger_obj->getId()."' ".
00580 "AND target_obj_id = '".$target_obj->getId()."'";
00581
00582 $res = $this->db->query($query);
00583 if($res->numRows() > 1)
00584 {
00585 $this->setErrorMessage($this->lng->txt('condition_already_assigned'));
00586
00587 unset($trigger_obj);
00588 unset($target_obj);
00589 return false;
00590 }
00591
00592
00593 $this->target_obj_id = $target_obj->getId();
00594 if($this->checkCircle($target_obj->getId()))
00595 {
00596 $this->setErrorMessage($this->lng->txt('condition_circle_created'));
00597
00598 unset($trigger_obj);
00599 unset($target_obj);
00600 return false;
00601 }
00602 return true;
00603 }
00604
00605 function checkCircle($a_obj_id)
00606 {
00607 foreach(ilConditionHandler::_getConditionsOfTarget($a_obj_id) as $condition)
00608 {
00609 if($condition['trigger_obj_id'] == $this->target_obj_id and $condition['operator'] == $this->getOperator())
00610 {
00611 $this->circle = true;
00612 break;
00613 }
00614 else
00615 {
00616 $this->checkCircle($condition['trigger_obj_id']);
00617 }
00618 }
00619 return $this->circle;
00620 }
00621 }
00622
00623 ?>