ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilLPCollection.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=0);
25 abstract class ilLPCollection
26 {
27  protected int $obj_id;
28  protected int $mode;
29  protected array $items = [];
30 
31  protected ilDBInterface $db;
32  protected ilLogger $logger;
33 
34  public function __construct(int $a_obj_id, int $a_mode)
35  {
36  global $DIC;
37 
38  $this->db = $DIC->database();
39  $this->logger = $DIC->logger()->trac();
40 
41  $this->obj_id = $a_obj_id;
42  $this->mode = $a_mode;
43 
44  if ($a_obj_id) {
45  $this->read($a_obj_id);
46  }
47  }
48 
49  public static function getInstanceByMode(
50  int $a_obj_id,
51  int $a_mode
52  ): ?ilLPCollection {
53  $path = "components/ILIAS/Tracking/classes/collection/";
54 
55  switch ($a_mode) {
59  $a_obj_id,
60  $a_mode
61  );
62 
64  return new ilLPCollectionOfObjectives($a_obj_id, $a_mode);
65 
67  return new ilLPCollectionOfSCOs($a_obj_id, $a_mode);
68 
71  return new ilLPCollectionOfLMChapters($a_obj_id, $a_mode);
72 
74  return new ilLPCollectionOfMediaObjects($a_obj_id, $a_mode);
75  }
76  return null;
77  }
78 
79  public static function getCollectionModes(): array
80  {
81  return array(
83  ,
85  ,
87  ,
89  ,
91  ,
93  );
94  }
95 
96  public function hasSelectableItems(): bool
97  {
98  return true;
99  }
100 
101  public function cloneCollection(int $a_target_id, int $a_copy_id): void
102  {
103  $target_obj_id = ilObject::_lookupObjId($a_target_id);
104  $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
105  $mappings = $cwo->getMappings();
106 
107  // #12067
108  $new_collection = new static($target_obj_id, $this->mode);
109  foreach ($this->items as $item) {
110  if (!isset($mappings[$item]) or !$mappings[$item]) {
111  continue;
112  }
113 
114  $new_collection->addEntry($mappings[$item]);
115  }
116  $this->logger->debug('cloned learning progress collection.');
117  }
118 
119  public function getItems(): array
120  {
121  return $this->items;
122  }
123 
124  protected function read(int $a_obj_id): void
125  {
126  $items = array();
127  $res = $this->db->query(
128  "SELECT * FROM ut_lp_collections" .
129  " WHERE obj_id = " . $this->db->quote($a_obj_id, "integer")
130  );
131  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
132  if ($this->validateEntry((int) $row->item_id)) {
133  $items[] = $row->item_id;
134  } else {
135  $this->deleteEntry($row->item_id);
136  }
137  }
138  $this->items = $items;
139  }
140 
141  public function delete(): void
142  {
143  $query = "DELETE FROM ut_lp_collections" .
144  " WHERE obj_id = " . $this->db->quote($this->obj_id, "integer");
145  $this->db->manipulate($query);
146 
147  $query = "DELETE FROM ut_lp_coll_manual" .
148  " WHERE obj_id = " . $this->db->quote($this->obj_id, "integer");
149  $this->db->manipulate($query);
150  // #15462 - reset internal data
151  $this->items = array();
152  }
153 
154  //
155  // ENTRIES
156  //
157 
158  protected function validateEntry(int $a_item_id): bool
159  {
160  return true;
161  }
162 
163  public function isAssignedEntry(int $a_item_id): bool
164  {
165  if (is_array($this->items)) {
166  return in_array($a_item_id, $this->items);
167  }
168  return false;
169  }
170 
171  protected function addEntry(int $a_item_id): bool
172  {
173  if (!$this->isAssignedEntry($a_item_id)) {
174  $query = "INSERT INTO ut_lp_collections" .
175  " (obj_id, lpmode, item_id)" .
176  " VALUES (" . $this->db->quote($this->obj_id, "integer") .
177  ", " . $this->db->quote($this->mode, "integer") .
178  ", " . $this->db->quote($a_item_id, "integer") .
179  ")";
180  $this->db->manipulate($query);
181  $this->items[] = $a_item_id;
182  }
183  return true;
184  }
185 
186  protected function deleteEntry(int $a_item_id): bool
187  {
188  $query = "DELETE FROM ut_lp_collections" .
189  " WHERE obj_id = " . $this->db->quote($this->obj_id, "integer") .
190  " AND item_id = " . $this->db->quote($a_item_id, "integer");
191  $this->db->manipulate($query);
192  return true;
193  }
194 
198  public function deactivateEntries(array $a_item_ids): void
199  {
200  foreach ($a_item_ids as $item_id) {
201  $this->deleteEntry($item_id);
202  }
203  }
204 
208  public function activateEntries(array $a_item_ids): void
209  {
210  foreach ($a_item_ids as $item_id) {
211  $this->addEntry($item_id);
212  }
213  }
214 }
cloneCollection(int $a_target_id, int $a_copy_id)
$res
Definition: ltiservices.php:66
LP collection of learning module chapters.
deleteEntry(int $a_item_id)
isAssignedEntry(int $a_item_id)
$path
Definition: ltiservices.php:29
static _lookupObjId(int $ref_id)
validateEntry(int $a_item_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getInstanceByMode(int $a_obj_id, int $a_mode)
deactivateEntries(array $a_item_ids)
global $DIC
Definition: shib_login.php:22
__construct(int $a_obj_id, int $a_mode)
LP collection base class.
addEntry(int $a_item_id)
activateEntries(array $a_item_ids)
static _getInstance(int $a_copy_id)