ILIAS  release_8 Revision v8.24
class.ilMDRelation.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 private string $kind = '';
29
30 // METHODS OF CHILD OBJECTS (Taxon)
31
35 public function getIdentifier_Ids(): array
36 {
37 return ilMDIdentifier_::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_relation');
38 }
39
40 public function getIdentifier_(int $a_identifier__id): ?ilMDIdentifier_
41 {
42 if (!$a_identifier__id) {
43 return null;
44 }
45 $ide = new ilMDIdentifier_();
46 $ide->setMetaId($a_identifier__id);
47
48 return $ide;
49 }
50
52 {
53 $ide = new ilMDIdentifier_($this->getRBACId(), $this->getObjId(), $this->getObjType());
54 $ide->setParentId($this->getMetaId());
55 $ide->setParentType('meta_relation');
56
57 return $ide;
58 }
59
63 public function getDescriptionIds(): array
64 {
65 return ilMDDescription::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_relation');
66 }
67
68 public function getDescription(int $a_description_id): ?ilMDDescription
69 {
70 if (!$a_description_id) {
71 return null;
72 }
73 $des = new ilMDDescription();
74 $des->setMetaId($a_description_id);
75
76 return $des;
77 }
78
80 {
81 $des = new ilMDDescription($this->getRBACId(), $this->getObjId(), $this->getObjType());
82 $des->setParentId($this->getMetaId());
83 $des->setParentType('meta_relation');
84
85 return $des;
86 }
87
88 // SET/GET
89 public function setKind(string $a_kind): bool
90 {
91 switch ($a_kind) {
92 case 'IsPartOf':
93 case 'HasPart':
94 case 'IsVersionOf':
95 case 'HasVersion':
96 case 'IsFormatOf':
97 case 'HasFormat':
98 case 'References':
99 case 'IsReferencedBy':
100 case 'IsBasedOn':
101 case 'IsBasisFor':
102 case 'Requires':
103 case 'IsRequiredBy':
104 $this->kind = $a_kind;
105 return true;
106
107 default:
108 return false;
109 }
110 }
111
112 public function getKind(): string
113 {
114 return $this->kind;
115 }
116
117 public function save(): int
118 {
119 $fields = $this->__getFields();
120 $fields['meta_relation_id'] = array('integer', $next_id = $this->db->nextId('il_meta_relation'));
121
122 if ($this->db->insert('il_meta_relation', $fields)) {
123 $this->setMetaId($next_id);
124 return $this->getMetaId();
125 }
126 return 0;
127 }
128
129 public function update(): bool
130 {
131 return $this->getMetaId() && $this->db->update(
132 'il_meta_relation',
133 $this->__getFields(),
134 array("meta_relation_id" => array('integer', $this->getMetaId()))
135 );
136 }
137
138 public function delete(): bool
139 {
140 if ($this->getMetaId()) {
141 $query = "DELETE FROM il_meta_relation " .
142 "WHERE meta_relation_id = " . $this->db->quote($this->getMetaId(), 'integer');
143 $res = $this->db->manipulate($query);
144
145 foreach ($this->getIdentifier_Ids() as $id) {
146 $ide = $this->getIdentifier_($id);
147 $ide->delete();
148 }
149 foreach ($this->getDescriptionIds() as $id) {
150 $des = $this->getDescription($id);
151 $des->delete();
152 }
153
154 return true;
155 }
156 return false;
157 }
158
162 public function __getFields(): array
163 {
164 return array(
165 'rbac_id' => array('integer', $this->getRBACId()),
166 'obj_id' => array('integer', $this->getObjId()),
167 'obj_type' => array('text', $this->getObjType()),
168 'kind' => array('text', $this->getKind())
169 );
170 }
171
172 public function read(): bool
173 {
174 if ($this->getMetaId()) {
175 $query = "SELECT * FROM il_meta_relation " .
176 "WHERE meta_relation_id = " . $this->db->quote($this->getMetaId(), 'integer');
177
178 $res = $this->db->query($query);
179 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
180 $this->setRBACId((int) $row->rbac_id);
181 $this->setObjId((int) $row->obj_id);
182 $this->setObjType($row->obj_type);
183 $this->setKind($row->kind ?? '');
184 }
185 }
186 return true;
187 }
188
189 public function toXML(ilXmlWriter $writer): void
190 {
191 $writer->xmlStartTag('Relation', array(
192 'Kind' => $this->getKind() ?: 'IsPartOf'
193 ));
194 $writer->xmlStartTag('Resource');
195
196 // Identifier_
197 $ides = $this->getIdentifier_Ids();
198 foreach ($ides as $id) {
199 $ide = $this->getIdentifier_($id);
200 $ide->toXML($writer);
201 }
202 if (!count($ides)) {
203 $ide = new ilMDIdentifier_($this->getRBACId(), $this->getObjId());
204 $ide->toXML($writer);
205 }
206
207 // Description
208 $dess = $this->getDescriptionIds();
209 foreach ($dess as $id) {
210 $des = $this->getDescription($id);
211 $des->toXML($writer);
212 }
213 if (!count($dess)) {
214 $des = new ilMDDescription($this->getRBACId(), $this->getObjId());
215 $des->toXML($writer);
216 }
217
218 $writer->xmlEndTag('Resource');
219 $writer->xmlEndTag('Relation');
220 }
221
222 // STATIC
223
227 public static function _getIds(int $a_rbac_id, int $a_obj_id): array
228 {
229 global $DIC;
230
231 $ilDB = $DIC->database();
232
233 $query = "SELECT meta_relation_id FROM il_meta_relation " .
234 "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
235 "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
236
237 $res = $ilDB->query($query);
238 $ids = [];
239 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
240 $ids[] = (int) $row->meta_relation_id;
241 }
242 return $ids;
243 }
244}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
setObjType(string $a_type)
setObjId(int $a_id)
setMetaId(int $a_meta_id, bool $a_read_data=true)
setRBACId(int $a_id)
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
static _getIds(int $a_rbac_id, int $a_obj_id)
setKind(string $a_kind)
toXML(ilXmlWriter $writer)
getDescription(int $a_description_id)
getIdentifier_(int $a_identifier__id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlEndTag(string $tag)
Writes an endtag.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
$query