ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilDidacticTemplateBlockRoleAction.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5 
12 {
14  private array $pattern = [];
15  private int $filter_type = self::FILTER_SOURCE_TITLE;
16 
17  public function __construct(int $action_id = 0)
18  {
20  }
21 
22  public function addFilterPattern(ilDidacticTemplateFilterPattern $pattern): void
23  {
24  $this->pattern[] = $pattern;
25  }
26 
31  public function setFilterPatterns(array $patterns): void
32  {
33  $this->pattern = $patterns;
34  }
35 
40  public function getFilterPattern(): array
41  {
42  return $this->pattern;
43  }
44 
45  public function setFilterType(int $a_type): void
46  {
47  $this->filter_type = $a_type;
48  }
49 
50  public function getFilterType(): int
51  {
52  return $this->filter_type;
53  }
54 
55  public function save(): int
56  {
57  parent::save();
58  $query = 'INSERT INTO didactic_tpl_abr (action_id,filter_type) ' .
59  'VALUES( ' .
60  $this->db->quote($this->getActionId(), \ilDBConstants::T_INTEGER) . ', ' .
61  $this->db->quote($this->getFilterType(), \ilDBConstants::T_INTEGER) . ' ' .
62  ')';
63  $this->db->manipulate($query);
64 
65  foreach ($this->getFilterPattern() as $pattern) {
66  /* @var ilDidacticTemplateFilterPattern $pattern */
67  $pattern->setParentId($this->getActionId());
68  $pattern->setParentType(self::PATTERN_PARENT_TYPE);
69  $pattern->save();
70  }
71 
72  return $this->getActionId();
73  }
74 
75  public function delete(): void
76  {
77  parent::delete();
78  $query = 'DELETE FROM didactic_tpl_abr ' .
79  'WHERE action_id = ' . $this->db->quote($this->getActionId(), 'integer');
80  $this->db->manipulate($query);
81 
82  foreach ($this->getFilterPattern() as $pattern) {
83  $pattern->delete();
84  }
85  }
86 
87  public function apply(): bool
88  {
89  $source = $this->initSourceObject();
90  $roles = $this->filterRoles($source);
91 
92  // Create local policy for filtered roles
93  foreach ($roles as $role_id => $role) {
94  $this->blockRole($role_id, $source);
95  }
96 
97  return true;
98  }
99 
100  protected function blockRole(int $a_role_id, ilObject $source): bool
101  {
102  // Set assign to 'y' only if it is a local role
103  $assign = $this->review->isAssignable($a_role_id, $source->getRefId()) ? 'y' : 'n';
104 
105  // Delete permissions
106  $this->admin->revokeSubtreePermissions($source->getRefId(), $a_role_id);
107 
108  // Delete template permissions
109  $this->admin->deleteSubtreeTemplates($source->getRefId(), $a_role_id);
110 
111  $this->admin->assignRoleToFolder(
112  $a_role_id,
113  $source->getRefId(),
114  $assign
115  );
116 
117  return true;
118  }
119 
120  public function revert(): bool
121  {
122  $source = $this->initSourceObject();
123  $roles = $this->filterRoles($source);
124 
125  // Create local policy for filtered roles
126  foreach ($roles as $role_id => $role) {
127  $this->deleteLocalPolicy($role_id, $source);
128  }
129 
130  return true;
131  }
132 
133  protected function deleteLocalPolicy(int $a_role_id, ilObject $source): bool
134  {
135  // Create role folder if it does not exist
136  //$rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
137 
138  if ($this->review->getRoleFolderOfRole($a_role_id) === $source->getRefId()) {
139  $this->logger->debug('Ignoring local role: ' . ilObject::_lookupTitle($a_role_id));
140  return false;
141  }
142 
143  $this->admin->deleteLocalRole($a_role_id, $source->getRefId());
144 
145  // Change existing object
146  $role = new ilObjRole($a_role_id);
147  $role->changeExistingObjects(
148  $source->getRefId(),
150  ['all']
151  );
152  return true;
153  }
154 
155  public function getType(): int
156  {
157  return self::TYPE_BLOCK_ROLE;
158  }
159 
160  public function toXml(ilXmlWriter $writer): void
161  {
162  $writer->xmlStartTag('blockRoleAction');
163 
164  switch ($this->getFilterType()) {
165  case self::FILTER_SOURCE_OBJ_ID:
166  $writer->xmlStartTag('roleFilter', ['source' => 'objId']);
167  break;
168 
169  case self::FILTER_PARENT_ROLES:
170  $writer->xmlStartTag('roleFilter', ['source' => 'parentRoles']);
171  break;
172 
173  case self::FILTER_SOURCE_TITLE:
174  default:
175  $writer->xmlStartTag('roleFilter', ['source' => 'title']);
176  break;
177  }
178 
179  foreach ($this->getFilterPattern() as $pattern) {
180  $pattern->toXml($writer);
181  }
182  $writer->xmlEndTag('roleFilter');
183  $writer->xmlEndTag('blockRoleAction');
184  }
185 
186  public function __clone()
187  {
188  parent::__clone();
189 
190  $clones = [];
191  foreach ($this->getFilterPattern() as $pattern) {
192  $clones[] = clone $pattern;
193  }
194  $this->setFilterPatterns($clones);
195  }
196 
197  public function read(): void
198  {
199  parent::read();
200  $query = 'SELECT * FROM didactic_tpl_abr ' .
201  'WHERE action_id = ' . $this->db->quote($this->getActionId(), ilDBConstants::T_INTEGER);
202  $res = $this->db->query($query);
203  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
204  $this->setFilterType((int) $row->filter_type);
205  }
206 
207  // Read filter
209  $this->getActionId(),
210  self::PATTERN_PARENT_TYPE
211  ) as $pattern) {
212  $this->addFilterPattern($pattern);
213  }
214  }
215 }
Class ilObjRole.
$res
Definition: ltiservices.php:69
Description of ilDidacticTemplateBlockRoleAction.
addFilterPattern(ilDidacticTemplateFilterPattern $pattern)
setFilterPatterns(array $patterns)
Set filter patterns.
xmlEndTag(string $tag)
Writes an endtag.
static _lookupTitle(int $obj_id)
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.