ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDTaxonPath.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 class ilMDTaxonPath extends ilMDBase
27 {
28  private string $source = '';
30 
31  // METHODS OF CHILD OBJECTS (Taxon)
32 
36  public function getTaxonIds(): array
37  {
38  if (!$this->getMetaId()) {
39  return [];
40  }
41  return ilMDTaxon::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_taxon_path');
42  }
43 
44  public function getTaxon(int $a_taxon_id): ?ilMDTaxon
45  {
46  if (!$a_taxon_id) {
47  return null;
48  }
49  $tax = new ilMDTaxon();
50  $tax->setMetaId($a_taxon_id);
51 
52  return $tax;
53  }
54 
55  public function addTaxon(): ilMDTaxon
56  {
57  $tax = new ilMDTaxon($this->getRBACId(), $this->getObjId(), $this->getObjType());
58  $tax->setParentId($this->getMetaId());
59  $tax->setParentType('meta_taxon_path');
60 
61  return $tax;
62  }
63 
64  // SET/GET
65  public function setSource(string $a_source): void
66  {
67  $this->source = $a_source;
68  }
69 
70  public function getSource(): string
71  {
72  return $this->source;
73  }
74 
75  public function setSourceLanguage(ilMDLanguageItem $lng_obj): void
76  {
77  $this->source_language = $lng_obj;
78  }
79 
80  public function getSourceLanguage(): ?ilMDLanguageItem
81  {
82  return is_object($this->source_language) ? $this->source_language : null;
83  }
84 
85  public function getSourceLanguageCode(): string
86  {
87  return is_object($this->source_language) ? $this->source_language->getLanguageCode() : '';
88  }
89 
90  public function save(): int
91  {
92  $fields = $this->__getFields();
93  $fields['meta_taxon_path_id'] = array('integer', $next_id = $this->db->nextId('il_meta_taxon_path'));
94 
95  if ($this->db->insert('il_meta_taxon_path', $fields)) {
96  $this->setMetaId($next_id);
97  return $this->getMetaId();
98  }
99  return 0;
100  }
101 
102  public function update(): bool
103  {
104  return $this->getMetaId() && $this->db->update(
105  'il_meta_taxon_path',
106  $this->__getFields(),
107  array("meta_taxon_path_id" => array('integer', $this->getMetaId()))
108  );
109  }
110 
111  public function delete(): bool
112  {
113  if ($this->getMetaId()) {
114  $query = "DELETE FROM il_meta_taxon_path " .
115  "WHERE meta_taxon_path_id = " . $this->db->quote($this->getMetaId(), 'integer');
116  $res = $this->db->manipulate($query);
117 
118  foreach ($this->getTaxonIds() as $id) {
119  $tax = $this->getTaxon($id);
120  $tax->delete();
121  }
122 
123  return true;
124  }
125  return false;
126  }
127 
131  public function __getFields(): array
132  {
133  return array(
134  'rbac_id' => array('integer', $this->getRBACId()),
135  'obj_id' => array('integer', $this->getObjId()),
136  'obj_type' => array('text', $this->getObjType()),
137  'parent_type' => array('text', $this->getParentType()),
138  'parent_id' => array('integer', $this->getParentId()),
139  'source' => array('text', $this->getSource()),
140  'source_language' => array('text', $this->getSourceLanguageCode())
141  );
142  }
143 
144  public function read(): bool
145  {
146  if ($this->getMetaId()) {
147  $query = "SELECT * FROM il_meta_taxon_path " .
148  "WHERE meta_taxon_path_id = " . $this->db->quote($this->getMetaId(), 'integer');
149 
150  $res = $this->db->query($query);
151  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
152  $this->setRBACId((int) $row->rbac_id);
153  $this->setObjId((int) $row->obj_id);
154  $this->setObjType($row->obj_type);
155  $this->setParentId((int) $row->parent_id);
156  $this->setParentType($row->parent_type);
157  $this->setSource($row->source ?? '');
158  $this->source_language = new ilMDLanguageItem($row->source_language ?? '');
159  }
160  }
161  return true;
162  }
163 
164  public function toXML(ilXmlWriter $writer): void
165  {
166  $writer->xmlStartTag('TaxonPath');
167 
168  $writer->xmlElement(
169  'Source',
170  array(
171  'Language' => $this->getSourceLanguageCode() ?: 'en'
172  ),
173  $this->getSource()
174  );
175 
176  // Taxon
177  $taxs = $this->getTaxonIds();
178  foreach ($taxs as $id) {
179  $tax = $this->getTaxon($id);
180  $tax->toXML($writer);
181  }
182  if (!count($taxs)) {
183  $tax = new ilMDTaxon($this->getRBACId(), $this->getObjId());
184  $tax->toXML($writer);
185  }
186 
187  $writer->xmlEndTag('TaxonPath');
188  }
189 
193  public static function _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type): array
194  {
195  global $DIC;
196 
197  $ilDB = $DIC->database();
198 
199  $query = "SELECT meta_taxon_path_id FROM il_meta_taxon_path " .
200  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
201  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
202  "AND parent_id = " . $ilDB->quote($a_parent_id, 'integer') . " " .
203  "AND parent_type = " . $ilDB->quote($a_parent_type, 'text');
204 
205  $res = $ilDB->query($query);
206  $ids = [];
207  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
208  $ids[] = (int) $row->meta_taxon_path_id;
209  }
210  return $ids;
211  }
212 }
$res
Definition: ltiservices.php:69
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)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setRBACId(int $a_id)
xmlEndTag(string $tag)
Writes an endtag.
global $DIC
Definition: feed.php:28
setObjId(int $a_id)
ilMDLanguageItem $source_language
setParentId(int $a_id)
$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.
setParentType(string $a_parent_type)
setSource(string $a_source)
setSourceLanguage(ilMDLanguageItem $lng_obj)
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
setMetaId(int $a_meta_id, bool $a_read_data=true)
getTaxon(int $a_taxon_id)
setObjType(string $a_type)
toXML(ilXmlWriter $writer)