ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilCmiXapiDataSet.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
13{
14 private $_data = [];
15 private $_archive = [];
16 private $_dataSetMapping = null;
17 private $_main_object_id = null;
20 "LrsTypeId" => ["db_col" => "lrs_type_id", "db_type" => "integer"]
21 ,"ContentType" => ["db_col" => "content_type", "db_type" => "text"]
22 ,"SourceType" => ["db_col" => "source_type", "db_type" => "text"]
23 ,"ActivityId" => ["db_col" => "activity_id", "db_type" => "text"]
24 ,"Instructions" => ["db_col" => "instructions", "db_type" => "text"]
25 // ,"OfflineStatus" => ["db_col" => "offline_status", "db_type" => "integer"]
26 ,"LaunchUrl" => ["db_col" => "launch_url", "db_type" => "text"]
27 ,"AuthFetchUrl" => ["db_col" => "auth_fetch_url", "db_type" => "integer"]
28 ,"LaunchMethod" => ["db_col" => "launch_method", "db_type" => "text"]
29 ,"LaunchMode" => ["db_col" => "launch_mode", "db_type" => "text"]
30 ,"MasteryScore" => ["db_col" => "mastery_score", "db_type" => "float"]
31 ,"KeepLp" => ["db_col" => "keep_lp", "db_type" => "integer"]
32 ,"PrivacyIdent" => ["db_col" => "privacy_ident", "db_type" => "integer"]
33 ,"PrivacyName" => ["db_col" => "privacy_name", "db_type" => "integer"]
34 ,"UsrPrivacyComment" => ["db_col" => "usr_privacy_comment", "db_type" => "text"]
35 ,"ShowStatements" => ["db_col" => "show_statements", "db_type" => "integer"]
36 ,"XmlManifest" => ["db_col" => "xml_manifest", "db_type" => "text"]
37 ,"Version" => ["db_col" => "version", "db_type" => "integer"]
38 ,"HighscoreEnabled" => ["db_col" => "highscore_enabled", "db_type" => "integer"]
39 ,"HighscoreAchievedTs" => ["db_col" => "highscore_achieved_ts", "db_type" => "integer"]
40 ,"HighscorePercentage" => ["db_col" => "highscore_percentage", "db_type" => "integer"]
41 ,"HighscoreWtime" => ["db_col" => "highscore_wtime", "db_type" => "integer"]
42 ,"HighscoreOwnTable" => ["db_col" => "highscore_own_table", "db_type" => "integer"]
43 ,"HighscoreTopTable" => ["db_col" => "highscore_top_table", "db_type" => "integer"]
44 ,"HighscoreTopNum" => ["db_col" => "highscore_top_num", "db_type" => "integer"]
45 ,"BypassProxy" => ["db_col" => "bypass_proxy", "db_type" => "integer"]
46 ,"OnlyMoveon" => ["db_col" => "only_moveon", "db_type" => "integer"]
47 ,"Achieved" => ["db_col" => "achieved", "db_type" => "integer"]
48 ,"Answered" => ["db_col" => "answered", "db_type" => "integer"]
49 ,"Completed" => ["db_col" => "completed", "db_type" => "integer"]
50 ,"Failed" => ["db_col" => "failed", "db_type" => "integer"]
51 ,"Initialized" => ["db_col" => "initialized", "db_type" => "integer"]
52 ,"Passed" => ["db_col" => "passed", "db_type" => "integer"]
53 ,"Progressed" => ["db_col" => "progressed", "db_type" => "integer"]
54 ,"Satisfied" => ["db_col" => "satisfied", "db_type" => "integer"]
55 ,"Terminated" => ["db_col" => "c_terminated", "db_type" => "integer"]
56 ,"HideData" => ["db_col" => "hide_data", "db_type" => "integer"]
57 ,"Timestamp" => ["db_col" => "c_timestamp", "db_type" => "integer"]
58 ,"Duration" => ["db_col" => "duration", "db_type" => "integer"]
59 ,"NoSubstatements" => ["db_col" => "no_substatements", "db_type" => "integer"]
60 ,"PublisherId" => ["db_col" => "publisher_id", "db_type" => "text"]
61 ,"AnonymousHomepage" => ["db_col" => "anonymous_homepage", "db_type" => "integer"]
62 ,"MoveOn" => ["db_col" => "moveon", "db_type" => "text"]
63 ,"LaunchParameters" => ["db_col" => "launch_parameters", "db_type" => "text"]
64 ,"EntitlementKey" => ["db_col" => "entitlement_key", "db_type" => "text"]
65 ,"SwitchToReview" => ["db_col" => "switch_to_review", "db_type" => "integer"]
66 ];
67
68
69
70
71 public function __construct($a_id = 0, $a_reference = true)
72 {
73 global $DIC;
75 $this->_main_object_id = $a_id;
76 $this->_dataSetMapping = ilObjCmiXapi::getInstance($a_id, $a_reference)->getDataSetMapping();
77
78 //var_dump($this->_dataSetMapping); exit;
80
81 foreach ($this->_cmixSettingsProperties as $key => $value) {
82 $this->_element_db_mapping [$value["db_col"]] = $key;
83 }
84 }
85
86
93 public function readData($a_entity, $a_version, $a_ids) : void
94 {
95 global $DIC;
97 //$a_ids = [];
98 if (!is_array($a_ids)) {
99 $a_ids = array($a_ids);
100 }
101
102 //var_dump([$a_entity, $a_version, $a_id]); exit;
103 if ($a_entity == "cmix") {
104 switch ($a_version) {
105 case "5.1.0":
106 $this->getDirectDataFromQuery("SELECT obj_id id, title, description " .
107 " FROM object_data " .
108 "WHERE " .
109 $DIC->database()->in("obj_id", $a_ids, false, "integer"));
110 break;
111 } // EOF switch
112 } // EOF if( $a_entity == "cmix" )
113
114 foreach ($this->data as $key => $data) {
115 $query = "SELECT " . implode(",", array_keys($this->_element_db_mapping)) . " ";
116 $query .= "FROM `cmix_settings` ";
117 $query .= "WHERE " . $DIC->database()->in("obj_id", $a_ids, false, "integer");
118 $result = $DIC->database()->query($query);
119 //$this->data = [];
120 if ($dataset = $DIC->database()->fetchAssoc($result)) {
121 $this->_data = $dataset;
122 }
123
124 //foreach( $this->_data AS $key => $data ) {
125 foreach ($this->_data as $dbColName => $value) {
126 $attr = $this->_element_db_mapping[$dbColName];
127 $this->data[$key][$attr] = $value;
128 //$this->data[$key][$dbColName] = $value;
129 } // EOF foreach ($this->_dataSetMapping as $dbColName => $value)
130 } // EOF foreach( $this->_data AS $key => $data )
131 $this->data = $this->data[0];
132 //var_dump($this->data); exit;
133 } // EOf function readData
134
135
136
143 protected function getTypes($a_entity, $a_version) : array
144 {
145 $types = [];
146 foreach ($this->_cmixSettingsProperties as $key => $value) {
147 $types[$key] = $value["db_type"];
148 }
149 //var_dump($types); exit;
150 //return $types;
151
152 if ($a_entity == "cmix") {
153 switch ($a_version) {
154 case "5.1.0":
155 $types = [];
156 foreach ($this->_cmixSettingsProperties as $key => $value) {
157 $types[$key] = $value["db_type"];
158 }
159 //return $types;
160 break;
161 }
162 }
163 return $types;
164 }
165
166
167
168
169 public function getCmiXapiXmlRepresentation($a_entity, $a_schema_version, $a_ids, $a_field = "", $a_omit_header = false, $a_omit_types = false)
170 {
171 global $DIC;
173 $GLOBALS["ilLog"]->write(json_encode($this->getTypes("cmix", "5.1.0"), JSON_PRETTY_PRINT));
174
175 $this->dircnt = 1;
176
177 $this->readData($a_entity, $a_schema_version, $a_ids);
178
179 //var_dump($this->data); exit;
180 $id = $this->data["Id"];
181
182 // requirements
183 require_once("./Services/Export/classes/class.ilExport.php");
184 require_once("./Services/Xml/classes/class.ilXmlWriter.php");
185
186 // prepare archive skeleton
187 $objTypeAndId = "cmix_" . $id;
188 $this->_archive['directories'] = [
189 "exportDir" => ilExport::_getExportDirectory($id)
190 ,"tempDir" => ilExport::_getExportDirectory($id) . "/temp"
191 ,"archiveDir" => time() . "__" . IL_INST_ID . "__" . $objTypeAndId
192 ,"moduleDir" => "cmix_" . $id
193 ];
194
195 $this->_archive['files'] = [
196 "properties" => "properties.xml",
197 "metadata" => "metadata.xml",
198 "manifest" => 'manifest.xml',
199 ];
200 if (false !== strpos($this->data['SourceType'], 'local')) {
201 $this->_archive['files']['content'] = "content.zip";
202 }
203
204 //var_dump([$this->_archive, $this->buildManifest()]); exit;
205
206
207 // Prepare temp storage on the local filesystem
208 if (!file_exists($this->_archive['directories']['exportDir'])) {
209 mkdir($this->_archive['directories']['exportDir'], 0755, true);
210 //$DIC->filesystem()->storage()->createDir($this->_archive['directories']['tempDir']);
211 }
212 if (!file_exists($this->_archive['directories']['tempDir'])) {
213 mkdir($this->_archive['directories']['tempDir'], 0755, true);
214 //$DIC->filesystem()->storage()->createDir($this->_archive['directories']['tempDir']);
215 }
216
217
218 // build metadata xml file
219 file_put_contents(
220 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['metadata'],
221 $this->buildMetaData($id)
222 );
223
224 // build manifest xml file
225 file_put_contents(
226 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['manifest'],
227 $this->buildManifest()
228 );
229
230 // build content zip file
231 if (isset($this->_archive['files']['content'])) {
232 $lmDir = ilUtil::getWebspaceDir("filesystem") . "/lm_data/lm_" . $id;
233 ilUtil::zip($lmDir, $this->_archive['directories']['tempDir'] . "/" . substr($this->_archive['files']['content'], 0, -4), true);
234 }
235
236 // build property xml file
237 file_put_contents(
238 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['properties'],
239 $this->buildProperties($a_entity, $a_omit_header)
240 );
241
242
243 // zip tempDir and append to export folder
244
245 $fileName = $this->_archive['directories']['exportDir'] . "/" . $this->_archive['directories']['archiveDir'] . ".zip";
246 $zArchive = new ZipArchive();
247 if ($zArchive->open($fileName, ZipArchive::CREATE) !== true) {
248 exit("cannot open <$fileName>\n");
249 }
250 $zArchive->addFile(
251 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['properties'],
252 $this->_archive['directories']['archiveDir'] . '/properties.xml'
253 );
254 $zArchive->addFile(
255 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['manifest'],
256 $this->_archive['directories']['archiveDir'] . '/' . "manifest.xml"
257 );
258 $zArchive->addFile(
259 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['metadata'],
260 $this->_archive['directories']['archiveDir'] . '/' . "metadata.xml"
261 );
262 if (isset($this->_archive['files']['content'])) {
263 $zArchive->addFile(
264 $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['content'],
265 $this->_archive['directories']['archiveDir'] . '/content.zip'
266 );
267 }
268 //var_dump($zArchive); exit;
269 $zArchive->close();
270
271 /*
272 ilUtil::zip(
273 $this->_archive['directories']['tempDir'],
274 $this->_archive['directories']['archiveDir'] . ".zip"
275 );
276 */
277
278
279 // unlink tempDir and its content
280 unlink($this->_archive['directories']['tempDir'] . "/metadata.xml");
281 unlink($this->_archive['directories']['tempDir'] . "/manifest.xml");
282 unlink($this->_archive['directories']['tempDir'] . "/properties.xml");
283 if (isset($this->_archive['files']['content'])) {
284 unlink($this->_archive['directories']['tempDir'] . "/content.zip");
285 }
286 //unlink($this->_archive['directories']['tempDir']);
287 //$DIC->filesystem()->storage()->readAndDelete($this->_archive['directories']['tempDir']);
288 //$DIC->filesystem()->storage()->deleteDir($this->_archive['directories']['tempDir']);
289
290
291
292 //var_dump($this->_archive); exit;
293
294
295
296 return $fileName;
297 }
298
303 public function buildMetaData($id)
304 {
305 $md2xml = new ilMD2XML($id, $id, "cmix");
306 $md2xml->startExport();
307 return $md2xml->getXML();
308 }
309
313 private function buildManifest()
314 {
315 $manWriter = new ilXmlWriter();
316 $manWriter->xmlHeader();
317 foreach ($this->_archive['files'] as $key => $value) {
318 $manWriter->xmlElement($key, null, $value, true, true);
319 }
320 #$manWriter->appendXML ("</content>\n");
321 return $manWriter->xmlDumpMem(true);
322 }
323
329 private function buildProperties($a_entity, $a_omit_header = false)
330 {
331 $atts = array(
332 "InstallationId" => IL_INST_ID,
333 "InstallationUrl" => ILIAS_HTTP_PATH,
334 "TopEntity" => $a_entity
335 );
336
337 $writer = new ilXmlWriter();
338
339 $writer->xmlStartTag('DataSet', $atts);
340
341 if (!$a_omit_header) {
342 $writer->xmlHeader();
343 }
344
345 foreach ($this->data as $key => $value) {
346 $writer->xmlElement($key, null, $value, true, true);
347 }
348
349 $writer->xmlEndTag("DataSet");
350
351 return $writer->xmlDumpMem(true);
352 }
353
361 public function importRecord(
362 string $a_entity,
363 array $a_types,
364 array $a_rec,
365 ilImportMapping $a_mapping,
366 string $a_schema_version
367 ) : void {
368 //var_dump( [$a_entity, $a_types, $a_rec, $a_mapping, $a_schema_version] ); exit;
369 switch ($a_entity) {
370 case "cmix":
371
372 include_once("./Modules/CmiXapi/classes/class.ilObjCmiXapi.php");
373
374 if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_rec['Id'])) {
375 $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
376 } else {
377 $newObj = new ilObjCmiXapi();
378 $newObj->setType("cmix");
379 $newObj->create(true);
380 }
381
382 $newObj->setTitle($a_rec["Title"]);
383 $newObj->setDescription($a_rec["Description"]);
384 $newObj->update();
385
386
387 //$this->current_obj = $newObj;
388 $a_mapping->addMapping("Modules/CmiXapi", "cmix", $a_rec["Id"], $newObj->getId());
389 break;
390 }
391 }
392
398 public function getSupportedVersions() : array
399 {
400 return array("5.1.0");
401 }
402
409 public function getXmlNamespace($a_entity, $a_schema_version) : string
410 {
411 return "http://www.ilias.de/xml/Modules/CmiXapi/" . $a_entity;
412 }
413
417 protected function getDependencies(
418 string $a_entity,
419 string $a_version,
420 ?array $a_rec = null,
421 ?array $a_ids = null
422 ) : array {
423 return [];
424 }
425}
$result
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
getSupportedVersions()
Get supported versions.
buildProperties($a_entity, $a_omit_header=false)
getXmlNamespace($a_entity, $a_schema_version)
Get xml namespace.
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)
getTypes($a_entity, $a_version)
Get field types for entity.
A dataset contains in data in a common structure that can be shared and transformed for different pur...
__construct()
Constructor.
getDirectDataFromQuery($a_query, $a_convert_to_leading_upper=true, $a_set=true)
Get data from query.This is a standard procedure, all db field names are directly mapped to abstract ...
readData($a_entity, $a_version, $a_ids)
Read data from DB.
static _getExportDirectory($a_obj_id, $a_type="xml", $a_obj_type="", $a_entity="")
Get export directory for an repository object.
getMapping($a_comp, $a_entity, $a_old_id)
Get a mapping.
addMapping($a_comp, $a_entity, $a_old_id, $a_new_id)
Add mapping.
static getInstance($a_id=0, $a_reference=true)
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static getWebspaceDir($mode="filesystem")
get webspace directory
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
XML writer class.
exit
Definition: login.php:29
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$query
$data
Definition: storeScorm.php:23
$DIC
Definition: xapitoken.php:46