ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilGlossaryTerm.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 
11 {
15  protected $db;
16 
17  public $lng;
18  public $tpl;
19 
20  public $id;
21  public $glossary;
22  public $term;
23  public $language;
24  public $glo_id;
25  public $import_id;
26 
31  public function __construct($a_id = 0)
32  {
33  global $DIC;
34 
35  $this->db = $DIC->database();
36  $lng = $DIC->language();
37  $tpl = $DIC["tpl"];
38 
39  $this->lng = $lng;
40  $this->tpl = $tpl;
41 
42  $this->id = $a_id;
43  $this->type = "term";
44  if ($a_id != 0) {
45  $this->read();
46  }
47  }
48 
52  public function read()
53  {
54  $ilDB = $this->db;
55 
56  $q = "SELECT * FROM glossary_term WHERE id = " .
57  $ilDB->quote($this->id, "integer");
58  $term_set = $ilDB->query($q);
59  $term_rec = $ilDB->fetchAssoc($term_set);
60 
61  $this->setTerm($term_rec["term"]);
62  $this->setImportId($term_rec["import_id"]);
63  $this->setLanguage($term_rec["language"]);
64  $this->setGlossaryId($term_rec["glo_id"]);
65  }
66 
74  public static function _getIdForImportId($a_import_id)
75  {
76  global $DIC;
77 
78  $ilDB = $DIC->database();
79 
80  if ($a_import_id == "") {
81  return 0;
82  }
83 
84  $q = "SELECT * FROM glossary_term WHERE import_id = " .
85  $ilDB->quote($a_import_id, "text") .
86  " ORDER BY create_date DESC";
87  $term_set = $ilDB->query($q);
88  while ($term_rec = $ilDB->fetchAssoc($term_set)) {
89  $glo_id = ilGlossaryTerm::_lookGlossaryID($term_rec["id"]);
90 
91  $ref_ids = ilObject::_getAllReferences($glo_id); // will be 0 if import of lm is in progress (new import)
92  if (count($ref_ids) == 0 || ilObject::_hasUntrashedReference($glo_id)) {
93  return $term_rec["id"];
94  }
95  }
96 
97  return 0;
98  }
99 
100 
108  public static function _exists($a_id)
109  {
110  global $DIC;
111 
112  $ilDB = $DIC->database();
113 
114  if (is_int(strpos($a_id, "_"))) {
116  }
117 
118  $q = "SELECT * FROM glossary_term WHERE id = " .
119  $ilDB->quote($a_id, "integer");
120  $obj_set = $ilDB->query($q);
121  if ($obj_rec = $ilDB->fetchAssoc($obj_set)) {
122  return true;
123  } else {
124  return false;
125  }
126  }
127 
128 
134  public function setId($a_id)
135  {
136  $this->id = $a_id;
137  }
138 
139 
145  public function getId()
146  {
147  return $this->id;
148  }
149 
155  public function setGlossary(&$a_glossary)
156  {
157  $this->glossary = $a_glossary;
158  $this->setGlossaryId($a_glossary->getId());
159  }
160 
161 
167  public function setGlossaryId($a_glo_id)
168  {
169  $this->glo_id = $a_glo_id;
170  }
171 
172 
178  public function getGlossaryId()
179  {
180  return $this->glo_id;
181  }
182 
183 
189  public function setTerm($a_term)
190  {
191  $this->term = $a_term;
192  }
193 
194 
200  public function getTerm()
201  {
202  return $this->term;
203  }
204 
205 
211  public function setLanguage($a_language)
212  {
213  $this->language = $a_language;
214  }
215 
220  public function getLanguage()
221  {
222  return $this->language;
223  }
224 
225 
229  public function setImportId($a_import_id)
230  {
231  $this->import_id = $a_import_id;
232  }
233 
234 
238  public function getImportId()
239  {
240  return $this->import_id;
241  }
242 
243 
247  public function create()
248  {
249  $ilDB = $this->db;
250 
251  $this->setId($ilDB->nextId("glossary_term"));
252  $ilDB->manipulate("INSERT INTO glossary_term (id, glo_id, term, language, import_id, create_date, last_update)" .
253  " VALUES (" .
254  $ilDB->quote($this->getId(), "integer") . ", " .
255  $ilDB->quote($this->getGlossaryId(), "integer") . ", " .
256  $ilDB->quote($this->term, "text") . ", " .
257  $ilDB->quote($this->language, "text") . "," .
258  $ilDB->quote($this->getImportId(), "text") . "," .
259  $ilDB->now() . ", " .
260  $ilDB->now() . ")");
261  }
262 
263 
267  public function delete()
268  {
269  $ilDB = $this->db;
270 
272  foreach ($defs as $def) {
273  $def_obj = new ilGlossaryDefinition($def["id"]);
274  $def_obj->delete();
275  }
276 
277  // delete term references
279 
280  // delete glossary_term record
281  $ilDB->manipulate("DELETE FROM glossary_term " .
282  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
283  }
284 
285 
289  public function update()
290  {
291  $ilDB = $this->db;
292 
293  $ilDB->manipulate("UPDATE glossary_term SET " .
294  " glo_id = " . $ilDB->quote($this->getGlossaryId(), "integer") . ", " .
295  " term = " . $ilDB->quote($this->getTerm(), "text") . ", " .
296  " import_id = " . $ilDB->quote($this->getImportId(), "text") . ", " .
297  " language = " . $ilDB->quote($this->getLanguage(), "text") . ", " .
298  " last_update = " . $ilDB->now() . " " .
299  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
300  }
301 
305  public static function _lookGlossaryID($term_id)
306  {
307  global $DIC;
308 
309  $ilDB = $DIC->database();
310 
311  $query = "SELECT * FROM glossary_term WHERE id = " .
312  $ilDB->quote($term_id, "integer");
313  $obj_set = $ilDB->query($query);
314  $obj_rec = $ilDB->fetchAssoc($obj_set);
315 
316  return $obj_rec["glo_id"];
317  }
318 
322  public static function _lookGlossaryTerm($term_id)
323  {
324  global $DIC;
325 
326  $ilDB = $DIC->database();
327 
328  $query = "SELECT * FROM glossary_term WHERE id = " .
329  $ilDB->quote($term_id, "integer");
330  $obj_set = $ilDB->query($query);
331  $obj_rec = $ilDB->fetchAssoc($obj_set);
332 
333  return $obj_rec["term"];
334  }
335 
339  public static function _lookLanguage($term_id)
340  {
341  global $DIC;
342 
343  $ilDB = $DIC->database();
344 
345  $query = "SELECT * FROM glossary_term WHERE id = " .
346  $ilDB->quote($term_id, "integer");
347  $obj_set = $ilDB->query($query);
348  $obj_rec = $ilDB->fetchAssoc($obj_set);
349 
350  return $obj_rec["language"];
351  }
352 
361  public static function getTermList(
362  $a_glo_ref_id,
363  $searchterm = "",
364  $a_first_letter = "",
365  $a_def = "",
366  $a_tax_node = 0,
367  $a_add_amet_fields = false,
368  array $a_amet_filter = null,
369  $a_include_references = false
370  ) {
371  global $DIC;
372 
373  if (is_array($a_glo_ref_id)) {
374  $a_glo_id = array_map(function ($id) {
376  }, $a_glo_ref_id);
377  } else {
378  $a_glo_id = ilObject::_lookupObjectId($a_glo_ref_id);
379  }
380  $ilDB = $DIC->database();
381 
382  $join = $in = "";
383 
384  $terms = array();
385 
386  // get all term ids under taxonomy node (if given)
387  if ($a_tax_node > 1) {
388  $tax_ids = ilObjTaxonomy::getUsageOfObject($a_glo_id);
389  if (count($tax_ids) > 0) {
390  $items = ilObjTaxonomy::getSubTreeItems("glo", $a_glo_id, "term", $tax_ids[0], $a_tax_node);
391  $sub_tree_ids = array();
392  foreach ($items as $i) {
393  $sub_tree_ids[] = $i["item_id"];
394  }
395  $in = " AND " . $ilDB->in("gt.id", $sub_tree_ids, false, "integer");
396  }
397  }
398 
399  if ($a_def != "") {
400  // meta glossary?
401  if (is_array($a_glo_id)) {
402  $glo_where = $ilDB->in("page_object.parent_id", $a_glo_id, false, "integer");
403  } else {
404  $glo_where = " page_object.parent_id = " . $ilDB->quote($a_glo_id, "integer");
405  }
406 
407  $join = " JOIN glossary_definition gd ON (gd.term_id = gt.id)" .
408  " JOIN page_object ON (" .
409  $glo_where .
410  " AND page_object.parent_type = " . $ilDB->quote("gdf", "text") .
411  " AND page_object.page_id = gd.id" .
412  " AND " . $ilDB->like("page_object.content", "text", "%" . $a_def . "%") .
413  ")";
414  }
415 
416  $searchterm = (!empty($searchterm))
417  ? " AND " . $ilDB->like("term", "text", "%" . $searchterm . "%") . " "
418  : "";
419 
420  if ($a_first_letter != "") {
421  $searchterm .= " AND " . $ilDB->upper($ilDB->substr("term", 1, 1)) . " = " . $ilDB->upper($ilDB->quote($a_first_letter, "text")) . " ";
422  }
423 
424  // include references
425  $where_glo_id_or = "";
426  if ($a_include_references) {
427  $join .= " LEFT JOIN glo_term_reference tr ON (gt.id = tr.term_id) ";
428  if (is_array($a_glo_id)) {
429  $where_glo_id_or = " OR " . $ilDB->in("tr.glo_id", $a_glo_id, false, "integer");
430  } else {
431  $where_glo_id_or = " OR tr.glo_id = " . $ilDB->quote($a_glo_id, "integer");
432  }
433  }
434 
435  // meta glossary
436  if (is_array($a_glo_id)) {
437  $where = "(" . $ilDB->in("gt.glo_id", $a_glo_id, false, "integer") . $where_glo_id_or . ")";
438  } else {
439  $where = "(gt.glo_id = " . $ilDB->quote($a_glo_id, "integer") . $where_glo_id_or . ")";
440  }
441 
442  $where .= $in;
443 
444 
445  $q = "SELECT DISTINCT(gt.term), gt.id, gt.glo_id, gt.language FROM glossary_term gt " . $join . " WHERE " . $where . $searchterm . " ORDER BY term";
446 
447  //echo $q; exit;
448 
449  $term_set = $ilDB->query($q);
450  $glo_ids = array();
451  while ($term_rec = $ilDB->fetchAssoc($term_set)) {
452  $terms[] = array("term" => $term_rec["term"],
453  "language" => $term_rec["language"], "id" => $term_rec["id"], "glo_id" => $term_rec["glo_id"]);
454  $glo_ids[] = $term_rec["glo_id"];
455  }
456 
457  // add advanced metadata
458  if (($a_add_amet_fields || is_array($a_amet_filter)) && !is_array($a_glo_ref_id)) {
459  $terms = ilAdvancedMDValues::queryForRecords($a_glo_ref_id, "glo", "term", $glo_ids, "term", $terms, "glo_id", "id", $a_amet_filter);
460  }
461  return $terms;
462  }
463 
472  public static function getFirstLetters($a_glo_id, $a_tax_node = 0)
473  {
474  global $DIC;
475 
476  $ilDB = $DIC->database();
477 
478  $terms = array();
479 
480  // meta glossary
481  if (is_array($a_glo_id)) {
482  $where = $ilDB->in("glo_id", $a_glo_id, false, "integer");
483  } else {
484  $where = " glo_id = " . $ilDB->quote($a_glo_id, "integer") . " ";
485 
486  // get all term ids under taxonomy node (if given)
487  if ($a_tax_node > 1) {
488  $tax_ids = ilObjTaxonomy::getUsageOfObject($a_glo_id);
489  if (count($tax_ids) > 0) {
490  $items = ilObjTaxonomy::getSubTreeItems("glo", $a_glo_id, "term", $tax_ids[0], $a_tax_node);
491  $sub_tree_ids = array();
492  foreach ($items as $i) {
493  $sub_tree_ids[] = $i["item_id"];
494  }
495  $in = " AND " . $ilDB->in("id", $sub_tree_ids, false, "integer");
496  }
497  }
498 
499  $where .= $in;
500  }
501 
502  $q = "SELECT DISTINCT " . $ilDB->upper($ilDB->substr("term", 1, 1)) . " let FROM glossary_term WHERE " . $where . " ORDER BY let";
503  $let_set = $ilDB->query($q);
504 
505  $lets = array();
506  while ($let_rec = $ilDB->fetchAssoc($let_set)) {
507  $let[$let_rec["let"]] = $let_rec["let"];
508  }
509  return $let;
510  }
511 
515  public function exportXML(&$a_xml_writer, $a_inst)
516  {
517  $attrs = array();
518  $attrs["Language"] = $this->getLanguage();
519  $attrs["Id"] = "il_" . IL_INST_ID . "_git_" . $this->getId();
520  $a_xml_writer->xmlStartTag("GlossaryItem", $attrs);
521 
522  $attrs = array();
523  $a_xml_writer->xmlElement("GlossaryTerm", $attrs, $this->getTerm());
524 
526 
527  foreach ($defs as $def) {
528  $definition = new ilGlossaryDefinition($def["id"]);
529  $definition->exportXML($a_xml_writer, $a_inst);
530  }
531 
532  $a_xml_writer->xmlEndTag("GlossaryItem");
533  }
534 
541  public static function getNumberOfUsages($a_term_id)
542  {
543  return count(ilGlossaryTerm::getUsages($a_term_id));
544  }
545 
552  public static function getUsages($a_term_id)
553  {
554  $usages = (ilInternalLink::_getSourcesOfTarget("git", $a_term_id, 0));
555 
557  $usages["glo:termref:" . $glo_id . ":-"] = array(
558  "type" => "glo:termref",
559  "id" => $glo_id,
560  "lang" => "-"
561  );
562  }
563 
564  return $usages;
565  }
566 
573  public static function _copyTerm($a_term_id, $a_glossary_id)
574  {
575  $old_term = new ilGlossaryTerm($a_term_id);
576 
577  // copy the term
578  $new_term = new ilGlossaryTerm();
579  $new_term->setTerm($old_term->getTerm());
580  $new_term->setLanguage($old_term->getLanguage());
581  $new_term->setGlossaryId($a_glossary_id);
582  $new_term->create();
583 
584  // copy the definitions
585  $def_list = ilGlossaryDefinition::getDefinitionList($a_term_id);
586  foreach ($def_list as $def) {
587  $old_def = new ilGlossaryDefinition($def["id"]);
588 
589  $new_def = new ilGlossaryDefinition();
590  $new_def->setShortText($old_def->getShortText());
591  $new_def->setNr($old_def->getNr());
592  $new_def->setTermId($new_term->getId());
593  $new_def->create();
594 
595  // copy meta data
596  $md = new ilMD(
597  $old_term->getGlossaryId(),
598  $old_def->getPageObject()->getId(),
599  $old_def->getPageObject()->getParentType()
600  );
601  $new_md = $md->cloneMD(
602  $a_glossary_id,
603  $new_def->getPageObject()->getId(),
604  $old_def->getPageObject()->getParentType()
605  );
606 
607 
608  $new_page = $new_def->getPageObject();
609  $old_def->getPageObject()->copy($new_page->getId(), $new_page->getParentType(), $new_page->getParentId(), true);
610 
611  // page content
612  //$new_def->getPageObject()->setXMLContent($old_def->getPageObject()->copyXmlContent(true));
613  //$new_def->getPageObject()->buildDom();
614  //$new_def->getPageObject()->update();
615  }
616 
617  // adv metadata
618  $old_recs = ilAdvancedMDRecord::_getSelectedRecordsByObject("glo", $old_term->getGlossaryId(), "term");
619  $new_recs = ilAdvancedMDRecord::_getSelectedRecordsByObject("glo", $a_glossary_id, "term");
620  foreach ($old_recs as $old_record_obj) {
621  reset($new_recs);
622  foreach ($new_recs as $new_record_obj) {
623  if ($old_record_obj->getRecordId() == $new_record_obj->getRecordId()) {
624  foreach (ilAdvancedMDFieldDefinition::getInstancesByRecordId($old_record_obj->getRecordId()) as $def) {
625  // now we need to copy $def->getFieldId() values from old term to new term
626  // how?
627  // clone values
628 
629  $source_primary = array("obj_id" => array("integer", $old_term->getGlossaryId()));
630  $source_primary["sub_type"] = array("text", "term");
631  $source_primary["sub_id"] = array("integer", $old_term->getId());
632  $source_primary["field_id"] = array("integer", $def->getFieldId());
633  $target_primary = array("obj_id" => array("integer", $new_term->getGlossaryId()));
634  $target_primary["sub_type"] = array("text", "term");
635  $target_primary["sub_id"] = array("integer", $new_term->getId());
636 
637  ilADTFactory::getInstance()->initActiveRecordByType();
639  "adv_md_values",
640  array(
641  "obj_id" => "integer",
642  "sub_type" => "text",
643  "sub_id" => "integer",
644  "field_id" => "integer"
645  ),
646  $source_primary,
647  $target_primary,
648  array("disabled" => "integer")
649  );
650  }
651  }
652  }
653  }
654 
655 
656  return $new_term->getId();
657  }
658 
665  public static function getTermsOfGlossary($a_glo_id)
666  {
667  global $DIC;
668 
669  $ilDB = $DIC->database();
670 
671  $set = $ilDB->query(
672  "SELECT id FROM glossary_term WHERE " .
673  " glo_id = " . $ilDB->quote($a_glo_id, "integer")
674  );
675  $ids = array();
676  while ($rec = $ilDB->fetchAssoc($set)) {
677  $ids[] = $rec["id"];
678  }
679  return $ids;
680  }
681 }
& cloneMD($a_rbac_id, $a_obj_id, $a_obj_type)
Definition: class.ilMD.php:326
static getDefinitionList($a_term_id)
static
static _hasUntrashedReference($a_obj_id)
checks wether an object has at least one reference that is not in trash
getId()
get term id (= glossary item id)
const IL_INST_ID
Definition: constants.php:38
static getUsageOfObject($a_obj_id, $a_include_titles=false)
Get usage of object.
static getNumberOfUsages($a_term_id)
Get number of usages.
Glossary terms.
setGlossary(&$a_glossary)
set glossary object
getImportId()
get import id
create()
create new glossary term
static _getIdForImportId($a_import_id)
get current term id for import id (static)
__construct($a_id=0)
Constructor public.
setLanguage($a_language)
set language
static getInstance()
Get singleton.
getLanguage()
get language
static cloneByPrimary($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 _getAllReferences($a_id)
get all reference ids of object
static lookupReferencesOfTerm($a_term_id)
Lookup references of a term.
static _getSelectedRecordsByObject($a_obj_type, $a_ref_id, $a_sub_type="")
Get selected records by object.
static _lookupObjectId($a_ref_id)
lookup object id
static _exists($a_id)
checks wether a glossary term with specified id exists or not
getGlossaryId()
get glossary id
static getUsages($a_term_id)
Get number of usages.
setGlossaryId($a_glo_id)
set glossary id
static getFirstLetters($a_glo_id, $a_tax_node=0)
Get all terms for given set of glossary ids.
setTerm($a_term)
set term
global $DIC
Definition: goto.php:24
static getTermList( $a_glo_ref_id, $searchterm="", $a_first_letter="", $a_def="", $a_tax_node=0, $a_add_amet_fields=false, array $a_amet_filter=null, $a_include_references=false)
Get all terms for given set of glossary ids.
$query
static _lookLanguage($term_id)
lookup term language
setImportId($a_import_id)
set import id
setId($a_id)
set glossary term id (= glossary item id)
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
static _lookGlossaryID($term_id)
get glossary id form term id
Class ilGlossaryDefinition.
update()
update glossary term
static _copyTerm($a_term_id, $a_glossary_id)
Copy a term to a glossary.
static deleteReferencesOfTerm($a_term_id)
Delete all references of a term.
global $ilDB
exportXML(&$a_xml_writer, $a_inst)
export xml
static getTermsOfGlossary($a_glo_id)
Get terms of glossary.
language()
Definition: language.php:2
static getSubTreeItems($a_comp, $a_obj_id, $a_item_type, $a_tax_id, $a_node)
Get all assigned items under a node.
static getInstancesByRecordId($a_record_id, $a_only_searchable=false, string $language='')
Get definitions by record id.
static _lookGlossaryTerm($term_id)
get glossary term
read()
read glossary term data
static queryForRecords($adv_rec_obj_ref_id, $adv_rec_obj_type, $adv_rec_obj_subtype, $a_obj_id, $a_subtype, $a_records, $a_obj_id_key, $a_obj_subid_key, array $a_amet_filter=null)
Query data for given object records.
$i
Definition: metadata.php:24