ILIAS  release_8 Revision v8.23
class.ilGlossaryTermReferences.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected int $glo_id;
27  protected array $terms = array();
28  protected ilDBInterface $db;
29 
30  public function __construct(int $a_glo_id = 0)
31  {
32  global $DIC;
33 
34  $this->db = $DIC->database();
35 
36  $this->setGlossaryId($a_glo_id);
37  if ($a_glo_id > 0) {
38  $this->read();
39  }
40  }
41 
42  public function setGlossaryId(int $a_val): void
43  {
44  $this->glo_id = $a_val;
45  }
46 
47  public function getGlossaryId(): int
48  {
49  return $this->glo_id;
50  }
51 
55  public function setTerms(array $a_val): void
56  {
57  $this->terms = $a_val;
58  }
59 
63  public function getTerms(): array
64  {
65  return $this->terms;
66  }
67 
68  public function addTerm(int $a_term_id): void
69  {
70  if (!in_array($a_term_id, $this->terms)) {
71  $this->terms[] = $a_term_id;
72  }
73  }
74 
75  public function deleteTerm(int $a_term_id): void
76  {
77  foreach ($this->terms as $k => $v) {
78  if ($v == $a_term_id) {
79  unset($this->terms[$k]);
80  }
81  }
82  }
83 
84  public function read(): void
85  {
86  $set = $this->db->query("SELECT term_id FROM glo_term_reference " .
87  " WHERE glo_id = " . $this->db->quote($this->getGlossaryId(), "integer"));
88  while ($rec = $this->db->fetchAssoc($set)) {
89  $this->addTerm($rec["term_id"]);
90  }
91  }
92 
93  public function update(): void
94  {
95  $this->delete();
96  foreach ($this->getTerms() as $t) {
97  $this->db->replace(
98  "glo_term_reference",
99  array(
100  "glo_id" => array("integer", $this->getGlossaryId()),
101  "term_id" => array("integer", $t),
102  ),
103  array()
104  );
105  }
106  }
107 
111  public function delete(): void
112  {
113  $this->db->manipulate(
114  "DELETE FROM glo_term_reference WHERE " .
115  " glo_id = " . $this->db->quote($this->getGlossaryId(), "integer")
116  );
117  }
118 
122  public static function deleteReferencesOfTerm(int $a_term_id): void
123  {
124  global $DIC;
125 
126  $db = $DIC->database();
127  $db->manipulate(
128  "DELETE FROM glo_term_reference WHERE " .
129  " term_id = " . $db->quote($a_term_id, "integer")
130  );
131  }
132 
133 
137  public static function hasReferences(int $a_glossary_id): bool
138  {
139  global $DIC;
140 
141  $db = $DIC->database();
142  $set = $db->query(
143  "SELECT * FROM glo_term_reference " .
144  " WHERE glo_id = " . $db->quote($a_glossary_id, "integer")
145  );
146  if ($rec = $db->fetchAssoc($set)) {
147  return true;
148  }
149  return false;
150  }
151 
158  public static function isReferenced(
159  array $a_glo_id,
160  int $a_term_id
161  ): bool {
162  global $DIC;
163 
164  $db = $DIC->database();
165  $set = $db->query(
166  "SELECT * FROM glo_term_reference " .
167  " WHERE " . $db->in("glo_id", $a_glo_id, false, "integer") .
168  " AND term_id = " . $db->quote($a_term_id, "integer")
169  );
170  if ($db->fetchAssoc($set)) {
171  return true;
172  }
173  return false;
174  }
175 
180  public static function lookupReferencesOfTerm(
181  int $a_term_id
182  ): array {
183  global $DIC;
184 
185  $db = $DIC->database();
186  $set = $db->query(
187  $q = "SELECT DISTINCT glo_id FROM glo_term_reference " .
188  " WHERE term_id = " . $db->quote($a_term_id, "integer")
189  );
190  $glos = array();
191  while ($rec = $db->fetchAssoc($set)) {
192  $glos[] = $rec["glo_id"];
193  }
194  return $glos;
195  }
196 }
static deleteReferencesOfTerm(int $a_term_id)
Delete all references of a term.
fetchAssoc(ilDBStatement $statement)
quote($value, string $type)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static hasReferences(int $a_glossary_id)
Check if a glossary uses references.
static isReferenced(array $a_glo_id, int $a_term_id)
Is a term referenced by a set of glossaries.
global $DIC
Definition: feed.php:28
static lookupReferencesOfTerm(int $a_term_id)
query(string $query)
Run a (read-only) Query on the database.
in(string $field, array $values, bool $negate=false, string $type="")
manipulate(string $query)
Run a (write) Query on the database.