ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilGlossaryDefinition.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once("./Services/COPage/classes/class.ilPageObject.php");
5 
15 {
16  var $ilias;
17  var $lng;
18  var $tpl;
19 
20  var $id;
21  var $term_id;
22  var $glo_id;
25  var $nr;
26 
31  function ilGlossaryDefinition($a_id = 0)
32  {
33  global $lng, $ilias, $tpl;
34 
35  $this->lng =& $lng;
36  $this->ilias =& $ilias;
37  $this->tpl =& $tpl;
38 
39  $this->id = $a_id;
40  if ($a_id != 0)
41  {
42  $this->read();
43  }
44  }
45 
49  function read()
50  {
51  global $ilDB;
52 
53  $q = "SELECT * FROM glossary_definition WHERE id = ".
54  $ilDB->quote($this->id, "integer");
55  $def_set = $ilDB->query($q);
56  $def_rec = $ilDB->fetchAssoc($def_set);
57 
58  $this->setTermId($def_rec["term_id"]);
59  $this->setShortText($def_rec["short_text"]);
60  $this->setNr($def_rec["nr"]);
61 
62  $this->page_object =& new ilPageObject("gdf", $this->id);
63  }
64 
65  function setId($a_id)
66  {
67  $this->id = $a_id;
68  }
69 
70  function getId()
71  {
72  return $this->id;
73  }
74 
75  function getType()
76  {
77  return "gdf";
78  }
79 
80  function setTermId($a_term_id)
81  {
82  $this->term_id = $a_term_id;
83  }
84 
85  function getTermId()
86  {
87  return $this->term_id;
88  }
89 
90  function setShortText($a_text)
91  {
92  $this->short_text = $this->shortenShortText($a_text);
93  }
94 
95  function getShortText()
96  {
97  return $this->short_text;
98  }
99 
100  function setNr($a_nr)
101  {
102  $this->nr = $a_nr;
103  }
104 
105  function getNr()
106  {
107  return $this->nr;
108  }
109 
110  function assignPageObject(&$a_page_object)
111  {
112  $this->page_object =& $a_page_object;
113  }
114 
115  function &getPageObject()
116  {
117  return $this->page_object;
118  }
119 
125  function getTitle()
126  {
127  return $this->title;
128  }
129 
133  function setTitle($a_title)
134  {
135  $this->title = $a_title;
136  }
137 
143  function getDescription()
144  {
145  return $this->description;
146  }
147 
151  function setDescription($a_description)
152  {
153  $this->description = $a_description;
154  }
155 
156  function create($a_upload = false)
157  {
158  global $ilDB;
159 
160  $term =& new ilGlossaryTerm($this->getTermId());
161 
162  $this->setId($ilDB->nextId("glossary_definition"));
163 
164  // lock glossary_definition table
165  $ilDB->lockTables(
166  array(
167  0 => array('name' => 'glossary_definition', 'type' => ilDB::LOCK_WRITE)));
168 
169  // get maximum definition number
170  $q = "SELECT max(nr) AS max_nr FROM glossary_definition WHERE term_id = ".
171  $ilDB->quote($this->getTermId(), "integer");
172  $max_set = $ilDB->query($q);
173  $max_rec = $ilDB->fetchAssoc($max_set);
174  $max = (int) $max_rec["max_nr"];
175 
176  // insert new definition record
177  $ilDB->manipulate("INSERT INTO glossary_definition (id, term_id, short_text, nr)".
178  " VALUES (".
179  $ilDB->quote($this->getId(), "integer").",".
180  $ilDB->quote($this->getTermId(), "integer").",".
181  $ilDB->quote($this->getShortText(), "text").", ".
182  $ilDB->quote(($max + 1), "integer").")");
183 
184  // unlock glossary definition table
185  $ilDB->unlockTables();
186 
187  // get number
188  $q = "SELECT nr FROM glossary_definition WHERE id = ".
189  $ilDB->quote($this->id, "integer");
190  $def_set = $ilDB->query($q);
191  $def_rec = $ilDB->fetchAssoc($def_set);
192  $this->setNr($def_rec["nr"]);
193 
194  // meta data will be created by
195  // import parser
196  if (!$a_upload)
197  {
198  $this->createMetaData();
199  }
200 
201  $this->page_object =& new ilPageObject("gdf");
202  $this->page_object->setId($this->getId());
203  $this->page_object->setParentId($term->getGlossaryId());
204  $this->page_object->create();
205  }
206 
207  function delete()
208  {
209  global $ilDB;
210 
211  // lock glossary_definition table
212  #ilDB::_lockTables(array('glossary_definition' => 'WRITE'));
213  $ilDB->lockTables(
214  array(
215  0 => array('name' => 'glossary_definition', 'type' => ilDB::LOCK_WRITE)));
216 
217 
218  // be sure to get the right number
219  $q = "SELECT * FROM glossary_definition WHERE id = ".
220  $ilDB->quote($this->id, "integer");
221  $def_set = $ilDB->query($q);
222  $def_rec = $ilDB->fetchAssoc($def_set);
223  $this->setNr($def_rec["nr"]);
224 
225  // update numbers of other definitions
226  $ilDB->manipulate("UPDATE glossary_definition SET ".
227  " nr = nr - 1 ".
228  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
229  " AND nr > ".$ilDB->quote($this->getNr(), "integer"));
230 
231  // delete current definition
232  $ilDB->manipulate("DELETE FROM glossary_definition ".
233  " WHERE id = ".$ilDB->quote($this->getId(), "integer"));
234 
235  // unlock glossary_definition table
236  $ilDB->unlockTables();
237 
238  // delete page and meta data
239  $this->page_object->delete();
240 
241  // delete meta data
242  $this->deleteMetaData();
243 /*
244  $nested = new ilNestedSetXML();
245  $nested->init($this->getId(), $this->getType());
246  $nested->deleteAllDBData();
247 */
248  }
249 
250 
251  function moveUp()
252  {
253  global $ilDB;
254 
255  // lock glossary_definition table
256  #ilDB::_lockTables(array('glossary_definition' => 'WRITE'));
257  $ilDB->lockTables(
258  array(
259  0 => array('name' => 'glossary_definition', 'type' => ilDB::LOCK_WRITE)));
260 
261 
262  // be sure to get the right number
263  $q = "SELECT * FROM glossary_definition WHERE id = ".
264  $ilDB->quote($this->id, "integer");
265  $def_set = $ilDB->query($q);
266  $def_rec = $ilDB->fetchAssoc($def_set);
267  $this->setNr($def_rec["nr"]);
268 
269  if ($this->getNr() < 2)
270  {
271  $ilDB->unlockTables();
272  return;
273  }
274 
275  // update numbers of other definitions
276  $ilDB->manipulate("UPDATE glossary_definition SET ".
277  " nr = nr + 1 ".
278  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
279  " AND nr = ".$ilDB->quote(($this->getNr() - 1), "integer"));
280 
281  // delete current definition
282  $ilDB->manipulate("UPDATE glossary_definition SET ".
283  " nr = nr - 1 ".
284  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
285  " AND id = ".$ilDB->quote($this->getId(), "integer"));
286 
287  // unlock glossary_definition table
288  $ilDB->unlockTables();
289  }
290 
291 
292  function moveDown()
293  {
294  global $ilDB;
295 
296  // lock glossary_definition table
297  #ilDB::_lockTables(array('glossary_definition' => 'WRITE'));
298  $ilDB->lockTables(
299  array(
300  0 => array('name' => 'glossary_definition', 'type' => ilDB::LOCK_WRITE)));
301 
302  // be sure to get the right number
303  $q = "SELECT * FROM glossary_definition WHERE id = ".
304  $ilDB->quote($this->id, "integer");
305  $def_set = $ilDB->query($q);
306  $def_rec = $ilDB->fetchAssoc($def_set);
307  $this->setNr($def_rec["nr"]);
308 
309  // get max number
310  $q = "SELECT max(nr) as max_nr FROM glossary_definition WHERE term_id = ".
311  $ilDB->quote($this->getTermId(), "integer");
312  $max_set = $ilDB->query($q);
313  $max_rec = $ilDB->fetchAssoc($max_set);
314 
315  if ($this->getNr() >= $max_rec["max_nr"])
316  {
317  $ilDB->unlockTables();
318  return;
319  }
320 
321  // update numbers of other definitions
322  $ilDB->manipulate("UPDATE glossary_definition SET ".
323  " nr = nr - 1 ".
324  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
325  " AND nr = ".$ilDB->quote(($this->getNr() + 1), "integer"));
326 
327  // delete current definition
328  $ilDB->manipulate("UPDATE glossary_definition SET ".
329  " nr = nr + 1 ".
330  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
331  " AND id = ".$ilDB->quote($this->getId(), "integer"));
332 
333  // unlock glossary_definition table
334  $ilDB->unlockTables();
335 
336  }
337 
338 
339  function update()
340  {
341  global $ilDB;
342 
343  $this->updateMetaData();
344 
345  $ilDB->manipulate("UPDATE glossary_definition SET ".
346  " term_id = ".$ilDB->quote($this->getTermId(), "integer").", ".
347  " nr = ".$ilDB->quote($this->getNr(), "integer").", ".
348  " short_text = ".$ilDB->quote($this->getShortText(), "text")." ".
349  " WHERE id = ".$ilDB->quote($this->getId(), "integer"));
350  }
351 
358  function shortenShortText($text)
359  {
360  $text = str_replace("<br/>", "<br>", $text);
361  $text = strip_tags($text, "<br>");
362  if (is_int(strpos(substr($text, 175, 10), "[tex]")))
363  {
364  $offset = 5;
365  }
366  $short = ilUtil::shortenText($text, 180 + $offset, true);
367 
368  // make short text longer, if tex end tag is missing
369  $ltexs = strrpos($short, "[tex]");
370  $ltexe = strrpos($short, "[/tex]");
371  if ($ltexs > $ltexe)
372  {
373  $ltexe = strpos($text, "[/tex]", $ltexs);
374  if ($ltexe > 0)
375  {
376  $short = ilUtil::shortenText($text, $ltexe+6, true);
377  }
378  }
379 
380  $short = ilUtil::shortenText($text, 196, true);
381 
382  return $short;
383  }
384 
385  function updateShortText()
386  {
387  $this->page_object->buildDom();
388  $text = $this->page_object->getFirstParagraphText();
389 
390  $short = $this->shortenShortText($text);
391 
392  $this->setShortText($short);
393  $this->update();
394  }
395 
399  function getDefinitionList($a_term_id)
400  {
401  global $ilDB;
402 
403  $defs = array();
404  $q = "SELECT * FROM glossary_definition WHERE term_id = ".
405  $ilDB->quote($a_term_id, "integer").
406  " ORDER BY nr";
407  $def_set = $ilDB->query($q);
408  while ($def_rec = $ilDB->fetchAssoc($def_set))
409  {
410  $defs[] = array("term_id" => $def_rec["term_id"],
411  "page_id" => $def_rec["page_id"], "id" => $def_rec["id"],
412  "short_text" => strip_tags($def_rec["short_text"], "<br>"),
413  "nr" => $def_rec["nr"]);
414  }
415  return $defs;
416  }
417 
421  function exportXML(&$a_xml_writer, $a_inst)
422  {
423  $attrs = array();
424  $a_xml_writer->xmlStartTag("Definition", $attrs);
425 
426  $this->exportXMLMetaData($a_xml_writer);
427  $this->exportXMLDefinition($a_xml_writer, $a_inst);
428 
429  $a_xml_writer->xmlEndTag("Definition");
430  }
431 
432 
439  function exportXMLMetaData(&$a_xml_writer)
440  {
442  include_once("Services/MetaData/classes/class.ilMD2XML.php");
443  $md2xml = new ilMD2XML($glo_id, $this->getId(), $this->getType());
444  $md2xml->setExportMode(true);
445  $md2xml->startExport();
446  $a_xml_writer->appendXML($md2xml->getXML());
447  }
448 
452  function modifyExportIdentifier($a_tag, $a_param, $a_value)
453  {
454  if ($a_tag == "Identifier" && $a_param == "Entry")
455  {
456  $a_value = "il_".IL_INST_ID."_gdf_".$this->getId();
457  }
458 
459  return $a_value;
460  }
461 
462 
469  function exportXMLDefinition(&$a_xml_writer, $a_inst = 0)
470  {
471 
472  $this->page_object->buildDom();
473  $this->page_object->insertInstIntoIDs($a_inst);
474  $this->mobs_contained = $this->page_object->collectMediaObjects(false);
475  $this->files_contained = $this->page_object->collectFileItems();
476  $xml = $this->page_object->getXMLFromDom(false, false, false, "", true);
477  $xml = str_replace("&","&amp;", $xml);
478  $a_xml_writer->appendXML($xml);
479 
480  $this->page_object->freeDom();
481  }
482 
486  function createMetaData()
487  {
488  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
489 
490  global $ilUser;
491 
494  $md_creator = new ilMDCreator($glo_id,$this->getId(),$this->getType());
495  $md_creator->setTitle($this->getTitle());
496  $md_creator->setTitleLanguage($lang);
497  $md_creator->setDescription($this->getDescription());
498  $md_creator->setDescriptionLanguage($lang);
499  $md_creator->setKeywordLanguage($lang);
500  $md_creator->setLanguage($lang);
501  $md_creator->create();
502 
503  return true;
504  }
505 
509  function updateMetaData()
510  {
511  include_once("Services/MetaData/classes/class.ilMD.php");
512  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
513  include_once("Services/MetaData/classes/class.ilMDDescription.php");
514 
516  $md =& new ilMD($glo_id, $this->getId(), $this->getType());
517  $md_gen =& $md->getGeneral();
518  $md_gen->setTitle($this->getTitle());
519 
520  // sets first description (maybe not appropriate)
521  $md_des_ids =& $md_gen->getDescriptionIds();
522  if (count($md_des_ids) > 0)
523  {
524  $md_des =& $md_gen->getDescription($md_des_ids[0]);
525  $md_des->setDescription($this->getDescription());
526  $md_des->update();
527  }
528  $md_gen->update();
529  }
530 
534  function deleteMetaData()
535  {
536  // Delete meta data
537  include_once('Services/MetaData/classes/class.ilMD.php');
539  $md = new ilMD($glo_id, $this->getId(), $this->getType());
540  $md->deleteAll();
541  }
542 
557  function MDUpdateListener($a_element)
558  {
559  include_once 'Services/MetaData/classes/class.ilMD.php';
560 
561  switch($a_element)
562  {
563  case 'General':
564 
565  // Update Title and description
567  $md =& new ilMD($glo_id, $this->getId(), $this->getType());
568  $md_gen = $md->getGeneral();
569 
570  //ilObject::_writeTitle($this->getId(),$md_gen->getTitle());
571  $this->setTitle($md_gen->getTitle());
572 
573  foreach($md_gen->getDescriptionIds() as $id)
574  {
575  $md_des = $md_gen->getDescription($id);
576  //ilObject::_writeDescription($this->getId(),$md_des->getDescription());
577  $this->setDescription($md_des->getDescription());
578  break;
579  }
580 
581  break;
582 
583  default:
584  }
585  return true;
586  }
587 
593  function _lookupTermId($a_def_id)
594  {
595  global $ilDB;
596 
597  $q = "SELECT * FROM glossary_definition WHERE id = ".
598  $ilDB->quote($a_def_id, "integer");
599  $def_set = $ilDB->query($q);
600  $def_rec = $ilDB->fetchAssoc($def_set);
601 
602  return $def_rec["term_id"];
603  }
604 } // END class ilGlossaryDefinition
605 
606 ?>