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