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