ILIAS  release_8 Revision v8.23
class.ilDidacticTemplateBlockRoleAction.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
27 {
29  private array $pattern = [];
30  private int $filter_type = self::FILTER_SOURCE_TITLE;
31 
32  public function __construct(int $action_id = 0)
33  {
35  }
36 
37  public function addFilterPattern(ilDidacticTemplateFilterPattern $pattern): void
38  {
39  $this->pattern[] = $pattern;
40  }
41 
46  public function setFilterPatterns(array $patterns): void
47  {
48  $this->pattern = $patterns;
49  }
50 
55  public function getFilterPattern(): array
56  {
57  return $this->pattern;
58  }
59 
60  public function setFilterType(int $a_type): void
61  {
62  $this->filter_type = $a_type;
63  }
64 
65  public function getFilterType(): int
66  {
67  return $this->filter_type;
68  }
69 
70  public function save(): int
71  {
72  parent::save();
73  $query = 'INSERT INTO didactic_tpl_abr (action_id,filter_type) ' .
74  'VALUES( ' .
75  $this->db->quote($this->getActionId(), \ilDBConstants::T_INTEGER) . ', ' .
76  $this->db->quote($this->getFilterType(), \ilDBConstants::T_INTEGER) . ' ' .
77  ')';
78  $this->db->manipulate($query);
79 
80  foreach ($this->getFilterPattern() as $pattern) {
81  /* @var ilDidacticTemplateFilterPattern $pattern */
82  $pattern->setParentId($this->getActionId());
83  $pattern->setParentType(self::PATTERN_PARENT_TYPE);
84  $pattern->save();
85  }
86 
87  return $this->getActionId();
88  }
89 
90  public function delete(): void
91  {
92  parent::delete();
93  $query = 'DELETE FROM didactic_tpl_abr ' .
94  'WHERE action_id = ' . $this->db->quote($this->getActionId(), 'integer');
95  $this->db->manipulate($query);
96 
97  foreach ($this->getFilterPattern() as $pattern) {
98  $pattern->delete();
99  }
100  }
101 
102  public function apply(): bool
103  {
104  $source = $this->initSourceObject();
105  $roles = $this->filterRoles($source);
106 
107  // Create local policy for filtered roles
108  foreach ($roles as $role_id => $role) {
109  $this->blockRole($role_id, $source);
110  }
111 
112  return true;
113  }
114 
115  protected function blockRole(int $a_role_id, ilObject $source): bool
116  {
117  // Set assign to 'y' only if it is a local role
118  $assign = $this->review->isAssignable($a_role_id, $source->getRefId()) ? 'y' : 'n';
119 
120  // Delete permissions
121  $this->admin->revokeSubtreePermissions($source->getRefId(), $a_role_id);
122 
123  // Delete template permissions
124  $this->admin->deleteSubtreeTemplates($source->getRefId(), $a_role_id);
125  if ($a_role_id !== SYSTEM_ROLE_ID) {
126  $this->admin->assignRoleToFolder(
127  $a_role_id,
128  $source->getRefId(),
129  $assign
130  );
131  }
132  return true;
133  }
134 
135  public function revert(): bool
136  {
137  $source = $this->initSourceObject();
138  $roles = $this->filterRoles($source);
139 
140  // Create local policy for filtered roles
141  foreach ($roles as $role_id => $role) {
142  $this->deleteLocalPolicy($role_id, $source);
143  }
144 
145  return true;
146  }
147 
148  protected function deleteLocalPolicy(int $a_role_id, ilObject $source): bool
149  {
150  // Create role folder if it does not exist
151  //$rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
152 
153  if ($this->review->getRoleFolderOfRole($a_role_id) === $source->getRefId()) {
154  $this->logger->debug('Ignoring local role: ' . ilObject::_lookupTitle($a_role_id));
155  return false;
156  }
157 
158  $this->admin->deleteLocalRole($a_role_id, $source->getRefId());
159 
160  // Change existing object
161  $role = new ilObjRole($a_role_id);
162  $role->changeExistingObjects(
163  $source->getRefId(),
165  ['all']
166  );
167  return true;
168  }
169 
170  public function getType(): int
171  {
172  return self::TYPE_BLOCK_ROLE;
173  }
174 
175  public function toXml(ilXmlWriter $writer): void
176  {
177  $writer->xmlStartTag('blockRoleAction');
178 
179  switch ($this->getFilterType()) {
180  case self::FILTER_SOURCE_OBJ_ID:
181  $writer->xmlStartTag('roleFilter', ['source' => 'objId']);
182  break;
183 
184  case self::FILTER_PARENT_ROLES:
185  $writer->xmlStartTag('roleFilter', ['source' => 'parentRoles']);
186  break;
187 
188  case self::FILTER_SOURCE_TITLE:
189  default:
190  $writer->xmlStartTag('roleFilter', ['source' => 'title']);
191  break;
192 
193  }
194 
195  foreach ($this->getFilterPattern() as $pattern) {
196  $pattern->toXml($writer);
197  }
198  $writer->xmlEndTag('roleFilter');
199  $writer->xmlEndTag('blockRoleAction');
200  }
201 
202  public function __clone()
203  {
204  parent::__clone();
205 
206  $clones = [];
207  foreach ($this->getFilterPattern() as $pattern) {
208  $clones[] = clone $pattern;
209  }
210  $this->setFilterPatterns($clones);
211  }
212 
213  public function read(): void
214  {
215  parent::read();
216  $query = 'SELECT * FROM didactic_tpl_abr ' .
217  'WHERE action_id = ' . $this->db->quote($this->getActionId(), ilDBConstants::T_INTEGER);
218  $res = $this->db->query($query);
219  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
220  $this->setFilterType((int) $row->filter_type);
221  }
222 
223  // Read filter
225  $this->getActionId(),
226  self::PATTERN_PARENT_TYPE
227  ) as $pattern) {
228  $this->addFilterPattern($pattern);
229  }
230  }
231 }
Class ilObjRole.
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const SYSTEM_ROLE_ID
Definition: constants.php:29
addFilterPattern(ilDidacticTemplateFilterPattern $pattern)
setFilterPatterns(array $patterns)
Set filter patterns.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlEndTag(string $tag)
Writes an endtag.
static _lookupTitle(int $obj_id)
$query
Abstract class for template actions.
static lookupPatternsByParentId(int $a_parent_id, string $a_parent_type)
const MODE_UNPROTECTED_DELETE_LOCAL_POLICIES
__construct(Container $dic, ilPlugin $plugin)
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
Represents a filter pattern for didactic template actions.
$source
Definition: metadata.php:93