ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDClassification.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
31  private const PURPOSE_TRANSLATION = [
32  'discipline' => 'Discipline',
33  'idea' => 'Idea',
34  'prerequisite' => 'Prerequisite',
35  'educational objective' => 'EducationalObjective',
36  'accessibility restrictions' => 'AccessibilityRestrictions',
37  'educational level' => 'EducationalLevel',
38  'skill level' => 'SkillLevel',
39  'security level' => 'SecurityLevel',
40  'competency' => 'Competency'
41  ];
42 
43  private string $purpose = '';
44  private string $description = '';
46 
47  // METHODS OF CLIENT OBJECTS (TaxonPath, Keyword)
48 
52  public function getTaxonPathIds(): array
53  {
54  return ilMDTaxonPath::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_classification');
55  }
56 
57  public function getTaxonPath(int $a_taxon_path_id): ?ilMDTaxonPath
58  {
59  if (!$a_taxon_path_id) {
60  return null;
61  }
62  $tax = new ilMDTaxonPath();
63  $tax->setMetaId($a_taxon_path_id);
64 
65  return $tax;
66  }
67 
68  public function addTaxonPath(): ilMDTaxonPath
69  {
70  $tax = new ilMDTaxonPath($this->getRBACId(), $this->getObjId(), $this->getObjType());
71  $tax->setParentId($this->getMetaId());
72  $tax->setParentType('meta_classification');
73 
74  return $tax;
75  }
76 
77  public function getKeywordIds(): ?array
78  {
79  return ilMDKeyword::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_classification');
80  }
81 
82  public function getKeyword(int $a_keyword_id): ?ilMDKeyword
83  {
84  if (!$a_keyword_id) {
85  return null;
86  }
87  $key = new ilMDKeyword();
88  $key->setMetaId($a_keyword_id);
89 
90  return $key;
91  }
92 
93  public function addKeyword(): ilMDKeyword
94  {
95  $key = new ilMDKeyword($this->getRBACId(), $this->getObjId(), $this->getObjType());
96  $key->setParentId($this->getMetaId());
97  $key->setParentType('meta_classification');
98 
99  return $key;
100  }
101 
102  // SET/GET
103  public function setPurpose(string $a_purpose): bool
104  {
105  switch ($a_purpose) {
106  case 'Discipline':
107  case 'Idea':
108  case 'Prerequisite':
109  case 'EducationalObjective':
110  case 'AccessibilityRestrictions':
111  case 'EducationalLevel':
112  case 'SkillLevel':
113  case 'SecurityLevel':
114  case 'Competency':
115  $this->purpose = $a_purpose;
116  return true;
117 
118  default:
119  return false;
120  }
121  }
122 
123  public function getPurpose(): string
124  {
125  return $this->purpose;
126  }
127 
128  public function setDescription(string $a_description): void
129  {
130  $this->description = $a_description;
131  }
132 
133  public function getDescription(): string
134  {
135  return $this->description;
136  }
137 
138  public function setDescriptionLanguage(ilMDLanguageItem $lng_obj): void
139  {
140  $this->description_language = $lng_obj;
141  }
142 
144  {
145  return is_object($this->description_language) ? $this->description_language : null;
146  }
147 
148  public function getDescriptionLanguageCode(): string
149  {
150  return is_object($this->description_language) ? $this->description_language->getLanguageCode() : '';
151  }
152 
153  public function save(): int
154  {
155  $fields = $this->__getFields();
156  $fields['meta_classification_id'] = array('integer', $next_id = $this->db->nextId('il_meta_classification'));
157 
158  if ($this->db->insert('il_meta_classification', $fields)) {
159  $this->setMetaId($next_id);
160  return $this->getMetaId();
161  }
162  return 0;
163  }
164 
165  public function update(): bool
166  {
167  return $this->getMetaId() && $this->db->update(
168  'il_meta_classification',
169  $this->__getFields(),
170  ["meta_classification_id" => ['integer', $this->getMetaId()]]
171  );
172  }
173 
174  public function delete(): bool
175  {
176  if ($this->getMetaId()) {
177  $query = "DELETE FROM il_meta_classification " .
178  "WHERE meta_classification_id = " . $this->db->quote($this->getMetaId(), 'integer');
179  $res = $this->db->manipulate($query);
180 
181  foreach ($this->getTaxonPathIds() as $id) {
182  $tax = $this->getTaxonPath($id);
183  $tax->delete();
184  }
185  foreach ($this->getKeywordIds() as $id) {
186  $key = $this->getKeyword($id);
187  $key->delete();
188  }
189 
190  return true;
191  }
192  return false;
193  }
194 
198  public function __getFields(): array
199  {
203  $purpose = (string) array_search(
204  $this->getPurpose(),
205  self::PURPOSE_TRANSLATION
206  );
207 
208  return array(
209  'rbac_id' => array('integer', $this->getRBACId()),
210  'obj_id' => array('integer', $this->getObjId()),
211  'obj_type' => array('text', $this->getObjType()),
212  'purpose' => array('text', $purpose),
213  'description' => array('text', $this->getDescription()),
214  'description_language' => array('text', $this->getDescriptionLanguageCode())
215  );
216  }
217 
218  public function read(): bool
219  {
220  if ($this->getMetaId()) {
221  $query = "SELECT * FROM il_meta_classification " .
222  "WHERE meta_classification_id = " . $this->db->quote($this->getMetaId(), 'integer');
223 
224  $res = $this->db->query($query);
225  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
229  if (key_exists($row->purpose ?? '', self::PURPOSE_TRANSLATION)) {
230  $row->purpose = self::PURPOSE_TRANSLATION[$row->purpose ?? ''];
231  }
232 
233  $this->setRBACId((int) $row->rbac_id);
234  $this->setObjId((int) $row->obj_id);
235  $this->setObjType($row->obj_type);
236  $this->setPurpose($row->purpose ?? '');
237  $this->setDescription($row->description ?? '');
238  $this->description_language = new ilMDLanguageItem($row->description_language ?? '');
239  }
240  }
241  return true;
242  }
243 
244  public function toXML(ilXmlWriter $writer): void
245  {
246  $writer->xmlStartTag('Classification', array(
247  'Purpose' => $this->getPurpose() ?: 'Idea'
248  ));
249 
250  // Taxon Path
251  $taxs = $this->getTaxonPathIds();
252  foreach ($taxs as $id) {
253  $tax = $this->getTaxonPath($id);
254  $tax->toXML($writer);
255  }
256  if (!count($taxs)) {
257  $tax = new ilMDTaxonPath($this->getRBACId(), $this->getObjId());
258  $tax->toXML($writer);
259  }
260 
261  // Description
262  $writer->xmlElement(
263  'Description',
264  array(
265  'Language' => $this->getDescriptionLanguageCode() ?: 'en'
266  ),
267  $this->getDescription()
268  );
269 
270  // Keyword
271  $keys = $this->getKeywordIds();
272  foreach ($keys as $id) {
273  $key = $this->getKeyword($id);
274  $key->toXML($writer);
275  }
276  if (!count($keys)) {
277  $key = new ilMDKeyword($this->getRBACId(), $this->getObjId());
278  $key->toXML($writer);
279  }
280  $writer->xmlEndTag('Classification');
281  }
282 
283  // STATIC
284 
288  public static function _getIds(int $a_rbac_id, int $a_obj_id): array
289  {
290  global $DIC;
291 
292  $ilDB = $DIC['ilDB'];
293 
294  $query = "SELECT meta_classification_id FROM il_meta_classification " .
295  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
296  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
297 
298  $res = $ilDB->query($query);
299  $ids = [];
300  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
301  $ids[] = (int) $row->meta_classification_id;
302  }
303  return $ids;
304  }
305 }
$res
Definition: ltiservices.php:69
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
const PURPOSE_TRANSLATION
Compatibility fix for legacy MD classes for new db tables.
getTaxonPath(int $a_taxon_path_id)
toXML(ilXmlWriter $writer)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setRBACId(int $a_id)
setPurpose(string $a_purpose)
xmlEndTag(string $tag)
Writes an endtag.
setDescriptionLanguage(ilMDLanguageItem $lng_obj)
global $DIC
Definition: feed.php:28
setObjId(int $a_id)
ilMDLanguageItem $description_language
string $key
Consumer key/client ID value.
Definition: System.php:193
static _getIds(int $a_rbac_id, int $a_obj_id)
getKeyword(int $a_keyword_id)
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
setMetaId(int $a_meta_id, bool $a_read_data=true)
setObjType(string $a_type)
setDescription(string $a_description)