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