ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilTaxNodeAssignment.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once("./Services/Taxonomy/exceptions/class.ilTaxonomyException.php");
6
18{
22 protected $db;
23
33 public function __construct($a_component_id, $a_obj_id, $a_item_type, $a_tax_id)
34 {
35 global $DIC;
36
37 $this->db = $DIC->database();
38 if ($a_component_id == "") {
39 throw new ilTaxonomyException('No component ID passed to ilTaxNodeAssignment.');
40 }
41
42 if ($a_item_type == "") {
43 throw new ilTaxonomyException('No item type passed to ilTaxNodeAssignment.');
44 }
45
46 if ((int) $a_tax_id == 0) {
47 throw new ilTaxonomyException('No taxonomy ID passed to ilTaxNodeAssignment.');
48 }
49
50 $this->setComponentId($a_component_id);
51 $this->setItemType($a_item_type);
52 $this->setTaxonomyId($a_tax_id);
53 $this->setObjectId($a_obj_id);
54 }
55
61 protected function setComponentId($a_val)
62 {
63 $this->component_id = $a_val;
64 }
65
71 public function getComponentId()
72 {
73 return $this->component_id;
74 }
75
81 protected function setItemType($a_val)
82 {
83 $this->item_type = $a_val;
84 }
85
91 public function getItemType()
92 {
93 return $this->item_type;
94 }
95
101 protected function setTaxonomyId($a_val)
102 {
103 $this->taxonomy_id = $a_val;
104 }
105
111 public function getTaxonomyId()
112 {
113 return $this->taxonomy_id;
114 }
115
121 public function setObjectId($a_val)
122 {
123 $this->obj_id = $a_val;
124 }
125
131 public function getObjectId()
132 {
133 return $this->obj_id;
134 }
135
142 final public function getAssignmentsOfNode($a_node_id)
143 {
145
146 if (is_array($a_node_id)) {
147 $set = $ilDB->query(
148 "SELECT * FROM tax_node_assignment " .
149 " WHERE " . $ilDB->in("node_id", $a_node_id, false, "integer") .
150 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
151 " AND component = " . $ilDB->quote($this->getComponentId(), "text") .
152 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
153 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
154 " ORDER BY order_nr ASC"
155 );
156 } else {
157 $set = $ilDB->query(
158 "SELECT * FROM tax_node_assignment " .
159 " WHERE node_id = " . $ilDB->quote($a_node_id, "integer") .
160 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
161 " AND component = " . $ilDB->quote($this->getComponentId(), "text") .
162 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
163 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
164 " ORDER BY order_nr ASC"
165 );
166 }
167 $ass = array();
168 while ($rec = $ilDB->fetchAssoc($set)) {
169 $ass[] = $rec;
170 }
171
172 return $ass;
173 }
174
181 final public function getAssignmentsOfItem($a_item_id)
182 {
184
185 $set = $ilDB->query(
186 "SELECT * FROM tax_node_assignment" .
187 " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
188 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
189 " AND item_id = " . $ilDB->quote($a_item_id, "integer") .
190 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
191 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
192 );
193 $ass = array();
194 while ($rec = $ilDB->fetchAssoc($set)) {
195 $ass[] = $rec;
196 }
197 return $ass;
198 }
199
208 public function addAssignment($a_node_id, $a_item_id, $a_order_nr = 0)
209 {
211
212 // nothing to do, if not both IDs are greater 0
213 if ((int) $a_node_id == 0 || (int) $a_item_id == 0) {
214 return;
215 }
216
217 // sanity check: does the node belong to the given taxonomy?
218 $set = $ilDB->query(
219 "SELECT tax_tree_id FROM tax_tree " .
220 " WHERE child = " . $ilDB->quote($a_node_id, "integer")
221 );
222 $rec = $ilDB->fetchAssoc($set);
223 if ($rec["tax_tree_id"] != $this->getTaxonomyId()) {
224 throw new ilTaxonomyException('addAssignment: Node ID does not belong to current taxonomy.');
225 }
226
227 // do not re-assign, if assignment already exists
228 // order number should be kept in this case
229 $set2 = $ilDB->query(
230 $q = "SELECT item_id FROM tax_node_assignment " .
231 " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
232 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
233 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
234 " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
235 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
236 " AND item_id = " . $ilDB->quote($a_item_id, "integer")
237 );
238 if ($rec2 = $ilDB->fetchAssoc($set2)) {
239 return;
240 }
241
242 if ($a_order_nr == 0) {
243 $a_order_nr = $this->getMaxOrderNr($a_node_id) + 10;
244 }
245
246 $ilDB->replace(
247 "tax_node_assignment",
248 array(
249 "node_id" => array("integer", $a_node_id),
250 "component" => array("text", $this->getComponentId()),
251 "item_type" => array("text", $this->getItemType()),
252 "obj_id" => array("integer", $this->getObjectId()),
253 "item_id" => array("integer", $a_item_id)
254 ),
255 array(
256 "tax_id" => array("integer", $this->getTaxonomyId()),
257 "order_nr" => array("integer", $a_order_nr)
258 )
259 );
260 }
261
269 public function deleteAssignment($a_node_id, $a_item_id)
270 {
272
273 // nothing to do, if not both IDs are greater 0
274 if ((int) $a_node_id == 0 || (int) $a_item_id == 0) {
275 return;
276 }
277
278 // sanity check: does the node belong to the given taxonomy?
279 $set = $ilDB->query(
280 "SELECT tax_tree_id FROM tax_tree " .
281 " WHERE child = " . $ilDB->quote($a_node_id, "integer")
282 );
283 $rec = $ilDB->fetchAssoc($set);
284 if ($rec["tax_tree_id"] != $this->getTaxonomyId()) {
285 throw new ilTaxonomyException('addAssignment: Node ID does not belong to current taxonomy.');
286 }
287
288 $ilDB->manipulate(
289 "DELETE FROM tax_node_assignment WHERE " .
290 " component = " . $ilDB->quote($this->getComponentId(), "text") .
291 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
292 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
293 " AND item_id = " . $ilDB->quote($a_item_id, "integer") .
294 " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
295 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
296 );
297 }
298
305 public function getMaxOrderNr($a_node_id)
306 {
308
309 $set = $ilDB->query(
310 "SELECT max(order_nr) mnr FROM tax_node_assignment " .
311 " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
312 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
313 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
314 " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
315 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
316 );
317 $rec = $ilDB->fetchAssoc($set);
318
319 return (int) $rec["mnr"];
320 }
321
328 public function setOrderNr($a_node_id, $a_item_id, $a_order_nr)
329 {
331
332 $ilDB->manipulate(
333 "UPDATE tax_node_assignment SET " .
334 " order_nr = " . $ilDB->quote($a_order_nr, "integer") .
335 " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
336 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
337 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
338 " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
339 " AND item_id = " . $ilDB->quote($a_item_id, "integer") .
340 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
341 );
342 }
343
344
350 public function deleteAssignmentsOfItem($a_item_id)
351 {
353
354 $ilDB->manipulate(
355 "DELETE FROM tax_node_assignment WHERE " .
356 " component = " . $ilDB->quote($this->getComponentId(), "text") .
357 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
358 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
359 " AND item_id = " . $ilDB->quote($a_item_id, "integer") .
360 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer")
361 );
362 }
363
369 public function deleteAssignmentsOfNode($a_node_id)
370 {
372
373 $ilDB->manipulate(
374 "DELETE FROM tax_node_assignment WHERE " .
375 " node_id = " . $ilDB->quote($a_node_id, "integer") .
376 " AND component = " . $ilDB->quote($this->getComponentId(), "text") .
377 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
378 " AND item_type = " . $ilDB->quote($this->getItemType(), "text")
379 );
380 }
381
387 public static function deleteAllAssignmentsOfNode($a_node_id)
388 {
389 global $DIC;
390
391 $ilDB = $DIC->database();
392
393 $ilDB->manipulate(
394 "DELETE FROM tax_node_assignment WHERE " .
395 " node_id = " . $ilDB->quote($a_node_id, "integer")
396 );
397 }
398
402 public function fixOrderNr($a_node_id)
403 {
405
406 $set = $ilDB->query(
407 "SELECT * FROM tax_node_assignment " .
408 " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
409 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
410 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
411 " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
412 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
413 " ORDER BY order_nr ASC"
414 );
415 $cnt = 10;
416 while ($rec = $ilDB->fetchAssoc($set)) {
417 $ilDB->manipulate(
418 "UPDATE tax_node_assignment SET " .
419 " order_nr = " . $ilDB->quote($cnt, "integer") .
420 " WHERE component = " . $ilDB->quote($this->getComponentId(), "text") .
421 " AND item_type = " . $ilDB->quote($this->getItemType(), "text") .
422 " AND obj_id = " . $ilDB->quote($this->getObjectId(), "integer") .
423 " AND node_id = " . $ilDB->quote($a_node_id, "integer") .
424 " AND tax_id = " . $ilDB->quote($this->getTaxonomyId(), "integer") .
425 " AND item_id = " . $ilDB->quote($rec["item_id"], "integer")
426 );
427 $cnt += 10;
428 }
429 }
430
431
432
441 public static function findObjectsByNode($a_tax_id, array $a_node_ids, $a_item_type)
442 {
443 global $DIC;
444
445 $ilDB = $DIC->database();
446
447 $res = array();
448
449 $set = $ilDB->query(
450 "SELECT * FROM tax_node_assignment" .
451 " WHERE " . $ilDB->in("node_id", $a_node_ids, "", "integer") .
452 " AND tax_id = " . $ilDB->quote($a_tax_id, "integer") .
453 " AND item_type = " . $ilDB->quote($a_item_type, "text") .
454 " ORDER BY order_nr ASC"
455 );
456 while ($row = $ilDB->fetchAssoc($set)) {
457 $res[] = $row["obj_id"];
458 }
459
460 return $res;
461 }
462}
An exception for terminatinating execution or to throw for unit testing.
Taxonomy node <-> item assignment.
fixOrderNr($a_node_id)
Fix Order Nr.
getMaxOrderNr($a_node_id)
Get maximum order number.
deleteAssignmentsOfNode($a_node_id)
Delete assignments of node.
getAssignmentsOfNode($a_node_id)
Get assignments of node.
__construct($a_component_id, $a_obj_id, $a_item_type, $a_tax_id)
Constructor.
setComponentId($a_val)
Set component id.
static findObjectsByNode($a_tax_id, array $a_node_ids, $a_item_type)
Find object which have assigned nodes.
setTaxonomyId($a_val)
Set taxonomy id.
deleteAssignment($a_node_id, $a_item_id)
Delete assignment.
deleteAssignmentsOfItem($a_item_id)
Delete assignments of item.
addAssignment($a_node_id, $a_item_id, $a_order_nr=0)
Add assignment.
setItemType($a_val)
Set item type.
setObjectId($a_val)
Set object id.
static deleteAllAssignmentsOfNode($a_node_id)
Delete assignments of node.
getComponentId()
Get component id.
getAssignmentsOfItem($a_item_id)
Get assignments for item.
setOrderNr($a_node_id, $a_item_id, $a_order_nr)
Set order nr.
Class for advanced editing exception handling in ILIAS.
global $DIC
Definition: goto.php:24
foreach($_POST as $key=> $value) $res
global $ilDB