ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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{
28 function __construct($a_component_id, $a_obj_id, $a_item_type, $a_tax_id)
29 {
30 if ($a_component_id == "")
31 {
32 throw new ilTaxonomyException('No component ID passed to ilTaxNodeAssignment.');
33 }
34
35 if ($a_item_type == "")
36 {
37 throw new ilTaxonomyException('No item type passed to ilTaxNodeAssignment.');
38 }
39
40 if ((int) $a_tax_id == 0)
41 {
42 throw new ilTaxonomyException('No taxonomy ID passed to ilTaxNodeAssignment.');
43 }
44
45 $this->setComponentId($a_component_id);
46 $this->setItemType($a_item_type);
47 $this->setTaxonomyId($a_tax_id);
48 $this->setObjectId($a_obj_id);
49 }
50
56 protected function setComponentId($a_val)
57 {
58 $this->component_id = $a_val;
59 }
60
66 public function getComponentId()
67 {
68 return $this->component_id;
69 }
70
76 protected function setItemType($a_val)
77 {
78 $this->item_type = $a_val;
79 }
80
86 public function getItemType()
87 {
88 return $this->item_type;
89 }
90
96 protected function setTaxonomyId($a_val)
97 {
98 $this->taxonomy_id = $a_val;
99 }
100
106 public function getTaxonomyId()
107 {
108 return $this->taxonomy_id;
109 }
110
116 function setObjectId($a_val)
117 {
118 $this->obj_id = $a_val;
119 }
120
126 function getObjectId()
127 {
128 return $this->obj_id;
129 }
130
137 final public function getAssignmentsOfNode($a_node_id)
138 {
139 global $ilDB;
140
141 if (is_array($a_node_id))
142 {
143 $set = $ilDB->query("SELECT * FROM tax_node_assignment ".
144 " WHERE ".$ilDB->in("node_id", $a_node_id, false, "integer").
145 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer").
146 " AND component = ".$ilDB->quote($this->getComponentId(), "text").
147 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
148 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
149 " ORDER BY order_nr ASC"
150 );
151 }
152 else
153 {
154 $set = $ilDB->query("SELECT * FROM tax_node_assignment ".
155 " WHERE node_id = ".$ilDB->quote($a_node_id, "integer").
156 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer").
157 " AND component = ".$ilDB->quote($this->getComponentId(), "text").
158 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
159 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
160 " ORDER BY order_nr ASC"
161 );
162 }
163 $ass = array();
164 while ($rec = $ilDB->fetchAssoc($set))
165 {
166 $ass[] = $rec;
167 }
168
169 return $ass;
170 }
171
178 final public function getAssignmentsOfItem($a_item_id)
179 {
180 global $ilDB;
181
182 $set = $ilDB->query("SELECT * FROM tax_node_assignment".
183 " WHERE component = ".$ilDB->quote($this->getComponentId(), "text").
184 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
185 " AND item_id = ".$ilDB->quote($a_item_id, "integer").
186 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
187 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer")
188 );
189 $ass = array();
190 while ($rec = $ilDB->fetchAssoc($set))
191 {
192 $ass[] = $rec;
193 }
194 return $ass;
195 }
196
205 function addAssignment($a_node_id, $a_item_id, $a_order_nr = 0)
206 {
207 global $ilDB;
208
209 // nothing to do, if not both IDs are greater 0
210 if ((int) $a_node_id == 0 || (int) $a_item_id == 0)
211 {
212 return;
213 }
214
215 // sanity check: does the node belong to the given taxonomy?
216 $set = $ilDB->query("SELECT tax_tree_id FROM tax_tree ".
217 " WHERE child = ".$ilDB->quote($a_node_id, "integer")
218 );
219 $rec = $ilDB->fetchAssoc($set);
220 if ($rec["tax_tree_id"] != $this->getTaxonomyId())
221 {
222 throw new ilTaxonomyException('addAssignment: Node ID does not belong to current taxonomy.');
223 }
224
225 // do not re-assign, if assignment already exists
226 // order number should be kept in this case
227 $set2 = $ilDB->query($q = "SELECT item_id FROM tax_node_assignment ".
228 " WHERE component = ".$ilDB->quote($this->getComponentId(), "text").
229 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
230 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
231 " AND node_id = ".$ilDB->quote($a_node_id, "integer").
232 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer").
233 " AND item_id = ".$ilDB->quote($a_item_id, "integer")
234 );
235 if ($rec2 = $ilDB->fetchAssoc($set2))
236 {
237 return;
238 }
239
240 if ($a_order_nr == 0)
241 {
242 $a_order_nr = $this->getMaxOrderNr($a_node_id) + 10;
243 }
244
245 $ilDB->replace("tax_node_assignment",
246 array(
247 "node_id" => array("integer", $a_node_id),
248 "component" => array("text", $this->getComponentId()),
249 "item_type" => array("text", $this->getItemType()),
250 "obj_id" => array("integer", $this->getObjectId()),
251 "item_id" => array("integer", $a_item_id)
252 ),
253 array(
254 "tax_id" => array("integer", $this->getTaxonomyId()),
255 "order_nr" => array("integer", $a_order_nr)
256 )
257 );
258 }
259
267 function deleteAssignment($a_node_id, $a_item_id)
268 {
269 global $ilDB;
270
271 // nothing to do, if not both IDs are greater 0
272 if ((int) $a_node_id == 0 || (int) $a_item_id == 0)
273 {
274 return;
275 }
276
277 // sanity check: does the node belong to the given taxonomy?
278 $set = $ilDB->query("SELECT tax_tree_id FROM tax_tree ".
279 " WHERE child = ".$ilDB->quote($a_node_id, "integer")
280 );
281 $rec = $ilDB->fetchAssoc($set);
282 if ($rec["tax_tree_id"] != $this->getTaxonomyId())
283 {
284 throw new ilTaxonomyException('addAssignment: Node ID does not belong to current taxonomy.');
285 }
286
287 $ilDB->manipulate("DELETE FROM tax_node_assignment WHERE ".
288 " component = ".$ilDB->quote($this->getComponentId(), "text").
289 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
290 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
291 " AND item_id = ".$ilDB->quote($a_item_id, "integer").
292 " AND node_id = ".$ilDB->quote($a_node_id, "integer").
293 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer")
294 );
295 }
296
303 function getMaxOrderNr($a_node_id)
304 {
305 global $ilDB;
306
307 $set = $ilDB->query("SELECT max(order_nr) mnr FROM tax_node_assignment ".
308 " WHERE component = ".$ilDB->quote($this->getComponentId(), "text").
309 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
310 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
311 " AND node_id = ".$ilDB->quote($a_node_id, "integer").
312 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer")
313 );
314 $rec = $ilDB->fetchAssoc($set);
315
316 return (int) $rec["mnr"];
317 }
318
325 function setOrderNr($a_node_id, $a_item_id, $a_order_nr)
326 {
327 global $ilDB;
328
329 $ilDB->manipulate("UPDATE tax_node_assignment SET ".
330 " order_nr = ".$ilDB->quote($a_order_nr, "integer").
331 " WHERE component = ".$ilDB->quote($this->getComponentId(), "text").
332 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
333 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
334 " AND node_id = ".$ilDB->quote($a_node_id, "integer").
335 " AND item_id = ".$ilDB->quote($a_item_id, "integer").
336 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer")
337 );
338 }
339
340
346 function deleteAssignmentsOfItem($a_item_id)
347 {
348 global $ilDB;
349
350 $ilDB->manipulate("DELETE FROM tax_node_assignment WHERE ".
351 " component = ".$ilDB->quote($this->getComponentId(), "text").
352 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
353 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
354 " AND item_id = ".$ilDB->quote($a_item_id, "integer").
355 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer")
356 );
357 }
358
364 function deleteAssignmentsOfNode($a_node_id)
365 {
366 global $ilDB;
367
368 $ilDB->manipulate("DELETE FROM tax_node_assignment WHERE ".
369 " node_id = ".$ilDB->quote($a_node_id, "integer").
370 " AND component = ".$ilDB->quote($this->getComponentId(), "text").
371 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
372 " AND item_type = ".$ilDB->quote($this->getItemType(), "text")
373 );
374 }
375
381 static function deleteAllAssignmentsOfNode($a_node_id)
382 {
383 global $ilDB;
384
385 $ilDB->manipulate("DELETE FROM tax_node_assignment WHERE ".
386 " node_id = ".$ilDB->quote($a_node_id, "integer")
387 );
388 }
389
393 function fixOrderNr($a_node_id)
394 {
395 global $ilDB;
396
397 $set = $ilDB->query("SELECT * FROM tax_node_assignment ".
398 " WHERE component = ".$ilDB->quote($this->getComponentId(), "text").
399 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
400 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
401 " AND node_id = ".$ilDB->quote($a_node_id, "integer").
402 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer").
403 " ORDER BY order_nr ASC"
404 );
405 $cnt = 10;
406 while ($rec = $ilDB->fetchAssoc($set))
407 {
408 $ilDB->manipulate("UPDATE tax_node_assignment SET ".
409 " order_nr = ".$ilDB->quote($cnt, "integer").
410 " WHERE component = ".$ilDB->quote($this->getComponentId(), "text").
411 " AND item_type = ".$ilDB->quote($this->getItemType(), "text").
412 " AND obj_id = ".$ilDB->quote($this->getObjectId(), "integer").
413 " AND node_id = ".$ilDB->quote($a_node_id, "integer").
414 " AND tax_id = ".$ilDB->quote($this->getTaxonomyId(), "integer").
415 " AND item_id = ".$ilDB->quote($rec["item_id"], "integer")
416 );
417 $cnt+= 10;
418 }
419 }
420
421
422
431 static function findObjectsByNode($a_tax_id, array $a_node_ids, $a_item_type)
432 {
433 global $ilDB;
434
435 $res = array();
436
437 $set = $ilDB->query("SELECT * FROM tax_node_assignment".
438 " WHERE ".$ilDB->in("node_id", $a_node_ids, "", "integer").
439 " AND tax_id = ".$ilDB->quote($a_tax_id, "integer").
440 " AND item_type = ".$ilDB->quote($a_item_type, "text").
441 " ORDER BY order_nr ASC"
442 );
443 while($row = $ilDB->fetchAssoc($set))
444 {
445 $res[] = $row["obj_id"];
446 }
447
448 return $res;
449 }
450}
451
452?>
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 $ilDB