ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestExport.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 require_once './Modules/Test/classes/inc.AssessmentConstants.php';
24 
36 abstract class ilTestExport
37 {
38  private string $export_dir;
39  private string $subdir;
40  private string $qti_filename;
41  private string $filename;
42  private string $resultsfile;
43 
44  private ?ilXmlWriter $xml;
45  private ilLanguage $lng;
46 
47  protected bool $resultExportingEnabledForTestExport = false;
48 
50  protected ilBenchmark $bench;
51 
52  protected ilErrorHandling $err;
53  protected ilDBInterface $db;
54  protected ILIAS $ilias;
55 
56  protected string $inst_id;
58 
59  public function __construct(
60  public ilObjTest $test_obj,
61  public string $mode = "xml"
62  ) {
63  global $DIC;
64  $this->err = $DIC['ilErr'];
65  $this->ilias = $DIC['ilias'];
66  $this->db = $DIC['ilDB'];
67  $this->lng = $DIC['lng'];
68  $this->bench = $DIC['ilBench'];
69  $this->irss = $DIC['resource_storage'];
70 
71  $this->inst_id = (string) IL_INST_ID;
72 
73  $date = time();
74  $this->export_dir = $this->test_obj->getExportDirectory();
75 
76  switch ($this->mode) {
77  case "results":
78  $this->subdir = $date . "__" . $this->inst_id . "__" .
79  "tst__results_" . $this->test_obj->getId();
80  break;
81  case "aggregated":
82  $this->subdir = $date . "__" . $this->inst_id . "__" .
83  "test__aggregated__results_" . $this->test_obj->getId();
84  break;
85  default:
86  $this->subdir = $date . "__" . $this->inst_id . "__" .
87  "tst" . "_" . $this->test_obj->getId();
88  $this->filename = $this->subdir . ".xml";
89  $this->resultsfile = $date . "__" . $this->inst_id . "__" .
90  "results" . "_" . $this->test_obj->getId() . ".xml";
91  $this->qti_filename = $date . "__" . $this->inst_id . "__" .
92  "qti" . "_" . $this->test_obj->getId() . ".xml";
93  break;
94  }
95  $this->filename = $this->subdir . "." . $this->getExtension();
96  }
97 
98  public function isResultExportingEnabledForTestExport(): bool
99  {
101  }
102 
103  public function setResultExportingEnabledForTestExport(bool $resultExprtingEnabledForTestExport): void
104  {
105  $this->resultExportingEnabledForTestExport = $resultExprtingEnabledForTestExport;
106  }
107 
109  {
111  }
112 
113  public function setForcedAccessFilteredParticipantList(ilTestParticipantList $forcedAccessFilteredParticipantList): void
114  {
115  $this->forcedAccessFilteredParticipantList = $forcedAccessFilteredParticipantList;
116  }
117 
119  {
122  }
123 
124  return $this->test_obj->buildStatisticsAccessFilteredParticipantList();
125  }
126 
127  public function getExtension(): string
128  {
129  switch ($this->mode) {
130  case "results":
131  return "csv";
132  break;
133  default:
134  return "xml";
135  break;
136  }
137  }
138 
139  public function getInstId()
140  {
141  return $this->inst_id;
142  }
143 
144  public function buildExportFile(): string
145  {
146  switch ($this->mode) {
147  case "results":
148  return $this->buildExportResultFile();
149  break;
150  default:
151  return $this->buildExportFileXML();
152  break;
153  }
154  }
155 
156  public function buildExportResultFile(): string
157  {
158  //get Log File
159  $expDir = $this->test_obj->getExportDirectory();
160 
161  // make_directories
162  $this->test_obj->createExportDirectory();
163  ilFileUtils::makeDir($this->export_dir);
164 
165  $expLog = new ilLog($expDir, "export.log");
166  $expLog->delete();
167  $expLog->setLogFormat("");
168  $expLog->write(date("[y-m-d H:i:s] ") . "Start Export Of Results");
169 
170  $data = (new ilCSVTestExport($this->test_obj, ilTestEvaluationData::FILTER_BY_NONE, '', false))->withAllResults()->getContent();
171  $file = fopen($this->export_dir . "/" . $this->filename, "wb");
172  fwrite($file, $data);
173  fclose($file);
174 
175  $worksheet = (new ilExcelTestExport($this->test_obj, ilTestEvaluationData::FILTER_BY_NONE, '', false, true))
176  ->withResultsPage()
177  ->withUserPages()
178  ->getContent();
179  $worksheet->writeToFile($this->export_dir . "/" . str_replace($this->getExtension(), "xlsx", $this->filename));
180  // end
181  $expLog->write(date("[y-m-d H:i:s] ") . "Finished Export of Results");
182 
183  return $this->export_dir . "/" . $this->filename;
184  }
185 
186  abstract protected function initXmlExport();
187 
188  abstract protected function getQuestionIds();
189 
190  public function buildExportFileXML(): string
191  {
192  $this->bench->start("TestExport", "buildExportFile");
193 
194  $this->initXmlExport();
195 
196  $this->xml = new ilXmlWriter();
197 
198  // set dtd definition
199  $this->xml->xmlSetDtdDef("<!DOCTYPE Test SYSTEM \"http://www.ilias.uni-koeln.de/download/dtd/ilias_co.dtd\">");
200 
201  // set generated comment
202  $this->xml->xmlSetGenCmt("Export of ILIAS Test " .
203  $this->test_obj->getId() . " of installation " . $this->inst_id . ".");
204 
205  // set xml header
206  $this->xml->xmlHeader();
207 
208  $this->xml->xmlStartTag("ContentObject", array('Type' => 'Test'));
209 
210  // create directories
211  $this->test_obj->createExportDirectory();
212  ilFileUtils::makeDir($this->export_dir . "/" . $this->subdir);
213  ilFileUtils::makeDir($this->export_dir . "/" . $this->subdir . "/objects");
214 
215  // get Log File
216  $expDir = $this->test_obj->getExportDirectory();
217  $expLog = new ilLog($expDir, "export.log");
218  $expLog->delete();
219  $expLog->setLogFormat("");
220  $expLog->write(date("[y-m-d H:i:s] ") . "Start Export");
221 
222  // write qti file
223  $qti_file = fopen($this->export_dir . "/" . $this->subdir . "/" . $this->qti_filename, "wb");
224  fwrite($qti_file, $this->getQtiXml());
225  fclose($qti_file);
226 
227  // get xml content
228  $this->bench->start("TestExport", "buildExportFile_getXML");
229  $this->test_obj->exportPagesXML(
230  $this->xml,
231  $this->inst_id,
232  $this->export_dir . "/" . $this->subdir,
233  $expLog
234  );
235  $this->bench->stop("TestExport", "buildExportFile_getXML");
236 
237  $this->populateQuestionSetConfigXml($this->xml);
238 
239  $assignmentList = $this->buildQuestionSkillAssignmentList();
240  $this->populateQuestionSkillAssignmentsXml($this->xml, $assignmentList, $this->getQuestionIds());
241  $this->populateSkillLevelThresholdsXml($this->xml, $assignmentList);
242 
243  $this->xml->xmlEndTag("ContentObject");
244 
245  $this->bench->start("TestExport", "buildExportFile_dumpToFile");
246  $this->xml->xmlDumpFile($this->export_dir . "/" . $this->subdir . "/" . $this->filename, false);
247  $this->bench->stop("TestExport", "buildExportFile_dumpToFile");
248 
250  $resultwriter = new ilTestResultsToXML(
251  $this->test_obj->getTestId(),
252  $this->db,
253  $this->irss,
254  $this->export_dir . "/" . $this->subdir . "/objects",
255  $this->test_obj->getAnonymity()
256  );
257  $resultwriter->setIncludeRandomTestQuestionsEnabled($this->test_obj->isRandomTest());
258  $this->bench->start("TestExport", "buildExportFile_results");
259  $resultwriter->xmlDumpFile($this->export_dir . "/" . $this->subdir . "/" . $this->resultsfile, false);
260  $this->bench->stop("TestExport", "buildExportFile_results");
261  }
262 
263  // add media objects which were added with tiny mce
264  $this->bench->start("QuestionpoolExport", "buildExportFile_saveAdditionalMobs");
265  $this->exportXHTMLMediaObjects($this->export_dir . "/" . $this->subdir);
266  $this->bench->stop("QuestionpoolExport", "buildExportFile_saveAdditionalMobs");
267 
268  // zip the file
269  $this->bench->start("TestExport", "buildExportFile_zipFile");
271  $this->export_dir . "/" . $this->subdir,
272  $this->export_dir . "/" . $this->subdir . ".zip"
273  );
274  $this->bench->stop("TestExport", "buildExportFile_zipFile");
275 
276  // destroy writer object
277  $this->xml = null;
278 
279  $expLog->write(date("[y-m-d H:i:s] ") . "Finished Export");
280  $this->bench->stop("TestExport", "buildExportFile");
281 
282  return $this->export_dir . "/" . $this->subdir . ".zip";
283  }
284 
285  abstract protected function populateQuestionSetConfigXml(ilXmlWriter $xmlWriter);
286 
287  protected function getQtiXml()
288  {
289  $tstQtiXml = $this->test_obj->toXML();
290  $qstQtiXml = $this->getQuestionsQtiXml();
291 
292  if (strpos($tstQtiXml, "</section>") !== false) {
293  $qtiXml = str_replace("</section>", "$qstQtiXml</section>", $tstQtiXml);
294  } else {
295  $qtiXml = str_replace("<section ident=\"1\"/>", "<section ident=\"1\">\n$qstQtiXml</section>", $tstQtiXml);
296  }
297 
298  return $qtiXml;
299  }
300 
301  abstract protected function getQuestionsQtiXml();
302 
303  protected function getQuestionQtiXml($questionId): string
304  {
305  $questionOBJ = assQuestion::instantiateQuestion($questionId);
306  $xml = $questionOBJ->toXML(false);
307 
308  // still neccessary? there is an include header flag!?
309  $xml = preg_replace("/<questestinterop>/", "", $xml);
310  $xml = preg_replace("/<\/questestinterop>/", "", $xml);
311 
312  return $xml;
313  }
314 
315  public function exportXHTMLMediaObjects($a_export_dir): void
316  {
317  $mobs = ilObjMediaObject::_getMobsOfObject("tst:html", $this->test_obj->getId());
318 
319  foreach ($mobs as $mob) {
320  if (ilObjMediaObject::_exists($mob)) {
321  $mob_obj = new ilObjMediaObject($mob);
322  $mob_obj->exportFiles($a_export_dir);
323  unset($mob_obj);
324  }
325  }
326  foreach ($this->getQuestionIds() as $question_id) {
327  $mobs = ilObjMediaObject::_getMobsOfObject("qpl:html", $question_id);
328  foreach ($mobs as $mob) {
329  if (ilObjMediaObject::_exists($mob)) {
330  $mob_obj = new ilObjMediaObject($mob);
331  $mob_obj->exportFiles($a_export_dir);
332  unset($mob_obj);
333  }
334  }
335  }
336  }
337 
339  ilXmlWriter $a_xml_writer,
340  ilAssQuestionSkillAssignmentList $assignmentList,
341  array $questions
342  ) {
343  $skillQuestionAssignmentExporter = new ilAssQuestionSkillAssignmentExporter();
344  $skillQuestionAssignmentExporter->setXmlWriter($a_xml_writer);
345  $skillQuestionAssignmentExporter->setQuestionIds($questions);
346  $skillQuestionAssignmentExporter->setAssignmentList($assignmentList);
347  $skillQuestionAssignmentExporter->export();
348  }
349 
351  ilXmlWriter $a_xml_writer,
352  ilAssQuestionSkillAssignmentList $assignmentList
353  ) {
354  $thresholdList = new ilTestSkillLevelThresholdList($this->db);
355  $thresholdList->setTestId($this->test_obj->getTestId());
356  $thresholdList->loadFromDb();
357 
358  $skillLevelThresholdExporter = new ilTestSkillLevelThresholdExporter();
359  $skillLevelThresholdExporter->setXmlWriter($a_xml_writer);
360  $skillLevelThresholdExporter->setAssignmentList($assignmentList);
361  $skillLevelThresholdExporter->setThresholdList($thresholdList);
362  $skillLevelThresholdExporter->export();
363  }
364 
366  {
367  $assignmentList = new ilAssQuestionSkillAssignmentList($this->db);
368  $assignmentList->setParentObjId($this->test_obj->getId());
369  $assignmentList->loadFromDb();
370  $assignmentList->loadAdditionalSkillData();
371 
372  return $assignmentList;
373  }
374 }
const IL_INST_ID
Definition: constants.php:40
Class ChatMainBarProvider .
getQuestionQtiXml($questionId)
ilTestParticipantList $forcedAccessFilteredParticipantList
setResultExportingEnabledForTestExport(bool $resultExprtingEnabledForTestExport)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Export class for tests.
__construct(public ilObjTest $test_obj, public string $mode="xml")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.ilLog.php:30
bool $resultExportingEnabledForTestExport
isResultExportingEnabledForTestExport()
global $DIC
Definition: feed.php:28
setForcedAccessFilteredParticipantList(ilTestParticipantList $forcedAccessFilteredParticipantList)
static instantiateQuestion(int $question_id)
ilDBInterface $db
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilErrorHandling $err
setIncludeRandomTestQuestionsEnabled(bool $include_random_test_questions_enabled)
exportXHTMLMediaObjects($a_export_dir)
header include for all ilias files.
populateQuestionSkillAssignmentsXml(ilXmlWriter $a_xml_writer, ilAssQuestionSkillAssignmentList $assignmentList, array $questions)
static _exists(int $id, bool $reference=false, ?string $type=null)
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
populateSkillLevelThresholdsXml(ilXmlWriter $a_xml_writer, ilAssQuestionSkillAssignmentList $assignmentList)
Error Handling & global info handling.
static zip(string $a_dir, string $a_file, bool $compress_content=false)
Class ilBenchmark.
ResourceStorage $irss
getForcedAccessFilteredParticipantList()
ilBenchmark $bench
ilXmlWriter $xml
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
populateQuestionSetConfigXml(ilXmlWriter $xmlWriter)