ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilGlossaryTerm.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected string $type;
26  protected ilDBInterface $db;
27  public ilLanguage $lng;
29  public int $id = 0;
31  public string $term = "";
32  public string $language = "";
33  public int $glo_id = 0;
34  public string $import_id = "";
35 
36  public function __construct(int $a_id = 0)
37  {
38  global $DIC;
39 
40  $this->db = $DIC->database();
41  $lng = $DIC->language();
42  $tpl = $DIC["tpl"];
43 
44  $this->lng = $lng;
45  $this->tpl = $tpl;
46 
47  $this->id = $a_id;
48  $this->type = "term";
49  if ($a_id != 0) {
50  $this->read();
51  }
52  }
53 
54  public function read(): void
55  {
56  $ilDB = $this->db;
57 
58  $q = "SELECT * FROM glossary_term WHERE id = " .
59  $ilDB->quote($this->id, "integer");
60  $term_set = $ilDB->query($q);
61  $term_rec = $ilDB->fetchAssoc($term_set);
62 
63  $this->setTerm((string) $term_rec["term"]);
64  $this->setImportId((string) $term_rec["import_id"]);
65  $this->setLanguage((string) $term_rec["language"]);
66  $this->setGlossaryId((int) $term_rec["glo_id"]);
67  }
68 
69  public static function _getIdForImportId(
70  string $a_import_id
71  ): int {
72  global $DIC;
73 
74  $ilDB = $DIC->database();
75 
76  if ($a_import_id == "") {
77  return 0;
78  }
79 
80  $q = "SELECT * FROM glossary_term WHERE import_id = " .
81  $ilDB->quote($a_import_id, "text") .
82  " ORDER BY create_date DESC";
83  $term_set = $ilDB->query($q);
84  while ($term_rec = $ilDB->fetchAssoc($term_set)) {
85  $glo_id = self::_lookGlossaryID($term_rec["id"]);
86 
87  $ref_ids = ilObject::_getAllReferences($glo_id); // will be 0 if import of lm is in progress (new import)
88  if (count($ref_ids) == 0 || ilObject::_hasUntrashedReference($glo_id)) {
89  return (int) $term_rec["id"];
90  }
91  }
92 
93  return 0;
94  }
95 
96 
100  public static function _exists(int $a_id): bool
101  {
102  global $DIC;
103 
104  $ilDB = $DIC->database();
105 
106  if (is_int(strpos($a_id, "_"))) {
108  }
109 
110  $q = "SELECT * FROM glossary_term WHERE id = " .
111  $ilDB->quote($a_id, "integer");
112  $obj_set = $ilDB->query($q);
113  if ($obj_rec = $ilDB->fetchAssoc($obj_set)) {
114  return true;
115  } else {
116  return false;
117  }
118  }
119 
120 
124  public function setId(int $a_id): void
125  {
126  $this->id = $a_id;
127  }
128 
129  public function getId(): int
130  {
131  return $this->id;
132  }
133 
134  public function setGlossary(
135  ilObjGlossary $a_glossary
136  ): void {
137  $this->glossary = $a_glossary;
138  $this->setGlossaryId($a_glossary->getId());
139  }
140 
141  public function setGlossaryId(
142  int $a_glo_id
143  ): void {
144  $this->glo_id = $a_glo_id;
145  }
146 
147  public function getGlossaryId(): int
148  {
149  return $this->glo_id;
150  }
151 
152  public function setTerm(string $a_term): void
153  {
154  $this->term = $a_term;
155  }
156 
157  public function getTerm(): string
158  {
159  return $this->term;
160  }
161 
162  public function setLanguage(
163  string $a_language
164  ): void {
165  $this->language = $a_language;
166  }
167 
168  public function getLanguage(): string
169  {
170  return $this->language;
171  }
172 
173  public function setImportId(string $a_import_id): void
174  {
175  $this->import_id = $a_import_id;
176  }
177 
178  public function getImportId(): string
179  {
180  return $this->import_id;
181  }
182 
183  public function create(): void
184  {
185  $ilDB = $this->db;
186 
187  $this->setId($ilDB->nextId("glossary_term"));
188  $ilDB->manipulate("INSERT INTO glossary_term (id, glo_id, term, language, import_id, create_date, last_update)" .
189  " VALUES (" .
190  $ilDB->quote($this->getId(), "integer") . ", " .
191  $ilDB->quote($this->getGlossaryId(), "integer") . ", " .
192  $ilDB->quote($this->term, "text") . ", " .
193  $ilDB->quote($this->language, "text") . "," .
194  $ilDB->quote($this->getImportId(), "text") . "," .
195  $ilDB->now() . ", " .
196  $ilDB->now() . ")");
197  }
198 
202  public function delete(): void
203  {
204  $ilDB = $this->db;
205 
207  foreach ($defs as $def) {
208  $def_obj = new ilGlossaryDefinition($def["id"]);
209  $def_obj->delete();
210  }
211 
212  // delete term references
214 
215  // delete glossary_term record
216  $ilDB->manipulate("DELETE FROM glossary_term " .
217  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
218  }
219 
220  public function update(): void
221  {
222  $ilDB = $this->db;
223 
224  $ilDB->manipulate("UPDATE glossary_term SET " .
225  " glo_id = " . $ilDB->quote($this->getGlossaryId(), "integer") . ", " .
226  " term = " . $ilDB->quote($this->getTerm(), "text") . ", " .
227  " import_id = " . $ilDB->quote($this->getImportId(), "text") . ", " .
228  " language = " . $ilDB->quote($this->getLanguage(), "text") . ", " .
229  " last_update = " . $ilDB->now() . " " .
230  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
231  }
232 
236  public static function _lookGlossaryID(int $term_id): int
237  {
238  global $DIC;
239 
240  $ilDB = $DIC->database();
241 
242  $query = "SELECT * FROM glossary_term WHERE id = " .
243  $ilDB->quote($term_id, "integer");
244  $obj_set = $ilDB->query($query);
245  $obj_rec = $ilDB->fetchAssoc($obj_set);
246 
247  return (int) ($obj_rec["glo_id"] ?? 0);
248  }
249 
253  public static function _lookGlossaryTerm(int $term_id): string
254  {
255  global $DIC;
256 
257  $ilDB = $DIC->database();
258 
259  $query = "SELECT * FROM glossary_term WHERE id = " .
260  $ilDB->quote($term_id, "integer");
261  $obj_set = $ilDB->query($query);
262  $obj_rec = $ilDB->fetchAssoc($obj_set);
263 
264  return $obj_rec["term"] ?? "";
265  }
266 
270  public static function _lookLanguage(int $term_id): string
271  {
272  global $DIC;
273 
274  $ilDB = $DIC->database();
275 
276  $query = "SELECT * FROM glossary_term WHERE id = " .
277  $ilDB->quote($term_id, "integer");
278  $obj_set = $ilDB->query($query);
279  $obj_rec = $ilDB->fetchAssoc($obj_set);
280 
281  return $obj_rec["language"];
282  }
283 
287  public static function getTermList(
288  array $a_glo_ref_id,
289  string $searchterm = "",
290  string $a_first_letter = "",
291  string $a_def = "",
292  int $a_tax_node = 0,
293  bool $a_add_amet_fields = false,
294  array $a_amet_filter = null,
295  bool $a_include_references = false
296  ): array {
297  global $DIC;
298 
299  if (count($a_glo_ref_id) > 1) {
300  $a_glo_id = array_map(static function ($id): int {
301  return ilObject::_lookupObjectId($id);
302  }, $a_glo_ref_id);
303  } else {
304  $a_glo_id = ilObject::_lookupObjectId(current($a_glo_ref_id));
305  }
306  $ilDB = $DIC->database();
307 
308  $join = $in = "";
309 
310  $terms = array();
311 
312  // get all term ids under taxonomy node (if given)
313  if ($a_tax_node > 1) {
314  $tax_ids = ilObjTaxonomy::getUsageOfObject($a_glo_id);
315  if (count($tax_ids) > 0) {
316  $items = ilObjTaxonomy::getSubTreeItems("glo", $a_glo_id, "term", $tax_ids[0], $a_tax_node);
317  $sub_tree_ids = array();
318  foreach ($items as $i) {
319  $sub_tree_ids[] = $i["item_id"];
320  }
321  $in = " AND " . $ilDB->in("gt.id", $sub_tree_ids, false, "integer");
322  }
323  }
324 
325  if ($a_def != "") {
326  // meta glossary?
327  if (is_array($a_glo_id)) {
328  $glo_where = $ilDB->in("page_object.parent_id", $a_glo_id, false, "integer");
329  } else {
330  $glo_where = " page_object.parent_id = " . $ilDB->quote($a_glo_id, "integer");
331  }
332 
333  $join = " JOIN glossary_definition gd ON (gd.term_id = gt.id)" .
334  " JOIN page_object ON (" .
335  $glo_where .
336  " AND page_object.parent_type = " . $ilDB->quote("gdf", "text") .
337  " AND page_object.page_id = gd.id" .
338  " AND " . $ilDB->like("page_object.content", "text", "%" . $a_def . "%") .
339  ")";
340  }
341 
342  $searchterm = (!empty($searchterm))
343  ? " AND " . $ilDB->like("term", "text", "%" . $searchterm . "%") . " "
344  : "";
345 
346  if ($a_first_letter != "") {
347  $searchterm .= " AND " . $ilDB->upper($ilDB->substr("term", 1, 1)) . " = " . $ilDB->upper($ilDB->quote($a_first_letter, "text")) . " ";
348  }
349 
350  // include references
351  $where_glo_id_or = "";
352  if ($a_include_references) {
353  $join .= " LEFT JOIN glo_term_reference tr ON (gt.id = tr.term_id) ";
354  if (is_array($a_glo_id)) {
355  $where_glo_id_or = " OR " . $ilDB->in("tr.glo_id", $a_glo_id, false, "integer");
356  } else {
357  $where_glo_id_or = " OR tr.glo_id = " . $ilDB->quote($a_glo_id, "integer");
358  }
359  }
360 
361  // meta glossary
362  if (is_array($a_glo_id)) {
363  $where = "(" . $ilDB->in("gt.glo_id", $a_glo_id, false, "integer") . $where_glo_id_or . ")";
364  } else {
365  $where = "(gt.glo_id = " . $ilDB->quote($a_glo_id, "integer") . $where_glo_id_or . ")";
366  }
367 
368  $where .= $in;
369 
370 
371  $q = "SELECT DISTINCT(gt.term), gt.id, gt.glo_id, gt.language FROM glossary_term gt " . $join . " WHERE " . $where . $searchterm . " ORDER BY term";
372 
373  //echo $q; exit;
374 
375  $term_set = $ilDB->query($q);
376  $glo_ids = array();
377  while ($term_rec = $ilDB->fetchAssoc($term_set)) {
378  $terms[] = array("term" => $term_rec["term"],
379  "language" => $term_rec["language"], "id" => $term_rec["id"], "glo_id" => $term_rec["glo_id"]);
380  $glo_ids[] = $term_rec["glo_id"];
381  }
382 
383  // add advanced metadata
384  if (($a_add_amet_fields || is_array($a_amet_filter)) && count($a_glo_ref_id) == 1) {
385  $terms = ilAdvancedMDValues::queryForRecords(current($a_glo_ref_id), "glo", "term", $glo_ids, "term", $terms, "glo_id", "id", $a_amet_filter);
386  }
387  return $terms;
388  }
389 
390  public static function getFirstLetters(
391  array $a_glo_id,
392  int $a_tax_node = 0
393  ): array {
394  global $DIC;
395 
396  $ilDB = $DIC->database();
397 
398  // meta glossary
399  if (count($a_glo_id) > 1) {
400  $where = $ilDB->in("glo_id", $a_glo_id, false, "integer");
401  } else {
402  $a_glo_id = current($a_glo_id);
403  $where = " glo_id = " . $ilDB->quote($a_glo_id, "integer") . " ";
404  $in = "";
405  // get all term ids under taxonomy node (if given)
406  if ($a_tax_node > 1) {
407  $tax_ids = ilObjTaxonomy::getUsageOfObject($a_glo_id);
408  if (count($tax_ids) > 0) {
409  $items = ilObjTaxonomy::getSubTreeItems("glo", $a_glo_id, "term", $tax_ids[0], $a_tax_node);
410  $sub_tree_ids = array();
411  foreach ($items as $i) {
412  $sub_tree_ids[] = $i["item_id"];
413  }
414  $in = " AND " . $ilDB->in("id", $sub_tree_ids, false, "integer");
415  }
416  }
417 
418  $where .= $in;
419  }
420 
421  $q = "SELECT DISTINCT " . $ilDB->upper($ilDB->substr("term", 1, 1)) . " let FROM glossary_term WHERE " . $where . " ORDER BY let";
422  $let_set = $ilDB->query($q);
423 
424  $let = array();
425  while ($let_rec = $ilDB->fetchAssoc($let_set)) {
426  $let[$let_rec["let"]] = $let_rec["let"];
427  }
428  return $let;
429  }
430 
431  public function exportXML(
432  ilXmlWriter $a_xml_writer,
433  int $a_inst
434  ): void {
435  $attrs = array();
436  $attrs["Language"] = $this->getLanguage();
437  $attrs["Id"] = "il_" . IL_INST_ID . "_git_" . $this->getId();
438  $a_xml_writer->xmlStartTag("GlossaryItem", $attrs);
439 
440  $attrs = array();
441  $a_xml_writer->xmlElement("GlossaryTerm", $attrs, $this->getTerm());
442 
444 
445  foreach ($defs as $def) {
446  $definition = new ilGlossaryDefinition($def["id"]);
447  $definition->exportXML($a_xml_writer, $a_inst);
448  }
449 
450  $a_xml_writer->xmlEndTag("GlossaryItem");
451  }
452 
453  public static function getNumberOfUsages(int $a_term_id): int
454  {
455  return count(self::getUsages($a_term_id));
456  }
457 
458  public static function getUsages(int $a_term_id): array
459  {
460  $usages = (ilInternalLink::_getSourcesOfTarget("git", $a_term_id, 0));
461 
463  $usages["glo:termref:" . $glo_id . ":-"] = array(
464  "type" => "glo:termref",
465  "id" => $glo_id,
466  "lang" => "-"
467  );
468  }
469 
470  return $usages;
471  }
472 
477  public static function _copyTerm(
478  int $a_term_id,
479  int $a_glossary_id
480  ): int {
481  $old_term = new ilGlossaryTerm($a_term_id);
482 
483  // copy the term
484  $new_term = new ilGlossaryTerm();
485  $new_term->setTerm($old_term->getTerm());
486  $new_term->setLanguage($old_term->getLanguage());
487  $new_term->setGlossaryId($a_glossary_id);
488  $new_term->create();
489 
490  // copy the definitions
491  $def_list = ilGlossaryDefinition::getDefinitionList($a_term_id);
492  foreach ($def_list as $def) {
493  $old_def = new ilGlossaryDefinition($def["id"]);
494 
495  $new_def = new ilGlossaryDefinition();
496  $new_def->setShortText($old_def->getShortText());
497  $new_def->setNr($old_def->getNr());
498  $new_def->setTermId($new_term->getId());
499  $new_def->create();
500 
501  // copy meta data
502  $md = new ilMD(
503  $old_term->getGlossaryId(),
504  $old_def->getPageObject()->getId(),
505  $old_def->getPageObject()->getParentType()
506  );
507  $new_md = $md->cloneMD(
508  $a_glossary_id,
509  $new_def->getPageObject()->getId(),
510  $old_def->getPageObject()->getParentType()
511  );
512 
513 
514  $new_page = $new_def->getPageObject();
515  $old_def->getPageObject()->copy($new_page->getId(), $new_page->getParentType(), $new_page->getParentId(), true);
516 
517  // page content
518  //$new_def->getPageObject()->setXMLContent($old_def->getPageObject()->copyXmlContent(true));
519  //$new_def->getPageObject()->buildDom();
520  //$new_def->getPageObject()->update();
521  }
522 
523  // adv metadata
524  $old_recs = ilAdvancedMDRecord::_getSelectedRecordsByObject("glo", $old_term->getGlossaryId(), "term");
525  $new_recs = ilAdvancedMDRecord::_getSelectedRecordsByObject("glo", $a_glossary_id, "term");
526  foreach ($old_recs as $old_record_obj) {
527  reset($new_recs);
528  foreach ($new_recs as $new_record_obj) {
529  if ($old_record_obj->getRecordId() == $new_record_obj->getRecordId()) {
530  foreach (ilAdvancedMDFieldDefinition::getInstancesByRecordId($old_record_obj->getRecordId()) as $def) {
531  // now we need to copy $def->getFieldId() values from old term to new term
532  // how?
533  // clone values
534 
535  $source_primary = array("obj_id" => array("integer", $old_term->getGlossaryId()));
536  $source_primary["sub_type"] = array("text", "term");
537  $source_primary["sub_id"] = array("integer", $old_term->getId());
538  $source_primary["field_id"] = array("integer", $def->getFieldId());
539  $target_primary = array("obj_id" => array("integer", $new_term->getGlossaryId()));
540  $target_primary["sub_type"] = array("text", "term");
541  $target_primary["sub_id"] = array("integer", $new_term->getId());
542 
543  ilADTFactory::getInstance()->initActiveRecordByType();
545  "adv_md_values",
546  array(
547  "obj_id" => "integer",
548  "sub_type" => "text",
549  "sub_id" => "integer",
550  "field_id" => "integer"
551  ),
552  $source_primary,
553  $target_primary,
554  array("disabled" => "integer")
555  );
556  }
557  }
558  }
559  }
560 
561  return $new_term->getId();
562  }
563 
567  public static function getTermsOfGlossary(
568  int $a_glo_id
569  ): array {
570  global $DIC;
571 
572  $ilDB = $DIC->database();
573 
574  $set = $ilDB->query(
575  "SELECT id FROM glossary_term WHERE " .
576  " glo_id = " . $ilDB->quote($a_glo_id, "integer")
577  );
578  $ids = array();
579  while ($rec = $ilDB->fetchAssoc($set)) {
580  $ids[] = (int) $rec["id"];
581  }
582  return $ids;
583  }
584 }
static deleteReferencesOfTerm(int $a_term_id)
Delete all references of a term.
const IL_INST_ID
Definition: constants.php:40
static getUsages(int $a_term_id)
static getSubTreeItems(string $a_comp, int $a_obj_id, string $a_item_type, int $a_tax_id, $a_node)
Get all assigned items under a node.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _exists(int $a_id)
checks whether a glossary term with specified id exists or not
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getIdForImportId(string $a_import_id)
static _getAllReferences(int $id)
get all reference ids for object ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _hasUntrashedReference(int $obj_id)
checks whether an object has at least one reference that is not in trash
static getUsageOfObject(int $a_obj_id, bool $a_include_titles=false)
static _getSelectedRecordsByObject(string $a_obj_type, int $a_id, string $a_sub_type="", bool $is_ref_id=true)
static getTermList(array $a_glo_ref_id, string $searchterm="", string $a_first_letter="", string $a_def="", int $a_tax_node=0, bool $a_add_amet_fields=false, array $a_amet_filter=null, bool $a_include_references=false)
Get all terms for given set of glossary ids.
static getInstancesByRecordId( $a_record_id, $a_only_searchable=false, string $language='')
Get definitions by record id.
setGlossary(ilObjGlossary $a_glossary)
static _copyTerm(int $a_term_id, int $a_glossary_id)
Copy a term to a glossary.
xmlEndTag(string $tag)
Writes an endtag.
exportXML(ilXmlWriter $a_xml_writer, int $a_inst)
global $DIC
Definition: feed.php:28
setTerm(string $a_term)
static lookupReferencesOfTerm(int $a_term_id)
static _lookGlossaryTerm(int $term_id)
get glossary term
static _lookupObjectId(int $ref_id)
static getTermsOfGlossary(int $a_glo_id)
ilGlobalTemplateInterface $tpl
$query
setId(int $a_id)
set glossary term id (= glossary item id)
static _lookLanguage(int $term_id)
lookup term language
static getDefinitionList(int $a_term_id)
setLanguage(string $a_language)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static cloneByPrimary(string $a_table, array $a_primary_def, array $a_source_primary, array $a_target_primary, array $a_additional=null)
Clone values by (partial) primary key.
static queryForRecords(int $adv_rec_obj_ref_id, string $adv_rec_obj_type, string $adv_rec_obj_subtype, array $a_obj_id, string $a_subtype, array $a_records, string $a_obj_id_key, string $a_obj_subid_key, array $a_amet_filter=null)
ilObjGlossary $glossary
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
__construct(int $a_id=0)
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
static getNumberOfUsages(int $a_term_id)
setGlossaryId(int $a_glo_id)
static getFirstLetters(array $a_glo_id, int $a_tax_node=0)
setImportId(string $a_import_id)
cloneMD(int $a_rbac_id, int $a_obj_id, string $a_obj_type)
Definition: class.ilMD.php:288
$i
Definition: metadata.php:41
static _lookGlossaryID(int $term_id)
get glossary id form term id