ILIAS  release_8 Revision v8.24
class.ilLPCollection.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=0);
4/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
5
11abstract class ilLPCollection
12{
13 protected int $obj_id;
14 protected int $mode;
15 protected array $items = [];
16
17 protected ilDBInterface $db;
18 protected ilLogger $logger;
19
20 public function __construct(int $a_obj_id, int $a_mode)
21 {
22 global $DIC;
23
24 $this->db = $DIC->database();
25 $this->logger = $DIC->logger()->trac();
26
27 $this->obj_id = $a_obj_id;
28 $this->mode = $a_mode;
29
30 if ($a_obj_id) {
31 $this->read($a_obj_id);
32 }
33 }
34
35 public static function getInstanceByMode(
36 int $a_obj_id,
37 int $a_mode
38 ): ?ilLPCollection {
39 $path = "Services/Tracking/classes/collection/";
40
41 switch ($a_mode) {
45 $a_obj_id,
46 $a_mode
47 );
48
50 return new ilLPCollectionOfObjectives($a_obj_id, $a_mode);
51
53 return new ilLPCollectionOfSCOs($a_obj_id, $a_mode);
54
57 return new ilLPCollectionOfLMChapters($a_obj_id, $a_mode);
58
60 return new ilLPCollectionOfMediaObjects($a_obj_id, $a_mode);
61 }
62 return null;
63 }
64
65 public static function getCollectionModes(): array
66 {
67 return array(
69 ,
71 ,
73 ,
75 ,
77 ,
79 );
80 }
81
82 public function hasSelectableItems(): bool
83 {
84 return true;
85 }
86
87 public function cloneCollection(int $a_target_id, int $a_copy_id): void
88 {
89 $target_obj_id = ilObject::_lookupObjId($a_target_id);
90 $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
91 $mappings = $cwo->getMappings();
92
93 // #12067
94 $new_collection = new static($target_obj_id, $this->mode);
95 foreach ($this->items as $item) {
96 if (!isset($mappings[$item]) or !$mappings[$item]) {
97 continue;
98 }
99
100 $new_collection->addEntry($mappings[$item]);
101 }
102 $this->logger->debug('cloned learning progress collection.');
103 }
104
105 public function getItems(): array
106 {
107 return $this->items;
108 }
109
110 protected function read(int $a_obj_id): void
111 {
112 $items = array();
113 $res = $this->db->query(
114 "SELECT * FROM ut_lp_collections" .
115 " WHERE obj_id = " . $this->db->quote($a_obj_id, "integer")
116 );
117 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
118 if ($this->validateEntry((int) $row->item_id)) {
119 $items[] = $row->item_id;
120 } else {
121 $this->deleteEntry($row->item_id);
122 }
123 }
124 $this->items = $items;
125 }
126
127 public function delete(): void
128 {
129 $query = "DELETE FROM ut_lp_collections" .
130 " WHERE obj_id = " . $this->db->quote($this->obj_id, "integer");
131 $this->db->manipulate($query);
132
133 $query = "DELETE FROM ut_lp_coll_manual" .
134 " WHERE obj_id = " . $this->db->quote($this->obj_id, "integer");
135 $this->db->manipulate($query);
136 // #15462 - reset internal data
137 $this->items = array();
138 }
139
140 //
141 // ENTRIES
142 //
143
144 protected function validateEntry(int $a_item_id): bool
145 {
146 return true;
147 }
148
149 public function isAssignedEntry(int $a_item_id): bool
150 {
151 if (is_array($this->items)) {
152 return in_array($a_item_id, $this->items);
153 }
154 return false;
155 }
156
157 protected function addEntry(int $a_item_id): bool
158 {
159 if (!$this->isAssignedEntry($a_item_id)) {
160 $query = "INSERT INTO ut_lp_collections" .
161 " (obj_id, lpmode, item_id)" .
162 " VALUES (" . $this->db->quote($this->obj_id, "integer") .
163 ", " . $this->db->quote($this->mode, "integer") .
164 ", " . $this->db->quote($a_item_id, "integer") .
165 ")";
166 $this->db->manipulate($query);
167 $this->items[] = $a_item_id;
168 }
169 return true;
170 }
171
172 protected function deleteEntry(int $a_item_id): bool
173 {
174 $query = "DELETE FROM ut_lp_collections" .
175 " WHERE obj_id = " . $this->db->quote($this->obj_id, "integer") .
176 " AND item_id = " . $this->db->quote($a_item_id, "integer");
177 $this->db->manipulate($query);
178 return true;
179 }
180
184 public function deactivateEntries(array $a_item_ids): void
185 {
186 foreach ($a_item_ids as $item_id) {
187 $this->deleteEntry($item_id);
188 }
189 }
190
194 public function activateEntries(array $a_item_ids): void
195 {
196 foreach ($a_item_ids as $item_id) {
197 $this->addEntry($item_id);
198 }
199 }
200}
static _getInstance(int $a_copy_id)
LP collection of learning module chapters.
LP collection base class.
validateEntry(int $a_item_id)
cloneCollection(int $a_target_id, int $a_copy_id)
isAssignedEntry(int $a_item_id)
deleteEntry(int $a_item_id)
__construct(int $a_obj_id, int $a_mode)
addEntry(int $a_item_id)
activateEntries(array $a_item_ids)
static getInstanceByMode(int $a_obj_id, int $a_mode)
deactivateEntries(array $a_item_ids)
Component logger with individual log levels by component id.
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$path
Definition: ltiservices.php:32
$res
Definition: ltiservices.php:69
$query