ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilDidacticTemplateImport.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once './Services/DidacticTemplate/exceptions/class.ilDidacticTemplateImportException.php';
5
13{
14 const IMPORT_FILE = 1;
15
16 private $type = 0;
17 private $xmlfile = '';
18
22 private $logger = null;
23
27 protected $objDefinition;
28
29
34 public function __construct($a_type)
35 {
36 global $DIC;
37
38 $this->logger = $DIC->logger()->otpl();
39 $this->type = $a_type;
40 $this->objDefinition = $DIC['objDefinition'];
41 }
42
47 public function setInputFile($a_file)
48 {
49 $this->xmlfile = $a_file;
50 }
51
56 public function getInputFile()
57 {
58 return $this->xmlfile;
59 }
60
65 public function getInputType()
66 {
67 return $this->type;
68 }
69
73 public function import($a_dtpl_id = 0)
74 {
75 libxml_use_internal_errors(true);
76
77 switch ($this->getInputType()) {
79
80 $root = simplexml_load_file($this->getInputFile());
81 if ($root == false) {
83 $this->parseXmlErrors()
84 );
85 }
86 break;
87 }
88
89 $settings = $this->parseSettings($root);
90 $this->parseActions($settings, $root->didacticTemplate->actions);
91
92 return $settings;
93 }
94
100 protected function parseSettings(SimpleXMLElement $root)
101 {
102 global $DIC;
103
104 $icon = '';
105
106 $ilSetting = $DIC['ilSetting'];
107 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateSetting.php';
108 $setting = new ilDidacticTemplateSetting();
109
110 foreach ($root->didacticTemplate as $tpl) {
111 switch ((string) $tpl->attributes()->type) {
112 case 'creation':
113 default:
115 break;
116 }
117 $setting->setTitle(trim((string) $tpl->title));
118 $setting->setDescription(trim((string) $tpl->description));
119
120 $icon = (string) $tpl->icon;
121
122 $info = '';
123 foreach ((array) $tpl->info->p as $paragraph) {
124 if (strlen($info)) {
125 $info .= "\n";
126 }
127 $info .= trim((string) $paragraph);
128 }
129 $setting->setInfo($info);
130
131 if (isset($tpl->effectiveFrom) && (string) $tpl->effectiveFrom["nic_id"] == $ilSetting->get('inst_id')) {
132 $node = array();
133 foreach ($tpl->effectiveFrom->node as $element) {
134 $node[] = (int) $element;
135 }
136
137 $setting->setEffectiveFrom($node);
138 }
139
140 if (isset($tpl->exclusive)) {
141 $setting->setExclusive(true);
142 }
143
144 foreach ($tpl->assignments->assignment as $element) {
145 $setting->addAssignment(trim((string) $element));
146 }
147 }
148 $setting->save();
149
150 if (strlen($icon) && $this->canUseIcons($setting)) {
151 $setting->getIconHandler()->writeSvg($icon);
152 }
153
154 include_once("./Services/Multilingualism/classes/class.ilMultilingualism.php");
155 $trans = ilMultilingualism::getInstance($setting->getId(), "dtpl");
156
157 if (isset($root->didacticTemplate->translations)) {
158 $trans->fromXML($root->didacticTemplate->translations);
159 }
160 $trans->save();
161
162 return $setting;
163 }
164
165 protected function canUseIcons(ilDidacticTemplateSetting $setting) : bool
166 {
167 foreach ($setting->getAssignments() as $assignment) {
168 if (!$this->objDefinition->isContainer($assignment)) {
169 return false;
170 }
171 }
172 return true;
173 }
174
181 protected function parseActions(ilDidacticTemplateSetting $set, SimpleXMLElement $actions = null)
182 {
183 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateActionFactory.php';
184
185 if ($actions === null) {
186 return;
187 }
188
190 // Local role action
192 foreach ($actions->localRoleAction as $ele) {
193 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateLocalRoleAction.php';
195 $act->setTemplateId($set->getId());
196
197 foreach ($ele->roleTemplate as $tpl) {
198 // extract role
199 foreach ($tpl->role as $roleDef) {
200 include_once './Services/AccessControl/classes/class.ilRoleXmlImporter.php';
201 $rimporter = new ilRoleXmlImporter(ROLE_FOLDER_ID);
202 $role_id = $rimporter->importSimpleXml($roleDef);
203 $act->setRoleTemplateId($role_id);
204 }
205 $act->save();
206 }
207 }
208
210 // Block role action
212 foreach ($actions->blockRoleAction as $ele) {
213 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateBlockRoleAction.php';
215 $act->setTemplateId($set->getId());
216
217 // Role filter
218 foreach ($ele->roleFilter as $rfi) {
219 switch ((string) $rfi->attributes()->source) {
220 case 'title':
222 break;
223
224 case 'objId':
226 break;
227
228 case 'parentRoles':
230 break;
231 }
232 foreach ($rfi->includePattern as $pat) {
233 // @TODO other subtypes
234 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateIncludeFilterPattern.php';
237 $pattern->setPattern((string) $pat->attributes()->preg);
238 $act->addFilterPattern($pattern);
239 }
240 foreach ($rfi->excludePattern as $pat) {
241 // @TODO other subtypes
242 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateExcludeFilterPattern.php';
245 $pattern->setPattern((string) $pat->attributes()->preg);
246 $act->addFilterPattern($pattern);
247 }
248 }
249
250 $act->save();
251 }
252
253
254
256 // Local policy action
258 foreach ($actions->localPolicyAction as $ele) {
259 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateLocalPolicyAction.php';
261 $act->setTemplateId($set->getId());
262
263 // Role filter
264 foreach ($ele->roleFilter as $rfi) {
265 $this->logger->dump($rfi->attributes(), \ilLogLevel::DEBUG);
266 $this->logger->debug('Current filter source: ' . (string) $rfi->attributes()->source);
267
268 switch ((string) $rfi->attributes()->source) {
269 case 'title':
271 break;
272
273 case 'objId':
275 break;
276
277 case 'parentRoles':
279 break;
280
281 case 'localRoles':
283 break;
284 }
285 foreach ($rfi->includePattern as $pat) {
286 // @TODO other subtypes
287 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateIncludeFilterPattern.php';
290 $pattern->setPattern((string) $pat->attributes()->preg);
291 $act->addFilterPattern($pattern);
292 }
293 foreach ($rfi->excludePattern as $pat) {
294 // @TODO other subtypes
295 include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateExcludeFilterPattern.php';
298 $pattern->setPattern((string) $pat->attributes()->preg);
299 $act->addFilterPattern($pattern);
300 }
301 }
302
303 // role template assignment
304 foreach ($ele->localPolicyTemplate as $lpo) {
305 switch ((string) $lpo->attributes()->type) {
306 case 'overwrite':
308 break;
309
310 case 'union':
312 break;
313
314 case 'intersect':
316 break;
317 }
318
319 // extract role
320 foreach ($lpo->role as $roleDef) {
321 try {
322 $rimporter = new ilRoleXmlImporter(ROLE_FOLDER_ID);
323 $role_id = $rimporter->importSimpleXml($roleDef);
324 $act->setRoleTemplateId($role_id);
325 } catch (ilRoleImporterException $e) {
326 // delete half-imported template
327 $set->delete();
328 throw new ilDidacticTemplateImportException($e->getMessage());
329 }
330 }
331 }
332
333 // Save action including all filter patterns
334 $act->save();
335 }
336 }
337
343 protected function parseXmlErrors()
344 {
345 $errors = '';
346 foreach (libxml_get_errors() as $err) {
347 $errors .= $err->code . '<br/>';
348 }
349 return $errors;
350 }
351}
An exception for terminatinating execution or to throw for unit testing.
Description of ilDidacticTemplateBlockRoleAction.
Implementation of an include filter pattern for didactic template actions.
Description of ilDidacticTemplateImportException.
Description of ilDidacticTemplateImport.
parseXmlErrors()
Parse xml errors from libxml_get_errors.
parseSettings(SimpleXMLElement $root)
Parse settings.
parseActions(ilDidacticTemplateSetting $set, SimpleXMLElement $actions=null)
Parse template action from xml.
canUseIcons(ilDidacticTemplateSetting $setting)
Implementation of an include filter pattern for didactic template actions.
represents a creation of local roles action
static getInstance($a_obj_id, $a_type)
Get instance.
Description of class.
const ROLE_FOLDER_ID
Definition: constants.php:32
global $DIC
Definition: goto.php:24
$errors
Definition: imgupload.php:49
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
global $ilSetting
Definition: privfeed.php:17