ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilLearningSequenceImporter.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
9{
13 protected $obj;
14
18 protected $data;
19
20 public function init()
21 {
22 global $DIC;
23 $this->user = $DIC["ilUser"];
24 $this->rbac_admin = $DIC["rbacadmin"];
25 $this->log = $DIC["ilLoggerFactory"]->getRootLogger();
26 }
27
28 public function importXmlRepresentation($entity, $id, $xml, $mapping)
29 {
30 if ($new_id = $mapping->getMapping("Services/Container", "objs", $id)) {
31 $this->obj = ilObjectFactory::getInstanceByObjId($new_id, false);
32 } else {
33 $this->obj = new ilObjLearningSequence();
34 $this->obj->create();
35 }
36
37 $parser = new ilLearningSequenceXMLParser($this->obj, $xml);
38 $this->data = $parser->start();
39
40 $mapping->addMapping("Modules/LearningSequence", "lso", $id, $this->obj->getId());
41 }
42
43 public function finalProcessing($mapping)
44 {
45 $this->buildSettings($this->data["settings"]);
46
47 $this->obj->update();
48 }
49
51 {
52 $this->updateRefId($mapping);
53 $this->buildLSItems($this->data["item_data"], $mapping);
54 $this->buildLPSettings($this->data["lp_settings"], $mapping);
55
56 $roles = $this->obj->getLSRoles();
57 $roles->addLSMember(
58 (int) $this->user->getId(),
59 $roles->getDefaultAdminRole()
60 );
61 }
62
63 protected function updateRefId(ilImportMapping $mapping)
64 {
65 $old_ref_id = $this->data["object"]["ref_id"];
66 $new_ref_id = $mapping->getMapping("Services/Container", "refs", $old_ref_id);
67
68 $this->obj->setRefId($new_ref_id);
69 }
70
71 protected function buildLSItems(array $ls_data, ilImportMapping $mapping)
72 {
73 $ls_items = array();
74 foreach ($ls_data as $data) {
75 $old_ref_id = $data["id"];
76 $new_ref_id = $mapping->getMapping("Services/Container", "refs", $old_ref_id);
77
78 $post_condition = new ilLSPostCondition(
79 (int) $new_ref_id,
80 $data["ls_item_pc_condition_type"],
81 $data["ls_item_pc_value"]
82 );
83
84 $ls_items[] = new LSItem(
85 $data["ls_item_type"] ?? "",
86 $data["ls_item_title"] ?? "",
87 $data["ls_item_description"] ?? "",
88 $data["ls_item_icon_path"] ?? "",
89 (bool) $data["ls_item_is_online"],
90 (int) $data["ls_item_order_number"],
91 $post_condition,
92 (int) $new_ref_id
93 );
94 }
95
96 $this->obj->storeLSItems($ls_items);
97 }
98
99 protected function buildSettings(array $ls_settings)
100 {
101 $settings = $this->obj->getLSSettings();
102 $settings = $settings
103 ->withAbstract($ls_settings["abstract"])
104 ->withExtro($ls_settings["extro"])
105 ->withMembersGallery((bool) $ls_settings["members_gallery"])
106 ;
107
108 if ($ls_settings["abstract_img"] != "") {
109 $path = $this->getNewImagePath(ilLearningSequenceFilesystem::IMG_ABSTRACT, $ls_settings['abstract_img']);
110 $abstract_img_data = $this->decodeImageData($ls_settings["abstract_img_data"]);
111 $this->writeToFileSystem($abstract_img_data, $path);
112 $settings = $settings
113 ->withAbstractImage($path)
114 ;
115 }
116
117 if ($ls_settings["extro_img"] != "") {
118 $path = $this->getNewImagePath(ilLearningSequenceFilesystem::IMG_EXTRO, $ls_settings['extro_img']);
119 $extro_img_data = $this->decodeImageData($ls_settings["extro_img_data"]);
120 $this->writeToFileSystem($extro_img_data, $path);
121 $settings = $settings
122 ->withExtroImage($path)
123 ;
124 }
125
126 $this->obj->updateSettings($settings);
127 }
128
129 protected function buildLPSettings(array $lp_settings, ilImportMapping $mapping)
130 {
131 $collection = ilLPCollection::getInstanceByMode($this->obj->getId(), (int) $lp_settings["lp_mode"]);
132
133 $new_ref_ids = array_map(function ($old_ref_id) use ($mapping) {
134 return $mapping->getMapping("Services/Container", "refs", $old_ref_id);
135 }, $lp_settings["lp_item_ref_ids"]);
136
137 if (!is_null($collection)) {
138 $collection->activateEntries($new_ref_ids);
139 }
140
141 $settings = new ilLPObjSettings($this->obj->getId());
142 $settings->setMode((int) $lp_settings["lp_mode"]);
143 $settings->insert();
144 }
145
146 protected function decodeImageData(string $data)
147 {
148 return base64_decode($data);
149 }
150
151 protected function getNewImagePath(string $type, string $path) : string
152 {
153 $fs = $this->obj->getDI()['db.filesystem'];
154 return $fs->getStoragePathFor(
155 $type,
156 (int) $this->obj->getId(),
157 $fs->getSuffix($path)
158 );
159 }
160
161 protected function writeToFileSystem($data, string $path)
162 {
163 file_put_contents($path, $data);
164 }
165}
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
Data holding class LSItem .
Definition: LSItem.php:12
getMapping($a_comp, $a_entity, $a_old_id)
Get a mapping.
static getInstanceByMode($a_obj_id, $a_mode)
A PostCondition does restrict the progression of a user through the learning sequence.
buildLSItems(array $ls_data, ilImportMapping $mapping)
finalProcessing($mapping)
Final processing.
buildLPSettings(array $lp_settings, ilImportMapping $mapping)
importXmlRepresentation($entity, $id, $xml, $mapping)
Import xml representation.
getNewImagePath(string $type, string $path)
afterContainerImportProcessing(ilImportMapping $mapping)
Called after all container objects have been implemented.
Class ilObjLearningSequence.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
Xml importer class.
global $DIC
Definition: goto.php:24
$xml
Definition: metadata.php:332
$type