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