ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilLPCollectionOfRepositoryObjects.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once "Services/Tracking/classes/collection/class.ilLPCollection.php";
6 
17 {
18  protected static $possible_items = array();
19 
20  public function getPossibleItems($a_ref_id, $a_full_data = false)
21  {
22  global $tree, $objDefinition;
23 
24  $cache_idx = $a_ref_id."__".$a_full_data;
25 
26  if(!isset(self::$possible_items[$cache_idx]))
27  {
28  $all_possible = array();
29 
30  if(!$tree->isDeleted($a_ref_id))
31  {
32  include_once 'Services/Repository/classes/class.ilRepositoryObjectPluginSlot.php';
33 
34  if(!$a_full_data)
35  {
36  $data = $tree->getRbacSubTreeInfo($a_ref_id);
37  }
38  else
39  {
40  $node = $tree->getNodeData($a_ref_id);
41  $data = $tree->getSubTree($node);
42  }
43  foreach($data as $node)
44  {
45  if(!$a_full_data)
46  {
47  $item_ref_id = $node['child'];
48  }
49  else
50  {
51  $item_ref_id = $node['ref_id'];
52  }
53 
54  // avoid recursion
55  if($item_ref_id == $a_ref_id ||
56  !$this->validateEntry($item_ref_id, $node['type']))
57  {
58  continue;
59  }
60 
61  switch($node['type'])
62  {
63  case 'sess':
64  case 'exc':
65  case 'fold':
66  case 'grp':
67  case 'sahs':
68  case 'lm':
69  case 'tst':
70  case 'file':
71  case 'mcst':
72  case 'htlm':
73  case 'svy':
74  case "prg":
75  if(!$a_full_data)
76  {
77  $all_possible[] = $item_ref_id;
78  }
79  else
80  {
81  $all_possible[$item_ref_id] = array(
82  'ref_id' => $item_ref_id,
83  'obj_id' => $node['obj_id'],
84  'title' => $node['title'],
85  'description' => $node['description'],
86  'type' => $node['type']
87  );
88  }
89  break;
90 
91  // repository plugin object?
92  case $objDefinition->isPluginTypeName($node['type']):
93  $only_active = false;
94  if(!$this->isAssignedEntry($item_ref_id))
95  {
96  $only_active = true;
97  }
98  if(ilRepositoryObjectPluginSlot::isTypePluginWithLP($node['type'], $only_active))
99  {
100  if(!$a_full_data)
101  {
102  $all_possible[] = $item_ref_id;
103  }
104  else
105  {
106  $all_possible[$item_ref_id] = array(
107  'ref_id' => $item_ref_id,
108  'obj_id' => $node['obj_id'],
109  'title' => $node['title'],
110  'description' => $node['description'],
111  'type' => $node['type']
112  );
113  }
114  }
115  break;
116  }
117  }
118  }
119 
120  self::$possible_items[$cache_idx] = $all_possible;
121  }
122 
123  return self::$possible_items[$cache_idx];
124  }
125 
126  protected function validateEntry($a_item_ref_id, $a_item_type = null)
127  {
128  if(!$a_item_type)
129  {
130  $a_item_type = ilObject::_lookupType($a_item_ref_id, true);
131  }
132 
133  // this is hardcoded so we do not need to call all ObjectLP types
134  if($a_item_type == 'tst')
135  {
136  // Check anonymized
137  $item_obj_id = ilObject::_lookupObjId($a_item_ref_id);
138  $olp = ilObjectLP::getInstance($item_obj_id);
139  if($olp->isAnonymized())
140  {
141  return false;
142  }
143  }
144  return true;
145  }
146 
147  public function cloneCollection($a_target_id, $a_copy_id)
148  {
149  parent::cloneCollection($a_target_id, $a_copy_id);
150 
151  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
152  $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
153  $mappings = $cwo->getMappings();
154 
155  $target_obj_id = ilObject::_lookupObjId($a_target_id);
156  $target_collection = new static($target_obj_id, $this->mode);
157 
158  // clone (active) groupings
159  foreach($this->getGroupedItemsForLPStatus() as $grouping_id => $group)
160  {
161  $target_item_ids = array();
162  foreach($group["items"] as $item)
163  {
164  if(!isset($mappings[$item]) or !$mappings[$item])
165  {
166  continue;
167  }
168 
169  $target_item_ids[] = $mappings[$item];
170  }
171 
172  // grouping - if not only single item left after copy?
173  if($grouping_id && sizeof($target_item_ids) > 1)
174  {
175  // should not be larger than group
176  $num_obligatory = min(sizeof($target_item_ids), $group["num_obligatory"]);
177 
178  $target_collection->createNewGrouping($target_item_ids, $num_obligatory);
179  }
180  else
181  {
182  // #15487 - single items
183  foreach($target_item_ids as $item_id)
184  {
185  $this->addEntry($item_id);
186  }
187  }
188  }
189  }
190 
191 
192  //
193  // CRUD
194  //
195 
196  protected function read()
197  {
198  global $ilDB;
199 
200  $items = array();
201 
202  $ref_ids = ilObject::_getAllReferences($this->obj_id);
203  $ref_id = end($ref_ids);
204  $possible = $this->getPossibleItems($ref_id);
205 
206  $res = $ilDB->query("SELECT utc.item_id, obd.type".
207  " FROM ut_lp_collections utc".
208  " JOIN object_reference obr ON item_id = ref_id".
209  " JOIN object_data obd ON obr.obj_id = obd.obj_id".
210  " WHERE utc.obj_id = ".$ilDB->quote($this->obj_id, "integer").
211  " AND active = ".$ilDB->quote(1, "integer").
212  " ORDER BY title");
213  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
214  {
215  if(in_array($row->item_id, $possible) &&
216  $this->validateEntry($row->item_id, $row->type))
217  {
218  $items[] = $row->item_id;
219  }
220  else
221  {
222  $this->deleteEntry($row->item_id);
223  }
224  }
225 
226  $this->items = $items;
227  }
228 
229  protected function addEntry($a_item_id)
230  {
231  global $ilDB;
232 
233  // only active entries are assigned!
234  if(!$this->isAssignedEntry($a_item_id))
235  {
236  // #13278 - because of grouping inactive items may exist
237  $this->deleteEntry($a_item_id);
238 
239  $query = "INSERT INTO ut_lp_collections".
240  " (obj_id, lpmode, item_id, grouping_id, num_obligatory, active)".
241  " VALUES (".$ilDB->quote($this->obj_id , "integer").
242  ", ".$ilDB->quote($this->mode, "integer").
243  ", ".$ilDB->quote($a_item_id , "integer").
244  ", ".$ilDB->quote(0, "integer").
245  ", ".$ilDB->quote(0, "integer").
246  ", ".$ilDB->quote(1, "integer").
247  ")";
248  $ilDB->manipulate($query);
249  $this->items[] = $a_item_id;
250  }
251  return true;
252  }
253 
254  protected function deleteEntry($a_item_id)
255  {
256  global $ilDB;
257 
258  $query = "DELETE FROM ut_lp_collections ".
259  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
260  " AND item_id = ".$ilDB->quote($a_item_id, "integer").
261  " AND grouping_id = ".$ilDB->quote(0, "integer");
262  $ilDB->manipulate($query);
263  return true;
264  }
265 
266 
267  //
268  // GROUPING
269  //
270 
271  public static function hasGroupedItems($a_obj_id)
272  {
273  global $ilDB;
274 
275  $query = "SELECT item_id FROM ut_lp_collections".
276  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer").
277  " AND grouping_id > ".$ilDB->quote(0, "integer");
278  $res = $ilDB->query($query);
279  return $res->numRows() ? true : false;
280  }
281 
282  protected function getGroupingIds(array $a_item_ids)
283  {
284  global $ilDB;
285 
286  $grouping_ids = array();
287 
288  $query = "SELECT grouping_id FROM ut_lp_collections".
289  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
290  " AND ".$ilDB->in("item_id", $a_item_ids, false, "integer").
291  " AND grouping_id > ".$ilDB->quote(0, "integer");
292  $res = $ilDB->query($query);
293  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
294  {
295  $grouping_ids[] = $row->grouping_id;
296  }
297 
298  return $grouping_ids;
299  }
300 
301  public function deactivateEntries(array $a_item_ids)
302  {
303  global $ilDB;
304 
305  parent::deactivateEntries($a_item_ids);
306 
307  $grouping_ids = $this->getGroupingIds($a_item_ids);
308  if($grouping_ids)
309  {
310  $query = "UPDATE ut_lp_collections".
311  " SET active = ".$ilDB->quote(0, "integer").
312  " WHERE ".$ilDB->in("grouping_id", $grouping_ids, false, "integer").
313  " AND obj_id = ".$ilDB->quote($this->obj_id, "integer");
314  $ilDB->manipulate($query);
315  }
316  }
317 
318  public function activateEntries(array $a_item_ids)
319  {
320  global $ilDB;
321 
322  parent::activateEntries($a_item_ids);
323 
324  $grouping_ids = $this->getGroupingIds($a_item_ids);
325  if($grouping_ids)
326  {
327  $query = "UPDATE ut_lp_collections".
328  " SET active = ".$ilDB->quote(1, "integer").
329  " WHERE ".$ilDB->in("grouping_id", $grouping_ids, false, "integer").
330  " AND obj_id = ".$ilDB->quote($this->obj_id, "integer");
331  $ilDB->manipulate($query);
332  }
333  }
334 
335  public function createNewGrouping(array $a_item_ids, $a_num_obligatory = 1)
336  {
337  global $ilDB;
338 
339  $this->activateEntries($a_item_ids);
340 
341  $all_item_ids = array();
342 
343  $grouping_ids = $this->getGroupingIds($a_item_ids);
344 
345  $query = "SELECT item_id FROM ut_lp_collections".
346  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
347  " AND ".$ilDB->in("grouping_id", $grouping_ids, false, "integer");
348  $res = $ilDB->query($query);
349  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
350  {
351  $all_item_ids[] = $row->item_id;
352  }
353 
354  $all_item_ids = array_unique(array_merge($all_item_ids, $a_item_ids));
355 
356  $this->releaseGrouping($a_item_ids);
357 
358  // Create new grouping
359  $query = "SELECT MAX(grouping_id) grp FROM ut_lp_collections".
360  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
361  " GROUP BY obj_id";
362  $res = $ilDB->query($query);
363  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
364  $grp_id = $row->grp;
365  ++$grp_id;
366 
367  $query = "UPDATE ut_lp_collections SET".
368  " grouping_id = ".$ilDB->quote($grp_id, "integer").
369  ", num_obligatory = ".$ilDB->quote($a_num_obligatory, "integer").
370  ", active = ".$ilDB->quote(1, "integer").
371  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
372  " AND ".$ilDB->in("item_id", $all_item_ids, false, "integer");
373  $ilDB->manipulate($query);
374 
375  return;
376  }
377 
378  public function releaseGrouping(array $a_item_ids)
379  {
380  global $ilDB;
381 
382  $grouping_ids = $this->getGroupingIds($a_item_ids);
383 
384  $query = "UPDATE ut_lp_collections".
385  " SET grouping_id = ".$ilDB->quote(0, "integer").
386  ", num_obligatory = ".$ilDB->quote(0, "integer").
387  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
388  " AND " . $ilDB->in("grouping_id", $grouping_ids, false, "integer");
389  $ilDB->manipulate($query);
390  }
391 
392  public function saveObligatoryMaterials(array $a_obl)
393  {
394  global $ilDB;
395 
396  foreach($a_obl as $grouping_id => $num)
397  {
398  $query = "SELECT count(obj_id) num FROM ut_lp_collections".
399  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
400  " AND grouping_id = ".$ilDB->quote($grouping_id,'integer').
401  " GROUP BY obj_id";
402  $res = $ilDB->query($query);
403  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
404  {
405  if($num <= 0 || $num >= $row->num)
406  {
407  throw new UnexpectedValueException();
408  }
409  }
410  }
411  foreach($a_obl as $grouping_id => $num)
412  {
413  $query = "UPDATE ut_lp_collections".
414  " SET num_obligatory = ".$ilDB->quote($num, "integer").
415  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
416  " AND grouping_id = ".$ilDB->quote($grouping_id, "integer");
417  $ilDB->manipulate($query);
418  }
419  }
420 
421 
422  //
423  // TABLE GUI
424  //
425 
426  public function getTableGUIData($a_parent_ref_id)
427  {
428  $items = $this->getPossibleItems($a_parent_ref_id, true);
429 
430  $data = array();
431  $done = array();
432  foreach($items as $item_id => $item)
433  {
434  if(in_array($item_id, $done))
435  {
436  continue;
437  }
438 
439  $table_item = $this->parseTableGUIItem($item_id, $item);
440 
441  // grouping
442  $table_item['grouped'] = array();
443  $grouped_items = $this->getTableGUItemGroup($item_id);
444  if(count((array)$grouped_items['items']) > 1)
445  {
446  foreach($grouped_items['items'] as $grouped_item_id)
447  {
448  if($grouped_item_id == $item_id ||
449  !is_array($items[$grouped_item_id])) // #15498
450  {
451  continue;
452  }
453 
454  $table_item['grouped'][] = $this->parseTableGUIItem($grouped_item_id, $items[$grouped_item_id]);
455  $table_item['num_obligatory'] = $grouped_items['num_obligatory'];
456  $table_item['grouping_id'] = $grouped_items['grouping_id'];
457 
458  $done[] = $grouped_item_id;
459  }
460  }
461 
462  $data[] = $table_item;
463  }
464 
465  return $data;
466  }
467 
468  protected function parseTableGUIItem($a_id, array $a_item)
469  {
470  $table_item = $a_item;
471  $table_item['id'] = $a_id;
472  $table_item['status'] = $this->isAssignedEntry($a_id);
473 
474  $olp = ilObjectLP::getInstance($a_item['obj_id']);
475  $table_item['mode_id'] = $olp->getCurrentMode();
476  $table_item['mode'] = $olp->getModeText($table_item['mode_id']);
477  $table_item['anonymized'] = $olp->isAnonymized();
478 
479  return $table_item;
480  }
481 
482  protected function getTableGUItemGroup($item_id)
483  {
484  global $ilDB;
485 
486  $items = array();
487 
488  $query = "SELECT grouping_id FROM ut_lp_collections".
489  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
490  " AND item_id = ".$ilDB->quote($item_id, "integer");
491  $res = $ilDB->query($query);
492  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
493  $grouping_id = $row->grouping_id;
494  if($grouping_id > 0)
495  {
496  $query = "SELECT item_id, num_obligatory FROM ut_lp_collections".
497  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
498  " AND grouping_id = ".$ilDB->quote($grouping_id, "integer");
499  $res = $ilDB->query($query);
500  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
501  {
502  $items['items'][] = $row->item_id;
503  $items['num_obligatory'] = $row->num_obligatory;
504  $items['grouping_id'] = $grouping_id;
505  }
506  }
507 
508  return $items;
509  }
510 
511  public function getGroupedItemsForLPStatus()
512  {
513  global $ilDB;
514 
515  $items = $this->getItems();
516 
517  $query = " SELECT * FROM ut_lp_collections".
518  " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer").
519  " AND active = ".$ilDB->quote(1, "integer");
520  $res = $ilDB->query($query);
521 
522  $grouped = array();
523  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
524  {
525  if(in_array($row->item_id, $items))
526  {
527  $grouped[$row->grouping_id]['items'][] = $row->item_id;
528  $grouped[$row->grouping_id]['num_obligatory'] = $row->num_obligatory;
529  }
530  }
531 
532  return $grouped;
533  }
534 }
535 
536 ?>
static isTypePluginWithLP($a_type, $a_active_status=true)
Check whether a repository type is a plugin which has active learning progress.
createNewGrouping(array $a_item_ids, $a_num_obligatory=1)
static _getAllReferences($a_id)
get all reference ids of object
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
static _getInstance($a_copy_id)
Get instance of copy wizard options.
$data
static _lookupObjId($a_id)
static _lookupType($a_id, $a_reference=false)
lookup object type
LP collection base class.
$ref_id
Definition: sahs_server.php:39
isAssignedEntry($a_item_id)
global $ilDB
static getInstance($a_obj_id)