ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCmiXapiDataSet.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
29{
30 private array $_data = [];
31 private array $_archive = [];
32 private array $_element_db_mapping = [];
33 public array $_cmixSettingsProperties = [
34 "LrsTypeId" => ["db_col" => "lrs_type_id", "db_type" => "integer"]
35 ,
36 "ContentType" => ["db_col" => "content_type", "db_type" => "text"]
37 ,
38 "SourceType" => ["db_col" => "source_type", "db_type" => "text"]
39 ,
40 "ActivityId" => ["db_col" => "activity_id", "db_type" => "text"]
41 ,
42 "Instructions" => ["db_col" => "instructions", "db_type" => "text"]
43 // ,"OfflineStatus" => ["db_col" => "offline_status", "db_type" => "integer"]
44 ,
45 "LaunchUrl" => ["db_col" => "launch_url", "db_type" => "text"]
46 ,
47 "AuthFetchUrl" => ["db_col" => "auth_fetch_url", "db_type" => "integer"]
48 ,
49 "LaunchMethod" => ["db_col" => "launch_method", "db_type" => "text"]
50 ,
51 "LaunchMode" => ["db_col" => "launch_mode", "db_type" => "text"]
52 ,
53 "MasteryScore" => ["db_col" => "mastery_score", "db_type" => "float"]
54 ,
55 "KeepLp" => ["db_col" => "keep_lp", "db_type" => "integer"]
56 ,
57 "PrivacyIdent" => ["db_col" => "privacy_ident", "db_type" => "integer"]
58 ,
59 "PrivacyName" => ["db_col" => "privacy_name", "db_type" => "integer"]
60 ,
61 "UsrPrivacyComment" => ["db_col" => "usr_privacy_comment", "db_type" => "text"]
62 ,
63 "ShowStatements" => ["db_col" => "show_statements", "db_type" => "integer"]
64 ,
65 "XmlManifest" => ["db_col" => "xml_manifest", "db_type" => "text"]
66 ,
67 "Version" => ["db_col" => "version", "db_type" => "integer"]
68 ,
69 "HighscoreEnabled" => ["db_col" => "highscore_enabled", "db_type" => "integer"]
70 ,
71 "HighscoreAchievedTs" => ["db_col" => "highscore_achieved_ts", "db_type" => "integer"]
72 ,
73 "HighscorePercentage" => ["db_col" => "highscore_percentage", "db_type" => "integer"]
74 ,
75 "HighscoreWtime" => ["db_col" => "highscore_wtime", "db_type" => "integer"]
76 ,
77 "HighscoreOwnTable" => ["db_col" => "highscore_own_table", "db_type" => "integer"]
78 ,
79 "HighscoreTopTable" => ["db_col" => "highscore_top_table", "db_type" => "integer"]
80 ,
81 "HighscoreTopNum" => ["db_col" => "highscore_top_num", "db_type" => "integer"]
82 ,
83 "BypassProxy" => ["db_col" => "bypass_proxy", "db_type" => "integer"]
84 ,
85 "OnlyMoveon" => ["db_col" => "only_moveon", "db_type" => "integer"]
86 ,
87 "Achieved" => ["db_col" => "achieved", "db_type" => "integer"]
88 ,
89 "Answered" => ["db_col" => "answered", "db_type" => "integer"]
90 ,
91 "Completed" => ["db_col" => "completed", "db_type" => "integer"]
92 ,
93 "Failed" => ["db_col" => "failed", "db_type" => "integer"]
94 ,
95 "Initialized" => ["db_col" => "initialized", "db_type" => "integer"]
96 ,
97 "Passed" => ["db_col" => "passed", "db_type" => "integer"]
98 ,
99 "Progressed" => ["db_col" => "progressed", "db_type" => "integer"]
100 ,
101 "Satisfied" => ["db_col" => "satisfied", "db_type" => "integer"]
102 ,
103 "Terminated" => ["db_col" => "c_terminated", "db_type" => "integer"]
104 ,
105 "HideData" => ["db_col" => "hide_data", "db_type" => "integer"]
106 ,
107 "Timestamp" => ["db_col" => "c_timestamp", "db_type" => "integer"]
108 ,
109 "Duration" => ["db_col" => "duration", "db_type" => "integer"]
110 ,
111 "NoSubstatements" => ["db_col" => "no_substatements", "db_type" => "integer"]
112 ,
113 "PublisherId" => ["db_col" => "publisher_id", "db_type" => "text"]
114 ,
115 "AnonymousHomepage" => ["db_col" => "anonymous_homepage", "db_type" => "integer"]
116 ,
117 "MoveOn" => ["db_col" => "moveon", "db_type" => "text"]
118 ,
119 "LaunchParameters" => ["db_col" => "launch_parameters", "db_type" => "text"]
120 ,
121 "EntitlementKey" => ["db_col" => "entitlement_key", "db_type" => "text"]
122 ,
123 "SwitchToReview" => ["db_col" => "switch_to_review", "db_type" => "integer"]
124 ,
125 "DeleteData" => ["db_col" => "delete_data", "db_type" => "integer"]
126 ];
127
132 public function __construct(int $a_id = 0, bool $a_reference = true)
133 {
134 global $DIC;
137 // $this->_main_object_id = $a_id;
138 // $this->_dataSetMapping = ilObjCmiXapi::getInstance($a_id, $a_reference)->getDataSetMapping();
139
140 //var_dump($this->_dataSetMapping); exit;
142
143 foreach ($this->_cmixSettingsProperties as $key => $value) {
144 $this->_element_db_mapping [$value["db_col"]] = $key;
145 }
146 }
147
152 protected function getDependencies(
153 string $a_entity,
154 string $a_version,
155 ?array $a_rec = null,
156 ?array $a_ids = null
157 ): array {
158 return [];
159 }
160 public function getCmiXapiXmlRepresentation(
161 string $a_entity,
162 string $a_schema_version,
163 array $a_ids,
164 string $a_field = "",
165 bool $a_omit_header = false,
166 bool $a_omit_types = false
167 ): string {
168 global $DIC;
171 $GLOBALS["ilLog"]->write(json_encode($this->getTypes("cmix", "5.1.0"), JSON_PRETTY_PRINT));
172
173 $this->dircnt = 1;
174
175 $this->readData($a_entity, $a_schema_version, $a_ids);
176
177 //var_dump($this->data); exit;
178 $id = (int) $this->data["Id"];
179
180 // prepare archive skeleton
181 $objTypeAndId = "cmix_" . $id;
182 $this->_archive['directories'] = [
183 "exportDir" => ilExport::_getExportDirectory($id)
184 ,
185 "tempDir" => ilExport::_getExportDirectory($id) . "/temp"
186 ,
187 "archiveDir" => time() . "__" . IL_INST_ID . "__" . $objTypeAndId
188 ,
189 "moduleDir" => "cmix_" . $id
190 ];
191
192 $this->_archive['files'] = [
193 "properties" => "properties.xml",
194 "manifest" => 'manifest.xml'
195 ];
196 if (false !== strpos($this->data['SourceType'], 'local')) {
197 $this->_archive['files']['content'] = "content.zip";
198 }
199
200 //var_dump([$this->_archive, $this->buildManifest()]); exit;
201
202 // Prepare temp storage on the local filesystem
203 if (!file_exists($this->_archive['directories']['exportDir'])) {
204 mkdir($this->_archive['directories']['exportDir'], 0755, true);
205 //$DIC->filesystem()->storage()->createDir($this->_archive['directories']['tempDir']);
206 }
207 if (!file_exists($this->_archive['directories']['tempDir'])) {
208 mkdir($this->_archive['directories']['tempDir'], 0755, true);
209 //$DIC->filesystem()->storage()->createDir($this->_archive['directories']['tempDir']);
210 }
211
212 // build manifest xml file
213 file_put_contents(
214 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['manifest'],
215 $this->buildManifest()
216 );
217
218 // build content zip file
219 if (isset($this->_archive['files']['content'])) {
220 $lmDir = ilFileUtils::getWebspaceDir("filesystem") . "/lm_data/lm_" . $id;
222 $lmDir,
223 $this->_archive['directories']['tempDir'] . "/" . substr($this->_archive['files']['content'], 0, -4),
224 true
225 );
226 }
227
228 // build property xml file
229 file_put_contents(
230 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['properties'],
231 $this->buildProperties($a_entity, $a_omit_header)
232 );
233
234 // zip tempDir and append to export folder
235
236 $fileName = $this->_archive['directories']['exportDir'] . "/" . $this->_archive['directories']['archiveDir'] . ".zip";
237 $zArchive = new ZipArchive();
238 if ($zArchive->open($fileName, ZipArchive::CREATE) !== true) {
239 exit("cannot open <$fileName>\n");
240 }
241 $zArchive->addFile(
242 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['properties'],
243 $this->_archive['directories']['archiveDir'] . '/properties.xml'
244 );
245 $zArchive->addFile(
246 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['manifest'],
247 $this->_archive['directories']['archiveDir'] . '/' . "manifest.xml"
248 );
249 if (isset($this->_archive['files']['content'])) {
250 $zArchive->addFile(
251 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['content'],
252 $this->_archive['directories']['archiveDir'] . '/content.zip'
253 );
254 }
255 //var_dump($zArchive); exit;
256 $zArchive->close();
257
258 /*
259 ilUtil::zip(
260 $this->_archive['directories']['tempDir'],
261 $this->_archive['directories']['archiveDir'] . ".zip"
262 );
263 */
264
265 // unlink tempDir and its content
266 unlink($this->_archive['directories']['tempDir'] . "/manifest.xml");
267 unlink($this->_archive['directories']['tempDir'] . "/properties.xml");
268 if (isset($this->_archive['files']['content'])) {
269 unlink($this->_archive['directories']['tempDir'] . "/content.zip");
270 }
271 //unlink($this->_archive['directories']['tempDir']);
272 //$DIC->filesystem()->storage()->readAndDelete($this->_archive['directories']['tempDir']);
273 //$DIC->filesystem()->storage()->deleteDir($this->_archive['directories']['tempDir']);
274
275 //var_dump($this->_archive); exit;
276
277 return $fileName;
278 }
279
286 protected function getTypes(string $a_entity, string $a_version): array
287 {
288 $types = [];
289 foreach ($this->_cmixSettingsProperties as $key => $value) {
290 $types[$key] = $value["db_type"];
291 }
292 //var_dump($types); exit;
293 //return $types;
294
295 if ($a_entity == "cmix") {
296 switch ($a_version) {
297 case "5.1.0":
298 $types = [];
299 foreach ($this->_cmixSettingsProperties as $key => $value) {
300 $types[$key] = $value["db_type"];
301 }
302 //return $types;
303 break;
304 }
305 }
306 return $types;
307 }
308
309 public function readData(string $a_entity, string $a_version, array $a_ids): void
310 {
311 global $DIC;
314 //$a_ids = [];
315 if (!is_array($a_ids)) {
316 $a_ids = array($a_ids);
317 }
318
319 //var_dump([$a_entity, $a_version, $a_id]); exit;
320 if ($a_entity == "cmix") {
321 switch ($a_version) {
322 case "5.1.0":
323 $this->getDirectDataFromQuery("SELECT obj_id id, title, description " .
324 " FROM object_data " .
325 "WHERE " .
326 $DIC->database()->in("obj_id", $a_ids, false, "integer"));
327 break;
328 } // EOF switch
329 } // EOF if( $a_entity == "cmix" )
330
331 foreach ($this->data as $key => $data) {
332 $query = "SELECT " . implode(",", array_keys($this->_element_db_mapping)) . " ";
333 $query .= "FROM `cmix_settings` ";
334 $query .= "WHERE " . $DIC->database()->in("obj_id", $a_ids, false, "integer");
335 $result = $DIC->database()->query($query);
336 //$this->data = [];
337 if ($dataset = $DIC->database()->fetchAssoc($result)) {
338 $this->_data = $dataset;
339 }
340
341 //foreach( $this->_data AS $key => $data ) {
342 foreach ($this->_data as $dbColName => $value) {
343 $attr = $this->_element_db_mapping[$dbColName];
344 $this->data[$key][$attr] = $value;
345 //$this->data[$key][$dbColName] = $value;
346 } // EOF foreach ($this->_dataSetMapping as $dbColName => $value)
347 } // EOF foreach( $this->_data AS $key => $data )
348 $this->data = $this->data[0];
349 //var_dump($this->data); exit;
350 }
351
352 private function buildManifest(): string
353 {
354 $manWriter = new ilXmlWriter();
355 $manWriter->xmlHeader();
356 foreach ($this->_archive['files'] as $key => $value) {
357 $manWriter->xmlElement($key, null, $value, true, true);
358 }
359 #$manWriter->appendXML ("</content>\n");
360 return $manWriter->xmlDumpMem(true);
361 }
362
363 private function buildProperties(mixed $a_entity, bool $a_omit_header = false): string
364 {
365 $atts = array(
366 "InstallationId" => IL_INST_ID,
367 "InstallationUrl" => ILIAS_HTTP_PATH,
368 "TopEntity" => $a_entity
369 );
370
371 $writer = new ilXmlWriter();
372
373 $writer->xmlStartTag('DataSet', $atts);
374
375 if (!$a_omit_header) {
376 $writer->xmlHeader();
377 }
378
379 foreach ($this->data as $key => $value) {
380 $writer->xmlElement($key, null, $value, true, true);
381 }
382
383 $writer->xmlEndTag("DataSet");
384
385 return $writer->xmlDumpMem(true);
386 }
387
388 public function importRecord(
389 string $a_entity,
390 array $a_types,
391 array $a_rec,
392 ilImportMapping $a_mapping,
393 string $a_schema_version
394 ): void {
395 //var_dump( [$a_entity, $a_types, $a_rec, $a_mapping, $a_schema_version] ); exit;
396 switch ($a_entity) {
397 case "cmix":
398
399 if ($new_id = $a_mapping->getMapping('components/ILIAS/Container', 'objs', $a_rec['Id'])) {
400 $newObj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
401 } else {
402 $newObj = new ilObjCmiXapi();
403 $newObj->setType("cmix");
404 $newObj->create(true);
405 }
406
407 $newObj->setTitle($a_rec["Title"]);
408 $newObj->setDescription($a_rec["Description"]);
409 $newObj->update();
410
411 //$this->current_obj = $newObj;
412 $a_mapping->addMapping("components/ILIAS/CmiXapi", "cmix", $a_rec["Id"], (string) $newObj->getId());
413 $a_mapping->addMapping(
414 "components/ILIAS/MetaData",
415 "md",
416 $a_rec["Id"] . ":0:cmix",
417 $newObj->getId() . ":0:cmix"
418 );
419 break;
420 }
421 }
422
427 public function getSupportedVersions(): array
428 {
429 return array("5.1.0");
430 }
431
435 public function getXmlNamespace(string $a_entity, string $a_schema_version): string
436 {
437 return "http://www.ilias.de/xml/Modules/CmiXapi/" . $a_entity;
438 }
439}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getSupportedVersions()
Get supported versions.
getXmlNamespace(string $a_entity, string $a_schema_version)
Get xml namespace.
getTypes(string $a_entity, string $a_version)
Get field types for entity.
buildProperties(mixed $a_entity, bool $a_omit_header=false)
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
Determine the dependent sets of data.
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
Needs to be overwritten for import use case.
A dataset contains in data in a common structure that can be shared and transformed for different pur...
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
@depricated Get export directory for an repository object
static getWebspaceDir(string $mode="filesystem")
get webspace directory
static zip(string $a_dir, string $a_file, bool $compress_content=false)
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)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_INST_ID
Definition: constants.php:40
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
exit
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54