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