ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 "./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  }
73  function getContainerRefId()
74  {
75  return $this->ref_id;
76  }
77  function setContainerObjId($a_obj_id)
78  {
79  $this->obj_id = $a_obj_id;
80  }
81  function getContainerObjId()
82  {
83  return $this->obj_id;
84  }
85  function getContainerType()
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 
133  function getAssignedItems()
134  {
135  global $tree;
136 
137  include_once './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 './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())." ";
160  $this->db->query($query);
161 
162  $query = "DELETE FROM crs_groupings ".
163  "WHERE crs_grp_id = ".$ilDB->quote($this->getId())." ";
164  $this->db->query($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  $query = "INSERT INTO object_data ".
181  "(type,title,description,owner,create_date,last_update,import_id) ".
182  "VALUES ".
183  "(".$ilDB->quote($this->type).",".$this->db->quote($this->getTitle()).",".$ilDB->quote($this->getDescription()).",".
184  " ".$ilDB->quote($ilUser->getId()).",now(),now(),'')";
185 
186  $this->db->query($query);
187 
188  // READ this id
189  $query = "SELECT LAST_INSERT_ID() as last";
190  $res = $this->db->query($query);
191  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
192  {
193  $this->setId($row->last);
194  }
195 
196 
197  // INSERT in crs_groupings
198  $query = "INSERT INTO crs_groupings ".
199  "SET crs_ref_id = ".$ilDB->quote($a_course_ref_id).", ".
200  "crs_id = ".$ilDB->quote($a_course_id).",".
201  "crs_grp_id = ".$ilDB->quote($this->getId()).", ".
202  "unique_field = ".$ilDB->quote($this->getUniqueField())." ";
203 
204  $this->db->query($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()).", ".
218  "description = ".$ilDB->quote($this->getDescription())." ".
219  "WHERE obj_id = ".$ilDB->quote($this->getId())." ".
220  "AND type = ".$ilDB->quote($this->getType())." ";
221 
222  $this->db->query($query);
223 
224  // UPDATE crs_groupings
225  $query = "UPDATE crs_groupings ".
226  "SET unique_field = ".$ilDB->quote($this->getUniqueField())." ".
227  "WHERE crs_grp_id = ".$ilDB->quote($this->getId())." ";
228 
229  $this->db->query($query);
230 
231  // UPDATE conditions
232  $query = "UPDATE conditions ".
233  "SET value = ".$ilDB->quote($this->getUniqueField())." ".
234  "WHERE trigger_obj_id = ".$ilDB->quote($this->getId())." ".
235  "AND trigger_type = 'crsg'";
236  $this->db->query($query);
237 
238  return true;
239  }
240  return false;
241  }
242 
243  function isAssigned($a_course_id)
244  {
245  foreach($this->getAssignedItems() as $condition_data)
246  {
247  if($a_course_id == $condition_data['target_obj_id'])
248  {
249  return true;
250  }
251  }
252  return false;
253  }
254 
255  function read()
256  {
257  global $ilObjDataCache,$ilDB;
258 
259  $query = "SELECT * FROM object_data ".
260  "WHERE obj_id = ".$ilDB->quote($this->getId())." ";
261 
262  $res = $this->db->query($query);
263  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
264  {
265  $this->setTitle($row->title);
266  $this->setDescription($row->description);
267  }
268 
269  $query = "SELECT * FROM crs_groupings ".
270  "WHERE crs_grp_id = ".$ilDB->quote($this->getId())." ";
271  $res = $this->db->query($query);
272 
273  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
274  {
275  $this->setUniqueField($row->unique_field);
276  $this->setContainerRefId($row->crs_ref_id);
277  $this->setContainerObjId($row->crs_id);
278  $this->setContainerType($ilObjDataCache->lookupType($this->getContainerObjId()));
279  }
280 
281  return true;
282  }
283 
284  function _checkAccess($grouping_id)
285  {
286  global $ilAccess,$tree;
287 
288  $tmp_grouping_obj = new ilObjCourseGrouping($grouping_id);
289 
290  $found_invisible = false;
291  foreach($tmp_grouping_obj->getAssignedItems() as $condition)
292  {
293  if(!$ilAccess->checkAccess('write','',$condition['target_ref_id']))
294  {
295  $found_invisible = true;
296  break;
297  }
298  }
299  return $found_invisible ? false : true;
300  }
301 
307  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(DB_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 './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 './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  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 './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)." ";
435 
436  $ilDB->query($query);
437 
438  return true;
439  }
440 
441  function _getGroupings($a_course_id)
442  {
443  global $ilDB;
444 
445  $query = "SELECT * FROM crs_groupings ".
446  "WHERE crs_id = ".$ilDB->quote($a_course_id)." ";
447 
448  $res = $ilDB->query($query);
449  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
450  {
451  $groupings[] = $row->crs_grp_id;
452  }
453  return $groupings ? $groupings : array();
454  }
455 
456  function _checkCondition($trigger_obj_id,$operator,$value,$a_usr_id = 0)
457  {
458  // in the moment i alway return true, there are some problems with presenting the condition if it fails,
459  // only course register class check manually if this condition is fullfilled
460  return true;
461  }
462 
463 
470  function _getGroupingCourseIds($a_course_ref_id,$a_course_id)
471  {
472  global $tree;
473 
474  include_once './classes/class.ilConditionHandler.php';
475 
476  // get all grouping ids the course is assigned to
477  foreach(ilConditionHandler::_getConditionsOfTarget($a_course_ref_id,$a_course_id,'crs') as $condition)
478  {
479  if($condition['trigger_type'] == 'crsg')
480  {
481  foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$condition['trigger_obj_id']) as $target_condition)
482  {
483  if($tree->isDeleted($target_condition['target_ref_id']))
484  {
485  continue;
486  }
487  $course_ids[] = array('id' => $target_condition['target_obj_id'],
488  'unique' => $target_condition['value']);
489  }
490  }
491  }
492  return $course_ids ? $course_ids : array();
493  }
494 
495 
500  public static function getAssignedObjects()
501  {
502  return self::$assignedObjects ? self::$assignedObjects : array();
503  }
504 
505  function _checkGroupingDependencies(&$container_obj, $a_user_id = null)
506  {
507  global $ilUser,$lng,$tree;
508 
509  include_once './classes/class.ilConditionHandler.php';
510 
511  $user_id = is_null($a_user_id) ? $ilUser->getId() : $a_user_id;
512 
513 
514  $trigger_ids = array();
515  foreach(ilConditionHandler::_getConditionsOfTarget($container_obj->getRefId(),
516  $container_obj->getId(),
517  $container_obj->getType()) as $condition)
518  {
519  if($condition['operator'] == 'not_member')
520  {
521  $trigger_ids[] = $condition['trigger_obj_id'];
522  break;
523  }
524  }
525  if(!count($trigger_ids))
526  {
527  return true;
528  }
529  $matriculation_message = $assigned_message = '';
530  self::$assignedObjects = array();
531  foreach($trigger_ids as $trigger_id)
532  {
533  foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$trigger_id) as $condition)
534  {
535  // Handle deleted items
536  if($tree->isDeleted($condition['target_ref_id']))
537  {
538  continue;
539  }
540  if($condition['operator'] == 'not_member')
541  {
542  switch($condition['value'])
543  {
544  case 'matriculation':
545  if(!strlen(ilObjUser::lookupMatriculation($user_id)))
546  {
547  if(!$matriculation_message)
548  {
549  $matriculation_message = $lng->txt('crs_grp_matriculation_required');
550  }
551  }
552  }
553  if($container_obj->getType() == 'crs')
554  {
555  include_once('Modules/Course/classes/class.ilCourseParticipants.php');
556  $members = ilCourseParticipants::_getInstanceByObjId($condition['target_obj_id']);
557  if($members->isGroupingMember($user_id,$condition['value']))
558  {
559  if(!$assigned_message)
560  {
561  self::$assignedObjects[] = $condition['target_obj_id'];
562  $assigned_message = $lng->txt('crs_grp_already_assigned');
563  }
564  }
565  }
566  elseif($container_obj->getType() == 'grp')
567  {
568  include_once('Modules/Group/classes/class.ilGroupParticipants.php');
569  $members = ilGroupParticipants::_getInstanceByObjId($condition['target_obj_id']);
570  if($members->isGroupingMember($user_id,$condition['value']))
571  {
572  if(!$assigned_message)
573  {
574  self::$assignedObjects[] = $condition['target_obj_id'];
575  $assigned_message = $lng->txt('crs_grp_already_assigned');
576  }
577  }
578 
579  }
580  else
581  {
582  if(ilObjGroup::_isMember($user_id,$condition['target_ref_id'],$condition['value']))
583  {
584  if(!$assigned_message)
585  {
586  self::$assignedObjects[] = $condition['target_obj_id'];
587  $assigned_message = $lng->txt('crs_grp_already_assigned');
588  }
589  }
590 
591  }
592  }
593  }
594  }
595  if($matriculation_message)
596  {
597  $container_obj->appendMessage($matriculation_message);
598  return false;
599  }
600  elseif($assigned_message)
601  {
602  $container_obj->appendMessage($assigned_message);
603  return false;
604  }
605  return true;
606  }
607 
608 
615  function _getGroupingItems($container_obj)
616  {
617  global $tree,$ilObjDataCache,$ilAccess,$tree;
618 
619  include_once './classes/class.ilConditionHandler.php';
620 
621  $trigger_ids = array();
622  foreach(ilConditionHandler::_getConditionsOfTarget($container_obj->getRefId(),
623  $container_obj->getId(),
624  $container_obj->getType()) as $condition)
625  {
626  if($condition['operator'] == 'not_member')
627  {
628  $trigger_ids[] = $condition['trigger_obj_id'];
629  }
630  }
631  if(!count($trigger_ids))
632  {
633  return false;
634  }
635  $hash_table = array();
636  foreach($trigger_ids as $trigger_id)
637  {
638  foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$trigger_id) as $condition)
639  {
640  // Continue if trigger is deleted
641  if($tree->isDeleted($condition['target_ref_id']))
642  {
643  continue;
644  }
645 
646  if($condition['operator'] == 'not_member')
647  {
648  if(!$hash_table[$condition['target_ref_id']])
649  {
650  $items[] = $condition['target_ref_id'];
651  }
652  $hash_table[$condition['target_ref_id']] = true;
653  }
654  }
655  }
656  return $items ? $items : array();
657  }
658 
659 } // END class.ilObjCourseGrouping
660 ?>