ILIAS  release_8 Revision v8.24
class.ilDidacticTemplateLocalPolicyAction.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5
12{
13 public const TPL_ACTION_OVERWRITE = 1;
14 public const TPL_ACTION_INTERSECT = 2;
15 public const TPL_ACTION_ADD = 3;
16 public const TPL_ACTION_SUBTRACT = 4;
17 public const TPL_ACTION_UNION = 5;
18
20 private array $pattern = [];
23 private int $role_template_id = 0;
24
25 public function __construct(int $action_id = 0)
26 {
28 }
29
31 {
32 $this->pattern[] = $pattern;
33 }
34
39 public function setFilterPatterns(array $patterns): void
40 {
41 $this->pattern = $patterns;
42 }
43
48 public function getFilterPattern(): array
49 {
50 return $this->pattern;
51 }
52
53 public function setFilterType(int $a_type): void
54 {
55 $this->filter_type = $a_type;
56 }
57
58 public function getFilterType(): int
59 {
60 return $this->filter_type;
61 }
62
63 public function setRoleTemplateType(int $a_tpl_type): void
64 {
65 $this->role_template_type = $a_tpl_type;
66 }
67
68 public function getRoleTemplateType(): int
69 {
71 }
72
73 public function setRoleTemplateId(int $a_id): void
74 {
75 $this->role_template_id = $a_id;
76 }
77
78 public function getRoleTemplateId(): int
79 {
81 }
82
83 public function save(): int
84 {
85 if (!parent::save()) {
86 return 0;
87 }
88
89 $query = 'INSERT INTO didactic_tpl_alp (action_id,filter_type,template_type,template_id) ' .
90 'VALUES( ' .
91 $this->db->quote($this->getActionId(), 'integer') . ', ' .
92 $this->db->quote($this->getFilterType(), 'integer') . ', ' .
93 $this->db->quote($this->getRoleTemplateType(), 'integer') . ', ' .
94 $this->db->quote($this->getRoleTemplateId(), 'integer') . ' ' .
95 ')';
96 $this->db->manipulate($query);
97
98 foreach ($this->getFilterPattern() as $pattern) {
99 /* @var ilDidacticTemplateFilterPattern $pattern */
100 $pattern->setParentId($this->getActionId());
101 $pattern->setParentType(self::PATTERN_PARENT_TYPE);
102 $pattern->save();
103 }
104
105 return $this->getActionId();
106 }
107
108 public function delete(): void
109 {
110 parent::delete();
111 $query = 'DELETE FROM didactic_tpl_alp ' .
112 'WHERE action_id = ' . $this->db->quote($this->getActionId(), 'integer');
113 $this->db->manipulate($query);
114
115 foreach ($this->getFilterPattern() as $pattern) {
116 $pattern->delete();
117 }
118 }
119
120 public function apply(): bool
121 {
122 $source = $this->initSourceObject();
123 // Create a role folder for the new local policies
124 $roles = $this->filterRoles($source);
125
126 // Create local policy for filtered roles
127 foreach ($roles as $role_id => $role) {
128 $this->getLogger()->debug('Apply to role: ' . $role['title']);
129 $this->getLogger()->debug('Role parent: ' . $role['parent']);
130 $this->getLogger()->debug('Source ref_id: ' . $source->getRefId());
131
132 // No local policies for protected roles of higher context
133 if (
134 (int) $role['parent'] !== $source->getRefId() &&
135 $this->review->isProtected($role['parent'], $role_id)
136 ) {
137 $this->getLogger()->debug('Ignoring protected role.');
138 continue;
139 }
140 $this->createLocalPolicy($source, $role);
141 }
142
143 return true;
144 }
145
146 public function revert(): bool
147 {
148 $source = $this->initSourceObject();
149 $roles = $this->filterRoles($source);
150
151 // Delete local policy for filtered roles
152 foreach ($roles as $role_id => $role) {
153 // Do not delete local policies of auto generated roles
154 if (!$this->review->isGlobalRole($role['obj_id']) &&
155 $this->review->isAssignable($role['obj_id'], $source->getRefId()) &&
156 $this->review->isSystemGeneratedRole($role['obj_id'])) {
157 $this->getLogger()->debug('Reverting local policy of auto generated role: ' . $role['title']);
158 $this->revertLocalPolicy($source, $role);
159 } else {
160 $this->getLogger()->debug('Reverting local policy and deleting local role: ' . $role['title']);
161
162 // delete local role and change exiting objects
163 $this->admin->deleteLocalRole($role_id, $source->getRefId());
164
165 // Change existing object
166 $role_obj = new ilObjRole($role_id);
167
168 $protected = $this->review->isProtected($role['parent'], $role['rol_id']);
169
170 $role_obj->changeExistingObjects(
171 $source->getRefId(),
172 $protected ?
175 ['all']
176 );
177 }
178 }
179
180 return true;
181 }
182
183 public function getType(): int
184 {
186 }
187
188 public function toXml(ilXmlWriter $writer): void
189 {
190 $writer->xmlStartTag('localPolicyAction');
191
192 switch ($this->getFilterType()) {
193
195 $writer->xmlStartTag('roleFilter', ['source' => 'objId']);
196 break;
197
199 $writer->xmlStartTag('roleFilter', ['source' => 'parentRoles']);
200 break;
201
203 $writer->xmlStartTag('roleFilter', ['source' => 'localRoles']);
204 break;
205
207 default:
208 $writer->xmlStartTag('roleFilter', ['source' => 'title']);
209 break;
210 }
211
212 foreach ($this->getFilterPattern() as $pattern) {
213 $pattern->toXml($writer);
214 }
215 $writer->xmlEndTag('roleFilter');
216
217 $il_role_id = 'il_' . IL_INST_ID . '_' . ilObject::_lookupType($this->getRoleTemplateId()) . '_' . $this->getRoleTemplateId();
218
219 switch ($this->getRoleTemplateType()) {
221 $writer->xmlStartTag(
222 'localPolicyTemplate',
223 [
224 'type' => 'overwrite',
225 'id' => $il_role_id
226 ]
227 );
228 break;
229
231 $writer->xmlStartTag(
232 'localPolicyTemplate',
233 [
234 'type' => 'intersect',
235 'id' => $il_role_id
236 ]
237 );
238 break;
239
241 $writer->xmlStartTag(
242 'localPolicyTemplate',
243 [
244 'type' => 'union',
245 'id' => $il_role_id
246 ]
247 );
248 break;
249 }
250
251 $exp = new ilRoleXmlExport();
252 $exp->setMode(ilRoleXmlExport::MODE_DTPL);
253 $exp->addRole($this->getRoleTemplateId(), ROLE_FOLDER_ID);
254 $exp->write();
255 $writer->appendXML($exp->xmlDumpMem(false));
256 $writer->xmlEndTag('localPolicyTemplate');
257 $writer->xmlEndTag('localPolicyAction');
258 }
259
260 public function __clone()
261 {
262 parent::__clone();
263
264 $clones = [];
265 foreach ($this->getFilterPattern() as $pattern) {
266 $clones[] = clone $pattern;
267 }
268 $this->setFilterPatterns($clones);
269 }
270
271 public function read(): void
272 {
273 parent::read();
274 $query = 'SELECT * FROM didactic_tpl_alp ' .
275 'WHERE action_id = ' . $this->db->quote($this->getActionId(), ilDBConstants::T_INTEGER);
276 $res = $this->db->query($query);
277 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
278 $this->setFilterType((int) $row->filter_type);
279 $this->setRoleTemplateType((int) $row->template_type);
280 $this->setRoleTemplateId((int) $row->template_id);
281 }
282 // Read filter
284 $this->getActionId(),
285 self::PATTERN_PARENT_TYPE
286 ) as $pattern) {
287 $this->addFilterPattern($pattern);
288 }
289 }
290
291 protected function createLocalPolicy(ilObject $source, array $role): bool
292 {
293 // fetch role information
294 $role_data = [];
295 foreach ($this->review->getParentRoleIds($source->getRefId()) as $role_id => $tmp_role) {
296 if ((int) $role_id === (int) $role['obj_id']) {
297 $role_data = $tmp_role;
298 }
299 }
300
301 // Add local policy
302 if (!$this->review->isRoleAssignedToObject((int) $role['obj_id'], $source->getRefId())) {
303 $this->admin->assignRoleToFolder(
304 (int) $role['obj_id'],
305 $source->getRefId(),
306 'n'
307 );
308 }
309
310 // do nothing if role is protected in higher context
311 if (
312 (int) $role['parent'] !== $source->getRefId() &&
313 $this->review->isProtected($source->getRefId(), (int) $role['obj_id'])
314 ) {
315 $this->getLogger()->info('Ignoring protected role: ' . $role['title']);
316 return false;
317 }
318
319 switch ($this->getRoleTemplateType()) {
321
322 $this->logger->info('Using ilRbacAdmin::copyRolePermissionUnion()');
323 $this->admin->copyRolePermissionUnion(
324 (int) $role_data['obj_id'],
325 (int) $role_data['parent'],
326 $this->getRoleTemplateId(),
328 (int) $role_data['obj_id'],
329 $source->getRefId()
330 );
331 break;
332
334
335 $this->logger->info('Using ilRbacAdmin::copyRoleTemplatePermission()');
336 $this->admin->copyRoleTemplatePermissions(
337 $this->getRoleTemplateId(),
339 $source->getRefId(),
340 (int) $role_data['obj_id'],
341 true
342 );
343 break;
344
346
347 $this->logger->info('Using ilRbacAdmin::copyRolePermissionIntersection()' . $this->getRoleTemplateId());
348 $this->admin->copyRolePermissionIntersection(
349 (int) $role_data['obj_id'],
350 (int) $role_data['parent'],
351 $this->getRoleTemplateId(),
353 $source->getRefId(),
354 (int) $role_data['obj_id']
355 );
356 break;
357
358 }
359 // Change existing object
360 $role_obj = new ilObjRole((int) $role_data['obj_id']);
361 $role_obj->changeExistingObjects(
362 $source->getRefId(),
364 ['all']
365 );
366
367 return true;
368 }
369
370 protected function revertLocalPolicy(ilObject $source, array $role): bool
371 {
372 $this->logger->info('Reverting policy for role ' . $role['title']);
373 // Local policies can only be reverted for auto generated roles. Otherwise the
374 // original role settings are unknown
375 if (strpos($role['title'], 'il_') !== 0) {
376 $this->logger->warning('Cannot revert local policy for role ' . $role['title']);
377 return false;
378 }
379
380 // No local policies
381 if (!$this->review->getLocalPolicies($source->getRefId())) {
382 return false;
383 }
384
385 $exploded_title = explode('_', $role['title']);
386 $rolt_title = $exploded_title[0] . '_' . $exploded_title[1] . '_' . $exploded_title[2];
387
388 // Lookup role template
389 $query = 'SELECT obj_id FROM object_data ' .
390 'WHERE title = ' . $this->db->quote($rolt_title, 'text') . ' ' .
391 'AND type = ' . $this->db->quote('rolt', 'text');
392 $res = $this->db->query($query);
393 $rolt_id = 0;
394 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
395 $rolt_id = (int) $row->obj_id;
396 }
397
398 // No template found
399 if (!$rolt_id) {
400 return false;
401 }
402
403 $this->admin->copyRoleTemplatePermissions(
404 $rolt_id,
406 $source->getRefId(),
407 (int) $role['obj_id'],
408 true
409 );
410
411 // Change existing object
412 $role_obj = new ilObjRole((int) $role['obj_id']);
413 $role_obj->changeExistingObjects(
414 $source->getRefId(),
416 ['all']
417 );
418
419 return true;
420 }
421}
Abstract class for template actions.
static lookupPatternsByParentId(int $a_parent_id, string $a_parent_type)
Represents a filter pattern for didactic template actions.
revert()
Implement everthing that is necessary to revert a didactic template return bool.
save()
Write action to db Overwrite for filling additional db fields.
addFilterPattern(ilDidacticTemplateFilterPattern $pattern)
Class ilObjRole.
const MODE_PROTECTED_DELETE_LOCAL_POLICIES
const MODE_UNPROTECTED_DELETE_LOCAL_POLICIES
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
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...
xmlEndTag(string $tag)
Writes an endtag.
appendXML(string $a_str)
append xml string to document
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
const IL_INST_ID
Definition: constants.php:40
const ROLE_FOLDER_ID
Definition: constants.php:34
$res
Definition: ltiservices.php:69
$source
Definition: metadata.php:93
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$query