ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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($this->obj->getRefId());
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 $updated[] = $item->withPostCondition($post_condition);
124 }
125 }
126
127 if($updated) {
128 $this->obj->storeLSItems($updated);
129 }
130 }
131
132 protected function buildSettings(array $ls_settings): void
133 {
134 $settings = $this->obj->getLSSettings();
135 $settings = $settings
136 ->withMembersGallery($ls_settings["members_gallery"] === 'true' ? true : false)
137 ;
138 $this->obj->updateSettings($settings);
139 }
140
141 protected function buildLPSettings(array $lp_settings, ilImportMapping $mapping): void
142 {
143 $collection = ilLPCollection::getInstanceByMode($this->obj->getId(), (int) $lp_settings["lp_mode"]);
144
145 $new_ref_ids = array_map(function ($old_ref_id) use ($mapping) {
146 return $mapping->getMapping("components/ILIAS/Container", "refs", $old_ref_id);
147 }, $lp_settings["lp_item_ref_ids"]);
148
149 if (!is_null($collection)) {
150 $collection->activateEntries($new_ref_ids);
151 }
152
153 $settings = new ilLPObjSettings($this->obj->getId());
154 $settings->setMode((int) $lp_settings["lp_mode"]);
155 $settings->insert();
156 }
157
158 protected function decodeImageData(string $data): string
159 {
160 return base64_decode($data);
161 }
162
163 protected function getNewImagePath(string $type, string $path): string
164 {
165 $fs = $this->obj->getDI()['db.filesystem'];
166 return $fs->getStoragePathFor(
167 $type,
168 $this->obj->getId(),
169 $fs->getSuffix($path)
170 );
171 }
172
173 protected function writeToFileSystem($data, string $path): void
174 {
175 file_put_contents($path, $data);
176 }
177}
@ 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