ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilTaxNodeAssignment.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  protected ilDBInterface $db;
27  protected string $component_id;
28  protected int $taxonomy_id;
29  protected string $item_type;
30  protected int $obj_id;
31 
41  public function __construct(
42  string $a_component_id,
43  int $a_obj_id,
44  string $a_item_type,
45  int $a_tax_id,
46  ilDBInterface $db = null
47  ) {
48  global $DIC;
49 
50  $this->db = (is_null($db))
51  ? $DIC->database()
52  : $db;
53 
54  if ($a_component_id == "") {
55  throw new ilTaxonomyException('No component ID passed to ilTaxNodeAssignment.');
56  }
57 
58  if ($a_item_type == "") {
59  throw new ilTaxonomyException('No item type passed to ilTaxNodeAssignment.');
60  }
61 
62  if ($a_tax_id == 0) {
63  throw new ilTaxonomyException('No taxonomy ID passed to ilTaxNodeAssignment.');
64  }
65 
66  $this->setComponentId($a_component_id);
67  $this->setItemType($a_item_type);
68  $this->setTaxonomyId($a_tax_id);
69  $this->setObjectId($a_obj_id);
70  }
71 
72  protected function setComponentId(string $a_val): void
73  {
74  $this->component_id = $a_val;
75  }
76 
77  public function getComponentId(): string
78  {
79  return $this->component_id;
80  }
81 
82  protected function setItemType(string $a_val): void
83  {
84  $this->item_type = $a_val;
85  }
86 
87  public function getItemType(): string
88  {
89  return $this->item_type;
90  }
91 
92  protected function setTaxonomyId(int $a_val): void
93  {
94  $this->taxonomy_id = $a_val;
95  }
96 
97  public function getTaxonomyId(): int
98  {
99  return $this->taxonomy_id;
100  }
101 
102  public function setObjectId(int $a_val): void
103  {
104  $this->obj_id = $a_val;
105  }
106 
107  public function getObjectId(): int
108  {
109  return $this->obj_id;
110  }
111 
117  final public function getAssignmentsOfNode($a_node_id): array
118  {
119  $ilDB = $this->db;
120 
121  if (is_array($a_node_id)) {
122  $set = $ilDB->query(
123  "SELECT * FROM tax_node_assignment " .
124  " WHERE " . $ilDB->in("node_id", $a_node_id, false, "integer") .
125  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
126  " AND component = " . $ilDB->quote($this->getComponentId(), "text") .
127  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
128  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
129  " ORDER BY order_nr ASC"
130  );
131  } else {
132  $set = $ilDB->query(
133  "SELECT * FROM tax_node_assignment " .
134  " WHERE node_id = " . $ilDB->quote($a_node_id, "integer") .
135  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
136  " AND component = " . $ilDB->quote($this->getComponentId(), "text") .
137  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
138  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
139  " ORDER BY order_nr ASC"
140  );
141  }
142  $ass = array();
143  while ($rec = $ilDB->fetchAssoc($set)) {
144  $ass[] = $rec;
145  }
146 
147  return $ass;
148  }
149 
154  final public function getAssignmentsOfItem(int $a_item_id): array
155  {
156  $ilDB = $this->db;
157 
158  $set = $ilDB->query(
159  "SELECT * FROM tax_node_assignment" .
160  " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
161  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
162  " AND item_id = " . $ilDB->quote($a_item_id, "integer") .
163  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
164  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
165  );
166  $ass = array();
167  while ($rec = $ilDB->fetchAssoc($set)) {
168  $ass[] = $rec;
169  }
170  return $ass;
171  }
172 
177  public function addAssignment(int $a_node_id, int $a_item_id, int $a_order_nr = 0): void
178  {
179  $ilDB = $this->db;
180 
181  // nothing to do, if not both IDs are greater 0
182  if ($a_node_id == 0 || $a_item_id == 0) {
183  return;
184  }
185 
186  // sanity check: does the node belong to the given taxonomy?
187  $set = $ilDB->query(
188  "SELECT tax_tree_id FROM tax_tree " .
189  " WHERE child = " . $ilDB->quote($a_node_id, "integer")
190  );
191  $rec = $ilDB->fetchAssoc($set);
192  if ($rec["tax_tree_id"] != $this->getTaxonomyId()) {
193  throw new ilTaxonomyException('addAssignment: Node ID does not belong to current taxonomy.');
194  }
195 
196  // do not re-assign, if assignment already exists
197  // order number should be kept in this case
198  $set2 = $ilDB->query(
199  $q = "SELECT item_id FROM tax_node_assignment " .
200  " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
201  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
202  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
203  " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
204  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
205  " AND item_id = " . $ilDB->quote($a_item_id, "integer")
206  );
207  if ($rec2 = $ilDB->fetchAssoc($set2)) {
208  return;
209  }
210 
211  if ($a_order_nr == 0) {
212  $a_order_nr = $this->getMaxOrderNr($a_node_id) + 10;
213  }
214 
215  $ilDB->replace(
216  "tax_node_assignment",
217  array(
218  "node_id" => array("integer", $a_node_id),
219  "component" => array("text", $this->getComponentId()),
220  "item_type" => array("text", $this->getItemType()),
221  "obj_id" => array("integer", $this->getObjectId()),
222  "item_id" => array("integer", $a_item_id)
223  ),
224  array(
225  "tax_id" => array("integer", $this->getTaxonomyId()),
226  "order_nr" => array("integer", $a_order_nr)
227  )
228  );
229  }
230 
231  public function deleteAssignment(int $a_node_id, int $a_item_id): void
232  {
233  $ilDB = $this->db;
234 
235  // nothing to do, if not both IDs are greater 0
236  if ($a_node_id == 0 || $a_item_id == 0) {
237  return;
238  }
239 
240  // sanity check: does the node belong to the given taxonomy?
241  $set = $ilDB->query(
242  "SELECT tax_tree_id FROM tax_tree " .
243  " WHERE child = " . $ilDB->quote($a_node_id, "integer")
244  );
245  $rec = $ilDB->fetchAssoc($set);
246  if ((int) $rec["tax_tree_id"] != $this->getTaxonomyId()) {
247  throw new ilTaxonomyException('addAssignment: Node ID does not belong to current taxonomy.');
248  }
249 
250  $ilDB->manipulate(
251  "DELETE FROM tax_node_assignment WHERE " .
252  " component = " . $ilDB->quote($this->getComponentId(), "text") .
253  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
254  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
255  " AND item_id = " . $ilDB->quote($a_item_id, "integer") .
256  " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
257  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
258  );
259  }
260 
261  public function getMaxOrderNr(int $a_node_id): int
262  {
263  $ilDB = $this->db;
264 
265  $set = $ilDB->query(
266  "SELECT max(order_nr) mnr FROM tax_node_assignment " .
267  " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
268  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
269  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
270  " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
271  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
272  );
273  $rec = $ilDB->fetchAssoc($set);
274 
275  return (int) $rec["mnr"];
276  }
277 
278  public function setOrderNr(int $a_node_id, int $a_item_id, int $a_order_nr): void
279  {
280  $ilDB = $this->db;
281 
282  $ilDB->manipulate(
283  "UPDATE tax_node_assignment SET " .
284  " order_nr = " . $ilDB->quote($a_order_nr, "integer") .
285  " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
286  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
287  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
288  " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
289  " AND item_id = " . $ilDB->quote($a_item_id, "integer") .
290  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
291  );
292  }
293 
294  public function deleteAssignmentsOfItem(int $a_item_id): void
295  {
296  $ilDB = $this->db;
297 
298  $ilDB->manipulate(
299  "DELETE FROM tax_node_assignment WHERE " .
300  " component = " . $ilDB->quote($this->getComponentId(), "text") .
301  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
302  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
303  " AND item_id = " . $ilDB->quote($a_item_id, "integer") .
304  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
305  );
306  }
307 
308  public function deleteAssignmentsOfNode(int $a_node_id): void
309  {
310  $ilDB = $this->db;
311 
312  $ilDB->manipulate(
313  "DELETE FROM tax_node_assignment WHERE " .
314  " node_id = " . $ilDB->quote($a_node_id, "integer") .
315  " AND component = " . $ilDB->quote($this->getComponentId(), "text") .
316  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
317  " AND item_type = " . $ilDB->quote($this->getItemType(), "text")
318  );
319  }
320 
321  public static function deleteAllAssignmentsOfNode(int $a_node_id): void
322  {
323  global $DIC;
324 
325  $ilDB = $DIC->database();
326 
327  $ilDB->manipulate(
328  "DELETE FROM tax_node_assignment WHERE " .
329  " node_id = " . $ilDB->quote($a_node_id, "integer")
330  );
331  }
332 
333  // renumber with 10, 20, ...
334  public function fixOrderNr(int $a_node_id): void
335  {
336  $ilDB = $this->db;
337 
338  $set = $ilDB->query(
339  "SELECT * FROM tax_node_assignment " .
340  " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
341  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
342  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
343  " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
344  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
345  " ORDER BY order_nr ASC"
346  );
347  $cnt = 10;
348  while ($rec = $ilDB->fetchAssoc($set)) {
349  $ilDB->manipulate(
350  "UPDATE tax_node_assignment SET " .
351  " order_nr = " . $ilDB->quote($cnt, "integer") .
352  " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
353  " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
354  " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
355  " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
356  " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
357  " AND item_id = " . $ilDB->quote($rec["item_id"], "integer")
358  );
359  $cnt += 10;
360  }
361  }
362 
366  public static function findObjectsByNode(int $a_tax_id, array $a_node_ids, string $a_item_type): array
367  {
368  global $DIC;
369 
370  $ilDB = $DIC->database();
371 
372  $res = array();
373 
374  $set = $ilDB->query(
375  "SELECT * FROM tax_node_assignment" .
376  " WHERE " . $ilDB->in("node_id", $a_node_ids, "", "integer") .
377  " AND tax_id = " . $ilDB->quote($a_tax_id, "integer") .
378  " AND item_type = " . $ilDB->quote($a_item_type, "text") .
379  " ORDER BY order_nr ASC"
380  );
381  while ($row = $ilDB->fetchAssoc($set)) {
382  $res[] = (int) $row["obj_id"];
383  }
384 
385  return $res;
386  }
387 }
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
deleteAssignment(int $a_node_id, int $a_item_id)
static findObjectsByNode(int $a_tax_id, array $a_node_ids, string $a_item_type)
Find object which have assigned nodes.
addAssignment(int $a_node_id, int $a_item_id, int $a_order_nr=0)
Add assignment.
deleteAssignmentsOfItem(int $a_item_id)
static deleteAllAssignmentsOfNode(int $a_node_id)
getAssignmentsOfNode($a_node_id)
Get assignments of node.
setOrderNr(int $a_node_id, int $a_item_id, int $a_order_nr)
global $DIC
Definition: feed.php:28
__construct(string $a_component_id, int $a_obj_id, string $a_item_type, int $a_tax_id, ilDBInterface $db=null)
Constructor.
deleteAssignmentsOfNode(int $a_node_id)
getAssignmentsOfItem(int $a_item_id)
Get assignments for item.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...