ILIAS  release_8 Revision v8.24
class.ilGlossaryDefinition.php
Go to the documentation of this file.
1<?php
2
23{
24 protected array $files_contained = [];
25 protected array $mobs_contained = [];
26 protected string $description = "";
27 protected string $title = "";
28 protected ilDBInterface $db;
29 protected ilObjUser $user;
32 public int $id = 0;
33 public int $term_id = 0;
34 public int $glo_id = 0;
36 public string $short_text = "";
37 public int $nr = 0;
38 public bool $short_text_dirty = false;
39
40 public function __construct(
41 int $a_id = 0
42 ) {
43 global $DIC;
44
45 $this->db = $DIC->database();
46 $this->user = $DIC->user();
47 $lng = $DIC->language();
48 $tpl = $DIC["tpl"];
49
50 $this->lng = $lng;
51 $this->tpl = $tpl;
52
53 $this->id = $a_id;
54 if ($a_id != 0) {
55 $this->read();
56 }
57 }
58
59 public function read(): void
60 {
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((int) $def_rec["term_id"]);
69 $this->setShortText((string) $def_rec["short_text"]);
70 $this->setNr((int) $def_rec["nr"]);
71 $this->setShortTextDirty((bool) $def_rec["short_text_dirty"]);
72
73 $this->page_object = new ilGlossaryDefPage($this->id);
74 }
75
76 public function setId(int $a_id): void
77 {
78 $this->id = $a_id;
79 }
80
81 public function getId(): int
82 {
83 return $this->id;
84 }
85
86 public function getType(): string
87 {
88 return "gdf";
89 }
90
91 public function setTermId(int $a_term_id): void
92 {
93 $this->term_id = $a_term_id;
94 }
95
96 public function getTermId(): int
97 {
98 return $this->term_id;
99 }
100
101 public function setShortText(string $a_text): void
102 {
103 $this->short_text = $this->shortenShortText($a_text);
104 }
105
106 public function getShortText(): string
107 {
108 return $this->short_text;
109 }
110
111 public function setNr(int $a_nr): void
112 {
113 $this->nr = $a_nr;
114 }
115
116 public function getNr(): int
117 {
118 return $this->nr;
119 }
120
121 public function assignPageObject(ilGlossaryDefPage $a_page_object): void
122 {
123 $this->page_object = $a_page_object;
124 }
125
127 {
128 return $this->page_object;
129 }
130
131 public function getTitle(): string
132 {
133 return $this->title;
134 }
135
136 public function setTitle(string $a_title): void
137 {
138 $this->title = $a_title;
139 }
140
141 public function getDescription(): string
142 {
143 return $this->description;
144 }
145
146 public function setDescription(string $a_description): void
147 {
148 $this->description = $a_description;
149 }
150
151 public function setShortTextDirty(bool $a_val): void
152 {
153 $this->short_text_dirty = $a_val;
154 }
155
156 public function getShortTextDirty(): bool
157 {
159 }
160 public function create(
161 bool $a_upload = false,
162 bool $a_omit_page_creation = false
163 ): void {
164 $ilDB = $this->db;
165
166 $term = new ilGlossaryTerm($this->getTermId());
167
168 $this->setId($ilDB->nextId("glossary_definition"));
169
170 $ilAtomQuery = $ilDB->buildAtomQuery();
171 $ilAtomQuery->addTableLock('glossary_definition');
172
173 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
174
175 // get maximum definition number
176 $q = "SELECT max(nr) AS max_nr FROM glossary_definition WHERE term_id = " .
177 $ilDB->quote($this->getTermId(), "integer");
178 $max_set = $ilDB->query($q);
179 $max_rec = $ilDB->fetchAssoc($max_set);
180 $max = (int) $max_rec["max_nr"];
181
182 // insert new definition record
183 $ilDB->manipulate("INSERT INTO glossary_definition (id, term_id, short_text, nr, short_text_dirty)" .
184 " VALUES (" .
185 $ilDB->quote($this->getId(), "integer") . "," .
186 $ilDB->quote($this->getTermId(), "integer") . "," .
187 $ilDB->quote($this->getShortText(), "text") . ", " .
188 $ilDB->quote(($max + 1), "integer") . ", " .
189 $ilDB->quote($this->getShortTextDirty(), "integer") .
190 ")");
191 });
192
193 $ilAtomQuery->run();
194
195 // get number
196 $q = "SELECT nr FROM glossary_definition WHERE id = " .
197 $ilDB->quote($this->id, "integer");
198 $def_set = $ilDB->query($q);
199 $def_rec = $ilDB->fetchAssoc($def_set);
200 $this->setNr($def_rec["nr"]);
201
202 // meta data will be created by
203 // import parser
204 if (!$a_upload) {
205 $this->createMetaData();
206 }
207
208 if (!$a_omit_page_creation) {
209 $this->page_object = new ilGlossaryDefPage();
210 $this->page_object->setId($this->getId());
211 $this->page_object->setParentId($term->getGlossaryId());
212 $this->page_object->create(false);
213 }
214 }
215
216 public function delete(): void
217 {
218 $ilDB = $this->db;
219
220 $ilAtomQuery = $ilDB->buildAtomQuery();
221 $ilAtomQuery->addTableLock("glossary_definition");
222
223 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
224
225 // be sure to get the right number
226 $q = "SELECT * FROM glossary_definition WHERE id = " .
227 $ilDB->quote($this->id, "integer");
228 $def_set = $ilDB->query($q);
229 $def_rec = $ilDB->fetchAssoc($def_set);
230 $this->setNr($def_rec["nr"]);
231
232 // update numbers of other definitions
233 $ilDB->manipulate("UPDATE glossary_definition SET " .
234 " nr = nr - 1 " .
235 " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
236 " AND nr > " . $ilDB->quote($this->getNr(), "integer"));
237
238 // delete current definition
239 $ilDB->manipulate("DELETE FROM glossary_definition " .
240 " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
241 });
242 $ilAtomQuery->run();
243
244 // delete page and meta data
245 $this->page_object->delete();
246
247 // delete meta data
248 $this->deleteMetaData();
249 }
250
251
252 public function moveUp(): void
253 {
254 $ilDB = $this->db;
255
256 $ilAtomQuery = $ilDB->buildAtomQuery();
257 $ilAtomQuery->addTableLock('glossary_definition');
258
259 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
260
261 // be sure to get the right number
262 $q = "SELECT * FROM glossary_definition WHERE id = " .
263 $ilDB->quote($this->id, "integer");
264 $def_set = $ilDB->query($q);
265 $def_rec = $ilDB->fetchAssoc($def_set);
266 $this->setNr($def_rec["nr"]);
267
268 if ($this->getNr() < 2) {
269 return;
270 }
271
272 // update numbers of other definitions
273 $ilDB->manipulate("UPDATE glossary_definition SET " .
274 " nr = nr + 1 " .
275 " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
276 " AND nr = " . $ilDB->quote(($this->getNr() - 1), "integer"));
277
278 // delete current definition
279 $ilDB->manipulate("UPDATE glossary_definition SET " .
280 " nr = nr - 1 " .
281 " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
282 " AND id = " . $ilDB->quote($this->getId(), "integer"));
283 });
284 $ilAtomQuery->run();
285 }
286
287 public function moveDown(): void
288 {
289 $ilDB = $this->db;
290
291 $ilAtomQuery = $ilDB->buildAtomQuery();
292 $ilAtomQuery->addTableLock('glossary_definition');
293
294 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
295
296 // be sure to get the right number
297 $q = "SELECT * FROM glossary_definition WHERE id = " .
298 $ilDB->quote($this->id, "integer");
299 $def_set = $ilDB->query($q);
300 $def_rec = $ilDB->fetchAssoc($def_set);
301 $this->setNr($def_rec["nr"]);
302
303 // get max number
304 $q = "SELECT max(nr) as max_nr FROM glossary_definition WHERE term_id = " .
305 $ilDB->quote($this->getTermId(), "integer");
306 $max_set = $ilDB->query($q);
307 $max_rec = $ilDB->fetchAssoc($max_set);
308
309 if ($this->getNr() >= $max_rec["max_nr"]) {
310 return;
311 }
312
313 // update numbers of other definitions
314 $ilDB->manipulate("UPDATE glossary_definition SET " .
315 " nr = nr - 1 " .
316 " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
317 " AND nr = " . $ilDB->quote(($this->getNr() + 1), "integer"));
318
319 // delete current definition
320 $ilDB->manipulate("UPDATE glossary_definition SET " .
321 " nr = nr + 1 " .
322 " WHERE term_id = " . $ilDB->quote($this->getTermId(), "integer") . " " .
323 " AND id = " . $ilDB->quote($this->getId(), "integer"));
324 });
325
326 $ilAtomQuery->run();
327 }
328
329 public function update(): void
330 {
331 $ilDB = $this->db;
332
333 $this->updateMetaData();
334
335 $ilDB->manipulate("UPDATE glossary_definition SET " .
336 " term_id = " . $ilDB->quote($this->getTermId(), "integer") . ", " .
337 " nr = " . $ilDB->quote($this->getNr(), "integer") . ", " .
338 " short_text = " . $ilDB->quote($this->getShortText(), "text") . ", " .
339 " short_text_dirty = " . $ilDB->quote($this->getShortTextDirty(), "integer") . " " .
340 " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
341 }
342
346 public function shortenShortText(string $text): string
347 {
348 $a_length = 196;
349
350 if ($this->getTermId() > 0) {
351 $glo_id = ilGlossaryTerm::_lookGlossaryID($this->getTermId());
352 $snippet_length = ilObjGlossary::lookupSnippetLength($glo_id);
353 if ($snippet_length > 0) {
354 $a_length = $snippet_length;
355 }
356 }
357
358 $text = str_replace("<br/>", "<br>", $text);
359 $text = strip_tags($text, "<br>");
360 $offset = 0;
361 if (is_int(strpos(substr($text, $a_length - 16 - 5, 10), "[tex]"))) {
362 $offset = 5;
363 }
364 $short = ilStr::shortenTextExtended($text, $a_length - 16 + $offset, true);
365
366 // make short text longer, if tex end tag is missing
367 $ltexs = strrpos($short, "[tex]");
368 $ltexe = strrpos($short, "[/tex]");
369 if ($ltexs > $ltexe) {
370 $ltexe = strpos($text, "[/tex]", $ltexs);
371 if ($ltexe > 0) {
372 $text = ilStr::shortenTextExtended($text, $ltexe + 6, true);
373 }
374 }
375
376 $short = ilStr::shortenTextExtended($text, $a_length, true);
377
378 return $short;
379 }
380
381 public function updateShortText(): void
382 {
383 $this->page_object->buildDom();
384 $text = $this->page_object->getFirstParagraphText();
385 $short = $this->shortenShortText($text);
386
387 $this->setShortText($short);
388 $this->setShortTextDirty(false);
389 $this->update();
390 }
391
392 public static function getDefinitionList(int $a_term_id): array
393 {
394 global $DIC;
395
396 $ilDB = $DIC->database();
397
398 $defs = array();
399 $q = "SELECT * FROM glossary_definition WHERE term_id = " .
400 $ilDB->quote($a_term_id, "integer") .
401 " ORDER BY nr";
402 $def_set = $ilDB->query($q);
403 while ($def_rec = $ilDB->fetchAssoc($def_set)) {
404 $defs[] = array("term_id" => $def_rec["term_id"],
405 "page_id" => (int) ($def_rec["page_id"] ?? 0), "id" => $def_rec["id"],
406 "short_text" => strip_tags($def_rec["short_text"], "<br>"),
407 "nr" => $def_rec["nr"],
408 "short_text_dirty" => $def_rec["short_text_dirty"]);
409 }
410 return $defs;
411 }
412
413 public function exportXML(
414 ilXmlWriter $a_xml_writer,
415 int $a_inst
416 ): void {
417 $attrs = array();
418 $a_xml_writer->xmlStartTag("Definition", $attrs);
419
420 $this->exportXMLMetaData($a_xml_writer);
421 $this->exportXMLDefinition($a_xml_writer, $a_inst);
422
423 $a_xml_writer->xmlEndTag("Definition");
424 }
425
426
427 public function exportXMLMetaData(
428 ilXmlWriter $a_xml_writer
429 ): void {
430 $glo_id = ilGlossaryTerm::_lookGlossaryID($this->getTermId());
431 $md2xml = new ilMD2XML($glo_id, $this->getId(), $this->getType());
432 $md2xml->setExportMode(true);
433 $md2xml->startExport();
434 $a_xml_writer->appendXML($md2xml->getXML());
435 }
436
437 public function modifyExportIdentifier(
438 string $a_tag,
439 string $a_param,
440 string $a_value
441 ): string {
442 if ($a_tag == "Identifier" && $a_param == "Entry") {
443 $a_value = "il_" . IL_INST_ID . "_gdf_" . $this->getId();
444 }
445
446 return $a_value;
447 }
448
452 public function exportXMLDefinition(
453 ilXmlWriter $a_xml_writer,
454 int $a_inst = 0
455 ): void {
456 $this->page_object->buildDom();
457 $this->page_object->insertInstIntoIDs($a_inst);
458 $this->mobs_contained = $this->page_object->collectMediaObjects(false);
459 $this->files_contained = ilPCFileList::collectFileItems($this->page_object, $this->page_object->getDomDoc());
460 $xml = $this->page_object->getXMLFromDom(false, false, false, "", true);
461 $xml = str_replace("&", "&amp;", $xml);
462 $a_xml_writer->appendXML($xml);
463
464 $this->page_object->freeDom();
465 }
466
467 public function createMetaData(): void
468 {
469 $glo_id = ilGlossaryTerm::_lookGlossaryID($this->getTermId());
470 $lang = ilGlossaryTerm::_lookLanguage($this->getTermId());
471 $md_creator = new ilMDCreator($glo_id, $this->getId(), $this->getType());
472 $md_creator->setTitle($this->getTitle());
473 $md_creator->setTitleLanguage($lang);
474 $md_creator->setDescription($this->getDescription());
475 $md_creator->setDescriptionLanguage($lang);
476 $md_creator->setKeywordLanguage($lang);
477 $md_creator->setLanguage($lang);
478 $md_creator->create();
479 }
480
481 public function updateMetaData(): void
482 {
483 $glo_id = ilGlossaryTerm::_lookGlossaryID($this->getTermId());
484 $md = new ilMD($glo_id, $this->getId(), $this->getType());
485 $md_gen = $md->getGeneral();
486 $md_gen->setTitle($this->getTitle());
487
488 // sets first description (maybe not appropriate)
489 $md_des_ids = $md_gen->getDescriptionIds();
490 if (count($md_des_ids) > 0) {
491 $md_des = $md_gen->getDescription($md_des_ids[0]);
492 $md_des->setDescription($this->getDescription());
493 $md_des->update();
494 }
495 $md_gen->update();
496 }
497
498 public function deleteMetaData(): void
499 {
500 // Delete meta data
501 $glo_id = ilGlossaryTerm::_lookGlossaryID($this->getTermId());
502 $md = new ilMD($glo_id, $this->getId(), $this->getType());
503 $md->deleteAll();
504 }
505
518 public function MDUpdateListener(string $a_element): bool
519 {
520 switch ($a_element) {
521 case 'General':
522
523 // Update Title and description
524 $glo_id = ilGlossaryTerm::_lookGlossaryID($this->getTermId());
525 $md = new ilMD($glo_id, $this->getId(), $this->getType());
526 $md_gen = $md->getGeneral();
527
528 //ilObject::_writeTitle($this->getId(),$md_gen->getTitle());
529 $this->setTitle($md_gen->getTitle());
530
531 foreach ($md_gen->getDescriptionIds() as $id) {
532 $md_des = $md_gen->getDescription($id);
533 //ilObject::_writeDescription($this->getId(),$md_des->getDescription());
534 $this->setDescription($md_des->getDescription());
535 break;
536 }
537
538 break;
539
540 default:
541 }
542 return true;
543 }
544
549 public static function _lookupTermId(int $a_def_id): int
550 {
551 global $DIC;
552
553 $ilDB = $DIC->database();
554
555 $q = "SELECT * FROM glossary_definition WHERE id = " .
556 $ilDB->quote($a_def_id, "integer");
557 $def_set = $ilDB->query($q);
558 $def_rec = $ilDB->fetchAssoc($def_set);
559
560 return $def_rec["term_id"];
561 }
562
567 public static function setShortTextsDirty(int $a_glo_id): void
568 {
569 global $DIC;
570
571 $ilDB = $DIC->database();
572
573 $term_ids = ilGlossaryTerm::getTermsOfGlossary($a_glo_id);
574
575 foreach ($term_ids as $term_id) {
576 $ilDB->manipulate(
577 "UPDATE glossary_definition SET " .
578 " short_text_dirty = " . $ilDB->quote(1, "integer") .
579 " WHERE term_id = " . $ilDB->quote($term_id, "integer")
580 );
581 }
582 }
583
587 public static function setShortTextsDirtyGlobally(): void
588 {
589 global $DIC;
590
591 $ilDB = $DIC->database();
592
593 $ilDB->manipulate(
594 "UPDATE glossary_definition SET " .
595 " short_text_dirty = " . $ilDB->quote(1, "integer")
596 );
597 }
598}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
assignPageObject(ilGlossaryDefPage $a_page_object)
static setShortTextsDirty(int $a_glo_id)
Set all short texts of glossary dirty (e.g.
exportXMLDefinition(ilXmlWriter $a_xml_writer, int $a_inst=0)
export page objects meta data to xml
static getDefinitionList(int $a_term_id)
ilGlobalTemplateInterface $tpl
static setShortTextsDirtyGlobally()
Set short texts dirty (for all glossaries)
setDescription(string $a_description)
exportXMLMetaData(ilXmlWriter $a_xml_writer)
static _lookupTermId(int $a_def_id)
Looks up term id for a definition id.
modifyExportIdentifier(string $a_tag, string $a_param, string $a_value)
exportXML(ilXmlWriter $a_xml_writer, int $a_inst)
shortenShortText(string $text)
Shorten short text.
MDUpdateListener(string $a_element)
Meta data update listener.
create(bool $a_upload=false, bool $a_omit_page_creation=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookLanguage(int $term_id)
lookup term language
static getTermsOfGlossary(int $a_glo_id)
static _lookGlossaryID(int $term_id)
get glossary id form term id
language handling
static lookupSnippetLength(int $a_id)
User class.
static collectFileItems(ilPageObject $a_page, DOMDocument $a_domdoc)
Get all file items that are used within the page.
static shortenTextExtended(string $a_str, int $a_len, bool $a_dots=false, bool $a_next_blank=false, bool $a_keep_extension=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlEndTag(string $tag)
Writes an endtag.
appendXML(string $a_str)
append xml string to document
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$xml
Definition: metadata.php:351
$lang
Definition: xapiexit.php:26