ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDMetaMetadata.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
28  private string $meta_data_scheme = 'LOM v 1.0';
29  private ?ilMDLanguageItem $language = null;
30 
34  private int $schema_id = 0;
35 
39  public function getPossibleSubelements(): array
40  {
41  $subs['Identifier'] = 'meta_identifier';
42  $subs['Contribute'] = 'meta_contribute';
43 
44  return $subs;
45  }
46 
47  // SUBELEMENTS
48 
52  public function getIdentifierIds(): array
53  {
54  return ilMDIdentifier::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_meta_data');
55  }
56 
57  public function getIdentifier(int $a_identifier_id): ?ilMDIdentifier
58  {
59  if (!$a_identifier_id) {
60  return null;
61  }
62  $ide = new ilMDIdentifier();
63  $ide->setMetaId($a_identifier_id);
64 
65  return $ide;
66  }
67 
68  public function addIdentifier(): ilMDIdentifier
69  {
70  $ide = new ilMDIdentifier($this->getRBACId(), $this->getObjId(), $this->getObjType());
71  $ide->setParentId($this->getMetaId());
72  $ide->setParentType('meta_meta_data');
73 
74  return $ide;
75  }
76 
80  public function getContributeIds(): array
81  {
82  return ilMDContribute::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_meta_data');
83  }
84 
85  public function getContribute(int $a_contribute_id): ?ilMDContribute
86  {
87  if (!$a_contribute_id) {
88  return null;
89  }
90  $con = new ilMDContribute();
91  $con->setMetaId($a_contribute_id);
92 
93  return $con;
94  }
95 
96  public function addContribute(): ilMDContribute
97  {
98  $con = new ilMDContribute($this->getRBACId(), $this->getObjId(), $this->getObjType());
99  $con->setParentId($this->getMetaId());
100  $con->setParentType('meta_meta_data');
101 
102  return $con;
103  }
104 
105  // SET/GET
106  //TODO: check fixed attribute
107  public function setMetaDataScheme(string $a_val): void
108  {
109  $this->meta_data_scheme = $a_val;
110  }
111 
112  public function getMetaDataScheme(): string
113  {
114  // Fixed attribute
115  return 'LOM v 1.0';
116  }
117 
118  public function setLanguage(ilMDLanguageItem $lng_obj): void
119  {
120  $this->language = $lng_obj;
121  }
122 
123  public function getLanguage(): ?ilMDLanguageItem
124  {
125  return is_object($this->language) ? $this->language : null;
126  }
127 
128  public function getLanguageCode(): string
129  {
130  return is_object($this->language) ? $this->language->getLanguageCode() : '';
131  }
132 
133  public function save(): int
134  {
135  $fields = $this->__getFields();
136  $fields['meta_meta_data_id'] = array('integer', $next_id = $this->db->nextId('il_meta_meta_data'));
137 
138  if ($this->db->insert('il_meta_meta_data', $fields)) {
139  $this->setMetaId($next_id);
140  $this->createOrUpdateFirstSchema();
141  return $this->getMetaId();
142  }
143  return 0;
144  }
145 
146  public function update(): bool
147  {
148  if (!$this->getMetaId()) {
149  return false;
150  }
151 
152  $this->createOrUpdateFirstSchema();
153 
154  return (bool) $this->db->update(
155  'il_meta_meta_data',
156  $this->__getFields(),
157  array("meta_meta_data_id" => array('integer', $this->getMetaId()))
158  );
159  }
160 
161  public function delete(): bool
162  {
163  if ($this->getMetaId()) {
164  $query = "DELETE FROM il_meta_meta_data " .
165  "WHERE meta_meta_data_id = " . $this->db->quote($this->getMetaId(), 'integer');
166  $res = $this->db->manipulate($query);
167 
168  $this->deleteAllSchemas();
169 
170  foreach ($this->getIdentifierIds() as $id) {
171  $ide = $this->getIdentifier($id);
172  $ide->delete();
173  }
174 
175  foreach ($this->getContributeIds() as $id) {
176  $con = $this->getContribute($id);
177  $con->delete();
178  }
179  return true;
180  }
181 
182  return false;
183  }
184 
188  public function __getFields(): array
189  {
190  return array(
191  'rbac_id' => array('integer', $this->getRBACId()),
192  'obj_id' => array('integer', $this->getObjId()),
193  'obj_type' => array('text', $this->getObjType()),
194  //'meta_data_scheme' => array('text', $this->getMetaDataScheme()),
195  'language' => array('text', $this->getLanguageCode())
196  );
197  }
198 
199  public function read(): bool
200  {
201  if ($this->getMetaId()) {
202  $query = "SELECT * FROM il_meta_meta_data " .
203  "WHERE meta_meta_data_id = " . $this->db->quote($this->getMetaId(), 'integer');
204 
205  $res = $this->db->query($query);
206  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
207  $this->setRBACId((int) $row->rbac_id);
208  $this->setObjId((int) $row->obj_id);
209  $this->setObjType($row->obj_type);
210  //$this->setMetaDataScheme($row->meta_data_scheme ?? '');
211  $this->setLanguage(new ilMDLanguageItem($row->language ?? ''));
212  }
213 
214  $this->readFirstSchema();
215 
216  return true;
217  }
218  return false;
219  }
220 
221  public function toXML(ilXmlWriter $writer): void
222  {
223  $attr = null;
224  if ($this->getMetaDataScheme()) {
225  $attr['MetadataScheme'] = $this->getMetaDataScheme();
226  }
227  if ($this->getLanguageCode()) {
228  $attr['Language'] = $this->getLanguageCode();
229  }
230  $writer->xmlStartTag('Meta-Metadata', $attr);
231 
232  // ELEMENT IDENTIFIER
233  $identifiers = $this->getIdentifierIds();
234  foreach ($identifiers as $id) {
235  $ide = $this->getIdentifier($id);
236  $ide->toXML($writer);
237  }
238  if (!count($identifiers)) {
239  $ide = new ilMDIdentifier($this->getRBACId(), $this->getObjId());
240  $ide->toXML($writer);
241  }
242 
243  // ELEMETN Contribute
244  $contributes = $this->getContributeIds();
245  foreach ($contributes as $id) {
246  $con = $this->getContribute($id);
247  $con->toXML($writer);
248  }
249  if (!count($contributes)) {
250  $con = new ilMDContribute($this->getRBACId(), $this->getObjId());
251  $con->toXML($writer);
252  }
253 
254  $writer->xmlEndTag('Meta-Metadata');
255  }
256 
257  // STATIC
258  public static function _getId(int $a_rbac_id, int $a_obj_id): int
259  {
260  global $DIC;
261 
262  $ilDB = $DIC->database();
263 
264  $query = "SELECT meta_meta_data_id FROM il_meta_meta_data " .
265  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
266  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
267 
268  $res = $ilDB->query($query);
269  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
270  return (int) $row->meta_meta_data_id;
271  }
272  return 0;
273  }
274 
275 
279  protected function createOrUpdateFirstSchema(): void
280  {
281  if ($this->getMetaDataScheme() === '') {
282  return;
283  }
284 
285  if (!$this->getSchemaId()) {
286  $this->db->insert(
287  'il_meta_meta_schema',
288  [
289  'meta_meta_schema_id' => ['integer', $next_id = $this->db->nextId('il_meta_meta_schema')],
290  'rbac_id' => ['integer', $this->getRBACId()],
291  'obj_id' => ['integer', $this->getObjId()],
292  'obj_type' => ['text', $this->getObjType()],
293  'parent_type' => ['text', 'meta_general'],
294  'parent_id' => ['integer', $this->getMetaId()],
295  'meta_data_schema' => ['text', 'LOMv1.0'],
296  ]
297  );
298  $this->schema_id = $next_id;
299  }
300  }
301 
305  protected function deleteAllSchemas(): void
306  {
307  $query = "DELETE FROM il_meta_meta_schema WHERE parent_type = 'meta_meta_data'
308  AND parent_id = " . $this->db->quote($this->getMetaId(), 'integer');
309  $res = $this->db->manipulate($query);
310  }
311 
315  protected function readFirstSchema(): void
316  {
317  $query = "SELECT * FROM il_meta_meta_schema WHERE meta_meta_schema_id = " .
318  $this->db->quote($this->getMetaId(), 'integer');
319 
320  $res = $this->db->query($query);
321  if ($row = $this->db->fetchAssoc($res)) {
322  $this->setMetaDataScheme((string) $row['meta_data_schema']);
323  }
324  }
325 
329  protected function getSchemaId(): int
330  {
331  return $this->schema_id;
332  }
333 
337  protected function readSchemaId(int $parent_id): void
338  {
339  $query = "SELECT meta_meta_schema_id FROM il_meta_meta_schema WHERE parent_type = 'meta_meta_data'
340  AND parent_id = " . $this->db->quote($parent_id, 'integer') .
341  " ORDER BY meta_meta_schema_id";
342 
343  $res = $this->db->query($query);
344  if ($row = $this->db->fetchAssoc($res)) {
345  $this->schema_id = (int) $row['meta_meta_schema_id'];
346  }
347  }
348 
352  public function setMetaId(int $a_meta_id, bool $a_read_data = true): void
353  {
354  $this->readSchemaId($a_meta_id);
355  parent::setMetaId($a_meta_id, $a_read_data);
356  }
357 }
setMetaId(int $a_meta_id, bool $a_read_data=true)
Compatibility fix for legacy MD classes for new db tables.
$res
Definition: ltiservices.php:69
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
getSchemaId()
Compatibility fix for legacy MD classes for new db tables.
setLanguage(ilMDLanguageItem $lng_obj)
static _getId(int $a_rbac_id, int $a_obj_id)
setMetaDataScheme(string $a_val)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getContribute(int $a_contribute_id)
setRBACId(int $a_id)
int $schema_id
Compatibility fix for legacy MD classes for new db tables.
toXML(ilXmlWriter $writer)
ilMDLanguageItem $language
xmlEndTag(string $tag)
Writes an endtag.
global $DIC
Definition: feed.php:28
setObjId(int $a_id)
getIdentifier(int $a_identifier_id)
readFirstSchema()
Compatibility fix for legacy MD classes for new db tables.
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
readSchemaId(int $parent_id)
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.
deleteAllSchemas()
Compatibility fix for legacy MD classes for new db tables.
setObjType(string $a_type)
createOrUpdateFirstSchema()
Compatibility fix for legacy MD classes for new db tables.