ILIAS  release_8 Revision v8.24
class.ilDidacticTemplateImport.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5
12{
13 public const IMPORT_FILE = 1;
14
15 private int $type = 0;
16 private string $xmlfile = '';
17
21
22 public function __construct(int $a_type)
23 {
24 global $DIC;
25
26 $this->logger = $DIC->logger()->otpl();
27 $this->type = $a_type;
28 $this->objDefinition = $DIC['objDefinition'];
29 $this->settings = $DIC->settings();
30 }
31
32 public function setInputFile(string $a_file): void
33 {
34 $this->xmlfile = $a_file;
35 }
36
37 public function getInputFile(): string
38 {
39 return $this->xmlfile;
40 }
41
42 public function getInputType(): int
43 {
44 return $this->type;
45 }
46
50 public function import(int $a_dtpl_id = 0): ilDidacticTemplateSetting
51 {
52 $root = null;
53 $use_internal_errors = libxml_use_internal_errors(true);
54 switch ($this->getInputType()) {
56 $root = simplexml_load_string(file_get_contents($this->getInputFile()));
57 break;
58 }
59 libxml_use_internal_errors($use_internal_errors);
60 if (!$root instanceof SimpleXMLElement) {
62 $this->parseXmlErrors()
63 );
64 }
65 $settings = $this->parseSettings($root);
66 $this->parseActions($settings, $root->didacticTemplate->actions);
67 return $settings;
68 }
69
73 protected function parseSettings(SimpleXMLElement $root): ilDidacticTemplateSetting
74 {
75 $icon = '';
76 $setting = new ilDidacticTemplateSetting();
77 foreach ($root->didacticTemplate as $tpl) {
78 switch ((string) $tpl->attributes()->type) {
79 case 'creation':
80 default:
82 break;
83 }
84 $setting->setTitle(trim((string) $tpl->title));
85 $setting->setDescription(trim((string) $tpl->description));
86
87 $icon = (string) $tpl->icon;
88
89 $info = '';
90 foreach ((array) $tpl->info->p as $paragraph) {
91 if ($info !== '') {
92 $info .= "\n";
93 }
94 $info .= trim((string) $paragraph);
95 }
96 $setting->setInfo($info);
97
98 if (isset($tpl->effectiveFrom) && (string) $tpl->effectiveFrom["nic_id"] == $this->settings->get('inst_id')) {
99 $node = array();
100 foreach ($tpl->effectiveFrom->node as $element) {
101 $node[] = (int) $element;
102 }
103
104 $setting->setEffectiveFrom($node);
105 }
106
107 if (isset($tpl->exclusive)) {
108 $setting->setExclusive(true);
109 }
110
111 foreach ($tpl->assignments->assignment as $element) {
112 $setting->addAssignment(trim((string) $element));
113 }
114 }
115 $setting->save();
116
117 if ($icon !== '' && $this->canUseIcons($setting)) {
118 $setting->getIconHandler()->writeSvg($icon);
119 }
120 $trans = ilMultilingualism::getInstance($setting->getId(), "dtpl");
121 if (isset($root->didacticTemplate->translations)) {
122 $trans->fromXML($root->didacticTemplate->translations);
123 }
124 $trans->save();
125
126 return $setting;
127 }
128
129 protected function canUseIcons(ilDidacticTemplateSetting $setting): bool
130 {
131 foreach ($setting->getAssignments() as $assignment) {
132 if (!$this->objDefinition->isContainer($assignment)) {
133 return false;
134 }
135 }
136
137 return true;
138 }
139
143 protected function parseActions(ilDidacticTemplateSetting $set, SimpleXMLElement $actions = null): void
144 {
145 if ($actions === null) {
146 return;
147 }
149 // Local role action
151 foreach ($actions->localRoleAction as $ele) {
153 $act->setTemplateId($set->getId());
154
155 foreach ($ele->roleTemplate as $tpl) {
156 // extract role
157 foreach ($tpl->role as $roleDef) {
158 $rimporter = new ilRoleXmlImporter(ROLE_FOLDER_ID);
159 $role_id = $rimporter->importSimpleXml($roleDef);
160 $act->setRoleTemplateId($role_id);
161 }
162 $act->save();
163 }
164 }
165
167 // Block role action
169 foreach ($actions->blockRoleAction as $ele) {
171 $act->setTemplateId($set->getId());
172
173 // Role filter
174 foreach ($ele->roleFilter as $rfi) {
175 switch ((string) $rfi->attributes()->source) {
176 case 'title':
178 break;
179
180 case 'objId':
182 break;
183
184 case 'parentRoles':
186 break;
187 }
188 foreach ($rfi->includePattern as $pat) {
189 // @TODO other subtypes
190
193 $pattern->setPattern((string) $pat->attributes()->preg);
194 $act->addFilterPattern($pattern);
195 }
196 foreach ($rfi->excludePattern as $pat) {
197 // @TODO other subtypes
198
201 $pattern->setPattern((string) $pat->attributes()->preg);
202 $act->addFilterPattern($pattern);
203 }
204 }
205
206 $act->save();
207 }
208
210 // Local policy action
212 foreach ($actions->localPolicyAction as $ele) {
214 $act->setTemplateId($set->getId());
215
216 // Role filter
217 foreach ($ele->roleFilter as $rfi) {
218 $this->logger->dump($rfi->attributes(), \ilLogLevel::DEBUG);
219 $this->logger->debug(
220 'Current filter source: ' . $rfi->attributes()->source
221 );
222
223 switch ((string) $rfi->attributes()->source) {
224 case 'title':
226 break;
227
228 case 'objId':
230 break;
231
232 case 'parentRoles':
234 break;
235
236 case 'localRoles':
238 break;
239 }
240 foreach ($rfi->includePattern as $pat) {
241 // @TODO other subtypes
242
245 $pattern->setPattern((string) $pat->attributes()->preg);
246 $act->addFilterPattern($pattern);
247 }
248 foreach ($rfi->excludePattern as $pat) {
249 // @TODO other subtypes
250
253 $pattern->setPattern((string) $pat->attributes()->preg);
254 $act->addFilterPattern($pattern);
255 }
256 }
257
258 // role template assignment
259 foreach ($ele->localPolicyTemplate as $lpo) {
260 switch ((string) $lpo->attributes()->type) {
261 case 'overwrite':
263 break;
264
265 case 'union':
267 break;
268
269 case 'intersect':
271 break;
272 }
273
274 // extract role
275 foreach ($lpo->role as $roleDef) {
276 try {
277 $rimporter = new ilRoleXmlImporter(ROLE_FOLDER_ID);
278 $role_id = $rimporter->importSimpleXml($roleDef);
279 $act->setRoleTemplateId($role_id);
280 } catch (ilRoleImporterException $e) {
281 // delete half-imported template
282 $set->delete();
283 throw new ilDidacticTemplateImportException($e->getMessage());
284 }
285 }
286 }
287
288 // Save action including all filter patterns
289 $act->save();
290 }
291 }
292
296 protected function parseXmlErrors(): string
297 {
298 $errors = '';
299 foreach (libxml_get_errors() as $err) {
300 $errors .= $err->code . '<br/>';
301 }
302 return $errors;
303 }
304}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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
Component logger with individual log levels by component id.
static getInstance(int $a_obj_id, string $a_type)
parses the objects.xml it handles the xml-description of all ilias objects
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Description of class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const ROLE_FOLDER_ID
Definition: constants.php:34
global $DIC
Definition: feed.php:28
$errors
Definition: imgupload.php:65
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41