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