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