ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDRequirement.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
31  private const OS_TRANSLATION = [
32  'pc-dos' => 'PC-DOS',
33  'ms-windows' => 'MS-Windows',
34  'macos' => 'MacOS',
35  'unix' => 'Unix',
36  'multi-os' => 'Multi-OS',
37  'none' => 'None'
38  ];
39 
40  private const BROWSER_TRANSLATION = [
41  'any' => 'Any',
42  'netscape communicator' => 'NetscapeCommunicator',
43  'ms-internet explorer' => 'MS-InternetExplorer',
44  'opera' => 'Opera',
45  'amaya' => 'Amaya'
46  ];
47 
48  private int $or_composite_id = 0;
49  private string $operating_system_name = '';
50  private string $operating_system_minimum_version = '';
51  private string $operating_system_maximum_version = '';
52  private string $browser_name = '';
53  private string $browser_minimum_version = '';
54  private string $browser_maximum_version = '';
55 
59  private int $or_id_browser = 0;
60  private int $or_id_os = 0;
61 
62  // SET/GET
63  public function setOrCompositeId(int $a_or_composite_id): void
64  {
65  $this->or_composite_id = $a_or_composite_id;
66  }
67 
68  public function getOrCompositeId(): int
69  {
71  }
72 
73  public function setOperatingSystemName(string $a_val): bool
74  {
75  switch ($a_val) {
76  case 'PC-DOS':
77  case 'MS-Windows':
78  case 'MacOS':
79  case 'Unix':
80  case 'Multi-OS':
81  case 'None':
82  $this->operating_system_name = $a_val;
83  return true;
84 
85  default:
86  return false;
87  }
88  }
89 
90  public function getOperatingSystemName(): string
91  {
93  }
94 
95  public function setOperatingSystemMinimumVersion(string $a_val): void
96  {
97  $this->operating_system_minimum_version = $a_val;
98  }
99 
100  public function getOperatingSystemMinimumVersion(): string
101  {
103  }
104 
105  public function setOperatingSystemMaximumVersion(string $a_val): void
106  {
107  $this->operating_system_maximum_version = $a_val;
108  }
109 
110  public function getOperatingSystemMaximumVersion(): string
111  {
113  }
114 
115  public function setBrowserName(string $a_val): bool
116  {
117  switch ($a_val) {
118  case 'Any':
119  case 'NetscapeCommunicator':
120  case 'MS-InternetExplorer':
121  case 'Opera':
122  case 'Amaya':
123  case 'Mozilla':
124  $this->browser_name = $a_val;
125  return true;
126 
127  default:
128  return false;
129  }
130  }
131 
132  public function getBrowserName(): string
133  {
134  return $this->browser_name;
135  }
136 
137  public function setBrowserMinimumVersion(string $a_val): void
138  {
139  $this->browser_minimum_version = $a_val;
140  }
141 
142  public function getBrowserMinimumVersion(): string
143  {
145  }
146 
147  public function setBrowserMaximumVersion(string $a_val): void
148  {
149  $this->browser_maximum_version = $a_val;
150  }
151 
152  public function getBrowserMaximumVersion(): string
153  {
155  }
156 
157  public function save(): int
158  {
159  $fields = $this->__getFields();
160  $fields['meta_requirement_id'] = array('integer', $next_id = $this->db->nextId('il_meta_requirement'));
161 
162  if ($this->db->insert('il_meta_requirement', $fields)) {
163  $this->setMetaId($next_id);
164  $this->createOrUpdateOrs();
165  return $this->getMetaId();
166  }
167  return 0;
168  }
169 
170  public function update(): bool
171  {
172  if (!$this->getMetaId()) {
173  return false;
174  }
175 
176  $this->createOrUpdateOrs();
177 
178  return (bool) $this->db->update(
179  'il_meta_requirement',
180  $this->__getFields(),
181  array("meta_requirement_id" => array('integer', $this->getMetaId()))
182  );
183  }
184 
185  public function delete(): bool
186  {
187  if ($this->getMetaId()) {
188  $query = "DELETE FROM il_meta_requirement " .
189  "WHERE meta_requirement_id = " . $this->db->quote($this->getMetaId(), 'integer');
190  $res = $this->db->manipulate($query);
191 
192  $this->deleteAllOrs();
193  return true;
194  }
195  return false;
196  }
197 
201  public function __getFields(): array
202  {
203  return array(
204  'rbac_id' => array('integer', $this->getRBACId()),
205  'obj_id' => array('integer', $this->getObjId()),
206  'obj_type' => array('text', $this->getObjType()),
207  'parent_type' => array('text', $this->getParentType()),
208  'parent_id' => array('integer', $this->getParentId()),
209  //'operating_system_name' => array('text', $this->getOperatingSystemName()),
210  //'os_min_version' => array('text', $this->getOperatingSystemMinimumVersion()),
211  //'os_max_version' => array('text', $this->getOperatingSystemMaximumVersion()),
212  //'browser_name' => array('text', $this->getBrowserName()),
213  //'browser_minimum_version' => array('text', $this->getBrowserMinimumVersion()),
214  //'browser_maximum_version' => array('text', $this->getBrowserMaximumVersion()),
215  'or_composite_id' => array('integer', $this->getOrCompositeId())
216  );
217  }
218 
219  public function read(): bool
220  {
221  if ($this->getMetaId()) {
222  $query = "SELECT * FROM il_meta_requirement " .
223  "WHERE meta_requirement_id = " . $this->db->quote($this->getMetaId(), 'integer');
224 
225  $res = $this->db->query($query);
226  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
227  $this->setRBACId((int) $row->rbac_id);
228  $this->setObjId((int) $row->obj_id);
229  $this->setObjType($row->obj_type ?? '');
230  $this->setParentId((int) $row->parent_id);
231  $this->setParentType($row->parent_type);
232  //$this->setOperatingSystemName($row->operating_system_name ?? '');
233  //$this->setOperatingSystemMinimumVersion($row->os_min_version ?? '');
234  //$this->setOperatingSystemMaximumVersion($row->os_max_version ?? '');
235  //$this->setBrowserName($row->browser_name ?? '');
236  //$this->setBrowserMinimumVersion($row->browser_minimum_version ?? '');
237  //$this->setBrowserMaximumVersion($row->browser_maximum_version ?? '');
238  $this->setOrCompositeId((int) $row->or_composite_id);
239  }
240 
241  $this->readFirstOrs();
242  }
243  return true;
244  }
245 
246  /*
247  * XML Export of all meta data
248  * @param object (xml writer) see class.ilMD2XML.php
249  *
250  */
251  public function toXML(ilXmlWriter $writer): void
252  {
253  $writer->xmlStartTag('Requirement');
254  $writer->xmlStartTag('Type');
255 
256  if ($this->getOperatingSystemName() !== '') {
257  $writer->xmlElement('OperatingSystem', array(
258  'Name' => $this->getOperatingSystemName() ?: 'None',
259  'MinimumVersion' => $this->getOperatingSystemMinimumVersion(),
260  'MaximumVersion' => $this->getOperatingSystemMaximumVersion()
261  ));
262  }
263  if ($this->getBrowserName() !== '') {
264  $writer->xmlElement('Browser', array(
265  'Name' => $this->getBrowserName() ?: 'Any',
266  'MinimumVersion' => $this->getBrowserMinimumVersion(),
267  'MaximumVersion' => $this->getBrowserMaximumVersion()
268  ));
269  }
270  $writer->xmlEndTag('Type');
271  $writer->xmlEndTag('Requirement');
272  }
273 
274  // STATIC
275 
279  public static function _getIds(
280  int $a_rbac_id,
281  int $a_obj_id,
282  int $a_parent_id,
283  string $a_parent_type,
284  int $a_or_composite_id = 0
285  ): array {
286  global $DIC;
287 
288  $ilDB = $DIC->database();
289 
290  $query = "SELECT meta_requirement_id FROM il_meta_requirement " .
291  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
292  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
293  "AND parent_id = " . $ilDB->quote($a_parent_id, 'integer') . " " .
294  "AND parent_type = " . $ilDB->quote($a_parent_type, 'text') . " " .
295  "AND or_composite_id = " . $ilDB->quote($a_or_composite_id, 'integer');
296 
297  $res = $ilDB->query($query);
298  $ids = [];
299  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
300  $ids[] = (int) $row->meta_requirement_id;
301  }
302  return $ids;
303  }
304 
308  protected function createOrUpdateOrs(): void
309  {
310  $os_name = (string) array_search(
311  $this->getOperatingSystemName(),
312  self::OS_TRANSLATION
313  );
314  $browser_name = (string) array_search(
315  $this->getBrowserName(),
316  self::BROWSER_TRANSLATION
317  );
318 
319  $this->or_id_os = $this->createOrUpdateOr(
320  $this->getOrIdOS(),
321  'operating system',
322  $os_name,
325  );
326 
327  $this->or_id_browser = $this->createOrUpdateOr(
328  $this->getOrIdBrowser(),
329  'browser',
330  $browser_name,
331  $this->getBrowserMinimumVersion(),
332  $this->getBrowserMaximumVersion()
333  );
334  }
335 
339  protected function createOrUpdateOr(
340  int $id,
341  string $type,
342  string $name,
343  string $min_version,
344  string $max_version
345  ): int {
346  if ($name === '' && $min_version === '' && $max_version === '') {
347  return 0;
348  }
349 
350  if (!$id) {
351  $this->db->insert(
352  'il_meta_or_composite',
353  [
354  'meta_or_composite_id' => ['integer', $next_id = $this->db->nextId('il_meta_or_composite')],
355  'rbac_id' => ['integer', $this->getRBACId()],
356  'obj_id' => ['integer', $this->getObjId()],
357  'obj_type' => ['text', $this->getObjType()],
358  'parent_type' => ['text', 'meta_requirement'],
359  'parent_id' => ['integer', $this->getMetaId()],
360  'type' => ['text', $type],
361  'name' => ['text', $name],
362  'min_version' => ['text', $min_version],
363  'max_version' => ['text', $max_version]
364  ]
365  );
366  return $next_id;
367  }
368 
369  $this->db->update(
370  'il_meta_or_composite',
371  [
372  'type' => ['text', $type],
373  'name' => ['text', $name],
374  'min_version' => ['text', $min_version],
375  'max_version' => ['text', $max_version]
376  ],
377  ['meta_or_composite_id' => ['integer', $id]]
378  );
379  return $id;
380  }
381 
385  protected function deleteAllOrs(): void
386  {
387  $query = "DELETE FROM il_meta_or_composite WHERE parent_type = 'meta_requirement'
388  AND parent_id = " . $this->db->quote($this->getMetaId(), 'integer');
389  $res = $this->db->manipulate($query);
390  }
391 
395  protected function readFirstOrs(): void
396  {
397  $query = "SELECT * FROM il_meta_or_composite WHERE meta_or_composite_id = " .
398  $this->db->quote($this->getOrIdOS(), 'integer') .
399  " OR meta_or_composite_id = " . $this->db->quote($this->getOrIdBrowser(), 'integer');
400 
401  $res = $this->db->query($query);
402  while ($row = $this->db->fetchAssoc($res)) {
403  switch ($row['type']) {
404  case 'operating system':
405  if (key_exists($row['name'] ?? '', self::OS_TRANSLATION)) {
406  $row['name'] = self::OS_TRANSLATION[$row['name'] ?? ''];
407  }
408  $this->setOperatingSystemName($row['name'] ?? '');
409  $this->setOperatingSystemMinimumVersion($row['min_version'] ?? '');
410  $this->setOperatingSystemMaximumVersion($row['max_version'] ?? '');
411  break;
412 
413  case 'browser':
414  if (key_exists($row['name'] ?? '', self::BROWSER_TRANSLATION)) {
415  $row['name'] = self::BROWSER_TRANSLATION[$row['name'] ?? ''];
416  }
417  $this->setBrowserName($row['name'] ?? '');
418  $this->setBrowserMinimumVersion($row['min_version'] ?? '');
419  $this->setBrowserMaximumVersion($row['max_version'] ?? '');
420  break;
421  }
422  }
423  }
424 
428  protected function readOrIds(int $parent_id): void
429  {
430  $query = "SELECT meta_or_composite_id, type FROM il_meta_or_composite WHERE
431  parent_id = " . $this->db->quote($parent_id, 'integer') .
432  " ORDER BY meta_or_composite_id";
433 
434  $res = $this->db->query($query);
435  $browser_id = 0;
436  $os_id = 0;
437  while ($row = $this->db->fetchAssoc($res)) {
438  if (!$browser_id && $row['type'] === 'browser') {
439  $browser_id = (int) $row['meta_or_composite_id'];
440  }
441  if (!$os_id && $row['type'] === 'operating system') {
442  $os_id = (int) $row['meta_or_composite_id'];
443  }
444  if ($browser_id && $os_id) {
445  break;
446  }
447  }
448 
449  $this->or_id_browser = $browser_id;
450  $this->or_id_os = $os_id;
451  }
452 
456  protected function getOrIdOS(): int
457  {
458  return $this->or_id_os ?? 0;
459  }
460 
464  protected function getOrIdBrowser(): int
465  {
466  return $this->or_id_browser ?? 0;
467  }
468 
472  public function setMetaId(int $a_meta_id, bool $a_read_data = true): void
473  {
474  $this->readOrIds($a_meta_id);
475  parent::setMetaId($a_meta_id, $a_read_data);
476  }
477 }
int $or_id_browser
Compatibility fix for legacy MD classes for new db tables.
$res
Definition: ltiservices.php:69
setOperatingSystemMaximumVersion(string $a_val)
setOrCompositeId(int $a_or_composite_id)
toXML(ilXmlWriter $writer)
const OS_TRANSLATION
Compatibility fix for legacy MD classes for new db tables.
createOrUpdateOr(int $id, string $type, string $name, string $min_version, string $max_version)
Compatibility fix for legacy MD classes for new db tables.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const const int $or_composite_id
setRBACId(int $a_id)
readFirstOrs()
Compatibility fix for legacy MD classes for new db tables.
string $operating_system_maximum_version
setMetaId(int $a_meta_id, bool $a_read_data=true)
Compatibility fix for legacy MD classes for new db tables.
xmlEndTag(string $tag)
Writes an endtag.
setBrowserMinimumVersion(string $a_val)
global $DIC
Definition: feed.php:28
setObjId(int $a_id)
setOperatingSystemMinimumVersion(string $a_val)
string $operating_system_minimum_version
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type, int $a_or_composite_id=0)
const const BROWSER_TRANSLATION
setBrowserName(string $a_val)
setBrowserMaximumVersion(string $a_val)
setParentId(int $a_id)
setOperatingSystemName(string $a_val)
readOrIds(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.
getOrIdOS()
Compatibility fix for legacy MD classes for new db tables.
setParentType(string $a_parent_type)
deleteAllOrs()
Compatibility fix for legacy MD classes for new db tables.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
setObjType(string $a_type)
getOrIdBrowser()
Compatibility fix for legacy MD classes for new db tables.
createOrUpdateOrs()
Compatibility fix for legacy MD classes for new db tables.