ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilQuestionpoolExport.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
5 
16 {
17  public $err; // error object
18  public $db; // database object
19  public $ilias; // ilias object
23  public $qpl_obj; // questionpool object
24  public $questions; // array with question ids to export
25  public $inst_id; // installation id
26  public $mode;
27  public $lng;
28 
33  public function __construct($a_qpl_obj, $a_mode = "xml", $array_questions = null)
34  {
35  global $DIC;
36  $ilErr = $DIC['ilErr'];
37  $ilDB = $DIC['ilDB'];
38  $ilias = $DIC['ilias'];
39  $lng = $DIC['lng'];
40 
41  $this->qpl_obj = &$a_qpl_obj;
42  if (!is_array($array_questions)) {
43  $array_questions = &$a_qpl_obj->getAllQuestionIds();
44  }
45 
46  $this->err = &$ilErr;
47  $this->ilias = &$ilias;
48  $this->db = &$ilDB;
49  $this->mode = $a_mode;
50  $this->lng = &$lng;
51 
52  $settings = $this->ilias->getAllSettings();
53  $this->inst_id = IL_INST_ID;
54  $this->questions = $array_questions;
55  $date = time();
56  $this->qpl_obj->createExportDirectory();
57  switch ($this->mode) {
58  case "xml":
59  $this->export_dir = $this->qpl_obj->getExportDirectory('xml');
60  $this->subdir = $date . "__" . $this->inst_id . "__" .
61  "qpl" . "_" . $this->qpl_obj->getId();
62  $this->filename = $this->subdir . ".xml";
63  $this->qti_filename = $date . "__" . $this->inst_id . "__" .
64  "qti" . "_" . $this->qpl_obj->getId() . ".xml";
65  break;
66  case "xls":
67  $this->export_dir = $this->qpl_obj->getExportDirectory('xls');
68  $this->filename = $date . "__" . $this->inst_id . "__" .
69  "qpl" . "_" . $this->qpl_obj->getId() . ".xlsx";
70  $this->zipfilename = $date . "__" . $this->inst_id . "__" .
71  "qpl" . "_" . $this->qpl_obj->getId() . ".zip";
72  break;
73  default:
74  $this->export_dir = $this->qpl_obj->getExportDirectory('zip');
75  $this->subdir = $date . "__" . $this->inst_id . "__" .
76  "qpl" . "_" . $this->qpl_obj->getId();
77  $this->filename = $this->subdir . ".xml";
78  $this->qti_filename = $date . "__" . $this->inst_id . "__" .
79  "qti" . "_" . $this->qpl_obj->getId() . ".xml";
80  break;
81  }
82  }
83 
84  public function getInstId()
85  {
86  return $this->inst_id;
87  }
88 
89 
96  public function buildExportFile()
97  {
98  switch ($this->mode) {
99  case "xls":
100  return $this->buildExportFileXLS();
101  break;
102  case "xml":
103  default:
104  return $this->buildExportFileXML();
105  break;
106  }
107  }
108 
112  public function buildExportFileXML()
113  {
114  global $DIC;
115  $ilBench = $DIC['ilBench'];
116 
117  $ilBench->start("QuestionpoolExport", "buildExportFile");
118 
119  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
120  $this->xml = new ilXmlWriter;
121 
122  // set dtd definition
123  $this->xml->xmlSetDtdDef("<!DOCTYPE Test SYSTEM \"http://www.ilias.uni-koeln.de/download/dtd/ilias_co.dtd\">");
124 
125  // set generated comment
126  $this->xml->xmlSetGenCmt("Export of ILIAS Test Questionpool " .
127  $this->qpl_obj->getId() . " of installation " . $this->inst . ".");
128 
129  // set xml header
130  $this->xml->xmlHeader();
131 
132  // create directories
133  include_once "./Services/Utilities/classes/class.ilUtil.php";
134  ilUtil::makeDir($this->export_dir . "/" . $this->subdir);
135  ilUtil::makeDir($this->export_dir . "/" . $this->subdir . "/objects");
136 
137  // get Log File
138  $expDir = $this->qpl_obj->getExportDirectory();
139  ilUtil::makeDirParents($expDir);
140 
141  include_once "./Services/Logging/classes/class.ilLog.php";
142  $expLog = new ilLog($expDir, "export.log");
143  $expLog->delete();
144  $expLog->setLogFormat("");
145  $expLog->write(date("[y-m-d H:i:s] ") . "Start Export");
146 
147  // write qti file
148  $qti_file = fopen($this->export_dir . "/" . $this->subdir . "/" . $this->qti_filename, "w");
149  fwrite($qti_file, $this->qpl_obj->questionsToXML($this->questions));
150  fclose($qti_file);
151 
152  // get xml content
153  $ilBench->start("QuestionpoolExport", "buildExportFile_getXML");
154  $this->qpl_obj->objectToXmlWriter(
155  $this->xml,
156  $this->inst_id,
157  $this->export_dir . "/" . $this->subdir,
158  $expLog,
159  $this->questions
160  );
161  $ilBench->stop("QuestionpoolExport", "buildExportFile_getXML");
162 
163  // dump xml document to screen (only for debugging reasons)
164  /*
165  echo "<PRE>";
166  echo htmlentities($this->xml->xmlDumpMem($format));
167  echo "</PRE>";
168  */
169 
170  // dump xml document to file
171  $ilBench->start("QuestionpoolExport", "buildExportFile_dumpToFile");
172  $this->xml->xmlDumpFile($this->export_dir . "/" . $this->subdir . "/" . $this->filename, false);
173  $ilBench->stop("QuestionpoolExport", "buildExportFile_dumpToFile");
174 
175  // add media objects which were added with tiny mce
176  $ilBench->start("QuestionpoolExport", "buildExportFile_saveAdditionalMobs");
177  $this->exportXHTMLMediaObjects($this->export_dir . "/" . $this->subdir);
178  $ilBench->stop("QuestionpoolExport", "buildExportFile_saveAdditionalMobs");
179 
180  // zip the file
181  $ilBench->start("QuestionpoolExport", "buildExportFile_zipFile");
182  ilUtil::zip($this->export_dir . "/" . $this->subdir, $this->export_dir . "/" . $this->subdir . ".zip");
183  if (@is_dir($this->export_dir . "/" . $this->subdir)) {
184  // Do not delete this dir, since it is required for container exports
185  #ilUtil::delDir($this->export_dir."/".$this->subdir);
186  }
187 
188  $ilBench->stop("QuestionpoolExport", "buildExportFile_zipFile");
189 
190  // destroy writer object
191  $this->xml->_XmlWriter;
192 
193  $expLog->write(date("[y-m-d H:i:s] ") . "Finished Export");
194  $ilBench->stop("QuestionpoolExport", "buildExportFile");
195 
196  return $this->export_dir . "/" . $this->subdir . ".zip";
197  }
198 
199  public function exportXHTMLMediaObjects($a_export_dir)
200  {
201  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
202 
203  foreach ($this->questions as $question_id) {
204  $mobs = ilObjMediaObject::_getMobsOfObject("qpl:html", $question_id);
205  foreach ($mobs as $mob) {
206  if (ilObjMediaObject::_exists($mob)) {
207  $mob_obj = new ilObjMediaObject($mob);
208  $mob_obj->exportFiles($a_export_dir);
209  unset($mob_obj);
210  }
211  }
212  }
213  }
214 
218  protected function buildExportFileXLS()
219  {
220  require_once 'Modules/TestQuestionPool/classes/class.ilAssExcelFormatHelper.php';
221  require_once 'Modules/TestQuestionPool/classes/class.assQuestion.php';
222 
223  $worksheet = new ilAssExcelFormatHelper();
224  $worksheet->addSheet('Sheet 1');
225  $row = 1;
226  $col = 0;
227 
228  $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("title"));
229  $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("description"));
230  $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("question_type"));
231  $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("author"));
232  $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col++) . $row, $this->lng->txt("create_date"));
233  $worksheet->setFormattedExcelTitle($worksheet->getColumnCoord($col) . $row, $this->lng->txt("last_update"));
234 
235  $col = 0;
236  $row++;
237  $questions = $this->qpl_obj->getQuestionList();
238  foreach ($questions as $question) {
239  $worksheet->setCell($row, $col++, $question["title"]);
240  $worksheet->setCell($row, $col++, $question["description"]);
241  $worksheet->setCell($row, $col++, $this->lng->txt($question["type_tag"]));
242  $worksheet->setCell($row, $col++, $question["author"]);
243  $created = new ilDate($question["created"], IL_CAL_UNIX);
244  $worksheet->setCell($row, $col++, $created);
245  $updated = new ilDate($question["tstamp"], IL_CAL_UNIX);
246  $worksheet->setCell($row, $col++, $updated);
247  $col = 0;
248  $row++;
249  }
250 
251  $excelfile = $this->export_dir . '/' . $this->filename;
252  $worksheet->writeToFile($excelfile);
253  ilUtil::zip($excelfile, $this->export_dir . "/" . $this->zipfilename);
254  if (@file_exists($this->export_dir . "/" . $this->filename)) {
255  @unlink($this->export_dir . "/" . $this->filename);
256  }
257  }
258 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
static _getMobsOfObject($a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
get mobs of object
xmlSetDtdDef($dtdDef)
Sets dtd definition.
global $DIC
Definition: saml.php:7
XML writer class.
logging
Definition: class.ilLog.php:18
const IL_CAL_UNIX
$ilErr
Definition: raiseError.php:18
Export class for questionpools.
Class for single dates.
$mobs
buildExportFileXLS()
build xml export file
buildExportFile()
build export file (complete zip file)
buildExportFileXML()
build xml export file
redirection script todo: (a better solution should control the processing via a xml file) ...
Class ilObjMediaObject.
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
$filename
Definition: buildRTE.php:89
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
$row
__construct($a_qpl_obj, $a_mode="xml", $array_questions=null)
Constructor public.
Class ilAssExcelFormatHelper.
global $ilBench
Definition: ilias.php:18
global $ilDB
static _exists($a_id, $a_reference=false, $a_type=null)
checks wether a lm content object with specified id exists or not