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