ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilGlossaryDefinition.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 
12 {
16  protected $db;
17 
21  protected $user;
22 
23  public $lng;
24  public $tpl;
25 
26  public $id;
27  public $term_id;
28  public $glo_id;
29  public $page_object;
30  public $short_text;
31  public $nr;
32  public $short_text_dirty = false;
33 
38  public function __construct($a_id = 0)
39  {
40  global $DIC;
41 
42  $this->db = $DIC->database();
43  $this->user = $DIC->user();
44  $lng = $DIC->language();
45  $tpl = $DIC["tpl"];
46 
47  $this->lng = $lng;
48  $this->tpl = $tpl;
49 
50  $this->id = $a_id;
51  if ($a_id != 0) {
52  $this->read();
53  }
54  }
55 
59  public function read()
60  {
61  $ilDB = $this->db;
62 
63  $q = "SELECT * FROM glossary_definition WHERE id = " .
64  $ilDB->quote($this->id, "integer");
65  $def_set = $ilDB->query($q);
66  $def_rec = $ilDB->fetchAssoc($def_set);
67 
68  $this->setTermId($def_rec["term_id"]);
69  $this->setShortText($def_rec["short_text"]);
70  $this->setNr($def_rec["nr"]);
71  $this->setShortTextDirty($def_rec["short_text_dirty"]);
72 
73  $this->page_object = new ilGlossaryDefPage($this->id);
74  }
75 
76  public function setId($a_id)
77  {
78  $this->id = $a_id;
79  }
80 
81  public function getId()
82  {
83  return $this->id;
84  }
85 
86  public function getType()
87  {
88  return "gdf";
89  }
90 
91  public function setTermId($a_term_id)
92  {
93  $this->term_id = $a_term_id;
94  }
95 
96  public function getTermId()
97  {
98  return $this->term_id;
99  }
100 
101  public function setShortText($a_text)
102  {
103  $this->short_text = $this->shortenShortText($a_text);
104  }
105 
106  public function getShortText()
107  {
108  return $this->short_text;
109  }
110 
111  public function setNr($a_nr)
112  {
113  $this->nr = $a_nr;
114  }
115 
116  public function getNr()
117  {
118  return $this->nr;
119  }
120 
121  public function assignPageObject(&$a_page_object)
122  {
123  $this->page_object = $a_page_object;
124  }
125 
126  public function &getPageObject()
127  {
128  return $this->page_object;
129  }
130 
136  public function getTitle()
137  {
138  return $this->title;
139  }
140 
144  public function setTitle($a_title)
145  {
146  $this->title = $a_title;
147  }
148 
154  public function getDescription()
155  {
156  return $this->description;
157  }
158 
164  public function setDescription($a_description)
165  {
166  $this->description = $a_description;
167  }
168 
174  public function setShortTextDirty($a_val)
175  {
176  $this->short_text_dirty = $a_val;
177  }
178 
184  public function getShortTextDirty()
185  {
187  }
193  public function create($a_upload = false, $a_omit_page_creation = false)
194  {
195  $ilDB = $this->db;
196 
197  $term = new ilGlossaryTerm($this->getTermId());
198 
199  $this->setId($ilDB->nextId("glossary_definition"));
200 
201  $ilAtomQuery = $ilDB->buildAtomQuery();
202  $ilAtomQuery->addTableLock('glossary_definition');
203 
204  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
205 
206  // get maximum definition number
207  $q = "SELECT max(nr) AS max_nr FROM glossary_definition WHERE term_id = " .
208  $ilDB->quote($this->getTermId(), "integer");
209  $max_set = $ilDB->query($q);
210  $max_rec = $ilDB->fetchAssoc($max_set);
211  $max = (int) $max_rec["max_nr"];
212 
213  // insert new definition record
214  $ilDB->manipulate("INSERT INTO glossary_definition (id, term_id, short_text, nr, short_text_dirty)" .
215  " VALUES (" .
216  $ilDB->quote($this->getId(), "integer") . "," .
217  $ilDB->quote($this->getTermId(), "integer") . "," .
218  $ilDB->quote($this->getShortText(), "text") . ", " .
219  $ilDB->quote(($max + 1), "integer") . ", " .
220  $ilDB->quote($this->getShortTextDirty(), "integer") .
221  ")");
222  });
223 
224  $ilAtomQuery->run();
225 
226  // get number
227  $q = "SELECT nr FROM glossary_definition WHERE id = " .
228  $ilDB->quote($this->id, "integer");
229  $def_set = $ilDB->query($q);
230  $def_rec = $ilDB->fetchAssoc($def_set);
231  $this->setNr($def_rec["nr"]);
232 
233  // meta data will be created by
234  // import parser
235  if (!$a_upload) {
236  $this->createMetaData();
237  }
238 
239  if (!$a_omit_page_creation) {
240  $this->page_object = new ilGlossaryDefPage();
241  $this->page_object->setId($this->getId());
242  $this->page_object->setParentId($term->getGlossaryId());
243  $this->page_object->create();
244  }
245  }
246 
247  public function delete()
248  {
249  $ilDB = $this->db;
250 
251  $ilAtomQuery = $ilDB->buildAtomQuery();
252  $ilAtomQuery->addTableLock("glossary_definition");
253 
254  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
255 
256  // be sure to get the right number
257  $q = "SELECT * FROM glossary_definition WHERE id = " .
258  $ilDB->quote($this->id, "integer");
259  $def_set = $ilDB->query($q);
260  $def_rec = $ilDB->fetchAssoc($def_set);
261  $this->setNr($def_rec["nr"]);
262 
263  // update numbers of other definitions
264  $ilDB->manipulate("UPDATE glossary_definition SET " .
265  " nr = nr - 1 " .
266  " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
267  " AND nr > " . $ilDB->quote($this->getNr(), "integer"));
268 
269  // delete current definition
270  $ilDB->manipulate("DELETE FROM glossary_definition " .
271  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
272  });
273  $ilAtomQuery->run();
274 
275  // delete page and meta data
276  $this->page_object->delete();
277 
278  // delete meta data
279  $this->deleteMetaData();
280  }
281 
282 
283  public function moveUp()
284  {
285  $ilDB = $this->db;
286 
287  $ilAtomQuery = $ilDB->buildAtomQuery();
288  $ilAtomQuery->addTableLock('glossary_definition');
289 
290  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
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  return;
301  }
302 
303  // update numbers of other definitions
304  $ilDB->manipulate("UPDATE glossary_definition SET " .
305  " nr = nr + 1 " .
306  " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
307  " AND nr = " . $ilDB->quote(($this->getNr() - 1), "integer"));
308 
309  // delete current definition
310  $ilDB->manipulate("UPDATE glossary_definition SET " .
311  " nr = nr - 1 " .
312  " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
313  " AND id = " . $ilDB->quote($this->getId(), "integer"));
314  });
315  $ilAtomQuery->run();
316  }
317 
318  public function moveDown()
319  {
320  $ilDB = $this->db;
321 
322  $ilAtomQuery = $ilDB->buildAtomQuery();
323  $ilAtomQuery->addTableLock('glossary_definition');
324 
325  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
326 
327  // be sure to get the right number
328  $q = "SELECT * FROM glossary_definition WHERE id = " .
329  $ilDB->quote($this->id, "integer");
330  $def_set = $ilDB->query($q);
331  $def_rec = $ilDB->fetchAssoc($def_set);
332  $this->setNr($def_rec["nr"]);
333 
334  // get max number
335  $q = "SELECT max(nr) as max_nr FROM glossary_definition WHERE term_id = " .
336  $ilDB->quote($this->getTermId(), "integer");
337  $max_set = $ilDB->query($q);
338  $max_rec = $ilDB->fetchAssoc($max_set);
339 
340  if ($this->getNr() >= $max_rec["max_nr"]) {
341  return;
342  }
343 
344  // update numbers of other definitions
345  $ilDB->manipulate("UPDATE glossary_definition SET " .
346  " nr = nr - 1 " .
347  " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
348  " AND nr = " . $ilDB->quote(($this->getNr() + 1), "integer"));
349 
350  // delete current definition
351  $ilDB->manipulate("UPDATE glossary_definition SET " .
352  " nr = nr + 1 " .
353  " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
354  " AND id = " . $ilDB->quote($this->getId(), "integer"));
355  });
356 
357  $ilAtomQuery->run();
358  }
359 
360 
361  public function update()
362  {
363  $ilDB = $this->db;
364 
365  $this->updateMetaData();
366 
367  $ilDB->manipulate("UPDATE glossary_definition SET " .
368  " term_id = " . $ilDB->quote($this->getTermId(), "integer") . ", " .
369  " nr = " . $ilDB->quote($this->getNr(), "integer") . ", " .
370  " short_text = " . $ilDB->quote($this->getShortText(), "text") . ", " .
371  " short_text_dirty = " . $ilDB->quote($this->getShortTextDirty(), "integer") . " " .
372  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
373  }
374 
381  public function shortenShortText($text)
382  {
383  $a_length = 196;
384 
385  if ($this->getTermId() > 0) {
386  $glo_id = ilGlossaryTerm::_lookGlossaryId($this->getTermId());
387  $snippet_length = ilObjGlossary::lookupSnippetLength($glo_id);
388  if ($snippet_length > 0) {
389  $a_length = $snippet_length;
390  }
391  }
392 
393  $text = str_replace("<br/>", "<br>", $text);
394  $text = strip_tags($text, "<br>");
395  if (is_int(strpos(substr($text, $a_length - 16 - 5, 10), "[tex]"))) {
396  $offset = 5;
397  }
398  $short = ilUtil::shortenText($text, $a_length - 16 + $offset, true);
399 
400  // make short text longer, if tex end tag is missing
401  $ltexs = strrpos($short, "[tex]");
402  $ltexe = strrpos($short, "[/tex]");
403  if ($ltexs > $ltexe) {
404  $ltexe = strpos($text, "[/tex]", $ltexs);
405  if ($ltexe > 0) {
406  $text = ilUtil::shortenText($text, $ltexe + 6, true);
407  }
408  }
409 
410  $short = ilUtil::shortenText($text, $a_length, true);
411 
412  return $short;
413  }
414 
415  public function updateShortText()
416  {
417  $this->page_object->buildDom();
418  $text = $this->page_object->getFirstParagraphText();
419  $short = $this->shortenShortText($text);
420 
421  $this->setShortText($short);
422  $this->setShortTextDirty(false);
423  $this->update();
424  }
425 
429  public static function getDefinitionList($a_term_id)
430  {
431  global $DIC;
432 
433  $ilDB = $DIC->database();
434 
435  $defs = array();
436  $q = "SELECT * FROM glossary_definition WHERE term_id = " .
437  $ilDB->quote($a_term_id, "integer") .
438  " ORDER BY nr";
439  $def_set = $ilDB->query($q);
440  while ($def_rec = $ilDB->fetchAssoc($def_set)) {
441  $defs[] = array("term_id" => $def_rec["term_id"],
442  "page_id" => $def_rec["page_id"], "id" => $def_rec["id"],
443  "short_text" => strip_tags($def_rec["short_text"], "<br>"),
444  "nr" => $def_rec["nr"],
445  "short_text_dirty" => $def_rec["short_text_dirty"]);
446  }
447  return $defs;
448  }
449 
453  public function exportXML(&$a_xml_writer, $a_inst)
454  {
455  $attrs = array();
456  $a_xml_writer->xmlStartTag("Definition", $attrs);
457 
458  $this->exportXMLMetaData($a_xml_writer);
459  $this->exportXMLDefinition($a_xml_writer, $a_inst);
460 
461  $a_xml_writer->xmlEndTag("Definition");
462  }
463 
464 
471  public function exportXMLMetaData(&$a_xml_writer)
472  {
474  $md2xml = new ilMD2XML($glo_id, $this->getId(), $this->getType());
475  $md2xml->setExportMode(true);
476  $md2xml->startExport();
477  $a_xml_writer->appendXML($md2xml->getXML());
478  }
479 
483  public function modifyExportIdentifier($a_tag, $a_param, $a_value)
484  {
485  if ($a_tag == "Identifier" && $a_param == "Entry") {
486  $a_value = "il_" . IL_INST_ID . "_gdf_" . $this->getId();
487  }
488 
489  return $a_value;
490  }
491 
492 
499  public function exportXMLDefinition(&$a_xml_writer, $a_inst = 0)
500  {
501  $this->page_object->buildDom();
502  $this->page_object->insertInstIntoIDs($a_inst);
503  $this->mobs_contained = $this->page_object->collectMediaObjects(false);
504  $this->files_contained = ilPCFileList::collectFileItems($this->page_object, $this->page_object->getDomDoc());
505  $xml = $this->page_object->getXMLFromDom(false, false, false, "", true);
506  $xml = str_replace("&", "&amp;", $xml);
507  $a_xml_writer->appendXML($xml);
508 
509  $this->page_object->freeDom();
510  }
511 
515  public function createMetaData()
516  {
518 
521  $md_creator = new ilMDCreator($glo_id, $this->getId(), $this->getType());
522  $md_creator->setTitle($this->getTitle());
523  $md_creator->setTitleLanguage($lang);
524  $md_creator->setDescription($this->getDescription());
525  $md_creator->setDescriptionLanguage($lang);
526  $md_creator->setKeywordLanguage($lang);
527  $md_creator->setLanguage($lang);
528  //echo "-".$this->getTitle()."-"; exit;
529  $md_creator->create();
530 
531  return true;
532  }
533 
537  public function updateMetaData()
538  {
540  $md = new ilMD($glo_id, $this->getId(), $this->getType());
541  $md_gen = $md->getGeneral();
542  $md_gen->setTitle($this->getTitle());
543 
544  // sets first description (maybe not appropriate)
545  $md_des_ids = $md_gen->getDescriptionIds();
546  if (count($md_des_ids) > 0) {
547  $md_des = $md_gen->getDescription($md_des_ids[0]);
548  $md_des->setDescription($this->getDescription());
549  $md_des->update();
550  }
551  $md_gen->update();
552  }
553 
557  public function deleteMetaData()
558  {
559  // Delete meta data
561  $md = new ilMD($glo_id, $this->getId(), $this->getType());
562  $md->deleteAll();
563  }
564 
579  public function MDUpdateListener($a_element)
580  {
581  switch ($a_element) {
582  case 'General':
583 
584  // Update Title and description
586  $md = new ilMD($glo_id, $this->getId(), $this->getType());
587  $md_gen = $md->getGeneral();
588 
589  //ilObject::_writeTitle($this->getId(),$md_gen->getTitle());
590  $this->setTitle($md_gen->getTitle());
591 
592  foreach ($md_gen->getDescriptionIds() as $id) {
593  $md_des = $md_gen->getDescription($id);
594  //ilObject::_writeDescription($this->getId(),$md_des->getDescription());
595  $this->setDescription($md_des->getDescription());
596  break;
597  }
598 
599  break;
600 
601  default:
602  }
603  return true;
604  }
605 
611  public static function _lookupTermId($a_def_id)
612  {
613  global $DIC;
614 
615  $ilDB = $DIC->database();
616 
617  $q = "SELECT * FROM glossary_definition WHERE id = " .
618  $ilDB->quote($a_def_id, "integer");
619  $def_set = $ilDB->query($q);
620  $def_rec = $ilDB->fetchAssoc($def_set);
621 
622  return $def_rec["term_id"];
623  }
624 
631  public static function setShortTextsDirty($a_glo_id)
632  {
633  global $DIC;
634 
635  $ilDB = $DIC->database();
636 
637  $term_ids = ilGlossaryTerm::getTermsOfGlossary($a_glo_id);
638 
639  foreach ($term_ids as $term_id) {
640  $ilDB->manipulate(
641  "UPDATE glossary_definition SET " .
642  " short_text_dirty = " . $ilDB->quote(1, "integer") .
643  " WHERE term_id = " . $ilDB->quote($term_id, "integer")
644  );
645  }
646  }
647 
654  public static function setShortTextsDirtyGlobally()
655  {
656  global $DIC;
657 
658  $ilDB = $DIC->database();
659 
660  $ilDB->manipulate(
661  "UPDATE glossary_definition SET " .
662  " short_text_dirty = " . $ilDB->quote(1, "integer")
663  );
664  }
665 }
static getDefinitionList($a_term_id)
static
static shortenText( $a_str, $a_len, $a_dots=false, $a_next_blank=false, $a_keep_extension=false)
shorten a string to given length.
__construct($a_id=0)
Constructor public.
modifyExportIdentifier($a_tag, $a_param, $a_value)
updateMetaData()
update meta data entry
Glossary terms.
getTitle()
get title of content object
setShortTextDirty($a_val)
Set short text dirty.
user()
Definition: user.php:4
static _lookupTermId($a_def_id)
Looks up term id for a definition id.
setDescription($a_description)
Set description.
Interface ilDBInterface.
quote($value, $type)
static setShortTextsDirtyGlobally()
Set short texts dirty.
static collectFileItems($a_page, $a_domdoc)
Get all file items that are used within the page.
deleteMetaData()
delete meta data entry
setTitle($a_title)
set title of content object
exportXMLDefinition(&$a_xml_writer, $a_inst=0)
export page objects meta data to xml (see ilias_co.dtd)
$ilUser
Definition: imgupload.php:18
read()
read data of content object
$xml
Definition: metadata.php:332
static _lookLanguage($term_id)
lookup term language
Glossary definition page object.
MDUpdateListener($a_element)
Meta data update listener.
static _lookGlossaryID($term_id)
get glossary id form term id
$lang
Definition: xapiexit.php:8
Class ilGlossaryDefinition.
createMetaData()
create meta data entry
exportXML(&$a_xml_writer, $a_inst)
export xml
global $ilDB
query($query)
Run a (read-only) Query on the database.
$DIC
Definition: xapitoken.php:46
fetchAssoc($query_result)
static getTermsOfGlossary($a_glo_id)
Get terms of glossary.
exportXMLMetaData(&$a_xml_writer)
export content objects meta data to xml (see ilias_co.dtd)
shortenShortText($text)
Shorten short text.
manipulate($query)
Run a (write) Query on the database.
static setShortTextsDirty($a_glo_id)
Set short texts dirty.
getShortTextDirty()
Get short text dirty.
static lookupSnippetLength($a_id)
Lookup snippet length.
create($a_upload=false, $a_omit_page_creation=false)
Create definition.