ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilScormAiccDataSet.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3 include_once("./Services/DataSet/classes/class.ilDataSet.php");
4 
6 {
7  function __construct()
8  {
9  $this->db_table = "sahs_lm";
10  $this->properties = [
11  //"OfflineZipCreated" => "datetime",
12  "Id" => ["db_col" => "id", "db_type" => "integer"],
13  //"EntryPage" => "integer",
14  "APIAdapterName" => ["db_col" => "api_adapter", "db_type" => "text"],
15  "APIFunctionsPrefix" => ["db_col" => "api_func_prefix", "db_type" => "text"],
16  "AssignedGlossary" => ["db_col" => "glossary", "db_type" => "integer"],
17  "AutoContinue" => ["db_col" => "auto_continue", "db_type" => "text"],
18  "AutoReviewChar" => ["db_col" => "auto_review", "db_type" => "text"],
19  "AutoSuspend" => ["db_col" => "auto_suspend", "db_type" => "text"],
20  "Auto_last_visited" => ["db_col" => "auto_last_visited", "db_type" => "text"],
21  "Check_values" => ["db_col" => "check_values", "db_type" => "text"],
22  "Comments" => ["db_col" => "comments", "db_type" => "text"],
23  "CreditMode" => ["db_col" => "credit", "db_type" => "text"],
24  "Debug" => ["db_col" => "debug", "db_type" => "text"],
25  "DebugPw" => ["db_col" => "debugpw", "db_type" => "text"],
26  "DefaultLessonMode" => ["db_col" => "default_lesson_mode", "db_type" => "text"],
27  "Editable" => ["db_col" => "editable", "db_type" => "integer"],
28  "Fourth_edition" => ["db_col" => "fourth_edition", "db_type" => "text"],
29  "Height" => ["db_col" => "height", "db_type" => "integer"],
30  "HideNavig" => ["db_col" => "hide_navig", "db_type" => "text"],
31  "Ie_compatibility" => ["db_col" => "ie_compatibility", "db_type" => "text"],
32  "Ie_force_render" => ["db_col" => "ie_force_render", "db_type" => "text"],
33  "Interactions" => ["db_col" => "interactions", "db_type" => "text"],
34  "Localization" => ["db_col" => "localization", "db_type" => "text"],
35  "MasteryScore" => ["db_col" => "mastery_score", "db_type" => "integer"],
36  "MaxAttempt" => ["db_col" => "max_attempt", "db_type" => "integer"],
37  "ModuleVersion" => ["db_col" => "module_version", "db_type" => "integer"],
38  "NoMenu" => ["db_col" => "no_menu", "db_type" => "text"],
39  "Objectives" => ["db_col" => "objectives", "db_type" => "text"],
40  "OfflineMode" => ["db_col" => "offline_mode", "db_type" => "text"],
41  "Online" => ["db_col" => "c_online", "db_type" => "text"],
42  "OpenMode" => ["db_col" => "open_mode", "db_type" => "integer"],
43  "Sequencing" => ["db_col" => "sequencing", "db_type" => "text"],
44  "SequencingExpertMode" => ["db_col" => "seq_exp_mode", "db_type" => "integer"],
45  "Session" => ["db_col" => "unlimited_session", "db_type" => "text"],
46  "StyleSheetId" => ["db_col" => "stylesheet", "db_type" => "integer"],
47  "SubType" => ["db_col" => "c_type", "db_type" => "text"],
48  "Time_from_lms" => ["db_col" => "time_from_lms", "db_type" => "text"],
49  "Tries" => ["db_col" => "question_tries", "db_type" => "integer"],
50  "Width" => ["db_col" => "width", "db_type" => "integer"]
51  ];
52 
53  $this->element_db_mapping = [];
54  foreach ($this->properties as $key => $value)
55  {
56  $this->element_db_mapping [$value["db_col"]] = $key;
57  }
58  }
59 
65  public function readData($a_entity, $a_version, $a_id, $a_field = "")
66  {
67  global $ilDB;
68 
69  $obj_id = ilObject::_lookupObjectId ($a_id);
70  $columns = [];
71  foreach ($this->properties as $property)
72  array_push ($columns, $property["db_col"]);
73 
74  $query = "SELECT " . implode (",", $columns) . " FROM " . $this->db_table;
75  $query .= " WHERE id=" . $ilDB->quote($obj_id, "integer");
76  $result = $ilDB->query($query);
77  $this->data = [];
78  if ($dataset = $ilDB->fetchAssoc($result))
79  {
80  $this->data = $dataset;
81  }
82 
83  $query = "SELECT title,description FROM object_data";
84  $query .= " WHERE obj_id=" . $ilDB->quote($obj_id, "integer");
85  $result = $ilDB->query($query);
86  while ($dataset = $ilDB->fetchAssoc($result))
87  {
88  $this->data ["title"] = $dataset["title"];
89  $this->data ["description"] = $dataset["description"];
90  }
91  }
92 
99  public function writeData ($a_entity, $a_version, $a_id, $data)
100  {
101  global $ilDB, $ilLog;
102  if (count ($data) > 0)
103  {
104  $columns = [];
105  foreach ($this->properties as $key => $value)
106  {
107  if ($key == "Id" || $key == "title"|| $key == "description") continue;
108  if (isset ($data[$key]))
109  if (count ($data[$key]) > 0)
110  $columns [$value["db_col"]] = [$value["db_type"], $data[$key][0]];
111  }
112  if (count ($columns) > 0)
113  {
114  $conditions ["id"] = ["integer", $a_id];
115  $ilDB->update ($this->db_table, $columns, $conditions);
116  }
117 
118  //setting title and description in table object_data
119  $od_table = "object_data";
120  $od_properties = [
121  "Title" => ["db_col" => "title", "db_type" => "text"],
122  "Description" => ["db_col" => "description", "db_type" => "text"]
123  ];
124  foreach ($od_properties as $key => $value)
125  {
126  if (isset ($data[$key]))
127  if (count ($data[$key]) > 0)
128  $od_columns [$value["db_col"]] = [$value["db_type"], $data[$key][0]];
129 
130  if (count ($od_columns) > 0)
131  {
132  $od_conditions ["obj_id"] = ["integer", $a_id];
133  $ilDB->update ("object_data", $od_columns, $od_conditions);
134  }
135  }
136  }
137  else
138  {
139  $ilLog->write ("no module properties for imported object");
140  }
141  }
142 
143  /* retrieve element name by database column name
144  */
145  public function getElementNameByDbColumn ($db_col_name)
146  {
147  if ($db_col_name == "title") return "Title";
148  if ($db_col_name == "description") return "Description";
149  return $this->element_db_mapping[$db_col_name];
150  }
151 
152  /* own getXmlRepresentation function to embed zipfile in xml
153  *
154  */
155  public function getExtendedXmlRepresentation($a_entity, $a_schema_version, $a_ids, $a_field = "", $a_omit_header = false, $a_omit_types = false)
156  {
157  $GLOBALS["ilLog"]->write(json_encode($this->getTypes("sahs", "5.1.0"), JSON_PRETTY_PRINT));
158 
159  global $ilCtrl, $ilDB;
160  $this->dircnt = 1;
161 
162  $this->readData($a_entity, $a_schema_version, $a_ids, $a_field = "");
163  $id = $this->data["id"];
164 
165  require_once ("./Services/Export/classes/class.ilExport.php");
166  $exportDir = ilExport::_getExportDirectory($id);
167 
168  // step 1: check target release and supported versions
169  // step 2: init writer
170  require_once ("./Services/Xml/classes/class.ilXmlWriter.php");
171  $writer = new ilXmlWriter();
172  if (!$a_omit_header) { $writer->xmlHeader(); }
173 
174  $atts = array("InstallationId" => IL_INST_ID,
175  "InstallationUrl" => ILIAS_HTTP_PATH, "TopEntity" => $a_entity);
176 
177  $writer->appendXML ("\n");
178  $writer->xmlStartTag($this->getDSPrefixString().'DataSet', $atts);
179  $writer->appendXML ("\n");
180 
181  foreach ($this->data as $key => $value)
182  {
183  $writer->xmlElement($this->getElementNameByDbColumn ($key), Null, $value, TRUE, TRUE);
184  $writer->appendXML ("\n");
185  }
186 
187  $lmDir = ilUtil::getWebspaceDir ("filesystem")."/lm_data/lm_".$id;
188  $baseFileName = "sahs_" . $id;
189  $scormBasePath=$exportDir . "/" . $baseFileName;
190  if (!file_exists ($exportDir)) mkdir ($exportDir, 0755, true);
191 
192  ilUtil::zip($lmDir, $scormBasePath, true);
193  $scormFilePath = $scormBasePath . ".zip";
194 
195  $writer->xmlEndTag($this->getDSPrefixString()."DataSet");
196  $writer->appendXML ("\n");
197 
198  $xml = $writer->xmlDumpMem(false);
199  $baseExportName = time() . "__" . IL_INST_ID . "__". $baseFileName;
200  $xmlFilePath = $exportDir . "/" . $baseExportName . ".xml";
201 
202  if (!file_exists ($xmlFilePath))
203  {
204  $xmlFile = fopen ($xmlFilePath, "w");
205  fwrite ($xmlFile, $xml);
206  fclose($xmlFile);
207  }
208 
209  //create metadata
210  $metaData = $this->buildMetaData ($id);
211 
212  $metaDataFilePath = $exportDir . "/" . $baseExportName . "_metadata.xml";
213  if (!file_exists ($metaDataFilePath))
214  {
215  $metaDataFile = fopen ($metaDataFilePath, "w");
216  fwrite ($metaDataFile, $metaData);
217  fclose($metaDataFile);
218  }
219 
220  //create manifest file
221  $manWriter = new ilXmlWriter();
222  $manWriter->xmlHeader();
223  $manWriter->appendXML ("\n<content>\n");
224 
225  $files = [
226  "scormFile" => $baseFileName . ".zip",
227  "properties" => $baseFileName . ".xml",
228  "metadata" => "metadata.xml"
229  ];
230  foreach ($files as $key => $value)
231  {
232  $manWriter->xmlElement($key, Null, $value, TRUE, TRUE);
233  $manWriter->appendXML ("\n");
234  }
235 
236  $manWriter->appendXML ("</content>\n");
237  $manifest = $manWriter->xmlDumpMem(false);
238 
239  $manifestFilePath = $exportDir . "/" . $baseExportName . "_manifest.xml";
240  if (!file_exists ($manifestFilePath))
241  {
242  $manifestFile = fopen ($manifestFilePath, "w");
243  fwrite ($manifestFile, $manifest);
244  fclose($manifestFile);
245  }
246 
247  $zArchive = new zipArchive();
248  $fileName = $exportDir . "/" . $baseExportName . ".zip";
249 
250  if ($zArchive->open($fileName, ZipArchive::CREATE)!==TRUE) {
251  exit("cannot open <$fileName>\n");
252  }
253 
254  //creating final zip file
255  $zArchive->addFile($xmlFilePath, $baseFileName . ".xml");
256  $zArchive->addFile($scormFilePath, $baseFileName . ".zip");
257  $zArchive->addFile($manifestFilePath, "manifest.xml");
258  $zArchive->addFile($metaDataFilePath, "metadata.xml");
259  $zArchive->close();
260  //delete temporary files
261  unlink ($xmlFilePath);
262  unlink ($scormFilePath);
263  unlink ($manifestFilePath);
264  unlink ($metaDataFilePath);
265 
266  return $fileName;
267  }
268 
269  public function buildMetaData($id)
270  {
271  require_once ("Services/MetaData/classes/class.ilMD2XML.php");
272  $md2xml = new ilMD2XML ($id, $id, "sahs");
273  $md2xml->startExport();
274  $xml = $md2xml->getXML();
275  return $xml;
276  }
277 
285  protected function getTypes($a_entity, $a_version)
286  {
287  if ($a_entity == "sahs")
288  {
289  switch ($a_version)
290  {
291  case "5.1.0":
292  $types = [];
293  foreach ($this->properties as $key => $value)
294  $types[$key] = $value["db_type"];
295  return $types;
296  break;
297  }
298  }
299  }
300 
306  function getXmlNamespace($a_entity, $a_schema_version)
307  {
308  return "http://www.ilias.de/xml/Modules/ScormAicc/".$a_entity;
309  }
310 
311  public function getDependencies()
312  {
313  return null;
314  }
315 
316  public function getSupportedVersions()
317  {
318  return ["5.1.0"];
319  }
320 }
321 
$files
Definition: add-vimline.php:18
Add some data
readData($a_entity, $a_version, $a_id, $a_field="")
Read data.
$result
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
XML writer class.
getExtendedXmlRepresentation($a_entity, $a_schema_version, $a_ids, $a_field="", $a_omit_header=false, $a_omit_types=false)
static _lookupObjectId($a_ref_id)
lookup object id
global $ilCtrl
Definition: ilias.php:18
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
Create styles array
The data for the language used.
writeData($a_entity, $a_version, $a_id, $data)
Write properties for imported object (actually updates !!)
global $ilDB
getElementNameByDbColumn($db_col_name)
static _getExportDirectory($a_obj_id, $a_type="xml", $a_obj_type="", $a_entity="")
Get export directory for an repository object.
Set document properties
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
A dataset contains in data in a common structure that can be shared and transformed for different pur...
getXmlNamespace($a_entity, $a_schema_version)
Get xml namespace.
if(! $in) $columns
Definition: Utf8Test.php:45
static getWebspaceDir($mode="filesystem")
get webspace directory
getTypes($a_entity, $a_version)
Get field types for entity.