00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00034 require_once "./classes/class.ilObject.php";
00035
00036 class ilObjCourseGrouping
00037 {
00038 var $db;
00039
00046 function ilObjCourseGrouping($a_id = 0)
00047 {
00048 global $ilDB;
00049
00050 $this->setType('crsg');
00051 $this->db =& $ilDB;
00052
00053 $this->setId($a_id);
00054
00055 if($a_id)
00056 {
00057 $this->read();
00058 }
00059 }
00060 function setId($a_id)
00061 {
00062 $this->id = $a_id;
00063 }
00064 function getId()
00065 {
00066 return $this->id;
00067 }
00068
00069 function setContainerRefId($a_ref_id)
00070 {
00071 $this->ref_id = $a_ref_id;
00072 }
00073 function getContainerRefId()
00074 {
00075 return $this->ref_id;
00076 }
00077 function setContainerObjId($a_obj_id)
00078 {
00079 $this->obj_id = $a_obj_id;
00080 }
00081 function getContainerObjId()
00082 {
00083 return $this->obj_id;
00084 }
00085 function getContainerType()
00086 {
00087 return $this->container_type;
00088 }
00089 function setContainerType($a_type)
00090 {
00091 $this->container_type = $a_type;
00092 }
00093
00094 function setType($a_type)
00095 {
00096 $this->type = $a_type;
00097 }
00098 function getType()
00099 {
00100 return $this->type;
00101 }
00102
00103 function setTitle($a_title)
00104 {
00105 $this->title = $a_title;
00106 }
00107 function getTitle()
00108 {
00109 return $this->title;
00110 }
00111 function setDescription($a_desc)
00112 {
00113 $this->description = $a_desc;
00114 }
00115 function getDescription()
00116 {
00117 return $this->description;
00118 }
00119 function setUniqueField($a_uni)
00120 {
00121 $this->unique_field = $a_uni;
00122 }
00123 function getUniqueField()
00124 {
00125 return $this->unique_field;
00126 }
00127
00128 function getCountAssignedItems()
00129 {
00130 return count($this->getAssignedItems());
00131 }
00132
00133 function getAssignedItems()
00134 {
00135 global $tree;
00136
00137 include_once './classes/class.ilConditionHandler.php';
00138 $condition_data = ilConditionHandler::_getConditionsOfTrigger($this->getType(),$this->getId());
00139 $conditions = array();
00140 foreach($condition_data as $condition)
00141 {
00142 if($tree->isDeleted($condition['target_ref_id']))
00143 {
00144 continue;
00145 }
00146 $conditions[] = $condition;
00147 }
00148 return count($conditions) ? $conditions : array();
00149 }
00150
00151 function delete()
00152 {
00153 include_once './classes/class.ilConditionHandler.php';
00154
00155 if($this->getId() and $this->getType() === 'crsg')
00156 {
00157 $query = "DELETE FROM object_data WHERE obj_id = '".$this->getId()."'";
00158 $this->db->query($query);
00159
00160 $query = "DELETE FROM crs_groupings ".
00161 "WHERE crs_grp_id = '".$this->getId()."'";
00162 $this->db->query($query);
00163
00164
00165 $condh =& new ilConditionHandler();
00166 $condh->deleteByObjId($this->getId());
00167
00168 return true;
00169 }
00170 return false;
00171 }
00172
00173 function create($a_course_ref_id,$a_course_id)
00174 {
00175 global $ilUser;
00176
00177
00178 $query = "INSERT INTO object_data ".
00179 "(type,title,description,owner,create_date,last_update,import_id) ".
00180 "VALUES ".
00181 "('".$this->type."',".$this->db->quote($this->getTitle()).",'".ilUtil::prepareDBString($this->getDescription())."',".
00182 "'".$ilUser->getId()."',now(),now(),'')";
00183
00184 $this->db->query($query);
00185
00186
00187 $query = "SELECT LAST_INSERT_ID() as last";
00188 $res = $this->db->query($query);
00189 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00190 {
00191 $this->setId($row->last);
00192 }
00193
00194
00195
00196 $query = "INSERT INTO crs_groupings ".
00197 "SET crs_ref_id = '".$a_course_ref_id."', ".
00198 "crs_id = '".$a_course_id."',".
00199 "crs_grp_id = '".$this->getId()."', ".
00200 "unique_field = '".$this->getUniqueField()."'";
00201
00202 $this->db->query($query);
00203
00204 return $this->getId();
00205 }
00206
00207 function update()
00208 {
00209 if($this->getId() and $this->getType() === 'crsg')
00210 {
00211
00212 $query = "UPDATE object_data ".
00213 "SET title = '".ilUtil::prepareDBString($this->getTitle())."', ".
00214 "description = '".ilUtil::prepareDBString($this->getDescription())."' ".
00215 "WHERE obj_id = '".$this->getId()."' ".
00216 "AND type = '".$this->getType()."'";
00217
00218 $this->db->query($query);
00219
00220
00221 $query = "UPDATE crs_groupings ".
00222 "SET unique_field = '".$this->getUniqueField()."' ".
00223 "WHERE crs_grp_id = '".$this->getId()."'";
00224
00225 $this->db->query($query);
00226
00227
00228 $query = "UPDATE conditions ".
00229 "SET value = '".$this->getUniqueField()."' ".
00230 "WHERE trigger_obj_id = '".$this->getId()."' ".
00231 "AND trigger_type = 'crsg'";
00232 $this->db->query($query);
00233
00234 return true;
00235 }
00236 return false;
00237 }
00238
00239 function isAssigned($a_course_id)
00240 {
00241 foreach($this->getAssignedItems() as $condition_data)
00242 {
00243 if($a_course_id == $condition_data['target_obj_id'])
00244 {
00245 return true;
00246 }
00247 }
00248 return false;
00249 }
00250
00251 function read()
00252 {
00253 global $ilObjDataCache;
00254
00255 $query = "SELECT * FROM object_data ".
00256 "WHERE obj_id = '".$this->getId()."'";
00257
00258 $res = $this->db->query($query);
00259 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00260 {
00261 $this->setTitle($row->title);
00262 $this->setDescription($row->description);
00263 }
00264
00265 $query = "SELECT * FROM crs_groupings ".
00266 "WHERE crs_grp_id = '".$this->getId()."'";
00267 $res = $this->db->query($query);
00268
00269 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00270 {
00271 $this->setUniqueField($row->unique_field);
00272 $this->setContainerRefId($row->crs_ref_id);
00273 $this->setContainerObjId($row->crs_id);
00274 $this->setContainerType($ilObjDataCache->lookupType($this->getContainerObjId()));
00275 }
00276
00277 return true;
00278 }
00279
00280 function _checkAccess($grouping_id)
00281 {
00282 global $ilAccess,$tree;
00283
00284 $tmp_grouping_obj = new ilObjCourseGrouping($grouping_id);
00285
00286 $found_invisible = false;
00287 foreach($tmp_grouping_obj->getAssignedItems() as $condition)
00288 {
00289 if(!$ilAccess->checkAccess('write','',$condition['target_ref_id']))
00290 {
00291 $found_invisible = true;
00292 break;
00293 }
00294 }
00295 return $found_invisible ? false : true;
00296 }
00297
00303 function _getVisibleGroupings($a_obj_id)
00304 {
00305 global $ilObjDataCache,$ilAccess,$ilDB;
00306
00307 $container_type = $ilObjDataCache->lookupType($a_obj_id) == 'grp' ? 'grp' : 'crs';
00308
00309
00310
00311 $query = "SELECT * FROM object_data WHERE type = 'crsg' ORDER BY title";
00312 $res = $ilDB->query($query);
00313 $groupings = array();
00314 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00315 {
00316 $groupings[] = $row->obj_id;
00317 }
00318
00319
00320 foreach($groupings as $grouping_id)
00321 {
00322 $tmp_grouping_obj = new ilObjCourseGrouping($grouping_id);
00323
00324
00325 if($tmp_grouping_obj->getContainerType() != $container_type)
00326 {
00327 continue;
00328 }
00329
00330 if($tmp_grouping_obj->getContainerObjId() == $a_obj_id)
00331 {
00332 $visible_groupings[] = $grouping_id;
00333 continue;
00334 }
00335
00336 if(count($items = $tmp_grouping_obj->getAssignedItems()))
00337 {
00338 foreach($items as $condition_data)
00339 {
00340 if($ilAccess->checkAccess('write','',$condition_data['target_ref_id']))
00341 {
00342 $visible_groupings[] = $grouping_id;
00343 break;
00344 }
00345 }
00346
00347 }
00348 }
00349 return $visible_groupings ? $visible_groupings : array();
00350 }
00351
00352 function assign($a_crs_ref_id,$a_course_id)
00353 {
00354
00355 $this->__addCondition($this->getContainerRefId(),$this->getContainerObjId());
00356 $this->__addCondition($a_crs_ref_id,$a_course_id);
00357
00358 return true;
00359 }
00360
00361 function deassign($a_crs_ref_id,$a_course_id)
00362 {
00363 include_once './classes/class.ilConditionHandler.php';
00364
00365
00366 $condh =& new ilConditionHandler();
00367
00368
00369 if($this->getCountAssignedCourses() == 2)
00370 {
00371 $condh->deleteByObjId($this->getId());
00372
00373 return true;
00374 }
00375
00376 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$this->getId()) as $cond_data)
00377 {
00378
00379 if($cond_data['target_ref_id'] == $a_crs_ref_id and
00380 $cond_data['target_obj_id'] == $a_course_id)
00381 {
00382 $condh->deleteCondition($cond_data['id']);
00383 }
00384 }
00385
00386 return true;
00387
00388 }
00389
00390 function __addCondition($a_target_ref_id,$a_target_obj_id)
00391 {
00392 include_once './classes/class.ilConditionHandler.php';
00393
00394 $tmp_condh =& new ilConditionHandler();
00395 $tmp_condh->enableAutomaticValidation(false);
00396
00397 $tmp_condh->setTargetRefId($a_target_ref_id);
00398 $tmp_condh->setTargetObjId($a_target_obj_id);
00399 $tmp_condh->setTargetType('crs');
00400 $tmp_condh->setTriggerRefId(0);
00401 $tmp_condh->setTriggerObjId($this->getId());
00402 $tmp_condh->setTriggerType('crsg');
00403 $tmp_condh->setOperator('not_member');
00404 $tmp_condh->setValue($this->getUniqueField());
00405
00406 if(!$tmp_condh->checkExists())
00407 {
00408 $tmp_condh->storeCondition();
00409
00410 return true;
00411 }
00412 return false;
00413 }
00414
00415
00416 function _deleteAll($a_course_id)
00417 {
00418 global $ilDB;
00419
00420
00421 foreach($groupings = ilObjCourseGrouping::_getGroupings($a_course_id) as $grouping_id)
00422 {
00423 include_once './classes/class.ilConditionHandler.php';
00424
00425 $condh =& new ilConditionHandler();
00426 $condh->deleteByObjId($grouping_id);
00427 }
00428
00429 $query = "DELETE FROM crs_groupings ".
00430 "WHERE crs_id = '".$a_course_id."'";
00431
00432 $ilDB->query($query);
00433
00434 return true;
00435 }
00436
00437 function _getGroupings($a_course_id)
00438 {
00439 global $ilDB;
00440
00441 $query = "SELECT * FROM crs_groupings ".
00442 "WHERE crs_id = '".$a_course_id."'";
00443
00444 $res = $ilDB->query($query);
00445 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00446 {
00447 $groupings[] = $row->crs_grp_id;
00448 }
00449 return $groupings ? $groupings : array();
00450 }
00451
00452 function _isInGrouping($a_crs_id)
00453 {
00454 include_once './classes/class.ilConditionHandler.php';
00455
00456 foreach(ilConditionHandler::_getConditionsOfTarget($a_crs_id,'crs') as $cond)
00457 {
00458 if($cond['operator'] == 'not_member')
00459 {
00460 return true;
00461 }
00462 }
00463 return false;
00464 }
00465
00466 function _checkCondition($trigger_obj_id,$operator,$value)
00467 {
00468
00469
00470 return true;
00471 }
00472
00473
00480 function _getGroupingCourseIds($a_course_id)
00481 {
00482 include_once './classes/class.ilConditionHandler.php';
00483
00484
00485 foreach(ilConditionHandler::_getConditionsOfTarget($a_course_id,'crs') as $condition)
00486 {
00487 if($condition['trigger_type'] == 'crsg')
00488 {
00489 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$condition['trigger_obj_id']) as $target_condition)
00490 {
00491 $course_ids[] = array('id' => $target_condition['target_obj_id'],
00492 'unique' => $target_condition['value']);
00493 }
00494 }
00495 }
00496 return $course_ids ? $course_ids : array();
00497 }
00498
00499 function _checkGroupingDependencies(&$container_obj)
00500 {
00501 global $ilUser,$lng,$tree;
00502
00503 include_once './classes/class.ilConditionHandler.php';
00504
00505 $trigger_ids = array();
00506 foreach(ilConditionHandler::_getConditionsOfTarget($container_obj->getId(),$container_obj->getType()) as $condition)
00507 {
00508 if($condition['operator'] == 'not_member')
00509 {
00510 $trigger_ids[] = $condition['trigger_obj_id'];
00511 break;
00512 }
00513 }
00514 if(!count($trigger_ids))
00515 {
00516 return true;
00517 }
00518
00519 foreach($trigger_ids as $trigger_id)
00520 {
00521 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$trigger_id) as $condition)
00522 {
00523
00524 if($tree->isDeleted($condition['target_ref_id']))
00525 {
00526 continue;
00527 }
00528 if($condition['operator'] == 'not_member')
00529 {
00530 switch($condition['value'])
00531 {
00532 case 'matriculation':
00533 if(!strlen($ilUser->getMatriculation()))
00534 {
00535 if(!$matriculation_message)
00536 {
00537 $matriculation_message = $lng->txt('crs_grp_matriculation_required');
00538 }
00539 }
00540 }
00541 if($container_obj->getType() == 'crs')
00542 {
00543 if(ilCourseMembers::_isMember($ilUser->getId(),$condition['target_obj_id'],$condition['value']))
00544 {
00545 if(!$assigned_message)
00546 {
00547 $assigned_message = $lng->txt('crs_grp_already_assigned');
00548 }
00549 }
00550 }
00551 else
00552 {
00553 if(ilObjGroup::_isMember($ilUser->getId(),$condition['target_ref_id'],$condition['value']))
00554 {
00555 if(!$assigned_message)
00556 {
00557 $assigned_message = $lng->txt('crs_grp_already_assigned');
00558 }
00559 }
00560
00561 }
00562 }
00563 }
00564 }
00565 if($matriculation_message)
00566 {
00567 $container_obj->appendMessage($matriculation_message);
00568 return false;
00569 }
00570 elseif($assigned_message)
00571 {
00572 $container_obj->appendMessage($assigned_message);
00573 return false;
00574 }
00575 return true;
00576 }
00577
00578 function _getGroupingItemsAsString(&$container_obj)
00579 {
00580 global $tree,$ilObjDataCache,$ilAccess,$tree;
00581
00582 include_once './classes/class.ilConditionHandler.php';
00583
00584 $trigger_ids = array();
00585 foreach(ilConditionHandler::_getConditionsOfTarget($container_obj->getId(),$container_obj->getType()) as $condition)
00586 {
00587 if($condition['operator'] == 'not_member')
00588 {
00589 $trigger_ids[] = $condition['trigger_obj_id'];
00590 }
00591 }
00592 if(!count($trigger_ids))
00593 {
00594 return false;
00595 }
00596 foreach($trigger_ids as $trigger_id)
00597 {
00598 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$trigger_id) as $condition)
00599 {
00600
00601 if($tree->isDeleted($condition['target_ref_id']))
00602 {
00603 continue;
00604 }
00605
00606 if($condition['operator'] == 'not_member')
00607 {
00608 if(!$hash_table[$condition['target_ref_id']])
00609 {
00610 $courses .= (' <br/>'.$ilObjDataCache->lookupTitle($condition['target_obj_id']));
00611 }
00612 $hash_table[$condition['target_ref_id']] = true;
00613 }
00614 }
00615 }
00616 return $courses;
00617 }
00618
00619 }
00620 ?>