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