ILIAS  release_4-3 Revision
 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  var $short_text_dirty = false;
27 
32  function ilGlossaryDefinition($a_id = 0)
33  {
34  global $lng, $ilias, $tpl;
35 
36  $this->lng =& $lng;
37  $this->ilias =& $ilias;
38  $this->tpl =& $tpl;
39 
40  $this->id = $a_id;
41  if ($a_id != 0)
42  {
43  $this->read();
44  }
45  }
46 
50  function read()
51  {
52  global $ilDB;
53 
54  $q = "SELECT * FROM glossary_definition WHERE id = ".
55  $ilDB->quote($this->id, "integer");
56  $def_set = $ilDB->query($q);
57  $def_rec = $ilDB->fetchAssoc($def_set);
58 
59  $this->setTermId($def_rec["term_id"]);
60  $this->setShortText($def_rec["short_text"]);
61  $this->setNr($def_rec["nr"]);
62  $this->setShortTextDirty($def_rec["short_text_dirty"]);
63 
64  $this->page_object =& new ilPageObject("gdf", $this->id);
65  }
66 
67  function setId($a_id)
68  {
69  $this->id = $a_id;
70  }
71 
72  function getId()
73  {
74  return $this->id;
75  }
76 
77  function getType()
78  {
79  return "gdf";
80  }
81 
82  function setTermId($a_term_id)
83  {
84  $this->term_id = $a_term_id;
85  }
86 
87  function getTermId()
88  {
89  return $this->term_id;
90  }
91 
92  function setShortText($a_text)
93  {
94  $this->short_text = $this->shortenShortText($a_text);
95  }
96 
97  function getShortText()
98  {
99  return $this->short_text;
100  }
101 
102  function setNr($a_nr)
103  {
104  $this->nr = $a_nr;
105  }
106 
107  function getNr()
108  {
109  return $this->nr;
110  }
111 
112  function assignPageObject(&$a_page_object)
113  {
114  $this->page_object =& $a_page_object;
115  }
116 
117  function &getPageObject()
118  {
119  return $this->page_object;
120  }
121 
127  function getTitle()
128  {
129  return $this->title;
130  }
131 
135  function setTitle($a_title)
136  {
137  $this->title = $a_title;
138  }
139 
145  function getDescription()
146  {
147  return $this->description;
148  }
149 
155  function setDescription($a_description)
156  {
157  $this->description = $a_description;
158  }
159 
165  function setShortTextDirty($a_val)
166  {
167  $this->short_text_dirty = $a_val;
168  }
169 
175  function getShortTextDirty()
176  {
178  }
184  function create($a_upload = false)
185  {
186  global $ilDB;
187 
188  $term =& new ilGlossaryTerm($this->getTermId());
189 
190  $this->setId($ilDB->nextId("glossary_definition"));
191 
192  // lock glossary_definition table
193  $ilDB->lockTables(
194  array(
195  0 => array('name' => 'glossary_definition', 'type' => ilDB::LOCK_WRITE)));
196 
197  // get maximum definition number
198  $q = "SELECT max(nr) AS max_nr FROM glossary_definition WHERE term_id = ".
199  $ilDB->quote($this->getTermId(), "integer");
200  $max_set = $ilDB->query($q);
201  $max_rec = $ilDB->fetchAssoc($max_set);
202  $max = (int) $max_rec["max_nr"];
203 
204  // insert new definition record
205  $ilDB->manipulate("INSERT INTO glossary_definition (id, term_id, short_text, nr, short_text_dirty)".
206  " VALUES (".
207  $ilDB->quote($this->getId(), "integer").",".
208  $ilDB->quote($this->getTermId(), "integer").",".
209  $ilDB->quote($this->getShortText(), "text").", ".
210  $ilDB->quote(($max + 1), "integer").", ".
211  $ilDB->quote($this->getShortTextDirty(), "integer").
212  ")");
213 
214  // unlock glossary definition table
215  $ilDB->unlockTables();
216 
217  // get number
218  $q = "SELECT nr FROM glossary_definition WHERE id = ".
219  $ilDB->quote($this->id, "integer");
220  $def_set = $ilDB->query($q);
221  $def_rec = $ilDB->fetchAssoc($def_set);
222  $this->setNr($def_rec["nr"]);
223 
224  // meta data will be created by
225  // import parser
226  if (!$a_upload)
227  {
228  $this->createMetaData();
229  }
230 
231  $this->page_object =& new ilPageObject("gdf");
232  $this->page_object->setId($this->getId());
233  $this->page_object->setParentId($term->getGlossaryId());
234  $this->page_object->create();
235  }
236 
237  function delete()
238  {
239  global $ilDB;
240 
241  // lock glossary_definition table
242  #ilDB::_lockTables(array('glossary_definition' => 'WRITE'));
243  $ilDB->lockTables(
244  array(
245  0 => array('name' => 'glossary_definition', 'type' => ilDB::LOCK_WRITE)));
246 
247 
248  // be sure to get the right number
249  $q = "SELECT * FROM glossary_definition WHERE id = ".
250  $ilDB->quote($this->id, "integer");
251  $def_set = $ilDB->query($q);
252  $def_rec = $ilDB->fetchAssoc($def_set);
253  $this->setNr($def_rec["nr"]);
254 
255  // update numbers of other definitions
256  $ilDB->manipulate("UPDATE glossary_definition SET ".
257  " nr = nr - 1 ".
258  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
259  " AND nr > ".$ilDB->quote($this->getNr(), "integer"));
260 
261  // delete current definition
262  $ilDB->manipulate("DELETE FROM glossary_definition ".
263  " WHERE id = ".$ilDB->quote($this->getId(), "integer"));
264 
265  // unlock glossary_definition table
266  $ilDB->unlockTables();
267 
268  // delete page and meta data
269  $this->page_object->delete();
270 
271  // delete meta data
272  $this->deleteMetaData();
273 /*
274  $nested = new ilNestedSetXML();
275  $nested->init($this->getId(), $this->getType());
276  $nested->deleteAllDBData();
277 */
278  }
279 
280 
281  function moveUp()
282  {
283  global $ilDB;
284 
285  // lock glossary_definition table
286  #ilDB::_lockTables(array('glossary_definition' => 'WRITE'));
287  $ilDB->lockTables(
288  array(
289  0 => array('name' => 'glossary_definition', 'type' => ilDB::LOCK_WRITE)));
290 
291 
292  // be sure to get the right number
293  $q = "SELECT * FROM glossary_definition WHERE id = ".
294  $ilDB->quote($this->id, "integer");
295  $def_set = $ilDB->query($q);
296  $def_rec = $ilDB->fetchAssoc($def_set);
297  $this->setNr($def_rec["nr"]);
298 
299  if ($this->getNr() < 2)
300  {
301  $ilDB->unlockTables();
302  return;
303  }
304 
305  // update numbers of other definitions
306  $ilDB->manipulate("UPDATE glossary_definition SET ".
307  " nr = nr + 1 ".
308  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
309  " AND nr = ".$ilDB->quote(($this->getNr() - 1), "integer"));
310 
311  // delete current definition
312  $ilDB->manipulate("UPDATE glossary_definition SET ".
313  " nr = nr - 1 ".
314  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
315  " AND id = ".$ilDB->quote($this->getId(), "integer"));
316 
317  // unlock glossary_definition table
318  $ilDB->unlockTables();
319  }
320 
321 
322  function moveDown()
323  {
324  global $ilDB;
325 
326  // lock glossary_definition table
327  #ilDB::_lockTables(array('glossary_definition' => 'WRITE'));
328  $ilDB->lockTables(
329  array(
330  0 => array('name' => 'glossary_definition', 'type' => ilDB::LOCK_WRITE)));
331 
332  // be sure to get the right number
333  $q = "SELECT * FROM glossary_definition WHERE id = ".
334  $ilDB->quote($this->id, "integer");
335  $def_set = $ilDB->query($q);
336  $def_rec = $ilDB->fetchAssoc($def_set);
337  $this->setNr($def_rec["nr"]);
338 
339  // get max number
340  $q = "SELECT max(nr) as max_nr FROM glossary_definition WHERE term_id = ".
341  $ilDB->quote($this->getTermId(), "integer");
342  $max_set = $ilDB->query($q);
343  $max_rec = $ilDB->fetchAssoc($max_set);
344 
345  if ($this->getNr() >= $max_rec["max_nr"])
346  {
347  $ilDB->unlockTables();
348  return;
349  }
350 
351  // update numbers of other definitions
352  $ilDB->manipulate("UPDATE glossary_definition SET ".
353  " nr = nr - 1 ".
354  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
355  " AND nr = ".$ilDB->quote(($this->getNr() + 1), "integer"));
356 
357  // delete current definition
358  $ilDB->manipulate("UPDATE glossary_definition SET ".
359  " nr = nr + 1 ".
360  " WHERE term_id = ".$ilDB->quote($this->getTermId(), "integer")." ".
361  " AND id = ".$ilDB->quote($this->getId(), "integer"));
362 
363  // unlock glossary_definition table
364  $ilDB->unlockTables();
365 
366  }
367 
368 
369  function update()
370  {
371  global $ilDB;
372 
373  $this->updateMetaData();
374 
375  $ilDB->manipulate("UPDATE glossary_definition SET ".
376  " term_id = ".$ilDB->quote($this->getTermId(), "integer").", ".
377  " nr = ".$ilDB->quote($this->getNr(), "integer").", ".
378  " short_text = ".$ilDB->quote($this->getShortText(), "text").", ".
379  " short_text_dirty = ".$ilDB->quote($this->getShortTextDirty(), "integer")." ".
380  " WHERE id = ".$ilDB->quote($this->getId(), "integer"));
381  }
382 
389  function shortenShortText($text)
390  {
391  $a_length = 196;
392 
393  if ($this->getTermId() > 0)
394  {
395  include_once("./Modules/Glossary/classes/class.ilObjGlossary.php");
396  include_once("./Modules/Glossary/classes/class.ilGlossaryTerm.php");
397  $glo_id = ilGlossaryTerm::_lookGlossaryId($this->getTermId());
398  $snippet_length = ilObjGlossary::lookupSnippetLength($glo_id);
399  if ($snippet_length > 0)
400  {
401  $a_length = $snippet_length;
402  }
403  }
404 
405  $text = str_replace("<br/>", "<br>", $text);
406  $text = strip_tags($text, "<br>");
407  if (is_int(strpos(substr($text, $a_length - 16 - 5, 10), "[tex]")))
408  {
409  $offset = 5;
410  }
411  $short = ilUtil::shortenText($text, $a_length - 16 + $offset, true);
412 
413  // make short text longer, if tex end tag is missing
414  $ltexs = strrpos($short, "[tex]");
415  $ltexe = strrpos($short, "[/tex]");
416  if ($ltexs > $ltexe)
417  {
418  $ltexe = strpos($text, "[/tex]", $ltexs);
419  if ($ltexe > 0)
420  {
421  $short = ilUtil::shortenText($text, $ltexe+6, true);
422  }
423  }
424 
425  $short = ilUtil::shortenText($text, $a_length, true);
426 
427  return $short;
428  }
429 
430  function updateShortText()
431  {
432  $this->page_object->buildDom();
433  $text = $this->page_object->getFirstParagraphText();
434 
435  $short = $this->shortenShortText($text);
436 
437  $this->setShortText($short);
438  $this->setShortTextDirty(false);
439  $this->update();
440  }
441 
445  function getDefinitionList($a_term_id)
446  {
447  global $ilDB;
448 
449  $defs = array();
450  $q = "SELECT * FROM glossary_definition WHERE term_id = ".
451  $ilDB->quote($a_term_id, "integer").
452  " ORDER BY nr";
453  $def_set = $ilDB->query($q);
454  while ($def_rec = $ilDB->fetchAssoc($def_set))
455  {
456  $defs[] = array("term_id" => $def_rec["term_id"],
457  "page_id" => $def_rec["page_id"], "id" => $def_rec["id"],
458  "short_text" => strip_tags($def_rec["short_text"], "<br>"),
459  "nr" => $def_rec["nr"],
460  "short_text_dirty" => $def_rec["short_text_dirty"]);
461  }
462  return $defs;
463  }
464 
468  function exportXML(&$a_xml_writer, $a_inst)
469  {
470  $attrs = array();
471  $a_xml_writer->xmlStartTag("Definition", $attrs);
472 
473  $this->exportXMLMetaData($a_xml_writer);
474  $this->exportXMLDefinition($a_xml_writer, $a_inst);
475 
476  $a_xml_writer->xmlEndTag("Definition");
477  }
478 
479 
486  function exportXMLMetaData(&$a_xml_writer)
487  {
489  include_once("Services/MetaData/classes/class.ilMD2XML.php");
490  $md2xml = new ilMD2XML($glo_id, $this->getId(), $this->getType());
491  $md2xml->setExportMode(true);
492  $md2xml->startExport();
493  $a_xml_writer->appendXML($md2xml->getXML());
494  }
495 
499  function modifyExportIdentifier($a_tag, $a_param, $a_value)
500  {
501  if ($a_tag == "Identifier" && $a_param == "Entry")
502  {
503  $a_value = "il_".IL_INST_ID."_gdf_".$this->getId();
504  }
505 
506  return $a_value;
507  }
508 
509 
516  function exportXMLDefinition(&$a_xml_writer, $a_inst = 0)
517  {
518 
519  $this->page_object->buildDom();
520  $this->page_object->insertInstIntoIDs($a_inst);
521  $this->mobs_contained = $this->page_object->collectMediaObjects(false);
522  $this->files_contained = $this->page_object->collectFileItems();
523  $xml = $this->page_object->getXMLFromDom(false, false, false, "", true);
524  $xml = str_replace("&","&amp;", $xml);
525  $a_xml_writer->appendXML($xml);
526 
527  $this->page_object->freeDom();
528  }
529 
533  function createMetaData()
534  {
535  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
536 
537  global $ilUser;
538 
541  $md_creator = new ilMDCreator($glo_id,$this->getId(),$this->getType());
542  $md_creator->setTitle($this->getTitle());
543  $md_creator->setTitleLanguage($lang);
544  $md_creator->setDescription($this->getDescription());
545  $md_creator->setDescriptionLanguage($lang);
546  $md_creator->setKeywordLanguage($lang);
547  $md_creator->setLanguage($lang);
548 //echo "-".$this->getTitle()."-"; exit;
549  $md_creator->create();
550 
551  return true;
552  }
553 
557  function updateMetaData()
558  {
559  include_once("Services/MetaData/classes/class.ilMD.php");
560  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
561  include_once("Services/MetaData/classes/class.ilMDDescription.php");
562 
564  $md =& new ilMD($glo_id, $this->getId(), $this->getType());
565  $md_gen =& $md->getGeneral();
566  $md_gen->setTitle($this->getTitle());
567 
568  // sets first description (maybe not appropriate)
569  $md_des_ids =& $md_gen->getDescriptionIds();
570  if (count($md_des_ids) > 0)
571  {
572  $md_des =& $md_gen->getDescription($md_des_ids[0]);
573  $md_des->setDescription($this->getDescription());
574  $md_des->update();
575  }
576  $md_gen->update();
577  }
578 
582  function deleteMetaData()
583  {
584  // Delete meta data
585  include_once('Services/MetaData/classes/class.ilMD.php');
587  $md = new ilMD($glo_id, $this->getId(), $this->getType());
588  $md->deleteAll();
589  }
590 
605  function MDUpdateListener($a_element)
606  {
607  include_once 'Services/MetaData/classes/class.ilMD.php';
608 
609  switch($a_element)
610  {
611  case 'General':
612 
613  // Update Title and description
615  $md =& new ilMD($glo_id, $this->getId(), $this->getType());
616  $md_gen = $md->getGeneral();
617 
618  //ilObject::_writeTitle($this->getId(),$md_gen->getTitle());
619  $this->setTitle($md_gen->getTitle());
620 
621  foreach($md_gen->getDescriptionIds() as $id)
622  {
623  $md_des = $md_gen->getDescription($id);
624  //ilObject::_writeDescription($this->getId(),$md_des->getDescription());
625  $this->setDescription($md_des->getDescription());
626  break;
627  }
628 
629  break;
630 
631  default:
632  }
633  return true;
634  }
635 
641  function _lookupTermId($a_def_id)
642  {
643  global $ilDB;
644 
645  $q = "SELECT * FROM glossary_definition WHERE id = ".
646  $ilDB->quote($a_def_id, "integer");
647  $def_set = $ilDB->query($q);
648  $def_rec = $ilDB->fetchAssoc($def_set);
649 
650  return $def_rec["term_id"];
651  }
652 
659  static function setShortTextsDirty($a_glo_id)
660  {
661  global $ilDB;
662 
663  include_once("./Modules/Glossary/classes/class.ilGlossaryTerm.php");
664  $term_ids = ilGlossaryTerm::getTermsOfGlossary($a_glo_id);
665 
666  foreach ($term_ids as $term_id)
667  {
668  $ilDB->manipulate("UPDATE glossary_definition SET ".
669  " short_text_dirty = ".$ilDB->quote(1, "integer").
670  " WHERE term_id = ".$ilDB->quote($term_id, "integer")
671  );
672  }
673  }
674 }
675 
676 ?>