ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDRelation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 class ilMDRelation extends ilMDBase
27 {
31  private const KIND_TRANSLATION = [
32  'ispartof' => 'IsPartOf',
33  'haspart' => 'HasPart',
34  'isversionof' => 'IsVersionOf',
35  'hasversion' => 'HasVersion',
36  'isformatof' => 'IsFormatOf',
37  'hasformat' => 'HasFormat',
38  'references' => 'References',
39  'isreferencedby' => 'IsReferencedBy',
40  'isbasedon' => 'IsBasedOn',
41  'isbasisfor' => 'IsBasisFor',
42  'requires' => 'Requires',
43  'isrequiredby' => 'IsRequiredBy'
44  ];
45 
46  private string $kind = '';
47 
48  // METHODS OF CHILD OBJECTS (Taxon)
49 
53  public function getIdentifier_Ids(): array
54  {
55  return ilMDIdentifier_::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_relation');
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_relation');
74 
75  return $ide;
76  }
77 
81  public function getDescriptionIds(): array
82  {
83  return ilMDDescription::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_relation');
84  }
85 
86  public function getDescription(int $a_description_id): ?ilMDDescription
87  {
88  if (!$a_description_id) {
89  return null;
90  }
91  $des = new ilMDDescription();
92  $des->setMetaId($a_description_id);
93 
94  return $des;
95  }
96 
97  public function addDescription(): ilMDDescription
98  {
99  $des = new ilMDDescription($this->getRBACId(), $this->getObjId(), $this->getObjType());
100  $des->setParentId($this->getMetaId());
101  $des->setParentType('meta_relation');
102 
103  return $des;
104  }
105 
106  // SET/GET
107  public function setKind(string $a_kind): bool
108  {
109  switch ($a_kind) {
110  case 'IsPartOf':
111  case 'HasPart':
112  case 'IsVersionOf':
113  case 'HasVersion':
114  case 'IsFormatOf':
115  case 'HasFormat':
116  case 'References':
117  case 'IsReferencedBy':
118  case 'IsBasedOn':
119  case 'IsBasisFor':
120  case 'Requires':
121  case 'IsRequiredBy':
122  $this->kind = $a_kind;
123  return true;
124 
125  default:
126  return false;
127  }
128  }
129 
130  public function getKind(): string
131  {
132  return $this->kind;
133  }
134 
135  public function save(): int
136  {
137  $fields = $this->__getFields();
138  $fields['meta_relation_id'] = array('integer', $next_id = $this->db->nextId('il_meta_relation'));
139 
140  if ($this->db->insert('il_meta_relation', $fields)) {
141  $this->setMetaId($next_id);
142  return $this->getMetaId();
143  }
144  return 0;
145  }
146 
147  public function update(): bool
148  {
149  return $this->getMetaId() && $this->db->update(
150  'il_meta_relation',
151  $this->__getFields(),
152  array("meta_relation_id" => array('integer', $this->getMetaId()))
153  );
154  }
155 
156  public function delete(): bool
157  {
158  if ($this->getMetaId()) {
159  $query = "DELETE FROM il_meta_relation " .
160  "WHERE meta_relation_id = " . $this->db->quote($this->getMetaId(), 'integer');
161  $res = $this->db->manipulate($query);
162 
163  foreach ($this->getIdentifier_Ids() as $id) {
164  $ide = $this->getIdentifier_($id);
165  $ide->delete();
166  }
167  foreach ($this->getDescriptionIds() as $id) {
168  $des = $this->getDescription($id);
169  $des->delete();
170  }
171 
172  return true;
173  }
174  return false;
175  }
176 
180  public function __getFields(): array
181  {
185  $kind = (string) array_search(
186  $this->getKind(),
187  self::KIND_TRANSLATION
188  );
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  'kind' => array('text', $kind)
195  );
196  }
197 
198  public function read(): bool
199  {
200  if ($this->getMetaId()) {
201  $query = "SELECT * FROM il_meta_relation " .
202  "WHERE meta_relation_id = " . $this->db->quote($this->getMetaId(), 'integer');
203 
204  $res = $this->db->query($query);
205  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
209  if (key_exists($row->kind ?? '', self::KIND_TRANSLATION)) {
210  $row->kind = self::KIND_TRANSLATION[$row->kind ?? ''];
211  }
212 
213  $this->setRBACId((int) $row->rbac_id);
214  $this->setObjId((int) $row->obj_id);
215  $this->setObjType($row->obj_type);
216  $this->setKind($row->kind ?? '');
217  }
218  }
219  return true;
220  }
221 
222  public function toXML(ilXmlWriter $writer): void
223  {
224  $writer->xmlStartTag('Relation', array(
225  'Kind' => $this->getKind() ?: 'IsPartOf'
226  ));
227  $writer->xmlStartTag('Resource');
228 
229  // Identifier_
230  $ides = $this->getIdentifier_Ids();
231  foreach ($ides as $id) {
232  $ide = $this->getIdentifier_($id);
233  $ide->toXML($writer);
234  }
235  if (!count($ides)) {
236  $ide = new ilMDIdentifier_($this->getRBACId(), $this->getObjId());
237  $ide->toXML($writer);
238  }
239 
240  // Description
241  $dess = $this->getDescriptionIds();
242  foreach ($dess as $id) {
243  $des = $this->getDescription($id);
244  $des->toXML($writer);
245  }
246  if (!count($dess)) {
247  $des = new ilMDDescription($this->getRBACId(), $this->getObjId());
248  $des->toXML($writer);
249  }
250 
251  $writer->xmlEndTag('Resource');
252  $writer->xmlEndTag('Relation');
253  }
254 
255  // STATIC
256 
260  public static function _getIds(int $a_rbac_id, int $a_obj_id): array
261  {
262  global $DIC;
263 
264  $ilDB = $DIC->database();
265 
266  $query = "SELECT meta_relation_id FROM il_meta_relation " .
267  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
268  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
269 
270  $res = $ilDB->query($query);
271  $ids = [];
272  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
273  $ids[] = (int) $row->meta_relation_id;
274  }
275  return $ids;
276  }
277 }
$res
Definition: ltiservices.php:69
setKind(string $a_kind)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getDescription(int $a_description_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
setObjId(int $a_id)
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
const KIND_TRANSLATION
Compatibility fix for legacy MD classes for new db tables.
getIdentifier_(int $a_identifier__id)
toXML(ilXmlWriter $writer)
const string $kind
$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)
static _getIds(int $a_rbac_id, int $a_obj_id)
setObjType(string $a_type)