ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilOrgUnitOperationContextRegisteredObjective.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Setup;
23
25{
26 protected string $context_name;
27 protected ?string $parent_context;
28
29 public function __construct(
30 string $context_name,
31 ?string $parent_context = null
32 ) {
33 $this->context_name = $context_name;
34 $this->parent_context = $parent_context;
35 }
36
37 public function getHash(): string
38 {
39 return hash('sha256', self::class . '::' . $this->context_name);
40 }
41
42 public function getLabel(): string
43 {
44 return 'Add OrgUnit operation context (name=' . $this->context_name .
45 ';parent_context=' . $this->parent_context . ')';
46 }
47
48 public function isNotable(): bool
49 {
50 return true;
51 }
52
53 public function getPreconditions(Environment $environment): array
54 {
55 return [
57 ];
58 }
59
60 public function achieve(Environment $environment): Environment
61 {
62 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
63
64 // abort if the context already exists, just to be safe
65 if ($this->doesContextExist($db, $this->context_name)) {
66 return $environment;
67 }
68
69 $parent_context_id = 0;
70 if (isset($this->parent_context)) {
71 // abort if the parent context does not exist, just to be safe
72 if (!($id = $this->getContextId($db, $this->parent_context))) {
73 throw new Exception(
74 'Parent context ' . $this->context_name . ' does not exist,
75 this objective should not be applied!'
76 );
77 }
78 $parent_context_id = $id;
79 }
80
81 $id = $db->nextId('il_orgu_op_contexts');
82 $db->insert('il_orgu_op_contexts', [
83 'id' => ['integer', $id],
84 'context' => ['text', $this->context_name],
85 'parent_context_id' => ['integer', $parent_context_id]
86 ]);
87
88 return $environment;
89 }
90
91 public function isApplicable(Environment $environment): bool
92 {
93 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
94
95 // not applicable if the context already exists
96 if ($this->doesContextExist($db, $this->context_name)) {
97 return false;
98 }
99
100 if (isset($this->parent_context)) {
101 // something is wrong if the parent context does not exist
102 if (!$this->doesContextExist($db, $this->parent_context)) {
103 throw new Exception(
104 'Cannot find parent context ' . $this->parent_context
105 );
106 }
107 }
108
109 return true;
110 }
111
112 protected function doesContextExist(
113 ilDBInterface $db,
114 string $context
115 ): bool {
116 return (bool) $this->getContextId($db, $context);
117 }
118
122 protected function getContextId(
123 ilDBInterface $db,
124 string $context
125 ): int {
126 $result = $db->query('SELECT id FROM il_orgu_op_contexts
127 WHERE context = ' . $db->quote($context, 'text'));
128 if (!($row = $result->fetchObject())) {
129 return 0;
130 }
131 return (int) $row->id;
132 }
133}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getContextId(ilDBInterface $db, string $context)
Defaults to 0 if context is not found.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:31
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$context
Definition: webdav.php:31