ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilTaxonomyNode.php
Go to the documentation of this file.
1<?php
2
24{
25 protected ilDBInterface $db;
26 public string $type;
27 public int $id;
28 public string $title;
29 protected int $order_nr = 0;
30 protected int $taxonomy_id;
31 protected ?array $data_record = null;
32
37 public function __construct($a_id = 0)
38 {
39 global $DIC;
40
41 $this->db = $DIC->database();
42 $this->id = $a_id;
43
44 if ($a_id != 0) {
45 $this->read();
46 }
47
48 $this->setType("taxn");
49 }
50
51 public function setTitle(string $a_title): void
52 {
53 $this->title = $a_title;
54 }
55
56 public function getTitle(): string
57 {
58 return $this->title;
59 }
60
61 public function setType(string $a_type): void
62 {
63 $this->type = $a_type;
64 }
65
66 public function getType(): string
67 {
68 return $this->type;
69 }
70
71 public function setId(int $a_id): void
72 {
73 $this->id = $a_id;
74 }
75
76 public function getId(): int
77 {
78 return $this->id;
79 }
80
81 public function setOrderNr(int $a_val): void
82 {
83 $this->order_nr = $a_val;
84 }
85
86 public function getOrderNr(): int
87 {
88 return $this->order_nr;
89 }
90
91 public function setTaxonomyId(int $a_val): void
92 {
93 $this->taxonomy_id = $a_val;
94 }
95
96 public function getTaxonomyId(): int
97 {
98 return $this->taxonomy_id;
99 }
100
101 public function read(): void
102 {
104
105 if (!isset($this->data_record)) {
106 $query = "SELECT * FROM tax_node WHERE obj_id = " .
107 $ilDB->quote($this->id, "integer");
108 $obj_set = $ilDB->query($query);
109 $this->data_record = $ilDB->fetchAssoc($obj_set);
110 }
111 $this->setType($this->data_record["type"]);
112 $this->setTitle($this->data_record["title"]);
113 $this->setOrderNr((int) $this->data_record["order_nr"]);
114 $this->setTaxonomyId((int) $this->data_record["tax_id"]);
115 }
116
117 public function create(): void
118 {
120
121 if ($this->getTaxonomyId() <= 0) {
122 die("ilTaxonomyNode->create: No taxonomy ID given");
123 }
124
125 // insert object data
126 $id = $ilDB->nextId("tax_node");
127 $query = "INSERT INTO tax_node (obj_id, title, type, create_date, order_nr, tax_id) " .
128 "VALUES (" .
129 $ilDB->quote($id, "integer") . "," .
130 $ilDB->quote($this->getTitle(), "text") . "," .
131 $ilDB->quote($this->getType(), "text") . ", " .
132 $ilDB->now() . ", " .
133 $ilDB->quote((int) $this->getOrderNr(), "integer") . ", " .
134 $ilDB->quote((int) $this->getTaxonomyId(), "integer") .
135 ")";
136 $ilDB->manipulate($query);
137 $this->setId($id);
138 }
139
140 public function update(): void
141 {
143
144 $query = "UPDATE tax_node SET " .
145 " title = " . $ilDB->quote($this->getTitle(), "text") .
146 " ,order_nr = " . $ilDB->quote((int) $this->getOrderNr(), "integer") .
147 " WHERE obj_id = " . $ilDB->quote($this->getId(), "integer");
148
149 $ilDB->manipulate($query);
150 }
151
152 public function delete(): void
153 {
155
156 // delete all assignments of the node
158
159 $query = "DELETE FROM tax_node WHERE obj_id= " .
160 $ilDB->quote($this->getId(), "integer");
161 $ilDB->manipulate($query);
162 }
163
164 public function copy(int $a_tax_id = 0): ilTaxonomyNode
165 {
166 $taxn = new ilTaxonomyNode();
167 $taxn->setTitle($this->getTitle());
168 $taxn->setType($this->getType());
169 $taxn->setOrderNr($this->getOrderNr());
170 if ($a_tax_id == 0) {
171 $taxn->setTaxonomyId($this->getTaxonomyId());
172 } else {
173 $taxn->setTaxonomyId($a_tax_id);
174 }
175
176 $taxn->create();
177
178 return $taxn;
179 }
180
181 protected static function _lookup(int $a_obj_id, string $a_field): string
182 {
183 global $DIC;
184
185 $ilDB = $DIC->database();
186
187 $query = "SELECT $a_field FROM tax_node WHERE obj_id = " .
188 $ilDB->quote($a_obj_id, "integer");
189 $obj_set = $ilDB->query($query);
190 if ($obj_rec = $ilDB->fetchAssoc($obj_set)) {
191 return $obj_rec[$a_field];
192 }
193 return "";
194 }
195
196 public static function _lookupTitle(int $a_obj_id): string
197 {
198 return self::_lookup($a_obj_id, "title");
199 }
200
201 public static function putInTree(
202 int $a_tax_id,
203 ilTaxonomyNode $a_node,
204 int $a_parent_id = 0,
205 int $a_target_node_id = 0,
206 int $a_order_nr = 0
207 ): void {
208 $tax_tree = new ilTaxonomyTree($a_tax_id);
209
210 // determine parent
211 $parent_id = ($a_parent_id != 0)
212 ? $a_parent_id
213 : $tax_tree->readRootId();
214
215 // determine target
216 if ($a_target_node_id != 0) {
217 $target = $a_target_node_id;
218 } else {
219 // determine last child that serves as predecessor
220 $childs = $tax_tree->getChilds($parent_id);
221
222 if (count($childs) == 0) {
223 $target = ilTree::POS_FIRST_NODE;
224 } else {
225 $target = $childs[count($childs) - 1]["obj_id"];
226 }
227 }
228
229 if ($tax_tree->isInTree($parent_id) && !$tax_tree->isInTree($a_node->getId())) {
230 $tax_tree->insertNode($a_node->getId(), $parent_id, $target);
231 }
232 }
233
234 // Write order nr
235 public static function writeOrderNr(int $a_node_id, int $a_order_nr): void
236 {
237 global $DIC;
238
239 $ilDB = $DIC->database();
240
241 $ilDB->manipulate(
242 "UPDATE tax_node SET " .
243 " order_nr = " . $ilDB->quote($a_order_nr, "integer") .
244 " WHERE obj_id = " . $ilDB->quote($a_node_id, "integer")
245 );
246 }
247
251 public static function writeTitle(int $a_node_id, string $a_title): void
252 {
253 global $DIC;
254
255 $ilDB = $DIC->database();
256
257 $ilDB->manipulate(
258 "UPDATE tax_node SET " .
259 " title = " . $ilDB->quote($a_title, "text") .
260 " WHERE obj_id = " . $ilDB->quote($a_node_id, "integer")
261 );
262 }
263
267 public static function getNextOrderNr(int $a_tax_id, int $a_parent_id): int
268 {
269 $tax_tree = new ilTaxonomyTree($a_tax_id);
270 if ($a_parent_id == 0) {
271 $a_parent_id = $tax_tree->readRootId();
272 }
273 $childs = $tax_tree->getChilds($a_parent_id);
274 $max = 0;
275
276 foreach ($childs as $c) {
277 if ((int) $c["order_nr"] > $max) {
278 $max = (int) $c["order_nr"] + 10;
279 }
280 }
281
282 return $max;
283 }
284
285 // set order nrs to 10, 20, ...
286 public static function fixOrderNumbers(int $a_tax_id, int $a_parent_id): void
287 {
288 $tax_tree = new ilTaxonomyTree($a_tax_id);
289 if ($a_parent_id == 0) {
290 $a_parent_id = $tax_tree->readRootId();
291 }
292 $childs = $tax_tree->getChilds($a_parent_id);
293 $childs = ilArrayUtil::sortArray($childs, "order_nr", "asc", true);
294
295 $cnt = 10;
296 foreach ($childs as $c) {
297 self::writeOrderNr((int) $c["child"], $cnt);
298 $cnt += 10;
299 }
300 }
301}
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
static deleteAllAssignmentsOfNode(int $a_node_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setTaxonomyId(int $a_val)
setTitle(string $a_title)
static getNextOrderNr(int $a_tax_id, int $a_parent_id)
Put this node into the taxonomy tree.
__construct($a_id=0)
Constructor @access public.
static _lookup(int $a_obj_id, string $a_field)
static writeTitle(int $a_node_id, string $a_title)
Write title.
setType(string $a_type)
static writeOrderNr(int $a_node_id, int $a_order_nr)
copy(int $a_tax_id=0)
static _lookupTitle(int $a_obj_id)
static putInTree(int $a_tax_id, ilTaxonomyNode $a_node, int $a_parent_id=0, int $a_target_node_id=0, int $a_order_nr=0)
static fixOrderNumbers(int $a_tax_id, int $a_parent_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const POS_FIRST_NODE
$c
Definition: deliver.php:25
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26