ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilCmiXapiDataSet.php
Go to the documentation of this file.
1 <?php
2 
19 declare(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;
138 
139  foreach ($this->_cmixSettingsProperties as $key => $value) {
140  $this->_element_db_mapping [$value["db_col"]] = $key;
141  }
142  }
143 
148  protected function getDependencies(
149  string $a_entity,
150  string $a_version,
151  ?array $a_rec = null,
152  ?array $a_ids = null
153  ): array {
154  return [];
155  }
156  public function getCmiXapiXmlRepresentation(
157  string $a_entity,
158  string $a_schema_version,
159  array $a_ids,
160  string $a_field = "",
161  bool $a_omit_header = false,
162  bool $a_omit_types = false
163  ): string {
164  global $DIC;
167  $GLOBALS["ilLog"]->write(json_encode($this->getTypes("cmix", "5.1.0"), JSON_PRETTY_PRINT));
168 
169  $this->dircnt = 1;
170 
171  $this->readData($a_entity, $a_schema_version, $a_ids);
172 
173  $id = (int) $this->data["Id"];
174 
175  // prepare archive skeleton
176  $objTypeAndId = "cmix_" . $id;
177  $this->_archive['directories'] = [
178  "exportDir" => ilExport::_getExportDirectory($id)
179  ,
180  "archiveDir" => time() . "__" . IL_INST_ID . "__" . $objTypeAndId
181  ,
182  "moduleDir" => "cmix_" . $id
183  ];
184 
185  $this->_archive['files'] = [
186  "properties" => "properties.xml",
187  "metadata" => "metadata.xml",
188  "manifest" => 'manifest.xml',
189  ];
190  if (false !== strpos($this->data['SourceType'], 'local')) {
191  $this->_archive['files']['content'] = "content.zip";
192  }
193 
194  $exportArchiveDir = ilExport::_getExportDirectory($id) . "/" . $this->_archive['directories']['archiveDir'] . "/";
195 
196  // build metadata xml file
197  file_put_contents(
198  $exportArchiveDir . $this->_archive['files']['metadata'],
199  $this->buildMetaData($id)
200  );
201 
202 
203  // build content zip file
204  if (isset($this->_archive['files']['content'])) {
205  $lmDir = './' . ILIAS_WEB_DIR . "/" . CLIENT_ID . "/lm_data/lm_" . $id;
206  $DIC->legacyArchives()->zip(
207  $lmDir,
208  $exportArchiveDir . $this->_archive['files']['content'],
209  false
210  );
211  }
212 
213  // build property xml file
214 
215  file_put_contents(
216  $exportArchiveDir . $this->_archive['files']['properties'],
217  $this->buildProperties($a_entity, $a_omit_header)
218  );
219 
220 
221  $fileName = $this->_archive['directories']['exportDir'] . "/" . $this->_archive['directories']['archiveDir'] . ".zip";
222 
223  return $fileName;
224  }
225 
232  protected function getTypes(string $a_entity, string $a_version): array
233  {
234  $types = [];
235  foreach ($this->_cmixSettingsProperties as $key => $value) {
236  $types[$key] = $value["db_type"];
237  }
238 
239  if ($a_entity == "cmix") {
240  switch ($a_version) {
241  case "5.1.0":
242  $types = [];
243  foreach ($this->_cmixSettingsProperties as $key => $value) {
244  $types[$key] = $value["db_type"];
245  }
246  //return $types;
247  break;
248  }
249  }
250  return $types;
251  }
252 
253  public function readData(string $a_entity, string $a_version, array $a_ids): void
254  {
255  global $DIC;
256 
257  if (!is_array($a_ids)) {
258  $a_ids = array($a_ids);
259  }
260 
261  //var_dump([$a_entity, $a_version, $a_id]); exit;
262  if ($a_entity == "cmix") {
263  switch ($a_version) {
264  case "5.1.0":
265  $this->getDirectDataFromQuery("SELECT obj_id id, title, description " .
266  " FROM object_data " .
267  "WHERE " .
268  $DIC->database()->in("obj_id", $a_ids, false, "integer"));
269  break;
270  } // EOF switch
271  } // EOF if( $a_entity == "cmix" )
272 
273  foreach ($this->data as $key => $data) {
274  $query = "SELECT " . implode(",", array_keys($this->_element_db_mapping)) . " ";
275  $query .= "FROM `cmix_settings` ";
276  $query .= "WHERE " . $DIC->database()->in("obj_id", $a_ids, false, "integer");
277  $result = $DIC->database()->query($query);
278  //$this->data = [];
279  if ($dataset = $DIC->database()->fetchAssoc($result)) {
280  $this->_data = $dataset;
281  }
282 
283  //foreach( $this->_data AS $key => $data ) {
284  foreach ($this->_data as $dbColName => $value) {
285  $attr = $this->_element_db_mapping[$dbColName];
286  $this->data[$key][$attr] = $value;
287  //$this->data[$key][$dbColName] = $value;
288  } // EOF foreach ($this->_dataSetMapping as $dbColName => $value)
289  } // EOF foreach( $this->_data AS $key => $data )
290  $this->data = $this->data[0];
291  //var_dump($this->data); exit;
292  }
293 
294  public function buildMetaData(int $id): string
295  {
296  $md2xml = new ilMD2XML($id, $id, "cmix");
297  $md2xml->startExport();
298  return $md2xml->getXML();
299  }
300 
301  private function buildManifest(): string
302  {
303  $manWriter = new ilXmlWriter();
304  $manWriter->xmlHeader();
305  foreach ($this->_archive['files'] as $key => $value) {
306  $manWriter->xmlElement($key, null, $value, true, true);
307  }
308  #$manWriter->appendXML ("</content>\n");
309  return $manWriter->xmlDumpMem(true);
310  }
311 
312  private function buildProperties(mixed $a_entity, bool $a_omit_header = false): string
313  {
314  $atts = array(
315  "InstallationId" => IL_INST_ID,
316  "InstallationUrl" => ILIAS_HTTP_PATH,
317  "TopEntity" => $a_entity
318  );
319 
320  $writer = new ilXmlWriter();
321 
322  $writer->xmlStartTag('DataSet', $atts);
323 
324  if (!$a_omit_header) {
325  $writer->xmlHeader();
326  }
327 
328  foreach ($this->data as $key => $value) {
329  $writer->xmlElement($key, null, $value, true, true);
330  }
331 
332  $writer->xmlEndTag("DataSet");
333 
334  return $writer->xmlDumpMem(true);
335  }
336 
337  public function importRecord(
338  string $a_entity,
339  array $a_types,
340  array $a_rec,
341  ilImportMapping $a_mapping,
342  string $a_schema_version
343  ): void {
344  //var_dump( [$a_entity, $a_types, $a_rec, $a_mapping, $a_schema_version] ); exit;
345  switch ($a_entity) {
346  case "cmix":
347 
348  if ($new_id = $a_mapping->getMapping('components/ILIAS/Container', 'objs', $a_rec['Id'])) {
349  $newObj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
350  } else {
351  $newObj = new ilObjCmiXapi();
352  $newObj->setType("cmix");
353  $newObj->create(true);
354  }
355 
356  $newObj->setTitle($a_rec["Title"]);
357  $newObj->setDescription($a_rec["Description"]);
358  $newObj->update();
359 
360  //$this->current_obj = $newObj;
361  $a_mapping->addMapping("components/ILIAS/CmiXapi", "cmix", $a_rec["Id"], (string) $newObj->getId());
362  $a_mapping->addMapping(
363  "components/ILIAS/MetaData",
364  "md",
365  $a_rec["Id"] . ":0:cmix",
366  $newObj->getId() . ":0:cmix"
367  );
368  break;
369  }
370  }
371 
376  public function getSupportedVersions(): array
377  {
378  return array("5.1.0");
379  }
380 
384  public function getXmlNamespace(string $a_entity, string $a_schema_version): string
385  {
386  return "http://www.ilias.de/xml/Modules/CmiXapi/" . $a_entity;
387  }
388 }
getSupportedVersions()
Get supported versions.
const IL_INST_ID
Definition: constants.php:40
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getTypes(string $a_entity, string $a_version)
Get field types for entity.
readData(string $a_entity, string $a_version, array $a_ids)
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
Get export directory for an repository object
getMapping(string $a_comp, string $a_entity, string $a_old_id)
$GLOBALS["DIC"]
Definition: wac.php:30
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:25
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
Determine the dependent sets of data.
getDirectDataFromQuery(string $a_query, bool $a_convert_to_leading_upper=true, bool $a_set=true)
Get data from query.This is a standard procedure, all db field names are directly mapped to abstract ...
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
__construct(Container $dic, ilPlugin $plugin)
buildProperties(mixed $a_entity, bool $a_omit_header=false)
getXmlNamespace(string $a_entity, string $a_schema_version)
Get xml namespace.
const ILIAS_WEB_DIR
Definition: constants.php:45