ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilScormAiccDataSet.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 {
23  private string $db_table;
24  public array $properties;
25  private array $_archive;
26  private array $element_db_mapping;
27 
28  public function __construct()
29  {
30  $this->db_table = "sahs_lm";
31  $this->_archive = [];
32  $this->properties = [
33  "Id" => ["db_col" => "id", "db_type" => "integer"],
34  "APIAdapterName" => ["db_col" => "api_adapter", "db_type" => "text"],
35  "APIFunctionsPrefix" => ["db_col" => "api_func_prefix", "db_type" => "text"],
36  "AssignedGlossary" => ["db_col" => "glossary", "db_type" => "integer"],
37  "AutoContinue" => ["db_col" => "auto_continue", "db_type" => "text"],
38  "AutoReviewChar" => ["db_col" => "auto_review", "db_type" => "text"],
39  "AutoSuspend" => ["db_col" => "auto_suspend", "db_type" => "text"],
40  "Auto_last_visited" => ["db_col" => "auto_last_visited", "db_type" => "text"],
41  "Check_values" => ["db_col" => "check_values", "db_type" => "text"],
42  "Comments" => ["db_col" => "comments", "db_type" => "text"],
43  "CreditMode" => ["db_col" => "credit", "db_type" => "text"],
44  "Debug" => ["db_col" => "debug", "db_type" => "text"],
45  "DebugPw" => ["db_col" => "debugpw", "db_type" => "text"],
46  "DefaultLessonMode" => ["db_col" => "default_lesson_mode", "db_type" => "text"],
47  "Editable" => ["db_col" => "editable", "db_type" => "integer"],
48  "Fourth_edition" => ["db_col" => "fourth_edition", "db_type" => "text"],
49  "Height" => ["db_col" => "height", "db_type" => "integer"],
50  "HideNavig" => ["db_col" => "hide_navig", "db_type" => "text"],
51  "Ie_force_render" => ["db_col" => "ie_force_render", "db_type" => "text"],
52  "Interactions" => ["db_col" => "interactions", "db_type" => "text"],
53  "Localization" => ["db_col" => "localization", "db_type" => "text"],
54  "MasteryScore" => ["db_col" => "mastery_score", "db_type" => "integer"],
55  "MaxAttempt" => ["db_col" => "max_attempt", "db_type" => "integer"],
56  "ModuleVersion" => ["db_col" => "module_version", "db_type" => "integer"],
57  "NoMenu" => ["db_col" => "no_menu", "db_type" => "text"],
58  "Objectives" => ["db_col" => "objectives", "db_type" => "text"],
59  "OfflineMode" => ["db_col" => "offline_mode", "db_type" => "text"],
60  "OpenMode" => ["db_col" => "open_mode", "db_type" => "integer"],
61  "Sequencing" => ["db_col" => "sequencing", "db_type" => "text"],
62  "SequencingExpertMode" => ["db_col" => "seq_exp_mode", "db_type" => "integer"],
63  "Session" => ["db_col" => "unlimited_session", "db_type" => "text"],
64  "StyleSheetId" => ["db_col" => "stylesheet", "db_type" => "integer"],
65  "SubType" => ["db_col" => "c_type", "db_type" => "text"],
66  "Time_from_lms" => ["db_col" => "time_from_lms", "db_type" => "text"],
67  "Tries" => ["db_col" => "question_tries", "db_type" => "integer"],
68  "Width" => ["db_col" => "width", "db_type" => "integer"],
69  "IdSetting" => ["db_col" => "id_setting", "db_type" => "integer"],
70  "NameSetting" => ["db_col" => "name_setting", "db_type" => "integer"]
71  ];
72 
73  $this->element_db_mapping = [];
74  foreach ($this->properties as $key => $value) {
75  $this->element_db_mapping [$value["db_col"]] = $key;
76  }
77  }
78 
79  protected function getDependencies(
80  string $a_entity,
81  string $a_version,
82  ?array $a_rec = null,
83  ?array $a_ids = null
84  ): array {
85  return [];
86  }
87 
88  public function writeData(string $a_entity, string $a_version, int $a_id, array $data): void
89  {
90  global $DIC;
91  $ilDB = $DIC->database();
92  $ilLog = ilLoggerFactory::getLogger('sahs');
93  if (count($data) > 0) {
94  $columns = [];
95  foreach ($this->properties as $key => $value) {
96  if ($key === "Id" || $key === "title" || $key === "description") {
97  continue;
98  }
99  //fix localization and mastery_score
100  if ($key === "MasteryScore" && isset($data[$key][0]) && $data[$key][0] == 0) {
101  continue;
102  }
103  if ($key === "Localization" && isset($data[$key][0]) && $data[$key][0] == "") {
104  continue;
105  }
106  //end fix
107  if ( isset( $data[ $key ] ) ) {
108  $data_value = '';
109  if ( is_array( $data[ $key ] ) ) {
110  $data_value = $data[ $key ][ 0 ] ?? '';
111  }
112  else {
113  $data_value = $data[ $key ];
114  }
115  $columns [ $value[ "db_col" ] ] = [
116  $value[ "db_type" ],
117  $data_value
118  ];
119  }
120  }
121  if (is_array($columns)) {
122  if (count($columns) > 0) {
123  $conditions ["id"] = ["integer", $a_id];
124  $ilDB->update($this->db_table, $columns, $conditions);
125  }
126  }
127 
128  //setting title and description in table object_data
129  $od_table = "object_data";
130  $od_properties = [
131  "Title" => ["db_col" => "title", "db_type" => "text"],
132  "Description" => ["db_col" => "description", "db_type" => "text"]
133  ];
134  foreach ($od_properties as $key => $value) {
135  if ( isset( $data[ $key ] ) ) {
136  $data_value = '';
137  if ( is_array( $data[ $key ] ) ) {
138  $data_value = $data[ $key ][ 0 ] ?? '';
139  }
140  else {
141  $data_value = $data[ $key ];
142  }
143  $od_columns [ $value[ "db_col" ] ] = [
144  $value[ "db_type" ],
145  $data_value
146  ];
147  }
148 
149  if (isset($od_columns) && is_array($od_columns)) {
150  if (count($od_columns) > 0) {
151  $od_conditions ["obj_id"] = ["integer", $a_id];
152  $ilDB->update("object_data", $od_columns, $od_conditions);
153  }
154  }
155  }
156  } else {
157  $ilLog->write("no module properties for imported object");
158  }
159  }
160 
165  string $a_entity,
166  string $a_schema_version,
167  array $a_ids,
168  string $a_field = "",
169  bool $a_omit_header = false,
170  bool $a_omit_types = false
171  ): string {
172  $GLOBALS['DIC']["ilLog"]->write(json_encode($this->getTypes("sahs", "5.1.0"), JSON_PRETTY_PRINT));
173 
174  $this->dircnt = 1;
175 
176  $this->readData($a_entity, $a_schema_version, $a_ids, $a_field = "");
177  $id = (int) $this->data["id"];
178  $exportDir = ilExport::_getExportDirectory((int) $id, "xml", "sahs");
179 
180  // prepare archive skeleton
181  $objTypeAndId = "sahs_" . $id;
182  $this->_archive['directories'] = [
183  "exportDir" => ilExport::_getExportDirectory($id)
184  ,"tempDir" => ilExport::_getExportDirectory($id) . "/temp"
185  ,"archiveDir" => time() . "__" . IL_INST_ID . "__" . $objTypeAndId
186  ,"moduleDir" => $objTypeAndId
187  ];
188 
189  $this->_archive['files'] = [
190  "properties" => "properties.xml",
191  "metadata" => "metadata.xml",
192  "manifest" => 'manifest.xml',
193  'scormFile' => "content.zip"
194  ];
195 
196  // Prepare temp storage on the local filesystem
197  if (!file_exists($this->_archive['directories']['exportDir'])) {
198  mkdir($this->_archive['directories']['exportDir'], 0755, true);
199  //$DIC->filesystem()->storage()->createDir($this->_archive['directories']['tempDir']);
200  }
201  if (!file_exists($this->_archive['directories']['tempDir'])) {
202  mkdir($this->_archive['directories']['tempDir'], 0755, true);
203  }
204 
205  // build metadata xml file
206  file_put_contents(
207  $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['metadata'],
208  $this->buildMetaData($id)
209  );
210 
211  // build manifest xml file
212  file_put_contents(
213  $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['manifest'],
214  $this->buildManifest()
215  );
216 
217  // build content zip file
218  if (isset($this->_archive['files']['scormFile'])) {
219  $lmDir = ilFileUtils::getWebspaceDir("filesystem") . "/lm_data/lm_" . $id;
220  ilFileUtils::zip($lmDir, $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['scormFile'], true);
221  }
222 
223  // build property xml file
224  file_put_contents(
225  $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['properties'],
226  $this->buildProperties($a_entity, $a_omit_header)
227  );
228 
229  // zip tempDir and append to export folder
230  $fileName = $this->_archive['directories']['exportDir'] . "/" . $this->_archive['directories']['archiveDir'] . ".zip";
231  $zArchive = new ZipArchive();
232  if ($zArchive->open($fileName, ZipArchive::CREATE) !== true) {
233  exit("cannot open <$fileName>\n");
234  }
235  $zArchive->addFile(
236  $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['properties'],
237  $this->_archive['directories']['archiveDir'] . '/properties.xml'
238  );
239  $zArchive->addFile(
240  $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['manifest'],
241  $this->_archive['directories']['archiveDir'] . '/' . "manifest.xml"
242  );
243  $zArchive->addFile(
244  $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['metadata'],
245  $this->_archive['directories']['archiveDir'] . '/' . "metadata.xml"
246  );
247  if (isset($this->_archive['files']['scormFile'])) {
248  $zArchive->addFile(
249  $this->_archive['directories']['tempDir'] . "/" . $this->_archive['files']['scormFile'],
250  $this->_archive['directories']['archiveDir'] . '/content.zip'
251  );
252  }
253  $zArchive->close();
254 
255  // unlink tempDir and its content
256  unlink($this->_archive['directories']['tempDir'] . "/metadata.xml");
257  unlink($this->_archive['directories']['tempDir'] . "/manifest.xml");
258  unlink($this->_archive['directories']['tempDir'] . "/properties.xml");
259  if (isset($this->_archive['files']['scormFile']) && file_exists($this->_archive['directories']['tempDir'] . "/content.zip")) {
260  unlink($this->_archive['directories']['tempDir'] . "/content.zip");
261  }
262 
263  return $fileName;
264  }
265 
272  protected function getTypes(string $a_entity, string $a_version): array
273  {
274  if ($a_entity === "sahs") {
275  switch ($a_version) {
276  case "5.1.0":
277  $types = [];
278  foreach ($this->properties as $key => $value) {
279  $types[$key] = $value["db_type"];
280  }
281  return $types;
282  }
283  }
284  return [];
285  }
286 
287  public function readData(string $a_entity, string $a_version, array $a_ids, string $a_field = ""): void
288  {
289  global $DIC;
290  $ilDB = $DIC->database();
291 
292  $obj_id = (int) $a_ids[0];
293  $columns = [];
294  foreach ($this->properties as $property) {
295  $columns[] = $property["db_col"];
296  }
297 
298  $query = "SELECT " . implode(",", $columns) . " FROM " . $this->db_table;
299  $query .= " WHERE id=" . $ilDB->quote($obj_id, "integer");
300  $result = $ilDB->query($query);
301  $this->data = [];
302  if ($dataset = $ilDB->fetchAssoc($result)) {
303  $this->data = $dataset;
304  }
305 
306  $query = "SELECT title,description FROM object_data";
307  $query .= " WHERE obj_id=" . $ilDB->quote($obj_id, "integer");
308  $result = $ilDB->query($query);
309  while ($dataset = $ilDB->fetchAssoc($result)) {
310  $this->data ["title"] = $dataset["title"];
311  $this->data ["description"] = $dataset["description"];
312  }
313  }
314 
318  public function getElementNameByDbColumn(string $db_col_name): string
319  {
320  if ($db_col_name === "title") {
321  return "Title";
322  }
323  if ($db_col_name === "description") {
324  return "Description";
325  }
326  return $this->element_db_mapping[$db_col_name];
327  }
328 
329  public function buildMetaData(int $id): string
330  {
331  $md2xml = new ilMD2XML($id, $id, "sahs");
332  $md2xml->startExport();
333  return $md2xml->getXML();
334  }
335 
339  protected function getXmlNamespace(string $a_entity, string $a_schema_version): string
340  {
341  return "http://www.ilias.de/xml/Modules/ScormAicc/" . $a_entity;
342  }
343 
347  public function getSupportedVersions(): array
348  {
349  return ["5.1.0"];
350  }
351 
355  private function buildManifest(): string
356  {
357  $manWriter = new ilXmlWriter();
358  $manWriter->xmlHeader();
359  foreach ($this->_archive['files'] as $key => $value) {
360  $manWriter->xmlElement($key, null, $value, true, true);
361  }
362 
363  return $manWriter->xmlDumpMem(true);
364  }
365 
371  private function buildProperties($a_entity, bool $a_omit_header = false): string
372  {
373  $writer = new ilXmlWriter();
374 
375  if (!$a_omit_header) {
376  $writer->xmlHeader();
377  }
378 
379  $writer->appendXML("\n");
380  $writer->xmlStartTag('DataSet', array(
381  "InstallationId" => IL_INST_ID,
382  "InstallationUrl" => ILIAS_HTTP_PATH,
383  "TopEntity" => $a_entity
384  ));
385 
386  $writer->appendXML("\n");
387 
388  foreach ($this->data as $key => $value) {
389  $writer->xmlElement($this->getElementNameByDbColumn($key), null, $value, true, true);
390  $writer->appendXML("\n");
391  }
392 
393  $writer->xmlEndTag("DataSet");
394 
395  return $writer->xmlDumpMem(false);
396  }
397 }
static getWebspaceDir(string $mode="filesystem")
get webspace directory
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
exit
Definition: login.php:29
const IL_INST_ID
Definition: constants.php:40
static getLogger(string $a_component_id)
Get component logger.
getTypes(string $a_entity, string $a_version)
Get field types for entity.
getXmlNamespace(string $a_entity, string $a_schema_version)
Get xml namespace.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
Get export directory for an repository object.
getExtendedXmlRepresentation(string $a_entity, string $a_schema_version, array $a_ids, string $a_field="", bool $a_omit_header=false, bool $a_omit_types=false)
own getXmlRepresentation function to embed zipfile in xml
global $DIC
Definition: feed.php:28
$GLOBALS["DIC"]
Definition: wac.php:31
writeData(string $a_entity, string $a_version, int $a_id, array $data)
string $key
Consumer key/client ID value.
Definition: System.php:193
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
buildProperties($a_entity, bool $a_omit_header=false)
static zip(string $a_dir, string $a_file, bool $compress_content=false)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
readData(string $a_entity, string $a_version, array $a_ids, string $a_field="")
getElementNameByDbColumn(string $db_col_name)
retrieve element name by database column name