ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilObjTaxonomy.php
Go to the documentation of this file.
1 <?php
2 
23 class ilObjTaxonomy extends ilObject2
24 {
25  public const SORT_ALPHABETICAL = 0;
26  public const SORT_MANUAL = 1;
27 
28  protected array $node_mapping = array();
29  protected bool $item_sorting = false;
30  protected int $sorting_mode = self::SORT_ALPHABETICAL;
31 
36  public function __construct($a_id = 0)
37  {
38  global $DIC;
39 
40  $this->db = $DIC->database();
41  $this->type = "tax";
42  parent::__construct($a_id, false);
43  }
44 
45  protected function initType(): void
46  {
47  $this->type = "tax";
48  }
49 
50  public function setSortingMode(int $a_val): void
51  {
52  $this->sorting_mode = $a_val;
53  }
54 
55  public function getSortingMode(): int
56  {
57  return $this->sorting_mode;
58  }
59 
60  public function setItemSorting(bool $a_val): void
61  {
62  $this->item_sorting = $a_val;
63  }
64 
65  public function getItemSorting(): bool
66  {
67  return $this->item_sorting;
68  }
69 
70  public function getTree(): ?ilTaxonomyTree
71  {
72  if ($this->getId() > 0) {
73  return new ilTaxonomyTree($this->getId());
74  }
75  return null;
76  }
77 
78  // node mapping is used during cloning
79  public function getNodeMapping(): array
80  {
81  return $this->node_mapping;
82  }
83 
84  protected function doCreate(bool $clone_mode = false): void
85  {
86  $ilDB = $this->db;
87 
88  // create tax data record
89  $ilDB->manipulate("INSERT INTO tax_data " .
90  "(id, sorting_mode, item_sorting) VALUES (" .
91  $ilDB->quote($this->getId(), "integer") . "," .
92  $ilDB->quote($this->getSortingMode(), "integer") . "," .
93  $ilDB->quote((int) $this->getItemSorting(), "integer") .
94  ")");
95  $node = new ilTaxonomyNode();
96  $node->setType(""); // empty type
97  $node->setTitle("Root node for taxonomy " . $this->getId());
98  $node->setTaxonomyId($this->getId());
99  $node->create();
100  $tax_tree = new ilTaxonomyTree($this->getId());
101  $tax_tree->addTree($this->getId(), $node->getId());
102  }
103 
104  protected function doCloneObject(ilObject2 $new_obj, int $a_target_id, ?int $a_copy_id = null): void
105  {
106  assert($new_obj instanceof ilObjTaxonomy);
107  $new_obj->setTitle($this->getTitle());
108  $new_obj->setDescription($this->getDescription());
109  $new_obj->setSortingMode($this->getSortingMode());
110 
111  $this->node_mapping = array();
112 
113  $this->cloneNodes(
114  $new_obj,
115  $new_obj->getTree()->readRootId(),
116  $this->getTree()->readRootId()
117  );
118  }
119 
120  // clone nodes of taxonomy
121  public function cloneNodes(
122  ilObjTaxonomy $a_new_obj,
123  int $a_target_parent,
124  int $a_source_parent
125  ): void {
126  // get all childs
127  $nodes = $this->getTree()->getChilds($a_source_parent);
128  foreach ($nodes as $node) {
129  if ($node["type"] === "taxn") {
130  $tax_node = new ilTaxonomyNode($node["child"]);
131  $new_node = $tax_node->copy($a_new_obj->getId());
132 
134  $a_new_obj->getId(),
135  $new_node,
136  $a_target_parent
137  );
138 
139  $this->node_mapping[$node["child"]] = $new_node->getId();
140 
141  // handle childs
142  $this->cloneNodes($a_new_obj, $new_node->getId(), $node["child"]);
143  }
144  }
145  }
146 
147  protected function doDelete(): void
148  {
149  $ilDB = $this->db;
150 
151  // delete usages
152  self::deleteUsagesOfTaxonomy($this->getId());
153 
154  // get all nodes
155  $tree = $this->getTree();
156  $subtree = $tree->getSubTreeIds($tree->readRootId());
157  $subtree[] = $tree->readRootId();
158 
159  // get root node data (important: must happen before we
160  // delete the nodes
161  $root_node_data = $tree->getNodeData($tree->readRootId());
162  foreach ($subtree as $node_id) {
163  // delete node (this also deletes its assignments)
164  $node = new ilTaxonomyNode($node_id);
165  $node->delete();
166  }
167 
168  // delete the tree
169  $tree->deleteTree($root_node_data);
170 
171  // delete taxonoymy properties record
172  $ilDB->manipulate(
173  "DELETE FROM tax_data WHERE " .
174  " id = " . $ilDB->quote($this->getId(), "integer")
175  );
176  }
177 
178  protected function doRead(): void
179  {
180  $ilDB = $this->db;
181 
182  $set = $ilDB->query(
183  "SELECT * FROM tax_data " .
184  " WHERE id = " . $ilDB->quote($this->getId(), "integer")
185  );
186  $rec = $ilDB->fetchAssoc($set);
187  $this->setSortingMode((int) $rec["sorting_mode"]);
188  $this->setItemSorting((bool) $rec["item_sorting"]);
189  }
190 
191  protected function doUpdate(): void
192  {
193  $ilDB = $this->db;
194 
195  $ilDB->manipulate(
196  $t = "UPDATE tax_data SET " .
197  " sorting_mode = " . $ilDB->quote($this->getSortingMode(), "integer") . ", " .
198  " item_sorting = " . $ilDB->quote((int) $this->getItemSorting(), "integer") .
199  " WHERE id = " . $ilDB->quote($this->getId(), "integer")
200  );
201  }
202 
203  public static function loadLanguageModule(): void
204  {
205  global $DIC;
206 
207  $lng = $DIC->language();
208 
209  $lng->loadLanguageModule("tax");
210  }
211 
212  public static function saveUsage(int $a_tax_id, int $a_obj_id): void
213  {
214  global $DIC;
215 
216  $ilDB = $DIC->database();
217 
218  if ($a_tax_id > 0 && $a_obj_id > 0) {
219  $ilDB->replace(
220  "tax_usage",
221  array("tax_id" => array("integer", $a_tax_id),
222  "obj_id" => array("integer", $a_obj_id)
223  ),
224  array()
225  );
226  }
227  }
228 
235  public static function getUsageOfObject(int $a_obj_id, bool $a_include_titles = false): array
236  {
237  global $DIC;
238  return $DIC->taxonomy()->internal()->domain()->usage()->getUsageOfObject($a_obj_id, $a_include_titles);
239  }
240 
241  // Delete all usages of a taxonomy
242  public static function deleteUsagesOfTaxonomy(int $a_id): void
243  {
244  global $DIC;
245 
246  $ilDB = $DIC->database();
247 
248  $ilDB->manipulate(
249  "DELETE FROM tax_usage WHERE " .
250  " tax_id = " . $ilDB->quote($a_id, "integer")
251  );
252  }
253 
264  public static function getSubTreeItems(
265  string $a_comp,
266  int $a_obj_id,
267  string $a_item_type,
268  int $a_tax_id,
269  $a_node
270  ): array {
271  $tree = new ilTaxonomyTree($a_tax_id);
272 
273  $sub_nodes = $tree->getSubTreeIds($a_node);
274  $sub_nodes[] = $a_node;
275 
276  $tn_ass = new ilTaxNodeAssignment($a_comp, $a_obj_id, $a_item_type, $a_tax_id);
277 
278  return $tn_ass->getAssignmentsOfNode($sub_nodes);
279  }
280 
281  // lookup property in tax_data record
282  protected static function lookup(string $a_field, int $a_id): string
283  {
284  global $DIC;
285 
286  $ilDB = $DIC->database();
287 
288  $set = $ilDB->query(
289  "SELECT " . $a_field . " FROM tax_data " .
290  " WHERE id = " . $ilDB->quote($a_id, "integer")
291  );
292  $rec = $ilDB->fetchAssoc($set);
293 
294  return $rec[$a_field];
295  }
296 
297  public static function lookupSortingMode(int $a_id): int
298  {
299  return (int) self::lookup("sorting_mode", $a_id);
300  }
301 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getNodeData(int $a_node_id, ?int $a_tree_pk=null)
get all information of a node.
setItemSorting(bool $a_val)
static getSubTreeItems(string $a_comp, int $a_obj_id, string $a_item_type, int $a_tax_id, $a_node)
Get all assigned items under a node.
static lookup(string $a_field, int $a_id)
doCreate(bool $clone_mode=false)
ilTree $tree
getSubTreeIds(int $a_ref_id)
Get all ids of subnodes.
static getUsageOfObject(int $a_obj_id, bool $a_include_titles=false)
deleteTree(array $a_node)
delete node and the whole subtree under this node
loadLanguageModule(string $a_module)
Load language module.
setTitle(string $title)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setSortingMode(int $a_val)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
ilLanguage $lng
ilDBInterface $db
global $DIC
Definition: shib_login.php:22
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 lookupSortingMode(int $a_id)
static saveUsage(int $a_tax_id, int $a_obj_id)
static deleteUsagesOfTaxonomy(int $a_id)
__construct($a_id=0)
ilObjTaxonomy constructor.
cloneNodes(ilObjTaxonomy $a_new_obj, int $a_target_parent, int $a_source_parent)
__construct(Container $dic, ilPlugin $plugin)
setDescription(string $description)
doCloneObject(ilObject2 $new_obj, int $a_target_id, ?int $a_copy_id=null)