ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilLearningSequenceImporter.php
Go to the documentation of this file.
1<?php
2
19declare(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("components/ILIAS/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("components/ILIAS/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 public function finalProcessing(ilImportMapping $a_mapping): void
65 {
66 $this->buildSettings($this->data["settings"]);
67 $this->obj->update();
68
69 // pages
70 $page_map = $a_mapping->getMappingsOfEntity('Services/COPage', 'pg');
71 foreach ($page_map as $old_pg_id => $new_pg_id) {
72 $parts = explode(':', $old_pg_id);
73 $pg_type = $parts[0];
74 $old_obj_id = $parts[1];
75 $parts = explode(':', $new_pg_id);
76 $new_pg_id = array_pop($parts);
77 $new_obj_id = $this->obj->getId();
78 ilPageObject::_writeParentId($pg_type, (int) $new_pg_id, (int) $new_obj_id);
79 }
80 }
81
82 public function afterContainerImportProcessing(ilImportMapping $mapping): void
83 {
84 $this->updateRefId($mapping);
85 $this->buildLSItems($this->data["item_data"], $mapping);
86 $this->buildLPSettings($this->data["lp_settings"], $mapping);
87
88 $roles = $this->obj->getLSRoles();
89 $roles->addLSMember(
90 $this->user->getId(),
91 $roles->getDefaultAdminRole()
92 );
93 }
94
95 protected function updateRefId(ilImportMapping $mapping): void
96 {
97 $old_ref_id = $this->data["object"]["ref_id"];
98 $new_ref_id = $mapping->getMapping("components/ILIAS/Container", "refs", $old_ref_id);
99
100 $this->obj->setRefId((int) $new_ref_id);
101 }
102
103 protected function buildLSItems(array $ls_data, ilImportMapping $mapping): void
104 {
105 $mapped = [];
106 foreach ($ls_data as $data) {
107 $old_ref_id = $data["ref_id"];
108 $new_ref_id = $mapping->getMapping("components/ILIAS/Container", "refs", $old_ref_id);
109 $mapped[$new_ref_id] = $data;
110 }
111
112 $ls_items = $this->obj->getLSItems();
113 $updated = [];
114 foreach ($ls_items as $item) {
115 $item_ref_id = $item->getRefId();
116 if (array_key_exists($item_ref_id, $mapped)) {
117 $item_data = $mapped[$item_ref_id];
118 $post_condition = new ilLSPostCondition(
119 $item_ref_id,
120 $item_data["condition_type"],
121 $item_data["condition_value"]
122 );
123 $item = $item->withPostCondition($post_condition);
124 if (isset($item_data["position"])) {
125 $item = $item->withOrderNumber((int) $item_data["position"]);
126 }
127 $updated[] = $item;
128 }
129 }
130
131 if ($updated) {
132 $this->obj->storeLSItems($updated);
133 }
134 }
135
136 protected function buildSettings(array $ls_settings): void
137 {
138 $settings = $this->obj->getLSSettings();
139 $settings = $settings
140 ->withMembersGallery($ls_settings["members_gallery"] === 'true' ? true : false)
141 ;
142 $this->obj->updateSettings($settings);
143 }
144
145 protected function buildLPSettings(array $lp_settings, ilImportMapping $mapping): void
146 {
147 $collection = ilLPCollection::getInstanceByMode($this->obj->getId(), (int) $lp_settings["lp_mode"]);
148
149 $new_ref_ids = array_map(function ($old_ref_id) use ($mapping) {
150 return $mapping->getMapping("components/ILIAS/Container", "refs", $old_ref_id);
151 }, $lp_settings["lp_item_ref_ids"]);
152
153 if (!is_null($collection)) {
154 $collection->activateEntries($new_ref_ids);
155 }
156
157 $settings = new ilLPObjSettings($this->obj->getId());
158 $settings->setMode((int) $lp_settings["lp_mode"]);
159 $settings->insert();
160 }
161
162 protected function decodeImageData(string $data): string
163 {
164 return base64_decode($data);
165 }
166
167 protected function getNewImagePath(string $type, string $path): string
168 {
169 $fs = $this->obj->getDI()['db.filesystem'];
170 return $fs->getStoragePathFor(
171 $type,
172 $this->obj->getId(),
173 $fs->getSuffix($path)
174 );
175 }
176
177 protected function writeToFileSystem($data, string $path): void
178 {
179 file_put_contents($path, $data);
180 }
181}
@ EXTRO
Definition: LSOPageType.php:24
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
getMappingsOfEntity(string $a_comp, string $a_entity)
static getInstanceByMode(int $a_obj_id, int $a_mode)
A PostCondition does restrict the progression of a user through the learning sequence.
buildLSItems(array $ls_data, ilImportMapping $mapping)
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping)
buildLPSettings(array $lp_settings, ilImportMapping $mapping)
getNewImagePath(string $type, string $path)
afterContainerImportProcessing(ilImportMapping $mapping)
finalProcessing(ilImportMapping $a_mapping)
Component logger with individual log levels by component id.
User class.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Class ilObject Basic functions for all objects.
static _writeParentId(string $a_parent_type, int $a_pg_id, int $a_par_id)
Class ilRbacAdmin Core functions for role based access control.
Xml importer class.
$path
Definition: ltiservices.php:30
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
global $DIC
Definition: shib_login.php:26