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