ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMDMetaMetadata.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
27 {
28  private string $meta_data_scheme = 'LOM v 1.0';
29  private ?ilMDLanguageItem $language = null;
30 
34  public function getPossibleSubelements(): array
35  {
36  $subs['Identifier'] = 'meta_identifier';
37  $subs['Contribute'] = 'meta_contribute';
38 
39  return $subs;
40  }
41 
42  // SUBELEMENTS
43 
47  public function getIdentifierIds(): array
48  {
49  return ilMDIdentifier::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_meta_data');
50  }
51 
52  public function getIdentifier(int $a_identifier_id): ?ilMDIdentifier
53  {
54  if (!$a_identifier_id) {
55  return null;
56  }
57  $ide = new ilMDIdentifier();
58  $ide->setMetaId($a_identifier_id);
59 
60  return $ide;
61  }
62 
63  public function addIdentifier(): ilMDIdentifier
64  {
65  $ide = new ilMDIdentifier($this->getRBACId(), $this->getObjId(), $this->getObjType());
66  $ide->setParentId($this->getMetaId());
67  $ide->setParentType('meta_meta_data');
68 
69  return $ide;
70  }
71 
75  public function getContributeIds(): array
76  {
77  return ilMDContribute::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_meta_data');
78  }
79 
80  public function getContribute(int $a_contribute_id): ?ilMDContribute
81  {
82  if (!$a_contribute_id) {
83  return null;
84  }
85  $con = new ilMDContribute();
86  $con->setMetaId($a_contribute_id);
87 
88  return $con;
89  }
90 
91  public function addContribute(): ilMDContribute
92  {
93  $con = new ilMDContribute($this->getRBACId(), $this->getObjId(), $this->getObjType());
94  $con->setParentId($this->getMetaId());
95  $con->setParentType('meta_meta_data');
96 
97  return $con;
98  }
99 
100  // SET/GET
101  //TODO: check fixed attribute
102  public function setMetaDataScheme(string $a_val): void
103  {
104  $this->meta_data_scheme = $a_val;
105  }
106 
107  public function getMetaDataScheme(): string
108  {
109  // Fixed attribute
110  return 'LOM v 1.0';
111  }
112 
113  public function setLanguage(ilMDLanguageItem $lng_obj): void
114  {
115  $this->language = $lng_obj;
116  }
117 
118  public function getLanguage(): ?ilMDLanguageItem
119  {
120  return is_object($this->language) ? $this->language : null;
121  }
122 
123  public function getLanguageCode(): string
124  {
125  return is_object($this->language) ? $this->language->getLanguageCode() : '';
126  }
127 
128  public function save(): int
129  {
130  $fields = $this->__getFields();
131  $fields['meta_meta_data_id'] = array('integer', $next_id = $this->db->nextId('il_meta_meta_data'));
132 
133  if ($this->db->insert('il_meta_meta_data', $fields)) {
134  $this->setMetaId($next_id);
135  return $this->getMetaId();
136  }
137  return 0;
138  }
139 
140  public function update(): bool
141  {
142  return $this->getMetaId() && $this->db->update(
143  'il_meta_meta_data',
144  $this->__getFields(),
145  array("meta_meta_data_id" => array('integer', $this->getMetaId()))
146  );
147  }
148 
149  public function delete(): bool
150  {
151  if ($this->getMetaId()) {
152  $query = "DELETE FROM il_meta_meta_data " .
153  "WHERE meta_meta_data_id = " . $this->db->quote($this->getMetaId(), 'integer');
154  $res = $this->db->manipulate($query);
155 
156  foreach ($this->getIdentifierIds() as $id) {
157  $ide = $this->getIdentifier($id);
158  $ide->delete();
159  }
160 
161  foreach ($this->getContributeIds() as $id) {
162  $con = $this->getContribute($id);
163  $con->delete();
164  }
165  return true;
166  }
167 
168  return false;
169  }
170 
174  public function __getFields(): array
175  {
176  return array(
177  'rbac_id' => array('integer', $this->getRBACId()),
178  'obj_id' => array('integer', $this->getObjId()),
179  'obj_type' => array('text', $this->getObjType()),
180  'meta_data_scheme' => array('text', $this->getMetaDataScheme()),
181  'language' => array('text', $this->getLanguageCode())
182  );
183  }
184 
185  public function read(): bool
186  {
187  if ($this->getMetaId()) {
188  $query = "SELECT * FROM il_meta_meta_data " .
189  "WHERE meta_meta_data_id = " . $this->db->quote($this->getMetaId(), 'integer');
190 
191  $res = $this->db->query($query);
192  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
193  $this->setRBACId((int) $row->rbac_id);
194  $this->setObjId((int) $row->obj_id);
195  $this->setObjType($row->obj_type);
196  $this->setMetaDataScheme($row->meta_data_scheme ?? '');
197  $this->setLanguage(new ilMDLanguageItem($row->language ?? ''));
198  }
199  return true;
200  }
201  return false;
202  }
203 
204  public function toXML(ilXmlWriter $writer): void
205  {
206  $attr = null;
207  if ($this->getMetaDataScheme()) {
208  $attr['MetadataScheme'] = $this->getMetaDataScheme();
209  }
210  if ($this->getLanguageCode()) {
211  $attr['Language'] = $this->getLanguageCode();
212  }
213  $writer->xmlStartTag('Meta-Metadata', $attr);
214 
215  // ELEMENT IDENTIFIER
216  $identifiers = $this->getIdentifierIds();
217  foreach ($identifiers as $id) {
218  $ide = $this->getIdentifier($id);
219  $ide->toXML($writer);
220  }
221  if (!count($identifiers)) {
222  $ide = new ilMDIdentifier($this->getRBACId(), $this->getObjId());
223  $ide->toXML($writer);
224  }
225 
226  // ELEMETN Contribute
227  $contributes = $this->getContributeIds();
228  foreach ($contributes as $id) {
229  $con = $this->getContribute($id);
230  $con->toXML($writer);
231  }
232  if (!count($contributes)) {
233  $con = new ilMDContribute($this->getRBACId(), $this->getObjId());
234  $con->toXML($writer);
235  }
236 
237  $writer->xmlEndTag('Meta-Metadata');
238  }
239 
240  // STATIC
241  public static function _getId(int $a_rbac_id, int $a_obj_id): int
242  {
243  global $DIC;
244 
245  $ilDB = $DIC->database();
246 
247  $query = "SELECT meta_meta_data_id FROM il_meta_meta_data " .
248  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
249  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
250 
251  $res = $ilDB->query($query);
252  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
253  return (int) $row->meta_meta_data_id;
254  }
255  return 0;
256  }
257 }
$res
Definition: ltiservices.php:69
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
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)
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)
$query
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.
setMetaId(int $a_meta_id, bool $a_read_data=true)
setObjType(string $a_type)