ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilLearningSequenceImporter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  protected ilObjUser $user;
25  protected ilLogger $log;
26  protected ilObject $obj;
27  protected array $data;
28 
29  public function init(): void
30  {
31  global $DIC;
32  $this->user = $DIC["ilUser"];
33  $this->rbac_admin = $DIC["rbacadmin"];
34  $this->log = $DIC["ilLoggerFactory"]->getRootLogger();
35  }
36 
37  public function importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping): void
38  {
39  if ($new_id = $a_mapping->getMapping("Services/Container", "objs", $a_id)) {
40  $this->obj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
41  } else {
42  $this->obj = new ilObjLearningSequence();
43  $this->obj->create();
44  }
45 
46  $parser = new ilLearningSequenceXMLParser($this->obj, $a_xml);
47  $this->data = $parser->start();
48 
49  $a_mapping->addMapping("Modules/LearningSequence", "lso", $a_id, (string) $this->obj->getId());
50  $a_mapping->addMapping(
51  'Services/COPage',
52  'pg',
53  LSOPageType::INTRO->value . ':' . $a_id,
54  LSOPageType::INTRO->value . ':' . (string) $this->obj->getId()
55  );
56  $a_mapping->addMapping(
57  'Services/COPage',
58  'pg',
59  LSOPageType::EXTRO->value . ':' . $a_id,
60  LSOPageType::EXTRO->value . ':' . (string) $this->obj->getId()
61  );
62 
63  }
64 
65  public function finalProcessing(ilImportMapping $a_mapping): void
66  {
67  $this->buildSettings($this->data["settings"]);
68  $this->obj->update();
69 
70  // pages
71  $page_map = $a_mapping->getMappingsOfEntity('Services/COPage', 'pg');
72  foreach ($page_map as $old_pg_id => $new_pg_id) {
73  $parts = explode(':', $old_pg_id);
74  $pg_type = $parts[0];
75  $old_obj_id = $parts[1];
76  $parts = explode(':', $new_pg_id);
77  $new_pg_id = array_pop($parts);
78  $new_obj_id = $this->obj->getId();
79  ilPageObject::_writeParentId($pg_type, (int) $new_pg_id, (int) $new_obj_id);
80  }
81  }
82 
83  public function afterContainerImportProcessing(ilImportMapping $mapping): void
84  {
85  $this->updateRefId($mapping);
86  $this->buildLSItems($this->data["item_data"], $mapping);
87  $this->buildLPSettings($this->data["lp_settings"], $mapping);
88 
89  $roles = $this->obj->getLSRoles();
90  $roles->addLSMember(
91  $this->user->getId(),
92  $roles->getDefaultAdminRole()
93  );
94  }
95 
96  protected function updateRefId(ilImportMapping $mapping): void
97  {
98  $old_ref_id = $this->data["object"]["ref_id"];
99  $new_ref_id = $mapping->getMapping("Services/Container", "refs", $old_ref_id);
100 
101  $this->obj->setRefId((int) $new_ref_id);
102  }
103 
104  protected function buildLSItems(array $ls_data, ilImportMapping $mapping): void
105  {
106  $mapped = [];
107  foreach ($ls_data as $data) {
108  $old_ref_id = $data["ref_id"];
109  $new_ref_id = $mapping->getMapping("Services/Container", "refs", $old_ref_id);
110  $mapped[$new_ref_id] = $data;
111  }
112 
113  $ls_items = $this->obj->getLSItems($this->obj->getRefId());
114  $updated = [];
115  foreach ($ls_items as $item) {
116  $item_ref_id = $item->getRefId();
117  if(array_key_exists($item_ref_id, $mapped)) {
118  $item_data = $mapped[$item_ref_id];
119  $post_condition = new ilLSPostCondition(
120  $item_ref_id,
121  $item_data["condition_type"],
122  $item_data["condition_value"]
123  );
124  $updated[] = $item->withPostCondition($post_condition);
125  }
126  }
127 
128  if($updated) {
129  $this->obj->storeLSItems($updated);
130  }
131  }
132 
133  protected function buildSettings(array $ls_settings): void
134  {
135  $settings = $this->obj->getLSSettings();
137  ->withMembersGallery($ls_settings["members_gallery"] === 'true' ? true : false)
138  ;
139  $this->obj->updateSettings($settings);
140  }
141 
142  protected function buildLPSettings(array $lp_settings, ilImportMapping $mapping): void
143  {
144  $collection = ilLPCollection::getInstanceByMode($this->obj->getId(), (int) $lp_settings["lp_mode"]);
145 
146  $new_ref_ids = array_map(function ($old_ref_id) use ($mapping) {
147  return $mapping->getMapping("Services/Container", "refs", $old_ref_id);
148  }, $lp_settings["lp_item_ref_ids"]);
149 
150  if (!is_null($collection)) {
151  $collection->activateEntries($new_ref_ids);
152  }
153 
154  $settings = new ilLPObjSettings($this->obj->getId());
155  $settings->setMode((int) $lp_settings["lp_mode"]);
156  $settings->insert();
157  }
158 
159  protected function decodeImageData(string $data): string
160  {
161  return base64_decode($data);
162  }
163 
164  protected function getNewImagePath(string $type, string $path): string
165  {
166  $fs = $this->obj->getDI()['db.filesystem'];
167  return $fs->getStoragePathFor(
168  $type,
169  $this->obj->getId(),
170  $fs->getSuffix($path)
171  );
172  }
173 
174  protected function writeToFileSystem($data, string $path): void
175  {
176  file_put_contents($path, $data);
177  }
178 }
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
getNewImagePath(string $type, string $path)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
static _writeParentId(string $a_parent_type, int $a_pg_id, int $a_par_id)
finalProcessing(ilImportMapping $a_mapping)
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
buildLSItems(array $ls_data, ilImportMapping $mapping)
$path
Definition: ltiservices.php:32
global $DIC
Definition: feed.php:28
static getInstanceByMode(int $a_obj_id, int $a_mode)
int $updated
Timestamp for when the object was last updated.
Definition: System.php:158
getMapping(string $a_comp, string $a_entity, string $a_old_id)
getMappingsOfEntity(string $a_comp, string $a_entity)
afterContainerImportProcessing(ilImportMapping $mapping)
A PostCondition does restrict the progression of a user through the learning sequence.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Class ilRbacAdmin Core functions for role based access control.
buildLPSettings(array $lp_settings, ilImportMapping $mapping)
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...