ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilOrgUnitOperationRegisteredObjective.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Setup;
23 
24 class ilOrgUnitOperationRegisteredObjective implements Setup\Objective
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
76  if ($this->doesOperationExistInContext(
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))) {
102  throw new Setup\UnachievableException(
103  'Cannot find context ' . $this->context
104  );
105  }
106 
107  // not applicable if operation already exists in this context
108  if ($this->doesOperationExistInContext(
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 }
doesOperationExistInContext(ilDBInterface $db, int $context_id, string $operation)
quote($value, string $type)
getContextId(ilDBInterface $db, string $context)
Defaults to 0 if context is not found.
query(string $query)
Run a (read-only) Query on the database.
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__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:27
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23