ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilOrgUnitPermissionQueries.php
Go to the documentation of this file.
1 <?php
24 {
28  public static function getTemplateSetForContextName(string $context_name, string $position_id, bool $editable = false): ilOrgUnitPermission
29  {
31  if (!$context) {
32  throw new ilException('No context found');
33  }
34  if (!$position_id) {
35  throw new ilException('$position_id cannot be null');
36  }
37 
38  $template_set = ilOrgUnitPermission::where([
40  'context_id' => $context->getId(),
41  'position_id' => $position_id,
42  ])->first();
43 
44  if (!$template_set) {
45  $template_set = new ilOrgUnitPermission();
46  $template_set->setParentId(ilOrgUnitPermission::PARENT_TEMPLATE);
47  $template_set->setContextId($context->getId());
48  $template_set->setPositionId($position_id);
49  $template_set->setNewlyCreated(true);
50  $template_set->create();
51  }
52 
53  $template_set->setProtected(!$editable);
54  $template_set->afterObjectLoad();
55  return $template_set;
56  }
57 
58  public static function hasLocalSet(int $ref_id, int $position_id): bool
59  {
61  'parent_id' => $ref_id,
62  'position_id' => $position_id,
63  ])->hasSets());
64  }
65 
70  public static function getSetForRefId(int $ref_id, int $position_id): ilOrgUnitPermission
71  {
72  // TODO write performant query
73  self::assertRefIdAndPositionIdIsNotNull($ref_id, $position_id);
74 
75  $context = self::getContextByRefId($ref_id);
76 
77  $ilOrgUnitGlobalSettings = ilOrgUnitGlobalSettings::getInstance();
78  $ilOrgUnitObjectPositionSetting = $ilOrgUnitGlobalSettings->getObjectPositionSettingsByType($context->getContext());
79 
80  if (!$ilOrgUnitObjectPositionSetting->isActive()) {
82  "Postion-related permissions not active in {$context->getContext()}",
83  $context->getContext()
84  );
85  }
86 
90  $dedicated_set = ilOrgUnitPermission::where([
91  'parent_id' => $ref_id,
92  'context_id' => $context->getId(),
93  'position_id' => $position_id,
94  ])->first();
95  if ($dedicated_set) {
96  $dedicated_set->afterObjectLoad();
97  return $dedicated_set;
98  }
99 
100  return self::getTemplateSetForContextName($context->getContext(), $position_id);
101  }
102 
107  public static function findOrCreateSetForRefId(int $ref_id, int $position_id): ilOrgUnitPermission
108  {
112  self::assertRefIdAndPositionIdIsNotNull($ref_id, $position_id);
113 
114  $context = self::getContextByRefId($ref_id);
115 
116  $ilOrgUnitGlobalSettings = ilOrgUnitGlobalSettings::getInstance();
117  $ilOrgUnitObjectPositionSetting = $ilOrgUnitGlobalSettings->getObjectPositionSettingsByType($context->getContext());
118 
119  if (!$ilOrgUnitObjectPositionSetting->isActive()) {
121  "Position-related permissions not active in {$context->getContext()}",
122  $context->getContext()
123  );
124  }
125 
126  $dedicated_set = ilOrgUnitPermission::where([
127  'parent_id' => $ref_id,
128  'context_id' => $context->getId(),
129  'position_id' => $position_id,
130  ])->first();
131  if ($dedicated_set) {
132  $dedicated_set->afterObjectLoad();
133  return $dedicated_set;
134  }
135 
136  $template = self::getTemplateSetForContextName($context->getContext(), $position_id);
137 
138  $set = new ilOrgUnitPermission();
139  $set->setProtected(false);
140  $set->setParentId($ref_id);
141  $set->setPositionId($position_id);
142  $set->setContextId($context->getId());
143  $set->setOperations($template->getOperations());
144  $set->setNewlyCreated(true);
145  $set->create();
146  $set->afterObjectLoad();
147 
148  return $set;
149  }
150 
155  public static function removeLocalSetForRefId(int $ref_id, int $position_id): bool
156  {
160  self::assertRefIdAndPositionIdIsNotNull($ref_id, $position_id);
161 
162  $context = self::getContextByRefId($ref_id);
163 
164  $ilOrgUnitGlobalSettings = ilOrgUnitGlobalSettings::getInstance();
165  $ilOrgUnitObjectPositionSetting = $ilOrgUnitGlobalSettings->getObjectPositionSettingsByType($context->getContext());
166 
167  if (!$ilOrgUnitObjectPositionSetting->isActive()) {
169  "Position-related permissions not active in {$context->getContext()}",
170  $context->getContext()
171  );
172  }
173 
174  $dedicated_set = ilOrgUnitPermission::where([
175  'parent_id' => $ref_id,
176  'context_id' => $context->getId(),
177  'position_id' => $position_id,
178  'protected' => false,
179  ])->first();
180  if ($dedicated_set) {
181  $dedicated_set->delete();
182 
183  return true;
184  }
185 
186  return false;
187  }
188 
193  public static function getAllTemplateSetsForAllActivedContexts(int $position_id, bool $editable = false): array
194  {
195  $activated_components = [];
196  foreach (ilOrgUnitGlobalSettings::getInstance()->getPositionSettings() as $ilOrgUnitObjectPositionSetting) {
197  if ($ilOrgUnitObjectPositionSetting->isActive()) {
198  $activated_components[] = $ilOrgUnitObjectPositionSetting->getType();
199  }
200  }
201  $sets = [];
202  foreach ($activated_components as $context) {
203  $sets[] = self::getTemplateSetForContextName($context, $position_id, $editable);
204  }
205 
206  return $sets;
207  }
208 
212  private static function getContextByRefId(int $ref_id): ilOrgUnitOperationContext
213  {
215  if (!$context) {
216  throw new ilException('Context not found');
217  }
218 
219  return $context;
220  }
221 
225  private static function assertRefIdAndPositionIdIsNotNull(int $ref_id, int $position_id): void
226  {
227  if (!$ref_id) {
228  throw new ilException('$ref_id cannot be null');
229  }
230  if (!$position_id) {
231  throw new ilException('$position_id cannot be null');
232  }
233  }
234 }
$context
Definition: webdav.php:29
static hasLocalSet(int $ref_id, int $position_id)
static getTemplateSetForContextName(string $context_name, string $position_id, bool $editable=false)
static where($where, $operator=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:67
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getAllTemplateSetsForAllActivedContexts(int $position_id, bool $editable=false)
static assertRefIdAndPositionIdIsNotNull(int $ref_id, int $position_id)