ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDGeneral.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 class ilMDGeneral extends ilMDBase
28 {
32  private const STRUCTURE_TRANSLATION = [
33  'atomic' => 'Atomic',
34  'collection' => 'Collection',
35  'networked' => 'Networked',
36  'hierarchical' => 'Hierarchical',
37  'linear' => 'Linear'
38  ];
39 
41 
42  private string $coverage = '';
43  private string $structure = '';
44  private string $title = '';
46 
50  private int $coverage_id = 0;
51 
55  public function getPossibleSubelements(): array
56  {
57  $subs['Keyword'] = 'meta_keyword';
58  $subs['Language'] = 'meta_language';
59  $subs['Identifier'] = 'meta_identifier';
60  $subs['Description'] = 'meta_description';
61 
62  return $subs;
63  }
64 
65  // Subelements (Identifier, Language, Description, Keyword)
66 
70  public function getIdentifierIds(): array
71  {
72  return ilMDIdentifier::_getIds($this->getRBACId(), $this->getObjId(), (int) $this->getMetaId(), 'meta_general');
73  }
74 
75  public function getIdentifier(int $a_identifier_id): ?ilMDIdentifier
76  {
77  if (!$a_identifier_id) {
78  return null;
79  }
80  $ide = new ilMDIdentifier();
81  $ide->setMetaId($a_identifier_id);
82 
83  return $ide;
84  }
85 
86  public function addIdentifier(): ilMDIdentifier
87  {
88  $ide = new ilMDIdentifier($this->getRBACId(), $this->getObjId(), $this->getObjType());
89  $ide->setParentId($this->getMetaId());
90  $ide->setParentType('meta_general');
91 
92  return $ide;
93  }
94 
98  public function getLanguageIds(): array
99  {
100  return ilMDLanguage::_getIds($this->getRBACId(), $this->getObjId(), (int) $this->getMetaId(), 'meta_general');
101  }
102 
103  public function getLanguage(int $a_language_id): ?ilMDLanguage
104  {
105  if (!$a_language_id) {
106  return null;
107  }
108  $lan = new ilMDLanguage();
109  $lan->setMetaId($a_language_id);
110 
111  return $lan;
112  }
113 
114  public function addLanguage(): ilMDLanguage
115  {
116  $lan = new ilMDLanguage($this->getRBACId(), $this->getObjId(), $this->getObjType());
117  $lan->setParentId($this->getMetaId());
118  $lan->setParentType('meta_general');
119 
120  return $lan;
121  }
122 
126  public function getDescriptionIds(): array
127  {
128  return ilMDDescription::_getIds($this->getRBACId(), $this->getObjId(), (int) $this->getMetaId(), 'meta_general');
129  }
130 
131  public function getDescription(int $a_description_id): ?ilMDDescription
132  {
133  if (!$a_description_id) {
134  return null;
135  }
136  $des = new ilMDDescription();
137  $des->setMetaId($a_description_id);
138 
139  return $des;
140  }
141 
142  public function addDescription(): ilMDDescription
143  {
144  $des = new ilMDDescription($this->getRBACId(), $this->getObjId(), $this->getObjType());
145  $des->setParentId($this->getMetaId());
146  $des->setParentType('meta_general');
147 
148  return $des;
149  }
150 
154  public function getKeywordIds(): array
155  {
156  return ilMDKeyword::_getIds($this->getRBACId(), $this->getObjId(), (int) $this->getMetaId(), 'meta_general');
157  }
158 
159  public function getKeyword(int $a_keyword_id): ?ilMDKeyword
160  {
161  if (!$a_keyword_id) {
162  return null;
163  }
164  $key = new ilMDKeyword();
165  $key->setMetaId($a_keyword_id);
166 
167  return $key;
168  }
169 
170  public function addKeyword(): ilMDKeyword
171  {
172  $key = new ilMDKeyword($this->getRBACId(), $this->getObjId(), $this->getObjType());
173  $key->setParentId($this->getMetaId());
174  $key->setParentType('meta_general');
175 
176  return $key;
177  }
178 
179  // SET/GET
180  public function setStructure(string $a_structure): bool
181  {
182  switch ($a_structure) {
183  case 'Atomic':
184  case 'Collection':
185  case 'Networked':
186  case 'Hierarchical':
187  case 'Linear':
188  $this->structure = $a_structure;
189  return true;
190 
191  default:
192  return false;
193  }
194  }
195 
196  public function getStructure(): string
197  {
198  return $this->structure;
199  }
200 
201  public function setTitle(string $a_title): void
202  {
203  $this->title = $a_title;
204  }
205 
206  public function getTitle(): string
207  {
208  return $this->title;
209  }
210 
211  public function setTitleLanguage(ilMDLanguageItem $lng_obj): void
212  {
213  $this->title_language = $lng_obj;
214  }
215 
216  public function getTitleLanguage(): ?ilMDLanguageItem
217  {
218  return is_object($this->title_language) ? $this->title_language : null;
219  }
220 
221  public function getTitleLanguageCode(): string
222  {
223  return is_object($this->title_language) ? $this->title_language->getLanguageCode() : '';
224  }
225 
226  public function setCoverage(string $a_coverage): void
227  {
228  $this->coverage = $a_coverage;
229  }
230 
231  public function getCoverage(): string
232  {
233  return $this->coverage;
234  }
235 
236  public function setCoverageLanguage(ilMDLanguageItem $lng_obj): void
237  {
238  $this->coverage_language = $lng_obj;
239  }
240 
242  {
243  return is_object($this->coverage_language) ? $this->coverage_language : null;
244  }
245 
246  public function getCoverageLanguageCode(): string
247  {
248  return is_object($this->coverage_language) ? $this->coverage_language->getLanguageCode() : '';
249  }
250 
251  public function save(): int
252  {
253  $fields = $this->__getFields();
254  $fields['meta_general_id'] = array('integer', $next_id = $this->db->nextId('il_meta_general'));
255 
256  $this->log->debug("Insert General " . print_r($fields, true));
257  $this->log->logStack(ilLogLevel::DEBUG);
258  //ilUtil::printBacktrace(10);
259 
260  if ($this->db->insert('il_meta_general', $fields)) {
261  $this->setMetaId($next_id);
262  $this->createOrUpdateCoverage();
263  return $this->getMetaId();
264  }
265  return 0;
266  }
267 
268  public function update(): bool
269  {
270  if (!$this->getMetaId()) {
271  return false;
272  }
273 
274  $this->createOrUpdateCoverage();
275 
276  return (bool) $this->db->update(
277  'il_meta_general',
278  $this->__getFields(),
279  array("meta_general_id" => array('integer', $this->getMetaId()))
280  );
281  }
282 
283  public function delete(): bool
284  {
285  if (!$this->getMetaId()) {
286  return false;
287  }
288  // Identifier
289  foreach ($this->getIdentifierIds() as $id) {
290  $ide = $this->getIdentifier($id);
291  $ide->delete();
292  }
293 
294  // Language
295  foreach ($this->getLanguageIds() as $id) {
296  $lan = $this->getLanguage($id);
297  $lan->delete();
298  }
299 
300  // Description
301  foreach ($this->getDescriptionIds() as $id) {
302  $des = $this->getDescription($id);
303  $des->delete();
304  }
305 
306  // Keyword
307  foreach ($this->getKeywordIds() as $id) {
308  $key = $this->getKeyword($id);
309  $key->delete();
310  }
311 
312  if ($this->getMetaId()) {
313  $query = "DELETE FROM il_meta_general " .
314  "WHERE meta_general_id = " . $this->db->quote($this->getMetaId(), 'integer');
315  $res = $this->db->manipulate($query);
316 
317  $this->deleteAllCoverages();
318  return true;
319  }
320 
321  return false;
322  }
323 
327  public function __getFields(): array
328  {
332  $structure = (string) array_search(
333  $this->getStructure(),
334  self::STRUCTURE_TRANSLATION
335  );
336 
337  return array(
338  'rbac_id' => array('integer', $this->getRBACId()),
339  'obj_id' => array('integer', $this->getObjId()),
340  'obj_type' => array('text', $this->getObjType()),
341  'general_structure' => array('text', $structure),
342  'title' => array('text', $this->getTitle()),
343  'title_language' => array('text', $this->getTitleLanguageCode()),
344  //'coverage' => array('text', $this->getCoverage()),
345  //'coverage_language' => array('text', $this->getCoverageLanguageCode())
346  );
347  }
348 
349  public function read(): bool
350  {
351  if ($this->getMetaId()) {
352  $query = "SELECT * FROM il_meta_general " .
353  "WHERE meta_general_id = " . $this->db->quote($this->getMetaId(), 'integer');
354 
355  $res = $this->db->query($query);
356  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
360  if (key_exists($row->general_structure ?? '', self::STRUCTURE_TRANSLATION)) {
361  $row->general_structure = self::STRUCTURE_TRANSLATION[$row->general_structure ?? ''];
362  }
363 
364  $this->setRBACId((int) $row->rbac_id);
365  $this->setObjId((int) $row->obj_id);
366  $this->setObjType((string) $row->obj_type);
367  $this->setStructure((string) $row->general_structure);
368  $this->setTitle((string) $row->title);
369  $this->setTitleLanguage(new ilMDLanguageItem($row->title_language ?? ''));
370  //$this->setCoverage((string) $row->coverage);
371  //$this->setCoverageLanguage(new ilMDLanguageItem($row->coverage_language ?? ''));
372  }
373 
374  $this->readFirstCoverage();
375  }
376  return true;
377  }
378 
379  public function toXML(ilXmlWriter $writer): void
380  {
381  $writer->xmlStartTag('General', array(
382  'Structure' => $this->getStructure() ?: 'Atomic'
383  ));
384 
385  // Identifier
386  $first = true;
387  $identifiers = $this->getIdentifierIds();
388  foreach ($identifiers as $id) {
389  $ide = $this->getIdentifier($id);
390  $ide->setExportMode($this->getExportMode());
391  $ide->toXML($writer);
392  $first = false;
393  }
394  if (!count($identifiers)) {
395  $ide = new ilMDIdentifier(
396  $this->getRBACId(),
397  $this->getObjId(),
398  $this->getObjType()
399  ); // added type, alex, 31 Oct 2007
400  $ide->setExportMode(true);
401  $ide->toXML($writer);
402  }
403 
404  // Title
405  $writer->xmlElement(
406  'Title',
407  array(
408  'Language' => $this->getTitleLanguageCode() ?: 'en'
409  ),
410  $this->getTitle()
411  );
412 
413  // Language
414  $languages = $this->getLanguageIds();
415  foreach ($languages as $id) {
416  $lan = $this->getLanguage($id);
417  $lan->toXML($writer);
418  }
419 
420  // Description
421  $descriptions = $this->getDescriptionIds();
422  foreach ($descriptions as $id) {
423  $des = $this->getDescription($id);
424  $des->toXML($writer);
425  }
426  if (!count($descriptions)) {
427  // Default
428 
429  $des = new ilMDDescription($this->getRBACId(), $this->getObjId());
430  $des->toXML($writer);
431  }
432 
433  // Keyword
434  $keywords = $this->getKeywordIds();
435  foreach ($keywords as $id) {
436  $key = $this->getKeyword($id);
437  $key->toXML($writer);
438  }
439  if (!count($keywords)) {
440  // Default
441 
442  $key = new ilMDKeyword($this->getRBACId(), $this->getObjId());
443  $key->toXML($writer);
444  }
445 
446  // Copverage
447  if ($this->getCoverage() !== '') {
448  $writer->xmlElement(
449  'Coverage',
450  array(
451  'Language' => $this->getCoverageLanguageCode() ?: 'en'
452  ),
453  $this->getCoverage()
454  );
455  }
456  $writer->xmlEndTag('General');
457  }
458 
459  // STATIC
460  public static function _getId(int $a_rbac_id, int $a_obj_id): int
461  {
462  global $DIC;
463 
464  $ilDB = $DIC->database();
465 
466  $query = "SELECT meta_general_id FROM il_meta_general " .
467  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
468  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
469 
470  $res = $ilDB->query($query);
471  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
472  return (int) $row->meta_general_id;
473  }
474  return 0;
475  }
476 
480  protected function createOrUpdateCoverage(): void
481  {
482  if ($this->getCoverage() === '' && $this->getCoverageLanguageCode() === '') {
483  return;
484  }
485 
486  if (!$this->getCoverageId()) {
487  $this->db->insert(
488  'il_meta_coverage',
489  [
490  'meta_coverage_id' => ['integer', $next_id = $this->db->nextId('il_meta_coverage')],
491  'rbac_id' => ['integer', $this->getRBACId()],
492  'obj_id' => ['integer', $this->getObjId()],
493  'obj_type' => ['text', $this->getObjType()],
494  'parent_type' => ['text', 'meta_general'],
495  'parent_id' => ['integer', $this->getMetaId()],
496  'coverage' => ['text', $this->getCoverage()],
497  'coverage_language' => ['text', $this->getCoverageLanguageCode()]
498  ]
499  );
500  $this->coverage_id = $next_id;
501  return;
502  }
503 
504  $this->db->update(
505  'il_meta_coverage',
506  [
507  'coverage' => ['text', $this->getCoverage()],
508  'coverage_language' => ['text', $this->getCoverageLanguageCode()]
509  ],
510  ['meta_coverage_id' => ['integer', $this->getCoverageId()]]
511  );
512  }
513 
517  protected function deleteAllCoverages(): void
518  {
519  $query = "DELETE FROM il_meta_coverage WHERE parent_type = 'meta_general'
520  AND parent_id = " . $this->db->quote($this->getMetaId(), 'integer');
521  $res = $this->db->manipulate($query);
522  }
523 
527  protected function readFirstCoverage(): void
528  {
529  $query = "SELECT * FROM il_meta_coverage WHERE meta_coverage_id = " .
530  $this->db->quote($this->getCoverageId(), 'integer');
531 
532  $res = $this->db->query($query);
533  if ($row = $this->db->fetchAssoc($res)) {
534  $this->setCoverage((string) $row['coverage']);
535  $this->setCoverageLanguage(new ilMDLanguageItem((string) $row['coverage_language']));
536  }
537  }
538 
542  protected function getCoverageId(): int
543  {
544  return $this->coverage_id;
545  }
546 
550  protected function readCoverageId(int $parent_id): void
551  {
552  $query = "SELECT meta_coverage_id FROM il_meta_coverage WHERE parent_type = 'meta_general'
553  AND parent_id = " . $this->db->quote($parent_id, 'integer') .
554  " ORDER BY meta_coverage_id";
555 
556  $res = $this->db->query($query);
557  if ($row = $this->db->fetchAssoc($res)) {
558  $this->coverage_id = (int) $row['meta_coverage_id'];
559  }
560  }
561 
565  public function setMetaId(int $a_meta_id, bool $a_read_data = true): void
566  {
567  $this->readCoverageId($a_meta_id);
568  parent::setMetaId($a_meta_id, $a_read_data);
569  }
570 }
int $coverage_id
Compatibility fix for legacy MD classes for new db tables.
$res
Definition: ltiservices.php:69
getIdentifier(int $a_identifier_id)
getCoverageId()
Compatibility fix for legacy MD classes for new db tables.
setTitle(string $a_title)
deleteAllCoverages()
Compatibility fix for legacy MD classes for new db tables.
setStructure(string $a_structure)
readCoverageId(int $parent_id)
Compatibility fix for legacy MD classes for new db tables.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getId(int $a_rbac_id, int $a_obj_id)
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
setRBACId(int $a_id)
xmlEndTag(string $tag)
Writes an endtag.
global $DIC
Definition: feed.php:28
setCoverage(string $a_coverage)
setObjId(int $a_id)
ilMDLanguageItem $title_language
const ilMDLanguageItem $coverage_language
setMetaId(int $a_meta_id, bool $a_read_data=true)
Compatibility fix for legacy MD classes for new db tables.
string $key
Consumer key/client ID value.
Definition: System.php:193
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
getDescription(int $a_description_id)
toXML(ilXmlWriter $writer)
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
createOrUpdateCoverage()
Compatibility fix for legacy MD classes for new db tables.
readFirstCoverage()
Compatibility fix for legacy MD classes for new db tables.
setTitleLanguage(ilMDLanguageItem $lng_obj)
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
getLanguage(int $a_language_id)
getKeyword(int $a_keyword_id)
const STRUCTURE_TRANSLATION
Compatibility fix for legacy MD classes for new db tables.
$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)
setCoverageLanguage(ilMDLanguageItem $lng_obj)
setObjType(string $a_type)