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