ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDidacticTemplateBlockRoleAction.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateAction.php';
5 
13 {
16 
17  const PATTERN_PARENT_TYPE = 'action';
18 
19 
20  private $pattern = array();
22 
27  public function __construct($action_id = 0)
28  {
30  }
31 
37  {
38  $this->pattern[] = $pattern;
39  }
40 
45  public function setFilterPatterns(Array $patterns)
46  {
47  $this->pattern = $patterns;
48  }
49 
54  public function getFilterPattern()
55  {
56  return $this->pattern;
57  }
58 
63  public function setFilterType($a_type)
64  {
65  $this->filter_type = $a_type;
66  }
67 
72  public function getFilterType()
73  {
74  return $this->filter_type;
75  }
76 
80  public function save()
81  {
82  global $ilDB;
83 
84  parent::save();
85 
86  $query = 'INSERT INTO didactic_tpl_abr (action_id,filter_type) '.
87  'VALUES( '.
88  $ilDB->quote($this->getActionId(),'integer').', '.
89  $ilDB->quote($this->getFilterType(),'integer').' '.
90  ')';
91  $ilDB->manipulate($query);
92 
93  foreach($this->getFilterPattern() as $pattern)
94  {
95  /* @var ilDidacticTemplateFilterPattern $pattern */
96  $pattern->setParentId($this->getActionId());
97  $pattern->setParentType(self::PATTERN_PARENT_TYPE);
98  $pattern->save();
99  }
100  }
101 
107  public function delete()
108  {
109  global $ilDB;
110 
111  parent::delete();
112 
113  $query = 'DELETE FROM didactic_tpl_abr '.
114  'WHERE action_id = '.$ilDB->quote($this->getActionId(),'integer');
115  $ilDB->manipulate($query);
116 
117  foreach($this->getFilterPattern() as $pattern)
118  {
119  $pattern->delete();
120  }
121  return true;
122  }
123 
124 
125 
126 
130  public function apply()
131  {
132  global $rbacreview;
133 
134  $source = $this->initSourceObject();
135  $roles = $this->filterRoles($source);
136 
137  $rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
138 
139  // Create local policy for filtered roles
140  foreach($roles as $role_id => $role)
141  {
142  // Do not block protected roles
143  if($role['protected'])
144  {
145  continue;
146  }
147  // Do not block local policies
148  if($role['parent'] == $rolf)
149  {
150  continue;
151  }
152  $this->blockRole($role_id, $source);
153  }
154  return true;
155  }
156 
162  protected function blockRole($a_role_id, $source)
163  {
164  global $rbacadmin, $rbacreview;
165 
166  // Create role folder if it does not exist
167  $rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
168  if(!$rolf)
169  {
170  $rolf_obj = $source->createRoleFolder();
171  $rolf = $rolf_obj->getRefId();
172  }
173 
174  // Set assign to 'y' only if it is a local role
175  $assign = $rbacreview->isAssignable($a_role_id, $rolf) ? 'y' : 'n';
176 
177  // Delete permissions
178  $rbacadmin->revokeSubtreePermissions($source->getRefId(), $a_role_id);
179 
180  // Delete template permissions
181  $rbacadmin->deleteSubtreeTemplates($source->getRefId(), $a_role_id);
182 
183  $rbacadmin->assignRoleToFolder(
184  $a_role_id,
185  $rolf,
186  $assign
187  );
188  return true;
189  }
190 
194  public function revert()
195  {
196  global $rbacreview;
197 
198  $source = $this->initSourceObject();
199  $roles = $this->filterRoles($source);
200 
201  $rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
202 
203  // Create local policy for filtered roles
204  foreach($roles as $role_id => $role)
205  {
206  // Do not delete policies of local roles
207  if($rbacreview->isLocalRole($role['obj_id'],$role['parent']))
208  {
209  continue;
210  }
211 
212  $this->deleteLocalPolicy($role, $source);
213  }
214  return true;
215  }
216 
223  protected function deleteLocalPolicy($a_role, $source)
224  {
225  global $rbacreview, $rbacadmin;
226 
227  // Create role folder if it does not exist
228  $rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
229  if(!$rolf)
230  {
231  return false;
232  }
233 
234  $rbacadmin->deleteLocalRole($a_role['rol_id'], $rolf);
235 
236  // Change existing object
237  include_once './Services/AccessControl/classes/class.ilObjRole.php';
238  $role_obj = new ilObjRole($a_role['obj_id']);
239  $role_obj->changeExistingObjects(
240  $source->getRefId(),
242  array('all')
243  );
244  return true;
245  }
246 
247 
252  public function getType()
253  {
254  return self::TYPE_BLOCK_ROLE;
255  }
256 
262  public function toXml(ilXmlWriter $writer)
263  {
264  $writer->xmlStartTag('blockRoleAction');
265 
266  switch($this->getFilterType())
267  {
268  case self::FILTER_SOURCE_OBJ_ID:
269  $writer->xmlStartTag('roleFilter',array('source' => 'objId'));
270  break;
271 
272  default:
273  case self::FILTER_SOURCE_TITLE:
274  $writer->xmlStartTag('roleFilter',array('source' => 'title'));
275  break;
276  }
277 
278  foreach($this->getFilterPattern() as $pattern)
279  {
280  $pattern->toXml($writer);
281  }
282  $writer->xmlEndTag('roleFilter');
283  $writer->xmlEndTag('blockRoleAction');
284  return;
285  }
286 
290  public function __clone()
291  {
292  parent::__clone();
293 
294  // Clone patterns
295  $cloned = array();
296  foreach($this->getFilterPattern() as $pattern)
297  {
298  $clones[] = clone $pattern;
299  }
300  $this->setFilterPatterns($clones);
301  }
302 
308  public function read()
309  {
310  global $ilDB;
311 
312  if(!parent::read())
313  {
314  return false;
315  }
316 
317  $query = 'SELECT * FROM didactic_tpl_abr '.
318  'WHERE action_id = '.$ilDB->quote($this->getActionId());
319  $res = $ilDB->query($query);
320  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
321  {
322  $this->setFilterType($row->filter_type);
323  }
324 
325  // Read filter
326  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateFilterPatternFactory.php';
327  foreach(ilDidacticTemplateFilterPatternFactory::lookupPatternsByParentId($this->getActionId(),self::PATTERN_PARENT_TYPE) as $pattern)
328  {
329  $this->addFilterPattern($pattern);
330  }
331  }
332 }
333 ?>