ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjTaxonomy.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 
5 require_once "./Services/Object/classes/class.ilObject2.php";
6 
14 class ilObjTaxonomy extends ilObject2
15 {
16  const SORT_ALPHABETICAL = 0;
17  const SORT_MANUAL = 1;
18  protected $node_mapping = array();
19  protected $item_sorting = false;
20 
26  function ilObjTaxonomy($a_id = 0)
27  {
28  $this->type = "tax";
29  parent::ilObject($a_id, false);
30  }
31 
38  function initType()
39  {
40  $this->type = "tax";
41  }
42 
48  function setSortingMode($a_val)
49  {
50  $this->sorting_mode = $a_val;
51  }
52 
58  function getSortingMode()
59  {
60  return $this->sorting_mode;
61  }
62 
68  function setItemSorting($a_val)
69  {
70  $this->item_sorting = $a_val;
71  }
72 
78  function getItemSorting()
79  {
80  return $this->item_sorting;
81  }
82 
89  function getTree()
90  {
91  if ($this->getId() > 0)
92  {
93  include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
94  $tax_tree = new ilTaxonomyTree($this->getId());
95  return $tax_tree;
96  }
97  return false;
98  }
99 
106  function getNodeMapping()
107  {
108  return $this->node_mapping;
109  }
110 
111 
115  function doCreate()
116  {
117  global $ilDB;
118 
119  // create tax data record
120  $ilDB->manipulate("INSERT INTO tax_data ".
121  "(id, sorting_mode, item_sorting) VALUES (".
122  $ilDB->quote($this->getId(), "integer").",".
123  $ilDB->quote((int) $this->getSortingMode(), "integer").",".
124  $ilDB->quote((int) $this->getItemSorting(), "integer").
125  ")");
126 
127  // create the taxonomy tree
128  include_once("./Services/Taxonomy/classes/class.ilTaxonomyNode.php");
129  $node = new ilTaxonomyNode();
130  $node->setType(""); // empty type
131  $node->setTitle("Root node for taxonomy ".$this->getId());
132  $node->setTaxonomyId($this->getId());
133  $node->create();
134  include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
135  $tax_tree = new ilTaxonomyTree($this->getId());
136  $tax_tree->addTree($this->getId(), $node->getId());
137  }
138 
145  function doCloneObject($a_new_obj, $a_target_id, $a_copy_id)
146  {
147  global $log, $lng;
148 
149  $a_new_obj->setTitle($this->getTitle());
150  $a_new_obj->setDescription($this->getDescription());
151  $a_new_obj->setSortingMode($this->getSortingMode());
152 
153  $this->node_mapping = array();
154 
155  $this->cloneNodes($a_new_obj, $a_new_obj->getTree()->readRootId(),
156  $this->getTree()->readRootId());
157  }
158 
165  function cloneNodes($a_new_obj, $a_target_parent, $a_source_parent)
166  {
167  include_once("./Services/Taxonomy/classes/class.ilTaxonomyNode.php");
168 
169  // get all childs
170  $nodes = $this->getTree()->getChilds($a_source_parent);
171  foreach ($nodes as $node)
172  {
173  switch ($node["type"])
174  {
175  case "taxn":
176  $tax_node = new ilTaxonomyNode($node["child"]);
177  $new_node = $tax_node->copy($a_new_obj->getId());
178  break;
179  }
180 
181  ilTaxonomyNode::putInTree($a_new_obj->getId(),
182  $new_node, $a_target_parent);
183 
184  $this->node_mapping[$node["child"]] = $new_node->getId();
185 
186  // handle childs
187  $this->cloneNodes($a_new_obj, $new_node->getId(), $node["child"]);
188  }
189 
190  }
191 
192 
196  function doDelete()
197  {
198  global $ilDB;
199 
200  // delete usages
202 
203  // get all nodes
204  $tree = $this->getTree();
205  $subtree = $tree->getSubTreeIds($tree->readRootId());
206  $subtree[] = $tree->readRootId();
207 
208  // get root node data (important: must happen before we
209  // delete the nodes
210  $root_node_data = $tree->getNodeData($tree->readRootId());
211 
212  // delete all nodes
213  include_once("./Services/Taxonomy/classes/class.ilTaxonomyNode.php");
214  foreach ($subtree as $node_id)
215  {
216  // delete node (this also deletes its assignments)
217  $node = new ilTaxonomyNode($node_id);
218  $node->delete();
219  }
220 
221  // delete the tree
222  $tree->deleteTree($root_node_data);
223 
224  // delete taxonoymy properties record
225  $ilDB->manipulate("DELETE FROM tax_data WHERE ".
226  " id = ".$ilDB->quote($this->getId(), "integer")
227  );
228 
229  }
230 
231 
235  function doRead()
236  {
237  global $ilDB;
238 
239  $set = $ilDB->query("SELECT * FROM tax_data ".
240  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
241  );
242  $rec = $ilDB->fetchAssoc($set);
243  $this->setSortingMode($rec["sorting_mode"]);
244  $this->setItemSorting($rec["item_sorting"]);
245  }
246 
250  function doUpdate()
251  {
252  global $ilDB;
253 
254  $ilDB->manipulate($t = "UPDATE tax_data SET ".
255  " sorting_mode = ".$ilDB->quote((int) $this->getSortingMode(), "integer").", ".
256  " item_sorting = ".$ilDB->quote((int) $this->getItemSorting(), "integer").
257  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
258  );
259  }
260 
267  static function loadLanguageModule()
268  {
269  global $lng;
270 
271  $lng->loadLanguageModule("tax");
272  }
273 
274 
281  static function saveUsage($a_tax_id, $a_obj_id)
282  {
283  global $ilDB;
284 
285  if ($a_tax_id > 0 && $a_obj_id > 0)
286  {
287  $ilDB->replace("tax_usage",
288  array("tax_id" => array("integer", $a_tax_id),
289  "obj_id" => array("integer", $a_obj_id)
290  ),
291  array()
292  );
293  }
294  }
295 
302  static function getUsageOfObject($a_obj_id, $a_include_titles = false)
303  {
304  global $ilDB;
305 
306  $set = $ilDB->query("SELECT tax_id FROM tax_usage ".
307  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer")
308  );
309  $tax = array();
310  while ($rec = $ilDB->fetchAssoc($set))
311  {
312  if (!$a_include_titles)
313  {
314  $tax[] = $rec["tax_id"];
315  }
316  else
317  {
318  $tax[] = array("tax_id" => $rec["tax_id"],
319  "title" => ilObject::_lookupTitle($rec["tax_id"])
320  );
321  }
322  }
323  return $tax;
324  }
325 
332  static function deleteUsagesOfTaxonomy($a_id)
333  {
334  global $ilDB;
335 
336  $ilDB->manipulate("DELETE FROM tax_usage WHERE ".
337  " tax_id = ".$ilDB->quote($a_id, "integer")
338  );
339 
340  }
341 
342 
349  static function getSubTreeItems($a_comp, $a_obj_id, $a_item_type, $a_tax_id, $a_node)
350  {
351  include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
352  $tree = new ilTaxonomyTree($a_tax_id);
353 
354  $sub_nodes = $tree->getSubTreeIds($a_node);
355  $sub_nodes[] = $a_node;
356  include_once("./Services/Taxonomy/classes/class.ilTaxNodeAssignment.php");
357 
358  $tn_ass = new ilTaxNodeAssignment($a_comp, $a_obj_id, $a_item_type, $a_tax_id);
359  $items = $tn_ass->getAssignmentsOfNode($sub_nodes);
360 
361  return $items;
362  }
363 
370  static protected function lookup($a_field, $a_id)
371  {
372  global $ilDB;
373 
374  $set = $ilDB->query("SELECT ".$a_field." FROM tax_data ".
375  " WHERE id = ".$ilDB->quote($a_id, "integer")
376  );
377  $rec = $ilDB->fetchAssoc($set);
378 
379  return $rec[$a_field];
380  }
381 
388  public static function lookupSortingMode($a_id)
389  {
390  return self::lookup("sorting_mode", $a_id);
391  }
392 }
393 ?>