ILIAS  release_8 Revision v8.23
class.ilDidacticTemplateAction.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 {
13  public const TYPE_LOCAL_POLICY = 1;
14  public const TYPE_LOCAL_ROLE = 2;
15  public const TYPE_BLOCK_ROLE = 3;
16 
17  public const FILTER_SOURCE_TITLE = 1;
18  public const FILTER_SOURCE_OBJ_ID = 2;
19  public const FILTER_PARENT_ROLES = 3;
20  public const FILTER_LOCAL_ROLES = 4;
21 
22  public const PATTERN_PARENT_TYPE = 'action';
23 
24  protected ilLogger $logger;
25  protected ilDBInterface $db;
26  protected ilRbacReview $review;
27  protected ilRbacAdmin $admin;
28 
29  private int $action_id = 0;
30  private int $tpl_id = 0;
31  private int $type = 0;
32 
33  private int $ref_id = 0;
34 
35  public function __construct(int $action_id = 0)
36  {
37  global $DIC;
38 
39  $this->logger = $DIC->logger()->otpl();
40  $this->db = $DIC->database();
41  $this->review = $DIC->rbac()->review();
42  $this->admin = $DIC->rbac()->admin();
43 
44  $this->setActionId($action_id);
45  $this->read();
46  }
47 
48  public function getLogger(): ilLogger
49  {
50  return $this->logger;
51  }
52 
53  public function getActionId(): int
54  {
55  return $this->action_id;
56  }
57 
58  public function setActionId(int $a_action_id): void
59  {
60  $this->action_id = $a_action_id;
61  }
62 
63  public function setType(int $a_type_id): void
64  {
65  $this->type = $a_type_id;
66  }
67 
68  public function setTemplateId(int $a_id): void
69  {
70  $this->tpl_id = $a_id;
71  }
72 
73  public function getTemplateId(): int
74  {
75  return $this->tpl_id;
76  }
77 
78  public function setRefId(int $a_ref_id): void
79  {
80  $this->ref_id = $a_ref_id;
81  }
82 
83  public function getRefId(): int
84  {
85  return $this->ref_id;
86  }
87 
93  public function save(): int
94  {
95  if ($this->getActionId()) {
96  return 0;
97  }
98 
99  $this->setActionId($this->db->nextId('didactic_tpl_a'));
100  $query = 'INSERT INTO didactic_tpl_a (id, tpl_id, type_id) ' .
101  'VALUES( ' .
102  $this->db->quote($this->getActionId(), 'integer') . ', ' .
103  $this->db->quote($this->getTemplateId(), 'integer') . ', ' .
104  $this->db->quote($this->getType(), 'integer') .
105  ')';
106  $this->db->manipulate($query);
107 
108  return $this->getActionId();
109  }
110 
115  public function delete(): void
116  {
117  $query = 'DELETE FROM didactic_tpl_a ' .
118  'WHERE id = ' . $this->db->quote($this->getActionId(), 'integer');
119  $this->db->manipulate($query);
120  }
121 
122  public function read(): void
123  {
124  $query = 'SELECT * FROM didactic_tpl_a ' .
125  'WHERE id = ' . $this->db->quote($this->getActionId(), 'integer');
126  $res = $this->db->query($query);
127  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
128  $this->setTemplateId((int) $row->tpl_id);
129  }
130  }
131 
136  abstract public function getType(): int;
137 
142  abstract public function apply(): bool;
143 
148  abstract public function revert(): bool;
149 
150  public function __clone()
151  {
152  $this->setActionId(0);
153  }
154 
155  abstract public function toXml(ilXmlWriter $writer): void;
156 
157  protected function initSourceObject(): ilObject
158  {
159  $s = ilObjectFactory::getInstanceByRefId($this->getRefId(), false);
160  return $s;
161  }
162 
163  protected function filterRoles(ilObject $source): array
164  {
166  $this->getActionId(),
167  self::PATTERN_PARENT_TYPE
168  );
169 
170  $filtered = array();
171  foreach ($this->review->getParentRoleIds($source->getRefId()) as $role_id => $role) {
172  $role_id = (int) $role_id;
173  switch ($this->getFilterType()) {
174  case self::FILTER_PARENT_ROLES:
175  $this->logger->dump($role);
176  if (
177  $role['assign'] === 'y' &&
178  (int) $role['parent'] === $source->getRefId()
179 
180  ) {
181  $this->logger->debug('Excluding local role: ' . $role['title']);
182  break;
183  }
184  foreach ($patterns as $pattern) {
185  if ($pattern->valid(ilObject::_lookupTitle($role_id))) {
186  $this->logger->debug('Role is valid: ' . ilObject::_lookupTitle($role_id));
187  $filtered[$role_id] = $role;
188  }
189  }
190  break;
191 
192  case self::FILTER_LOCAL_ROLES:
193  if (
194  $role['assign'] === 'n' ||
195  (int) $role['parent'] !== $source->getRefId()
196  ) {
197  $this->logger->debug('Excluding non local role' . $role['title']);
198  break;
199  }
200  foreach ($patterns as $pattern) {
201  if ($pattern->valid(ilObject::_lookupTitle($role_id))) {
202  $this->logger->debug('Role is valid ' . ilObject::_lookupTitle($role_id));
203  $filtered[$role_id] = $role;
204  }
205  }
206  break;
207 
208  default:
209  case self::FILTER_SOURCE_TITLE:
210  foreach ($patterns as $pattern) {
211  if ($pattern->valid(ilObject::_lookupTitle($role_id))) {
212  $this->logger->debug('Role is valid: ' . ilObject::_lookupTitle($role_id));
213  $filtered[$role_id] = $role;
214  }
215  }
216  break;
217  }
218  }
219 
220  return $filtered;
221  }
222 }
$res
Definition: ltiservices.php:69
save()
Write action to db Overwrite for filling additional db fields.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
getType()
Get type of template.
static _lookupTitle(int $obj_id)
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
$query
apply()
Apply action.
Abstract class for template actions.
static lookupPatternsByParentId(int $a_parent_id, string $a_parent_type)
Class ilRbacAdmin Core functions for role based access control.
toXml(ilXmlWriter $writer)
revert()
Implement everthing that is necessary to revert a didactic template return bool.
$source
Definition: metadata.php:93