ILIAS  release_8 Revision v8.24
class.ilQuestionpoolExport.php
Go to the documentation of this file.
1<?php
2
19include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
20
31{
32 public $err; // error object
33 public $db; // database object
34 public $ilias; // ilias object
38 public $qpl_obj; // questionpool object
39 public $questions; // array with question ids to export
40 public $inst_id; // installation id
41 public $mode;
42 public $lng;
43
44 private string $export_dir = '';
45 private string $subdir = '';
46 private string $filename = '';
47 private string $zipfilename = '';
49
54 public function __construct($a_qpl_obj, $a_mode = "xml", $array_questions = null)
55 {
56 global $DIC;
57 $ilErr = $DIC['ilErr'];
58 $ilDB = $DIC['ilDB'];
59 $ilias = $DIC['ilias'];
60 $lng = $DIC['lng'];
61
62 $this->qpl_obj = &$a_qpl_obj;
63 if (!is_array($array_questions)) {
64 $array_questions = &$a_qpl_obj->getAllQuestionIds();
65 }
66
67 $this->err = &$ilErr;
68 $this->ilias = &$ilias;
69 $this->db = &$ilDB;
70 $this->mode = $a_mode;
71 $this->lng = &$lng;
72 $settings = $this->ilias->getAllSettings();
73 $this->inst_id = IL_INST_ID;
74 $this->questions = $array_questions;
75 $date = time();
76 $this->qpl_obj->createExportDirectory();
77 switch ($this->mode) {
78 case "xml":
79 $this->export_dir = $this->qpl_obj->getExportDirectory('xml');
80 $this->subdir = $date . "__" . $this->inst_id . "__" .
81 "qpl" . "_" . $this->qpl_obj->getId();
82 $this->filename = $this->subdir . ".xml";
83 $this->qti_filename = $date . "__" . $this->inst_id . "__" .
84 "qti" . "_" . $this->qpl_obj->getId() . ".xml";
85 break;
86 case "xls":
87 $this->export_dir = $this->qpl_obj->getExportDirectory('xls');
88 $this->filename = $date . "__" . $this->inst_id . "__" .
89 "qpl" . "_" . $this->qpl_obj->getId() . ".xlsx";
90 $this->zipfilename = $date . "__" . $this->inst_id . "__" .
91 "qpl" . "_" . $this->qpl_obj->getId() . ".zip";
92 break;
93 default:
94 $this->export_dir = $this->qpl_obj->getExportDirectory('zip');
95 $this->subdir = $date . "__" . $this->inst_id . "__" .
96 "qpl" . "_" . $this->qpl_obj->getId();
97 $this->filename = $this->subdir . ".xml";
98 $this->qti_filename = $date . "__" . $this->inst_id . "__" .
99 "qti" . "_" . $this->qpl_obj->getId() . ".xml";
100 break;
101 }
102 }
103
104 public function getInstId()
105 {
106 return $this->inst_id;
107 }
108
109
113 public function buildExportFile(): string
114 {
115 switch ($this->mode) {
116 case "xls":
117 return $this->buildExportFileXLS();
118 case "xml":
119 default:
120 return $this->buildExportFileXML();
121 }
122 }
123
127 public function buildExportFileXML(): string
128 {
129 global $DIC;
130 $ilBench = $DIC['ilBench'];
131
132 $ilBench->start("QuestionpoolExport", "buildExportFile");
133
134 include_once("./Services/Xml/classes/class.ilXmlWriter.php");
135 $this->xml = new ilXmlWriter();
136
137 // set dtd definition
138 $this->xml->xmlSetDtdDef("<!DOCTYPE Test SYSTEM \"http://www.ilias.uni-koeln.de/download/dtd/ilias_co.dtd\">");
139
140 // set generated comment
141 $this->xml->xmlSetGenCmt("Export of ILIAS Test Questionpool " .
142 $this->qpl_obj->getId() . " of installation " . $this->inst_id);
143
144 // set xml header
145 $this->xml->xmlHeader();
146
147 // create directories
148 include_once "./Services/Utilities/classes/class.ilUtil.php";
149 ilFileUtils::makeDir($this->export_dir . "/" . $this->subdir);
150 ilFileUtils::makeDir($this->export_dir . "/" . $this->subdir . "/objects");
151
152 // get Log File
153 $expDir = $this->qpl_obj->getExportDirectory();
155
156 include_once "./Services/Logging/classes/class.ilLog.php";
157 $expLog = new ilLog($expDir, "export.log");
158 $expLog->delete();
159 $expLog->setLogFormat("");
160 $expLog->write(date("[y-m-d H:i:s] ") . "Start Export");
161
162 // write qti file
163 $qti_file = fopen($this->export_dir . "/" . $this->subdir . "/" . $this->qti_filename, "w");
164 fwrite($qti_file, $this->qpl_obj->questionsToXML($this->questions));
165 fclose($qti_file);
166
167 // get xml content
168 $ilBench->start("QuestionpoolExport", "buildExportFile_getXML");
169 $this->qpl_obj->objectToXmlWriter(
170 $this->xml,
171 $this->inst_id,
172 $this->export_dir . "/" . $this->subdir,
173 $expLog,
174 $this->questions
175 );
176 $ilBench->stop("QuestionpoolExport", "buildExportFile_getXML");
177 $ilBench->start("QuestionpoolExport", "buildExportFile_dumpToFile");
178 $this->xml->xmlDumpFile($this->export_dir . "/" . $this->subdir . "/" . $this->filename, false);
179 $ilBench->stop("QuestionpoolExport", "buildExportFile_dumpToFile");
180
181 // add media objects which were added with tiny mce
182 $ilBench->start("QuestionpoolExport", "buildExportFile_saveAdditionalMobs");
183 $this->exportXHTMLMediaObjects($this->export_dir . "/" . $this->subdir);
184 $ilBench->stop("QuestionpoolExport", "buildExportFile_saveAdditionalMobs");
185
186 // zip the file
187 $ilBench->start("QuestionpoolExport", "buildExportFile_zipFile");
188 ilFileUtils::zip($this->export_dir . "/" . $this->subdir, $this->export_dir . "/" . $this->subdir . ".zip");
189
190 $ilBench->stop("QuestionpoolExport", "buildExportFile_zipFile");
191
192 $expLog->write(date("[y-m-d H:i:s] ") . "Finished Export");
193 $ilBench->stop("QuestionpoolExport", "buildExportFile");
194
195 return $this->export_dir . "/" . $this->subdir . ".zip";
196 }
197
198 public function exportXHTMLMediaObjects($a_export_dir): void
199 {
200 include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
201
202 foreach ($this->questions as $question_id) {
203 $mobs = ilObjMediaObject::_getMobsOfObject("qpl:html", $question_id);
204 foreach ($mobs as $mob) {
205 if (ilObjMediaObject::_exists($mob)) {
206 $mob_obj = new ilObjMediaObject($mob);
207 $mob_obj->exportFiles($a_export_dir);
208 unset($mob_obj);
209 }
210 }
211 }
212 }
213
217 protected function buildExportFileXLS(): string
218 {
219 require_once 'Modules/TestQuestionPool/classes/class.ilAssExcelFormatHelper.php';
220 require_once 'Modules/TestQuestionPool/classes/class.assQuestion.php';
221
222 $worksheet = new ilAssExcelFormatHelper();
223 $worksheet->addSheet('Sheet 1');
224 $row = 1;
225 $col = 0;
226
227 $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("title"));
228 $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("description"));
229 $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("question_type"));
230 $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("author"));
231 $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("create_date"));
232 $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col) . $row, $this->lng->txt("last_update"));
233
234 $col = 0;
235 $row++;
236 $questions = $this->qpl_obj->getQuestionList();
237 foreach ($questions as $question) {
238 $worksheet->setCell($row, $col++, $question["title"]);
239 $worksheet->setCell($row, $col++, $question["description"]);
240 $worksheet->setCell($row, $col++, $this->lng->txt($question["type_tag"]));
241 $worksheet->setCell($row, $col++, $question["author"]);
242 $created = new ilDate($question["created"], IL_CAL_UNIX);
243 $worksheet->setCell($row, $col++, $created);
244 $updated = new ilDate($question["tstamp"], IL_CAL_UNIX);
245 $worksheet->setCell($row, $col++, $updated);
246 $col = 0;
247 $row++;
248 }
249
250 $excelfile = $this->export_dir . '/' . $this->filename;
251 $worksheet->writeToFile($excelfile);
252 ilFileUtils::zip($excelfile, $this->export_dir . "/" . $this->zipfilename);
253 if (@file_exists($this->export_dir . "/" . $this->filename)) {
254 @unlink($this->export_dir . "/" . $this->filename);
255 }
256 return $this->export_dir . "/" . $this->zipfilename;
257 }
258}
const IL_CAL_UNIX
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class for single dates.
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static zip(string $a_dir, string $a_file, bool $compress_content=false)
zips given directory/file into given zip.file
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.ilLog.php:31
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
buildExportFileXLS()
build xml export file
__construct($a_qpl_obj, $a_mode="xml", $array_questions=null)
Constructor @access public.
buildExportFileXML()
build xml export file
buildExportFile()
build export file (complete zip file)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
$mobs
Definition: imgupload.php:70
int $updated
Timestamp for when the object was last updated.
Definition: System.php:158
int $created
Timestamp for when the object was created.
Definition: System.php:151
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
header include for all ilias files.
$ilErr
Definition: raiseError.php:17