ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCmiXapiDataSet.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
28 {
29  private array $_data = [];
30  private array $_archive = [];
31  private array $_element_db_mapping = [];
32  public array $_cmixSettingsProperties = [
33  "LrsTypeId" => ["db_col" => "lrs_type_id", "db_type" => "integer"]
34  ,
35  "ContentType" => ["db_col" => "content_type", "db_type" => "text"]
36  ,
37  "SourceType" => ["db_col" => "source_type", "db_type" => "text"]
38  ,
39  "ActivityId" => ["db_col" => "activity_id", "db_type" => "text"]
40  ,
41  "Instructions" => ["db_col" => "instructions", "db_type" => "text"]
42  // ,"OfflineStatus" => ["db_col" => "offline_status", "db_type" => "integer"]
43  ,
44  "LaunchUrl" => ["db_col" => "launch_url", "db_type" => "text"]
45  ,
46  "AuthFetchUrl" => ["db_col" => "auth_fetch_url", "db_type" => "integer"]
47  ,
48  "LaunchMethod" => ["db_col" => "launch_method", "db_type" => "text"]
49  ,
50  "LaunchMode" => ["db_col" => "launch_mode", "db_type" => "text"]
51  ,
52  "MasteryScore" => ["db_col" => "mastery_score", "db_type" => "float"]
53  ,
54  "KeepLp" => ["db_col" => "keep_lp", "db_type" => "integer"]
55  ,
56  "PrivacyIdent" => ["db_col" => "privacy_ident", "db_type" => "integer"]
57  ,
58  "PrivacyName" => ["db_col" => "privacy_name", "db_type" => "integer"]
59  ,
60  "UsrPrivacyComment" => ["db_col" => "usr_privacy_comment", "db_type" => "text"]
61  ,
62  "ShowStatements" => ["db_col" => "show_statements", "db_type" => "integer"]
63  ,
64  "XmlManifest" => ["db_col" => "xml_manifest", "db_type" => "text"]
65  ,
66  "Version" => ["db_col" => "version", "db_type" => "integer"]
67  ,
68  "HighscoreEnabled" => ["db_col" => "highscore_enabled", "db_type" => "integer"]
69  ,
70  "HighscoreAchievedTs" => ["db_col" => "highscore_achieved_ts", "db_type" => "integer"]
71  ,
72  "HighscorePercentage" => ["db_col" => "highscore_percentage", "db_type" => "integer"]
73  ,
74  "HighscoreWtime" => ["db_col" => "highscore_wtime", "db_type" => "integer"]
75  ,
76  "HighscoreOwnTable" => ["db_col" => "highscore_own_table", "db_type" => "integer"]
77  ,
78  "HighscoreTopTable" => ["db_col" => "highscore_top_table", "db_type" => "integer"]
79  ,
80  "HighscoreTopNum" => ["db_col" => "highscore_top_num", "db_type" => "integer"]
81  ,
82  "BypassProxy" => ["db_col" => "bypass_proxy", "db_type" => "integer"]
83  ,
84  "OnlyMoveon" => ["db_col" => "only_moveon", "db_type" => "integer"]
85  ,
86  "Achieved" => ["db_col" => "achieved", "db_type" => "integer"]
87  ,
88  "Answered" => ["db_col" => "answered", "db_type" => "integer"]
89  ,
90  "Completed" => ["db_col" => "completed", "db_type" => "integer"]
91  ,
92  "Failed" => ["db_col" => "failed", "db_type" => "integer"]
93  ,
94  "Initialized" => ["db_col" => "initialized", "db_type" => "integer"]
95  ,
96  "Passed" => ["db_col" => "passed", "db_type" => "integer"]
97  ,
98  "Progressed" => ["db_col" => "progressed", "db_type" => "integer"]
99  ,
100  "Satisfied" => ["db_col" => "satisfied", "db_type" => "integer"]
101  ,
102  "Terminated" => ["db_col" => "c_terminated", "db_type" => "integer"]
103  ,
104  "HideData" => ["db_col" => "hide_data", "db_type" => "integer"]
105  ,
106  "Timestamp" => ["db_col" => "c_timestamp", "db_type" => "integer"]
107  ,
108  "Duration" => ["db_col" => "duration", "db_type" => "integer"]
109  ,
110  "NoSubstatements" => ["db_col" => "no_substatements", "db_type" => "integer"]
111  ,
112  "PublisherId" => ["db_col" => "publisher_id", "db_type" => "text"]
113  ,
114  "AnonymousHomepage" => ["db_col" => "anonymous_homepage", "db_type" => "integer"]
115  ,
116  "MoveOn" => ["db_col" => "moveon", "db_type" => "text"]
117  ,
118  "LaunchParameters" => ["db_col" => "launch_parameters", "db_type" => "text"]
119  ,
120  "EntitlementKey" => ["db_col" => "entitlement_key", "db_type" => "text"]
121  ,
122  "SwitchToReview" => ["db_col" => "switch_to_review", "db_type" => "integer"]
123  ,
124  "DeleteData" => ["db_col" => "delete_data", "db_type" => "integer"]
125  ];
126 
131  public function __construct(int $a_id = 0, bool $a_reference = true)
132  {
133  global $DIC;
137 
138  foreach ($this->_cmixSettingsProperties as $key => $value) {
139  $this->_element_db_mapping [$value["db_col"]] = $key;
140  }
141  }
142 
147  protected function getDependencies(
148  string $a_entity,
149  string $a_version,
150  ?array $a_rec = null,
151  ?array $a_ids = null
152  ): array {
153  return [];
154  }
155  public function getCmiXapiXmlRepresentation(
156  string $exportArchiveDir,
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  $this->_archive['files'] = [
176  "properties" => "properties.xml",
177  "metadata" => "metadata.xml",
178  "manifest" => 'manifest.xml',
179  ];
180  if (false !== strpos($this->data['SourceType'], 'local')) {
181  $this->_archive['files']['content'] = "content.zip";
182  }
183 
184  // build metadata xml file
185  file_put_contents(
186  $exportArchiveDir . "/" . $this->_archive['files']['metadata'],
187  $this->buildMetaData($id)
188  );
189 
190 
191  // build content zip file
192  if (isset($this->_archive['files']['content'])) {
193  $lmDir = './' . ILIAS_WEB_DIR . "/" . CLIENT_ID . "/lm_data/lm_" . $id;
194  $DIC->legacyArchives()->zip(
195  $lmDir,
196  $exportArchiveDir . "/" . $this->_archive['files']['content'],
197  false
198  );
199  }
200 
201  // build property xml file
202 
203  file_put_contents(
204  $exportArchiveDir . "/" . $this->_archive['files']['properties'],
205  $this->buildProperties($a_entity, $a_omit_header)
206  );
207 
208  return $exportArchiveDir . ".zip";
209  }
210 
217  protected function getTypes(string $a_entity, string $a_version): array
218  {
219  $types = [];
220  foreach ($this->_cmixSettingsProperties as $key => $value) {
221  $types[$key] = $value["db_type"];
222  }
223 
224  if ($a_entity == "cmix") {
225  switch ($a_version) {
226  case "5.1.0":
227  $types = [];
228  foreach ($this->_cmixSettingsProperties as $key => $value) {
229  $types[$key] = $value["db_type"];
230  }
231  //return $types;
232  break;
233  }
234  }
235  return $types;
236  }
237 
238  public function readData(string $a_entity, string $a_version, array $a_ids): void
239  {
240  global $DIC;
241 
242  if (!is_array($a_ids)) {
243  $a_ids = array($a_ids);
244  }
245 
246  //var_dump([$a_entity, $a_version, $a_id]); exit;
247  if ($a_entity == "cmix") {
248  switch ($a_version) {
249  case "5.1.0":
250  $this->getDirectDataFromQuery("SELECT obj_id id, title, description " .
251  " FROM object_data " .
252  "WHERE " .
253  $DIC->database()->in("obj_id", $a_ids, false, "integer"));
254  break;
255  } // EOF switch
256  } // EOF if( $a_entity == "cmix" )
257 
258  foreach ($this->data as $key => $data) {
259  $query = "SELECT " . implode(",", array_keys($this->_element_db_mapping)) . " ";
260  $query .= "FROM `cmix_settings` ";
261  $query .= "WHERE " . $DIC->database()->in("obj_id", $a_ids, false, "integer");
262  $result = $DIC->database()->query($query);
263  //$this->data = [];
264  if ($dataset = $DIC->database()->fetchAssoc($result)) {
265  $this->_data = $dataset;
266  }
267 
268  //foreach( $this->_data AS $key => $data ) {
269  foreach ($this->_data as $dbColName => $value) {
270  $attr = $this->_element_db_mapping[$dbColName];
271  $this->data[$key][$attr] = $value;
272  //$this->data[$key][$dbColName] = $value;
273  } // EOF foreach ($this->_dataSetMapping as $dbColName => $value)
274  } // EOF foreach( $this->_data AS $key => $data )
275  $this->data = $this->data[0];
276  //var_dump($this->data); exit;
277  }
278 
279  public function buildMetaData(int $id): string
280  {
281  $md2xml = new ilMD2XML($id, $id, "cmix");
282  $md2xml->startExport();
283  return $md2xml->getXML();
284  }
285 
286  private function buildManifest(): string
287  {
288  $manWriter = new ilXmlWriter();
289  $manWriter->xmlHeader();
290  foreach ($this->_archive['files'] as $key => $value) {
291  $manWriter->xmlElement($key, null, $value, true, true);
292  }
293  #$manWriter->appendXML ("</content>\n");
294  return $manWriter->xmlDumpMem(true);
295  }
296 
297  private function buildProperties(mixed $a_entity, bool $a_omit_header = false): string
298  {
299  $atts = array(
300  "InstallationId" => IL_INST_ID,
301  "InstallationUrl" => ILIAS_HTTP_PATH,
302  "TopEntity" => $a_entity
303  );
304 
305  $writer = new ilXmlWriter();
306 
307  $writer->xmlStartTag('DataSet', $atts);
308 
309  if (!$a_omit_header) {
310  $writer->xmlHeader();
311  }
312 
313  foreach ($this->data as $key => $value) {
314  $writer->xmlElement($key, null, $value, true, true);
315  }
316 
317  $writer->xmlEndTag("DataSet");
318 
319  return $writer->xmlDumpMem(true);
320  }
321 
322  public function importRecord(
323  string $a_entity,
324  array $a_types,
325  array $a_rec,
326  ilImportMapping $a_mapping,
327  string $a_schema_version
328  ): void {
329  //var_dump( [$a_entity, $a_types, $a_rec, $a_mapping, $a_schema_version] ); exit;
330  switch ($a_entity) {
331  case "cmix":
332 
333  if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_rec['Id'])) {
334  $newObj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
335  } else {
336  $newObj = new ilObjCmiXapi();
337  $newObj->setType("cmix");
338  $newObj->create(true);
339  }
340 
341  $newObj->setTitle($a_rec["Title"]);
342  $newObj->setDescription($a_rec["Description"]);
343  $newObj->update();
344 
345  //$this->current_obj = $newObj;
346  $a_mapping->addMapping("Modules/CmiXapi", "cmix", $a_rec["Id"], (string) $newObj->getId());
347  break;
348  }
349  }
350 
355  public function getSupportedVersions(): array
356  {
357  return array("5.1.0");
358  }
359 
363  public function getXmlNamespace(string $a_entity, string $a_schema_version): string
364  {
365  return "http://www.ilias.de/xml/Modules/CmiXapi/" . $a_entity;
366  }
367 }
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)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
$GLOBALS["DIC"]
Definition: wac.php:31
const CLIENT_ID
Definition: constants.php:41
string $key
Consumer key/client ID value.
Definition: System.php:193
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:23
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