ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilObjCourseGrouping.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2005 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
32require_once "./Services/Object/classes/class.ilObject.php";
33
35{
36 var $db;
37
38 protected static $assignedObjects = array();
39
46 function ilObjCourseGrouping($a_id = 0)
47 {
48 global $ilDB;
49
50 $this->setType('crsg');
51 $this->db =& $ilDB;
52
53 $this->setId($a_id);
54
55 if($a_id)
56 {
57 $this->read();
58 }
59 }
60 function setId($a_id)
61 {
62 $this->id = $a_id;
63 }
64 function getId()
65 {
66 return $this->id;
67 }
68
69 function setContainerRefId($a_ref_id)
70 {
71 $this->ref_id = $a_ref_id;
72 }
74 {
75 return $this->ref_id;
76 }
77 function setContainerObjId($a_obj_id)
78 {
79 $this->obj_id = $a_obj_id;
80 }
82 {
83 return $this->obj_id;
84 }
86 {
87 return $this->container_type;
88 }
89 function setContainerType($a_type)
90 {
91 $this->container_type = $a_type;
92 }
93
94 function setType($a_type)
95 {
96 $this->type = $a_type;
97 }
98 function getType()
99 {
100 return $this->type;
101 }
102
103 function setTitle($a_title)
104 {
105 $this->title = $a_title;
106 }
107 function getTitle()
108 {
109 return $this->title;
110 }
111 function setDescription($a_desc)
112 {
113 $this->description = $a_desc;
114 }
115 function getDescription()
116 {
117 return $this->description;
118 }
119 function setUniqueField($a_uni)
120 {
121 $this->unique_field = $a_uni;
122 }
123 function getUniqueField()
124 {
125 return $this->unique_field;
126 }
127
129 {
130 return count($this->getAssignedItems());
131 }
132
134 {
135 global $tree;
136
137 include_once './Services/AccessControl/classes/class.ilConditionHandler.php';
138 $condition_data = ilConditionHandler::_getConditionsOfTrigger($this->getType(),$this->getId());
139 $conditions = array();
140 foreach($condition_data as $condition)
141 {
142 if($tree->isDeleted($condition['target_ref_id']))
143 {
144 continue;
145 }
146 $conditions[] = $condition;
147 }
148 return count($conditions) ? $conditions : array();
149 }
150
151 function delete()
152 {
153 global $ilDB;
154
155 include_once './Services/AccessControl/classes/class.ilConditionHandler.php';
156
157 if($this->getId() and $this->getType() === 'crsg')
158 {
159 $query = "DELETE FROM object_data WHERE obj_id = ".$ilDB->quote($this->getId(),'integer')." ";
160 $res = $ilDB->manipulate($query);
161
162 $query = "DELETE FROM crs_groupings ".
163 "WHERE crs_grp_id = ".$ilDB->quote($this->getId(),'integer')." ";
164 $res = $ilDB->manipulate($query);
165
166 // Delete conditions
167 $condh =& new ilConditionHandler();
168 $condh->deleteByObjId($this->getId());
169
170 return true;
171 }
172 return false;
173 }
174
175 function create($a_course_ref_id,$a_course_id)
176 {
177 global $ilUser,$ilDB;
178
179 // INSERT IN object_data
180 $this->setId($ilDB->nextId("object_data"));
181 $query = "INSERT INTO object_data ".
182 "(obj_id, type,title,description,owner,create_date,last_update) ".
183 "VALUES ".
184 "(".
185 $ilDB->quote($this->getId(), "integer").",".
186 $ilDB->quote($this->type, "text").",".
187 $ilDB->quote($this->getTitle(), "text").",".
188 $ilDB->quote($this->getDescription(), "text").",".
189 $ilDB->quote($ilUser->getId(), "integer").",".
190 $ilDB->now().",".
191 $ilDB->now().
192 ')';
193
194 $ilDB->manipulate($query);
195
196 // INSERT in crs_groupings
197 $query = "INSERT INTO crs_groupings (crs_grp_id,crs_ref_id,crs_id,unique_field) ".
198 "VALUES (".
199 $ilDB->quote($this->getId(),'integer').", ".
200 $ilDB->quote($a_course_ref_id,'integer').", ".
201 $ilDB->quote($a_course_id,'integer').", ".
202 $ilDB->quote($this->getUniqueField(),'text')." ".
203 ")";
204 $res = $ilDB->manipulate($query);
205
206 return $this->getId();
207 }
208
209 function update()
210 {
211 global $ilDB;
212
213 if($this->getId() and $this->getType() === 'crsg')
214 {
215 // UPDATe object_data
216 $query = "UPDATE object_data ".
217 "SET title = ".$ilDB->quote($this->getTitle(),'text').", ".
218 "description = ".$ilDB->quote($this->getDescription(),'text')." ".
219 "WHERE obj_id = ".$ilDB->quote($this->getId(),'integer')." ".
220 "AND type = ".$ilDB->quote($this->getType(),'text')." ";
221 $res = $ilDB->manipulate($query);
222
223 // UPDATE crs_groupings
224 $query = "UPDATE crs_groupings ".
225 "SET unique_field = ".$ilDB->quote($this->getUniqueField(),'text')." ".
226 "WHERE crs_grp_id = ".$ilDB->quote($this->getId(),'integer')." ";
227 $res = $ilDB->manipulate($query);
228
229 // UPDATE conditions
230 $query = "UPDATE conditions ".
231 "SET value = ".$ilDB->quote($this->getUniqueField(),'text')." ".
232 "WHERE trigger_obj_id = ".$ilDB->quote($this->getId(),'integer')." ".
233 "AND trigger_type = 'crsg'";
234 $res = $ilDB->manipulate($query);
235
236 return true;
237 }
238 return false;
239 }
240
241 function isAssigned($a_course_id)
242 {
243 foreach($this->getAssignedItems() as $condition_data)
244 {
245 if($a_course_id == $condition_data['target_obj_id'])
246 {
247 return true;
248 }
249 }
250 return false;
251 }
252
253 function read()
254 {
255 global $ilObjDataCache,$ilDB;
256
257 $query = "SELECT * FROM object_data ".
258 "WHERE obj_id = ".$ilDB->quote($this->getId() ,'integer')." ";
259
260 $res = $this->db->query($query);
261 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
262 {
263 $this->setTitle($row->title);
264 $this->setDescription($row->description);
265 }
266
267 $query = "SELECT * FROM crs_groupings ".
268 "WHERE crs_grp_id = ".$ilDB->quote($this->getId(),'integer')." ";
269 $res = $ilDB->query($query);
270
271 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
272 {
273 $this->setUniqueField($row->unique_field);
274 $this->setContainerRefId($row->crs_ref_id);
275 $this->setContainerObjId($row->crs_id);
276 $this->setContainerType($ilObjDataCache->lookupType($this->getContainerObjId()));
277 }
278
279 return true;
280 }
281
282 function _checkAccess($grouping_id)
283 {
284 global $ilAccess,$tree;
285
286 $tmp_grouping_obj = new ilObjCourseGrouping($grouping_id);
287
288 $found_invisible = false;
289 foreach($tmp_grouping_obj->getAssignedItems() as $condition)
290 {
291 if(!$ilAccess->checkAccess('write','',$condition['target_ref_id']))
292 {
293 $found_invisible = true;
294 break;
295 }
296 }
297 return $found_invisible ? false : true;
298 }
299
305 function _getVisibleGroupings($a_obj_id)
306 {
307 global $ilObjDataCache,$ilAccess,$ilDB;
308
309 $container_type = $ilObjDataCache->lookupType($a_obj_id) == 'grp' ? 'grp' : 'crs';
310
311
312 // First get all groupings
313 $query = "SELECT * FROM object_data WHERE type = 'crsg' ORDER BY title";
314 $res = $ilDB->query($query);
315 $groupings = array();
316 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
317 {
318 $groupings[] = $row->obj_id;
319 }
320
321 //check access
322 foreach($groupings as $grouping_id)
323 {
324 $tmp_grouping_obj = new ilObjCourseGrouping($grouping_id);
325
326 // Check container type
327 if($tmp_grouping_obj->getContainerType() != $container_type)
328 {
329 continue;
330 }
331 // Check if container is current container
332 if($tmp_grouping_obj->getContainerObjId() == $a_obj_id)
333 {
334 $visible_groupings[] = $grouping_id;
335 continue;
336 }
337 // check if items are assigned
338 if(count($items = $tmp_grouping_obj->getAssignedItems()))
339 {
340 foreach($items as $condition_data)
341 {
342 if($ilAccess->checkAccess('write','',$condition_data['target_ref_id']))
343 {
344 $visible_groupings[] = $grouping_id;
345 break;
346 }
347 }
348
349 }
350 }
351 return $visible_groupings ? $visible_groupings : array();
352 }
353
354 function assign($a_crs_ref_id,$a_course_id)
355 {
356 // Add the parent course of grouping
357 $this->__addCondition($this->getContainerRefId(),$this->getContainerObjId());
358 $this->__addCondition($a_crs_ref_id,$a_course_id);
359
360 return true;
361 }
362
363 function deassign($a_crs_ref_id,$a_course_id)
364 {
365 include_once './Services/AccessControl/classes/class.ilConditionHandler.php';
366
367
368 $condh =& new ilConditionHandler();
369
370 // DELETE also original course if its the last
371 if($this->getCountAssignedCourses() == 2)
372 {
373 $condh->deleteByObjId($this->getId());
374
375 return true;
376 }
377
378 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$this->getId()) as $cond_data)
379 {
380
381 if($cond_data['target_ref_id'] == $a_crs_ref_id and
382 $cond_data['target_obj_id'] == $a_course_id)
383 {
384 $condh->deleteCondition($cond_data['id']);
385 }
386 }
387
388 return true;
389
390 }
391 // PRIVATE
392 function __addCondition($a_target_ref_id,$a_target_obj_id)
393 {
394 include_once './Services/AccessControl/classes/class.ilConditionHandler.php';
395
396 $tmp_condh =& new ilConditionHandler();
397 $tmp_condh->enableAutomaticValidation(false);
398
399 $tmp_condh->setTargetRefId($a_target_ref_id);
400 $tmp_condh->setTargetObjId($a_target_obj_id);
401 $tmp_condh->setTargetType('crs');
402 $tmp_condh->setTriggerRefId(0);
403 $tmp_condh->setTriggerObjId($this->getId());
404 $tmp_condh->setTriggerType('crsg');
405 $tmp_condh->setOperator('not_member');
406 $tmp_condh->setValue($this->getUniqueField());
407
408 if(!$tmp_condh->checkExists())
409 {
410 $tmp_condh->storeCondition();
411
412 return true;
413 }
414 return false;
415 }
416
417 // STATIC
418 function _deleteAll($a_course_id)
419 {
420 global $ilDB;
421
422 // DELETE CONDITIONS
423 foreach($groupings = ilObjCourseGrouping::_getGroupings($a_course_id) as $grouping_id)
424 {
425 include_once './Services/AccessControl/classes/class.ilConditionHandler.php';
426
427 $condh =& new ilConditionHandler();
428 $condh->deleteByObjId($grouping_id);
429 }
430
431 $query = "DELETE FROM crs_groupings ".
432 "WHERE crs_id = ".$ilDB->quote($a_course_id,'integer')." ";
433 $res = $ilDB->manipulate($query);
434
435 return true;
436 }
437
438 function _getGroupings($a_course_id)
439 {
440 global $ilDB;
441
442 $query = "SELECT * FROM crs_groupings ".
443 "WHERE crs_id = ".$ilDB->quote($a_course_id,'integer')." ";
444
445 $res = $ilDB->query($query);
446 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
447 {
448 $groupings[] = $row->crs_grp_id;
449 }
450 return $groupings ? $groupings : array();
451 }
452
453 function _checkCondition($trigger_obj_id,$operator,$value,$a_usr_id = 0)
454 {
455 // in the moment i alway return true, there are some problems with presenting the condition if it fails,
456 // only course register class check manually if this condition is fullfilled
457 return true;
458 }
459
460
467 function _getGroupingCourseIds($a_course_ref_id,$a_course_id)
468 {
469 global $tree;
470
471 include_once './Services/AccessControl/classes/class.ilConditionHandler.php';
472
473 // get all grouping ids the course is assigned to
474 foreach(ilConditionHandler::_getConditionsOfTarget($a_course_ref_id,$a_course_id,'crs') as $condition)
475 {
476 if($condition['trigger_type'] == 'crsg')
477 {
478 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$condition['trigger_obj_id']) as $target_condition)
479 {
480 if($tree->isDeleted($target_condition['target_ref_id']))
481 {
482 continue;
483 }
484 $course_ids[] = array('id' => $target_condition['target_obj_id'],
485 'unique' => $target_condition['value']);
486 }
487 }
488 }
489 return $course_ids ? $course_ids : array();
490 }
491
492
497 public static function getAssignedObjects()
498 {
499 return self::$assignedObjects ? self::$assignedObjects : array();
500 }
501
502 function _checkGroupingDependencies(&$container_obj, $a_user_id = null)
503 {
504 global $ilUser,$lng,$tree;
505
506 include_once './Services/AccessControl/classes/class.ilConditionHandler.php';
507
508 $user_id = is_null($a_user_id) ? $ilUser->getId() : $a_user_id;
509
510
511 $trigger_ids = array();
512 foreach(ilConditionHandler::_getConditionsOfTarget($container_obj->getRefId(),
513 $container_obj->getId(),
514 $container_obj->getType()) as $condition)
515 {
516 if($condition['operator'] == 'not_member')
517 {
518 $trigger_ids[] = $condition['trigger_obj_id'];
519 break;
520 }
521 }
522 if(!count($trigger_ids))
523 {
524 return true;
525 }
526 $matriculation_message = $assigned_message = '';
527 self::$assignedObjects = array();
528 foreach($trigger_ids as $trigger_id)
529 {
530 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$trigger_id) as $condition)
531 {
532 // Handle deleted items
533 if($tree->isDeleted($condition['target_ref_id']))
534 {
535 continue;
536 }
537 if($condition['operator'] == 'not_member')
538 {
539 switch($condition['value'])
540 {
541 case 'matriculation':
542 if(!strlen(ilObjUser::lookupMatriculation($user_id)))
543 {
544 if(!$matriculation_message)
545 {
546 $matriculation_message = $lng->txt('crs_grp_matriculation_required');
547 }
548 }
549 }
550 if($container_obj->getType() == 'crs')
551 {
552 include_once('Modules/Course/classes/class.ilCourseParticipants.php');
553 $members = ilCourseParticipants::_getInstanceByObjId($condition['target_obj_id']);
554 if($members->isGroupingMember($user_id,$condition['value']))
555 {
556 if(!$assigned_message)
557 {
558 self::$assignedObjects[] = $condition['target_obj_id'];
559 $assigned_message = $lng->txt('crs_grp_already_assigned');
560 }
561 }
562 }
563 elseif($container_obj->getType() == 'grp')
564 {
565 include_once('Modules/Group/classes/class.ilGroupParticipants.php');
566 $members = ilGroupParticipants::_getInstanceByObjId($condition['target_obj_id']);
567 if($members->isGroupingMember($user_id,$condition['value']))
568 {
569 if(!$assigned_message)
570 {
571 self::$assignedObjects[] = $condition['target_obj_id'];
572 $assigned_message = $lng->txt('crs_grp_already_assigned');
573 }
574 }
575
576 }
577 else
578 {
579 if(ilObjGroup::_isMember($user_id,$condition['target_ref_id'],$condition['value']))
580 {
581 if(!$assigned_message)
582 {
583 self::$assignedObjects[] = $condition['target_obj_id'];
584 $assigned_message = $lng->txt('crs_grp_already_assigned');
585 }
586 }
587
588 }
589 }
590 }
591 }
592 if($matriculation_message)
593 {
594 $container_obj->appendMessage($matriculation_message);
595 return false;
596 }
597 elseif($assigned_message)
598 {
599 $container_obj->appendMessage($assigned_message);
600 return false;
601 }
602 return true;
603 }
604
605
612 function _getGroupingItems($container_obj)
613 {
614 global $tree,$ilObjDataCache,$ilAccess,$tree;
615
616 include_once './Services/AccessControl/classes/class.ilConditionHandler.php';
617
618 $trigger_ids = array();
619 foreach(ilConditionHandler::_getConditionsOfTarget($container_obj->getRefId(),
620 $container_obj->getId(),
621 $container_obj->getType()) as $condition)
622 {
623 if($condition['operator'] == 'not_member')
624 {
625 $trigger_ids[] = $condition['trigger_obj_id'];
626 }
627 }
628 if(!count($trigger_ids))
629 {
630 return false;
631 }
632 $hash_table = array();
633 foreach($trigger_ids as $trigger_id)
634 {
635 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$trigger_id) as $condition)
636 {
637 // Continue if trigger is deleted
638 if($tree->isDeleted($condition['target_ref_id']))
639 {
640 continue;
641 }
642
643 if($condition['operator'] == 'not_member')
644 {
645 if(!$hash_table[$condition['target_ref_id']])
646 {
647 $items[] = $condition['target_ref_id'];
648 }
649 $hash_table[$condition['target_ref_id']] = true;
650 }
651 }
652 }
653 return $items ? $items : array();
654 }
655
656} // END class.ilObjCourseGrouping
657?>
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
Handles conditions for accesses to different ILIAS objects.
_getConditionsOfTrigger($a_trigger_obj_type, $a_trigger_id)
get all conditions of trigger object
static _getConditionsOfTarget($a_target_ref_id, $a_target_obj_id, $a_target_type="")
get all conditions of target object
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
Class ilObj<module_name>
__addCondition($a_target_ref_id, $a_target_obj_id)
_getVisibleGroupings($a_obj_id)
Returns a list of all groupings for which the current user hast write permission on all assigned obje...
deassign($a_crs_ref_id, $a_course_id)
_checkGroupingDependencies(&$container_obj, $a_user_id=null)
create($a_course_ref_id, $a_course_id)
assign($a_crs_ref_id, $a_course_id)
_checkCondition($trigger_obj_id, $operator, $value, $a_usr_id=0)
ilObjCourseGrouping($a_id=0)
Constructor @access public.
_getGroupingItems($container_obj)
Get courses/groups that are assigned to the same membership limitation.
static getAssignedObjects()
Alway call checkGroupingDependencies before.
_getGroupingCourseIds($a_course_ref_id, $a_course_id)
Get all ids of courses that are grouped with another course @access static.
_isMember($a_user_id, $a_ref_id, $a_field='')
static lookupMatriculation($a_usr_id)
Lookup matriculation.
global $lng
Definition: privfeed.php:40
$ref_id
Definition: sahs_server.php:39
global $ilDB
global $ilUser
Definition: imgupload.php:15