ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilOrgUnitOperationRegisteredObjective.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Setup;
23
25{
26 protected string $operation_name;
27 protected string $description;
28 protected string $context;
29
30 public function __construct(
31 string $operation_name,
32 string $description,
34 ) {
35 $this->operation_name = $operation_name;
36 $this->description = $description;
37 $this->context = $context;
38 }
39
40 public function getHash(): string
41 {
42 return hash('sha256', self::class . '::' . $this->operation_name);
43 }
44
45 public function getLabel(): string
46 {
47 return 'Add OrgUnit operation (name=' . $this->operation_name .
48 ';context=' . $this->context . ')';
49 }
50
51 public function isNotable(): bool
52 {
53 return true;
54 }
55
56 public function getPreconditions(Environment $environment): array
57 {
58 return [
60 ];
61 }
62
63 public function achieve(Environment $environment): Environment
64 {
65 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
66
67 // abort if context does not exist, just to be safe
68 if (!($context_id = $this->getContextId($db, $this->context))) {
69 throw new Exception(
70 'Context ' . $this->context . ' does not exists,
71 this objective should not be applied!'
72 );
73 }
74
75 // abort if operation already exists in this context, just to be safe
77 $db,
78 $context_id,
79 $this->operation_name
80 )) {
81 return $environment;
82 }
83
84 $id = $db->nextId('il_orgu_operations');
85 $db->insert('il_orgu_operations', [
86 'operation_id' => ['integer', $id],
87 'operation_string' => ['text', $this->operation_name],
88 'description' => ['text', $this->description],
89 'list_order' => ['integer', 0],
90 'context_id' => ['integer', $context_id],
91 ]);
92
93 return $environment;
94 }
95
96 public function isApplicable(Environment $environment): bool
97 {
98 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
99
100 // something is wrong if context does not exist
101 if (!($context_id = $this->getContextId($db, $this->context))) {
103 'Cannot find context ' . $this->context
104 );
105 }
106
107 // not applicable if operation already exists in this context
109 $db,
110 $context_id,
111 $this->operation_name
112 )) {
113 return false;
114 }
115
116 return true;
117 }
118
119 protected function doesOperationExistInContext(
120 ilDBInterface $db,
121 int $context_id,
122 string $operation
123 ): bool {
124 $result = $db->query('SELECT * FROM il_orgu_operations
125 WHERE context_id = ' . $db->quote($context_id, 'integer') .
126 ' AND operation_string = ' . $db->quote($operation, 'text'));
127 if ($result->numRows()) {
128 return true;
129 }
130 return false;
131 }
132
136 protected function getContextId(
137 ilDBInterface $db,
138 string $context
139 ): int {
140 $result = $db->query('SELECT id FROM il_orgu_op_contexts
141 WHERE context = ' . $db->quote($context, 'text'));
142 if (!($row = $result->fetchObject())) {
143 return 0;
144 }
145 return (int) $row->id;
146 }
147}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Signals that some goal won't be achievable by actions of the system ever.
getContextId(ilDBInterface $db, string $context)
Defaults to 0 if context is not found.
doesOperationExistInContext(ilDBInterface $db, int $context_id, string $operation)
__construct(string $operation_name, string $description, string $context=ilOrgUnitOperationContext::CONTEXT_OBJECT)
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