ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilOrgUnitOperationContextRegisteredObjective.php
Go to the documentation of this file.
1 <?php
2 
20 declare(strict_types=1);
21 
22 use ILIAS\Setup;
24 
25 class ilOrgUnitOperationContextRegisteredObjective implements Setup\Objective
26 {
27  protected string $context_name;
28  protected ?string $parent_context;
29 
30  public function __construct(
31  string $context_name,
32  ?string $parent_context = null
33  ) {
34  $this->context_name = $context_name;
35  $this->parent_context = $parent_context;
36  }
37 
38  public function getHash(): string
39  {
40  return hash('sha256', self::class . '::' . $this->context_name);
41  }
42 
43  public function getLabel(): string
44  {
45  return 'Add OrgUnit operation context (name=' . $this->context_name .
46  ';parent_context=' . $this->parent_context . ')';
47  }
48 
49  public function isNotable(): bool
50  {
51  return true;
52  }
53 
54  public function getPreconditions(Environment $environment): array
55  {
56  return [
58  ];
59  }
60 
61  public function achieve(Environment $environment): Environment
62  {
63  $db = $environment->getResource(Environment::RESOURCE_DATABASE);
64 
65  // abort if the context already exists, just to be safe
66  if ($this->doesContextExist($db, $this->context_name)) {
67  return $environment;
68  }
69 
70  $parent_context_id = 0;
71  if (isset($this->parent_context)) {
72  // abort if the parent context does not exist, just to be safe
73  if (!($id = $this->getContextId($db, $this->parent_context))) {
74  throw new Exception(
75  'Parent context ' . $this->context_name . ' does not exist,
76  this objective should not be applied!'
77  );
78  }
79  $parent_context_id = $id;
80  }
81 
82  $id = $db->nextId('il_orgu_op_contexts');
83  $db->insert('il_orgu_op_contexts', [
84  'id' => ['integer', $id],
85  'context' => ['text', $this->context_name],
86  'parent_context_id' => ['integer', $parent_context_id]
87  ]);
88 
89  return $environment;
90  }
91 
92  public function isApplicable(Environment $environment): bool
93  {
94  $db = $environment->getResource(Environment::RESOURCE_DATABASE);
95 
96  // not applicable if the context already exists
97  if ($this->doesContextExist($db, $this->context_name)) {
98  return false;
99  }
100 
101  if (isset($this->parent_context)) {
102  // something is wrong if the parent context does not exist
103  if (!$this->doesContextExist($db, $this->parent_context)) {
104  throw new Exception(
105  'Cannot find parent context ' . $this->parent_context
106  );
107  }
108  }
109 
110  return true;
111  }
112 
113  protected function doesContextExist(
114  ilDBInterface $db,
115  string $context
116  ): bool {
117  return (bool) $this->getContextId($db, $context);
118  }
119 
123  protected function getContextId(
124  ilDBInterface $db,
125  string $context
126  ): int {
127  $result = $db->query('SELECT id FROM il_orgu_op_contexts
128  WHERE context = ' . $db->quote($context, 'text'));
129  if (!($row = $result->fetchObject())) {
130  return 0;
131  }
132  return (int) $row->id;
133  }
134 }
$context
Definition: webdav.php:29
quote($value, string $type)
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.
getContextId(ilDBInterface $db, string $context)
Defaults to 0 if context is not found.
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:27
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23