ILIAS  trunk Revision v11.0_alpha-1843-g9e1fad99175
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilDidacticTemplateImport.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Export\ImportHandler\Factory 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(StatusType::FAILED)
98  ->withContent($status->content()->builder()->string()
99  ->withString("Invalid import status, import status 'IMPORT_FILE' expected.")));
100  }
101  $import = new ilImportHandlerFactory();
102  $xml_spl_info = new SplFileInfo($this->getInputFile());
103  $xsd_schema_info = $import->schemaFolder()->handler()->getLatest(self::SCHEMA_TYPE);
104  $xml_file_handler = $import->file()->xml()->handler()->withFileInfo($xml_spl_info);
105  $xsd_file_handler = $import->file()->xsd()->handler()->withFileInfo($xsd_schema_info->getFile());
106  return $import->validation()->handler()->validateXMLFile($xml_file_handler, $xsd_file_handler);
107  }
108 
113  {
114  $icon = '';
115  $setting = new ilDidacticTemplateSetting();
116  foreach ($root->didacticTemplate as $tpl) {
117  switch ((string) $tpl->attributes()->type) {
118  case 'creation':
119  default:
120  $setting->setType(ilDidacticTemplateSetting::TYPE_CREATION);
121  break;
122  }
123  $setting->setTitle(trim((string) $tpl->title));
124  $setting->setDescription(trim((string) $tpl->description));
125 
126  $icon = (string) $tpl->icon;
127 
128  $info = '';
129  foreach ((array) $tpl->info->p as $paragraph) {
130  if ($info !== '') {
131  $info .= "\n";
132  }
133  $info .= trim((string) $paragraph);
134  }
135  $setting->setInfo($info);
136 
137  if (isset($tpl->effectiveFrom) && (string) $tpl->effectiveFrom["nic_id"] == $this->settings->get('inst_id')) {
138  $node = array();
139  foreach ($tpl->effectiveFrom->node as $element) {
140  $node[] = (int) $element;
141  }
142 
143  $setting->setEffectiveFrom($node);
144  }
145 
146  if (isset($tpl->exclusive)) {
147  $setting->setExclusive(true);
148  }
149 
150  foreach ($tpl->assignments->assignment as $element) {
151  $setting->addAssignment(trim((string) $element));
152  }
153  }
154  $setting->save();
155 
156  if ($icon !== '' && $this->canUseIcons($setting)) {
157  $setting->getIconHandler()->writeSvg($icon);
158  }
159  $trans = ilMultilingualism::getInstance($setting->getId(), "dtpl");
160  if (isset($root->didacticTemplate->translations)) {
161  $trans->fromXML($root->didacticTemplate->translations);
162  }
163  $trans->save();
164 
165  return $setting;
166  }
167 
168  protected function canUseIcons(ilDidacticTemplateSetting $setting): bool
169  {
170  foreach ($setting->getAssignments() as $assignment) {
171  if (!$this->objDefinition->isContainer($assignment)) {
172  return false;
173  }
174  }
175 
176  return true;
177  }
178 
179  protected function parseLocalRoleAction(
180  ilDidacticTemplateSetting $didactic_template_setting,
181  SimpleXMLElement $local_role_action
182  ): void {
184  $act->setTemplateId($didactic_template_setting->getId());
185 
186  foreach ($local_role_action->roleTemplate as $tpl) {
187  // extract role
188  foreach ($tpl->role as $roleDef) {
189  $rimporter = new ilRoleXmlImporter(ROLE_FOLDER_ID);
190  $role_id = $rimporter->importSimpleXml($roleDef);
191  $act->setRoleTemplateId($role_id);
192  }
193  $act->save();
194  }
195  }
196 
197  protected function parseBlockRoleAction(
198  ilDidacticTemplateSetting $didactic_template_setting,
199  SimpleXMLElement $block_role_action
200  ): void {
202  $act->setTemplateId($didactic_template_setting->getId());
203  // Role filter
204  foreach ($block_role_action->roleFilter as $rfi) {
205  switch ((string) $rfi->attributes()->source) {
206  case 'title':
207  $act->setFilterType(\ilDidacticTemplateAction::FILTER_SOURCE_TITLE);
208  break;
209 
210  case 'objId':
212  break;
213  case 'parentRoles':
214  $act->setFilterType(\ilDidacticTemplateAction::FILTER_PARENT_ROLES);
215  break;
216  }
217  foreach ($rfi->includePattern as $pat) {
218  // @TODO other subtypes
220  $pattern->setPatternSubType(ilDidacticTemplateFilterPattern::PATTERN_SUBTYPE_REGEX);
221  $pattern->setPattern((string) $pat->attributes()->preg);
222  $act->addFilterPattern($pattern);
223  }
224  foreach ($rfi->excludePattern as $pat) {
225  // @TODO other subtypes
227  $pattern->setPatternSubType(ilDidacticTemplateFilterPattern::PATTERN_SUBTYPE_REGEX);
228  $pattern->setPattern((string) $pat->attributes()->preg);
229  $act->addFilterPattern($pattern);
230  }
231  }
232  $act->save();
233  }
234 
235  protected function parseLocalPolicyAction(
236  ilDidacticTemplateSetting $didactic_template_setting,
237  SimpleXMLElement $local_policy_action
238  ): void {
240  $act->setTemplateId($didactic_template_setting->getId());
241  // Role filter
242  foreach ($local_policy_action->roleFilter as $rfi) {
243  $this->logger->dump($rfi->attributes(), \ilLogLevel::DEBUG);
244  $this->logger->debug(
245  'Current filter source: ' . $rfi->attributes()->source
246  );
247  switch ((string) $rfi->attributes()->source) {
248  case 'title':
249  $act->setFilterType(\ilDidacticTemplateAction::FILTER_SOURCE_TITLE);
250  break;
251  case 'objId':
253  break;
254  case 'parentRoles':
255  $act->setFilterType(\ilDidacticTemplateAction::FILTER_PARENT_ROLES);
256  break;
257  case 'localRoles':
258  $act->setFilterType(\ilDidacticTemplateAction::FILTER_LOCAL_ROLES);
259  break;
260  }
261  foreach ($rfi->includePattern as $pat) {
262  // @TODO other subtypes
264  $pattern->setPatternSubType(ilDidacticTemplateFilterPattern::PATTERN_SUBTYPE_REGEX);
265  $pattern->setPattern((string) $pat->attributes()->preg);
266  $act->addFilterPattern($pattern);
267  }
268  foreach ($rfi->excludePattern as $pat) {
269  // @TODO other subtypes
271  $pattern->setPatternSubType(ilDidacticTemplateFilterPattern::PATTERN_SUBTYPE_REGEX);
272  $pattern->setPattern((string) $pat->attributes()->preg);
273  $act->addFilterPattern($pattern);
274  }
275  }
276  // role template assignment
277  foreach ($local_policy_action->localPolicyTemplate as $lpo) {
278  switch ((string) $lpo->attributes()->type) {
279  case 'overwrite':
281  break;
282  case 'union':
284  break;
285  case 'intersect':
287  break;
288  }
289  // extract role
290  foreach ($lpo->role as $roleDef) {
291  try {
292  $rimporter = new ilRoleXmlImporter(ROLE_FOLDER_ID);
293  $role_id = $rimporter->importSimpleXml($roleDef);
294  $act->setRoleTemplateId($role_id);
295  } catch (ilRoleImporterException $e) {
296  // delete half-imported template
297  $didactic_template_setting->delete();
298  throw new ilDidacticTemplateImportException($e->getMessage());
299  }
300  }
301  }
302  // Save action including all filter patterns
303  $act->save();
304  }
305 
309  protected function parseActions(ilDidacticTemplateSetting $set, ?SimpleXMLElement $actions = null): void
310  {
311  if ($actions === null) {
312  return;
313  }
314  foreach ($actions->children() as $action) {
315  if ($action->getName() === self::XML_ELEMENT_NAME_LOCAL_ROLE_ACTION) {
316  $this->parseLocalRoleAction($set, $action);
317  continue;
318  }
319  if ($action->getName() === self::XML_ELEMENT_NAME_BLOCK_ROLE_ACIONE) {
320  $this->parseBlockRoleAction($set, $action);
321  continue;
322  }
323  if ($action->getName() === self::XML_ELEMENT_NAME_LOCAL_POLICY_ACTION) {
324  $this->parseLocalPolicyAction($set, $action);
325  continue;
326  }
327  }
328  }
329 
333  protected function parseXmlErrors(): string
334  {
335  $errors = '';
336  foreach (libxml_get_errors() as $err) {
337  $errors .= $err->code . '<br/>';
338  }
339  return $errors;
340  }
341 }
static getInstance(int $a_obj_id, string $a_type)
Description of ilDidacticTemplateBlockRoleAction.
parseXmlErrors()
Parse xml errors from libxml_get_errors.
parseActions(ilDidacticTemplateSetting $set, ?SimpleXMLElement $actions=null)
Parse template action from xml.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
parseBlockRoleAction(ilDidacticTemplateSetting $didactic_template_setting, SimpleXMLElement $block_role_action)
parseSettings(SimpleXMLElement $root)
Parse settings.
global $DIC
Definition: shib_login.php:22
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.