ILIAS  Release_4_4_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  $ilDB->replace("tax_usage",
286  array("tax_id" => array("integer", $a_tax_id),
287  "obj_id" => array("integer", $a_obj_id)
288  ),
289  array()
290  );
291  }
292 
299  static function getUsageOfObject($a_obj_id, $a_include_titles = false)
300  {
301  global $ilDB;
302 
303  $set = $ilDB->query("SELECT tax_id FROM tax_usage ".
304  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer")
305  );
306  $tax = array();
307  while ($rec = $ilDB->fetchAssoc($set))
308  {
309  if (!$a_include_titles)
310  {
311  $tax[] = $rec["tax_id"];
312  }
313  else
314  {
315  $tax[] = array("tax_id" => $rec["tax_id"],
316  "title" => ilObject::_lookupTitle($rec["tax_id"])
317  );
318  }
319  }
320  return $tax;
321  }
322 
329  static function deleteUsagesOfTaxonomy($a_id)
330  {
331  global $ilDB;
332 
333  $ilDB->manipulate("DELETE FROM tax_usage WHERE ".
334  " tax_id = ".$ilDB->quote($a_id, "integer")
335  );
336 
337  }
338 
339 
346  static function getSubTreeItems($a_comp, $a_obj_id, $a_item_type, $a_tax_id, $a_node)
347  {
348  include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
349  $tree = new ilTaxonomyTree($a_tax_id);
350 
351  $sub_nodes = $tree->getSubTreeIds($a_node);
352  $sub_nodes[] = $a_node;
353  include_once("./Services/Taxonomy/classes/class.ilTaxNodeAssignment.php");
354 
355  $tn_ass = new ilTaxNodeAssignment($a_comp, $a_obj_id, $a_item_type, $a_tax_id);
356  $items = $tn_ass->getAssignmentsOfNode($sub_nodes);
357 
358  return $items;
359  }
360 
367  static protected function lookup($a_field, $a_id)
368  {
369  global $ilDB;
370 
371  $set = $ilDB->query("SELECT ".$a_field." FROM tax_data ".
372  " WHERE id = ".$ilDB->quote($a_id, "integer")
373  );
374  $rec = $ilDB->fetchAssoc($set);
375 
376  return $rec[$a_field];
377  }
378 
385  public static function lookupSortingMode($a_id)
386  {
387  return self::lookup("sorting_mode", $a_id);
388  }
389 }
390 ?>