ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDidacticTemplateImport.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Export\ImportHandler\ilFactory as ilImportHandlerFactory;
22 use ILIAS\Export\ImportStatus\ilFactory as ilImportStatusFactory;
25 
32 {
33  protected const XML_ELEMENT_NAME_LOCAL_ROLE_ACTION = 'localRoleAction';
34  protected const XML_ELEMENT_NAME_BLOCK_ROLE_ACIONE = 'blockRoleAction';
35  protected const XML_ELEMENT_NAME_LOCAL_POLICY_ACTION = 'localPolicyAction';
36  public const IMPORT_FILE = 1;
37  protected const SCHEMA_TYPE = 'otpl';
38 
39  protected int $type = 0;
40  protected string $xmlfile = '';
41  protected ilLogger $logger;
43  protected ilSetting $settings;
44 
45  public function __construct(int $a_type)
46  {
47  global $DIC;
48  $this->logger = $DIC->logger()->otpl();
49  $this->type = $a_type;
50  $this->objDefinition = $DIC['objDefinition'];
51  $this->settings = $DIC->settings();
52  }
53 
54  public function setInputFile(string $a_file): void
55  {
56  $this->xmlfile = $a_file;
57  }
58 
59  public function getInputFile(): string
60  {
61  return $this->xmlfile;
62  }
63 
64  public function getInputType(): int
65  {
66  return $this->type;
67  }
68 
72  public function import(int $a_dtpl_id = 0): ilDidacticTemplateSetting
73  {
74  $root = null;
75  $use_internal_errors = libxml_use_internal_errors(true);
76  switch ($this->getInputType()) {
77  case self::IMPORT_FILE:
78  $root = simplexml_load_string(file_get_contents($this->getInputFile()));
79  break;
80  }
81  libxml_use_internal_errors($use_internal_errors);
82  if (!$root instanceof SimpleXMLElement) {
84  $this->parseXmlErrors()
85  );
86  }
87  $settings = $this->parseSettings($root);
88  $this->parseActions($settings, $root->didacticTemplate->actions);
89  return $settings;
90  }
91 
93  {
94  $status = new ilImportStatusFactory();
95  if ($this->getInputType() !== self::IMPORT_FILE) {
96  return $status->collection()->withAddedStatus($status->handler()
97  ->withType(ImportStatus\StatusType::FAILED)
98  ->withContent($status->content()->builder()->string()
99  ->withString("Invalid import status, import status 'IMPORT_FILE' expected.")));
100  }
101  $schema = new ilXMLSchemaFactory();
102  $import = new ilImportHandlerFactory();
103  $xml_spl_info = new SplFileInfo($this->getInputFile());
104  $xsd_spl_info = $schema->getLatest(self::SCHEMA_TYPE);
105  $xml_file_handler = $import->file()->xml()->withFileInfo($xml_spl_info);
106  $xsd_file_handler = $import->file()->xsd()->withFileInfo($xsd_spl_info);
107  return $import->file()->validation()->handler()->validateXMLFile($xml_file_handler, $xsd_file_handler);
108  }
109 
113  protected function parseSettings(SimpleXMLElement $root): ilDidacticTemplateSetting
114  {
115  $icon = '';
116  $setting = new ilDidacticTemplateSetting();
117  foreach ($root->didacticTemplate as $tpl) {
118  switch ((string) $tpl->attributes()->type) {
119  case 'creation':
120  default:
121  $setting->setType(ilDidacticTemplateSetting::TYPE_CREATION);
122  break;
123  }
124  $setting->setTitle(trim((string) $tpl->title));
125  $setting->setDescription(trim((string) $tpl->description));
126 
127  $icon = (string) $tpl->icon;
128 
129  $info = '';
130  foreach ((array) $tpl->info->p as $paragraph) {
131  if ($info !== '') {
132  $info .= "\n";
133  }
134  $info .= trim((string) $paragraph);
135  }
136  $setting->setInfo($info);
137 
138  if (isset($tpl->effectiveFrom) && (string) $tpl->effectiveFrom["nic_id"] == $this->settings->get('inst_id')) {
139  $node = array();
140  foreach ($tpl->effectiveFrom->node as $element) {
141  $node[] = (int) $element;
142  }
143 
144  $setting->setEffectiveFrom($node);
145  }
146 
147  if (isset($tpl->exclusive)) {
148  $setting->setExclusive(true);
149  }
150 
151  foreach ($tpl->assignments->assignment as $element) {
152  $setting->addAssignment(trim((string) $element));
153  }
154  }
155  $setting->save();
156 
157  if ($icon !== '' && $this->canUseIcons($setting)) {
158  $setting->getIconHandler()->writeSvg($icon);
159  }
160  $trans = ilMultilingualism::getInstance($setting->getId(), "dtpl");
161  if (isset($root->didacticTemplate->translations)) {
162  $trans->fromXML($root->didacticTemplate->translations);
163  }
164  $trans->save();
165 
166  return $setting;
167  }
168 
169  protected function canUseIcons(ilDidacticTemplateSetting $setting): bool
170  {
171  foreach ($setting->getAssignments() as $assignment) {
172  if (!$this->objDefinition->isContainer($assignment)) {
173  return false;
174  }
175  }
176 
177  return true;
178  }
179 
180  protected function parseLocalRoleAction(
181  ilDidacticTemplateSetting $didactic_template_setting,
182  SimpleXMLElement $local_role_action
183  ): void {
185  $act->setTemplateId($didactic_template_setting->getId());
186 
187  foreach ($local_role_action->roleTemplate as $tpl) {
188  // extract role
189  foreach ($tpl->role as $roleDef) {
190  $rimporter = new ilRoleXmlImporter(ROLE_FOLDER_ID);
191  $role_id = $rimporter->importSimpleXml($roleDef);
192  $act->setRoleTemplateId($role_id);
193  }
194  $act->save();
195  }
196  }
197 
198  protected function parseBlockRoleAction(
199  ilDidacticTemplateSetting $didactic_template_setting,
200  SimpleXMLElement $block_role_action
201  ): void {
203  $act->setTemplateId($didactic_template_setting->getId());
204  // Role filter
205  foreach ($block_role_action->roleFilter as $rfi) {
206  switch ((string) $rfi->attributes()->source) {
207  case 'title':
208  $act->setFilterType(\ilDidacticTemplateAction::FILTER_SOURCE_TITLE);
209  break;
210 
211  case 'objId':
213  break;
214  case 'parentRoles':
215  $act->setFilterType(\ilDidacticTemplateAction::FILTER_PARENT_ROLES);
216  break;
217  }
218  foreach ($rfi->includePattern as $pat) {
219  // @TODO other subtypes
221  $pattern->setPatternSubType(ilDidacticTemplateFilterPattern::PATTERN_SUBTYPE_REGEX);
222  $pattern->setPattern((string) $pat->attributes()->preg);
223  $act->addFilterPattern($pattern);
224  }
225  foreach ($rfi->excludePattern as $pat) {
226  // @TODO other subtypes
228  $pattern->setPatternSubType(ilDidacticTemplateFilterPattern::PATTERN_SUBTYPE_REGEX);
229  $pattern->setPattern((string) $pat->attributes()->preg);
230  $act->addFilterPattern($pattern);
231  }
232  }
233  $act->save();
234  }
235 
236  protected function parseLocalPolicyAction(
237  ilDidacticTemplateSetting $didactic_template_setting,
238  SimpleXMLElement $local_policy_action
239  ): void {
241  $act->setTemplateId($didactic_template_setting->getId());
242  // Role filter
243  foreach ($local_policy_action->roleFilter as $rfi) {
244  $this->logger->dump($rfi->attributes(), \ilLogLevel::DEBUG);
245  $this->logger->debug(
246  'Current filter source: ' . $rfi->attributes()->source
247  );
248  switch ((string) $rfi->attributes()->source) {
249  case 'title':
250  $act->setFilterType(\ilDidacticTemplateAction::FILTER_SOURCE_TITLE);
251  break;
252  case 'objId':
254  break;
255  case 'parentRoles':
256  $act->setFilterType(\ilDidacticTemplateAction::FILTER_PARENT_ROLES);
257  break;
258  case 'localRoles':
259  $act->setFilterType(\ilDidacticTemplateAction::FILTER_LOCAL_ROLES);
260  break;
261  }
262  foreach ($rfi->includePattern as $pat) {
263  // @TODO other subtypes
265  $pattern->setPatternSubType(ilDidacticTemplateFilterPattern::PATTERN_SUBTYPE_REGEX);
266  $pattern->setPattern((string) $pat->attributes()->preg);
267  $act->addFilterPattern($pattern);
268  }
269  foreach ($rfi->excludePattern as $pat) {
270  // @TODO other subtypes
272  $pattern->setPatternSubType(ilDidacticTemplateFilterPattern::PATTERN_SUBTYPE_REGEX);
273  $pattern->setPattern((string) $pat->attributes()->preg);
274  $act->addFilterPattern($pattern);
275  }
276  }
277  // role template assignment
278  foreach ($local_policy_action->localPolicyTemplate as $lpo) {
279  switch ((string) $lpo->attributes()->type) {
280  case 'overwrite':
282  break;
283  case 'union':
285  break;
286  case 'intersect':
288  break;
289  }
290  // extract role
291  foreach ($lpo->role as $roleDef) {
292  try {
293  $rimporter = new ilRoleXmlImporter(ROLE_FOLDER_ID);
294  $role_id = $rimporter->importSimpleXml($roleDef);
295  $act->setRoleTemplateId($role_id);
296  } catch (ilRoleImporterException $e) {
297  // delete half-imported template
298  $didactic_template_setting->delete();
299  throw new ilDidacticTemplateImportException($e->getMessage());
300  }
301  }
302  }
303  // Save action including all filter patterns
304  $act->save();
305  }
306 
310  protected function parseActions(ilDidacticTemplateSetting $set, SimpleXMLElement $actions = null): void
311  {
312  if ($actions === null) {
313  return;
314  }
315  foreach ($actions->children() as $action) {
316  if ($action->getName() === self::XML_ELEMENT_NAME_LOCAL_ROLE_ACTION) {
317  $this->parseLocalRoleAction($set, $action);
318  continue;
319  }
320  if ($action->getName() === self::XML_ELEMENT_NAME_BLOCK_ROLE_ACIONE) {
321  $this->parseBlockRoleAction($set, $action);
322  continue;
323  }
324  if ($action->getName() === self::XML_ELEMENT_NAME_LOCAL_POLICY_ACTION) {
325  $this->parseLocalPolicyAction($set, $action);
326  continue;
327  }
328  }
329  }
330 
334  protected function parseXmlErrors(): string
335  {
336  $errors = '';
337  foreach (libxml_get_errors() as $err) {
338  $errors .= $err->code . '<br/>';
339  }
340  return $errors;
341  }
342 }
static getInstance(int $a_obj_id, string $a_type)
Description of ilDidacticTemplateBlockRoleAction.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
parseXmlErrors()
Parse xml errors from libxml_get_errors.
global $DIC
Definition: feed.php:28
parseBlockRoleAction(ilDidacticTemplateSetting $didactic_template_setting, SimpleXMLElement $block_role_action)
parses the objects.xml it handles the xml-description of all ilias objects
parseSettings(SimpleXMLElement $root)
Parse settings.
parseActions(ilDidacticTemplateSetting $set, SimpleXMLElement $actions=null)
Parse template action from xml.
Implementation of an include filter pattern for didactic template actions.
Description of ilDidacticTemplateImportException.
Description of ilDidacticTemplateImport.
const ROLE_FOLDER_ID
Definition: constants.php:34
parseLocalRoleAction(ilDidacticTemplateSetting $didactic_template_setting, SimpleXMLElement $local_role_action)
represents a creation of local roles action
parseLocalPolicyAction(ilDidacticTemplateSetting $didactic_template_setting, SimpleXMLElement $local_policy_action)
Description of class.
canUseIcons(ilDidacticTemplateSetting $setting)
Implementation of an include filter pattern for didactic template actions.