ILIAS  release_4-3 Revision
 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  $source = $this->initSourceObject();
133  $roles = $this->filterRoles($source);
134 
135  // Create local policy for filtered roles
136  foreach($roles as $role_id => $role)
137  {
138  $this->blockRole($role_id, $source);
139  }
140  return true;
141  }
142 
148  protected function blockRole($a_role_id, $source)
149  {
150  global $rbacadmin, $rbacreview;
151 
152  // Create role folder if it does not exist
153  $rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
154  if(!$rolf)
155  {
156  $rolf_obj = $source->createRoleFolder();
157  $rolf = $rolf_obj->getRefId();
158  }
159 
160  // Set assign to 'y' only if it is a local role
161  $assign = $rbacreview->isAssignable($a_role_id, $rolf) ? 'y' : 'n';
162 
163  // Delete permissions
164  $rbacadmin->revokeSubtreePermissions($source->getRefId(), $a_role_id);
165 
166  // Delete template permissions
167  $rbacadmin->deleteSubtreeTemplates($source->getRefId(), $a_role_id);
168 
169  $rbacadmin->assignRoleToFolder(
170  $a_role_id,
171  $rolf,
172  $assign
173  );
174  return true;
175  }
176 
180  public function revert()
181  {
182  $source = $this->initSourceObject();
183  $roles = $this->filterRoles($source);
184 
185  // Create local policy for filtered roles
186  foreach($roles as $role_id => $role)
187  {
188  $this->deleteLocalPolicy($role_id, $source);
189  }
190  return true;
191  }
192 
199  protected function deleteLocalPolicy($a_role_id, $source)
200  {
201  global $rbacreview, $rbacadmin;
202 
203  // Create role folder if it does not exist
204  $rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
205  if(!$rolf)
206  {
207  return false;
208  }
209 
210  if($rbacreview->getRoleFolderOfRole($a_role_id) == $rolf)
211  {
212  $GLOBALS['ilLog']->write(__METHOD__.': Ignoring local role: '.ilObject::_lookupTitle($a_role_id));
213  return false;
214  }
215 
216  $rbacadmin->deleteLocalRole($a_role_id, $rolf);
217 
218  // Change existing object
219  include_once './Services/AccessControl/classes/class.ilObjRole.php';
220  $role = new ilObjRole($a_role_id);
221  $role->changeExistingObjects(
222  $source->getRefId(),
224  array('all')
225  );
226 
227  return true;
228  }
229 
230 
235  public function getType()
236  {
237  return self::TYPE_BLOCK_ROLE;
238  }
239 
245  public function toXml(ilXmlWriter $writer)
246  {
247  $writer->xmlStartTag('blockRoleAction');
248 
249  switch($this->getFilterType())
250  {
251  case self::FILTER_SOURCE_OBJ_ID:
252  $writer->xmlStartTag('roleFilter',array('source' => 'objId'));
253  break;
254 
255  default:
256  case self::FILTER_SOURCE_TITLE:
257  $writer->xmlStartTag('roleFilter',array('source' => 'title'));
258  break;
259  }
260 
261  foreach($this->getFilterPattern() as $pattern)
262  {
263  $pattern->toXml($writer);
264  }
265  $writer->xmlEndTag('roleFilter');
266  $writer->xmlEndTag('blockRoleAction');
267  return;
268  }
269 
273  public function __clone()
274  {
275  parent::__clone();
276 
277  // Clone patterns
278  $cloned = array();
279  foreach($this->getFilterPattern() as $pattern)
280  {
281  $clones[] = clone $pattern;
282  }
283  $this->setFilterPatterns($clones);
284  }
285 
291  public function read()
292  {
293  global $ilDB;
294 
295  if(!parent::read())
296  {
297  return false;
298  }
299 
300  $query = 'SELECT * FROM didactic_tpl_abr '.
301  'WHERE action_id = '.$ilDB->quote($this->getActionId());
302  $res = $ilDB->query($query);
303  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
304  {
305  $this->setFilterType($row->filter_type);
306  }
307 
308  // Read filter
309  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateFilterPatternFactory.php';
310  foreach(ilDidacticTemplateFilterPatternFactory::lookupPatternsByParentId($this->getActionId(),self::PATTERN_PARENT_TYPE) as $pattern)
311  {
312  $this->addFilterPattern($pattern);
313  }
314  }
315 }
316 ?>