ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.SurveyImportParserPre38.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("./Services/Xml/classes/class.ilSaxParser.php");
25 
29 define("METRIC_QUESTION_IDENTIFIER", "Metric Question");
30 define("NOMINAL_QUESTION_IDENTIFIER", "Nominal Question");
31 define("ORDINAL_QUESTION_IDENTIFIER", "Ordinal Question");
32 define("TEXT_QUESTION_IDENTIFIER", "Text Question");
33 
44 {
45  public $path;
46  public $depth;
48  public $spl;
49  public $error_code;
50  public $error_line;
51  public $error_col;
52  public $error_msg;
53  public $has_error;
54  public $size;
55  public $elements;
56  public $attributes;
57  public $texts;
58  public $text_size;
60  public $activetag;
61  public $material;
62  public $metadata;
63  public $responses;
64  public $response_id;
65  public $matrix;
66  public $is_matrix;
67  public $adjectives;
68  public $spl_exists;
69  public $in_survey;
70  public $survey;
72  public $surveyaccess;
73  public $questions;
75  public $constraints;
76  public $textblock;
77  public $textblocks;
82 
84  public $in_question;
85  public $in_reponse;
87 
95  public function __construct($a_spl_id, $a_xml_file = '', $spl_exists = false)
96  {
97  die("Class surveyImportParserPre38 is deprecated.");
98  parent::__construct($a_xml_file);
99  $this->spl_id = $a_spl_id;
100  $this->has_error = false;
101  $this->characterbuffer = "";
102  $this->activetag = "";
103  $this->material = array();
104  $this->depth = array();
105  $this->path = array();
106  $this->metadata = array();
107  $this->responses = array();
108  $this->response_id = "";
109  $this->matrix = array();
110  $this->is_matrix = false;
111  $this->adjectives = array();
112  $this->spl_exists = $spl_exists;
113  $this->survey = null;
114  $this->in_survey = false;
115  $this->anonymisation = 0;
116  $this->surveyaccess = "restricted";
117  $this->questions = array();
118  $this->original_question_id = "";
119  $this->constraints = array();
120  $this->textblock = "";
121  $this->textblocks = array();
122  $this->in_questionblock = false;
123  $this->questionblocks = array();
124  $this->questionblock = array();
125  $this->questionblocktitle = "";
126  $this->question_title = "";
127  $this->in_question = false;
128  $this->in_response = false;
129  $this->question_description = "";
130  }
131 
136  public function setSurveyObject($a_svy)
137  {
138  $this->survey = $a_svy;
139  }
140 
146  public function setHandlers($a_xml_parser)
147  {
148  xml_set_object($a_xml_parser, $this);
149  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
150  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
151  }
152 
156  public function startParsing()
157  {
158  parent::startParsing();
159  }
160 
166  public function parse($a_xml_parser, $a_fp = null)
167  {
168  switch ($this->getInputType()) {
169  case 'file':
170 
171  while ($data = fread($a_fp, 4096)) {
172  $parseOk = xml_parse($a_xml_parser, $data, feof($a_fp));
173  }
174  break;
175 
176  case 'string':
177  $parseOk = xml_parse($a_xml_parser, $this->getXMLContent());
178  break;
179  }
180  if (!$parseOk
181  && (xml_get_error_code($a_xml_parser) != XML_ERROR_NONE)) {
182  $this->error_code = xml_get_error_code($a_xml_parser);
183  $this->error_line = xml_get_current_line_number($a_xml_parser);
184  $this->error_col = xml_get_current_column_number($a_xml_parser);
185  $this->error_msg = xml_error_string($a_xml_parser);
186  $this->has_error = true;
187  return false;
188  }
189  return true;
190  }
191 
192  public function getParent($a_xml_parser)
193  {
194  if ($this->depth[$a_xml_parser] > 0) {
195  return $this->path[$this->depth[$a_xml_parser]-1];
196  } else {
197  return "";
198  }
199  }
200 
204  public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
205  {
206  $this->depth[$a_xml_parser]++;
207  $this->path[$this->depth[$a_xml_parser]] = strtolower($a_name);
208  $this->characterbuffer = "";
209  $this->activetag = $a_name;
210  $this->elements++;
211  $this->attributes+=count($a_attribs);
212  switch ($a_name) {
213  case "survey":
214  $this->in_survey = true;
215  if (is_object($this->survey)) {
216  if (strlen($a_attribs["title"])) {
217  $this->survey->setTitle($a_attribs["title"]);
218  }
219  }
220  break;
221  case "section":
222  //$this->spl->setTitle($a_attribs["title"]);
223  break;
224  case "item":
225  $this->original_question_id = $a_attribs["ident"];
226  $this->question_title = $a_attribs["title"];
227  $this->in_question = true;
228  break;
229  case "qtimetadata":
230  $this->metadata = array();
231  break;
232  case "qtimetadatafield":
233  array_push($this->metadata, array("label" => "", "entry" => ""));
234  break;
235  case "material":
236  $this->material = array();
237  array_push($this->material, array("text" => "", "image" => "", "label" => $a_attribs["label"]));
238  break;
239  case "render_fib":
240  if (is_object($this->activequestion)) {
241  if (strlen($a_attribs["minnumber"])) {
242  $this->activequestion->setMinimum($a_attribs["minnumber"]);
243  }
244  if (strlen($a_attribs["maxnumber"])) {
245  $this->activequestion->setMaximum($a_attribs["maxnumber"]);
246  }
247  }
248  break;
249  case "response_lid":
250  if (is_object($this->activequestion)) {
251  if (strcmp($this->activequestion->getQuestiontype(), "SurveyNominalQuestion") == 0) {
252  switch (strtolower($a_attribs["rcardinality"])) {
253  case "single":
254  $this->activequestion->setSubtype(1);
255  break;
256  case "multiple":
257  $this->activequestion->setSubtype(2);
258  break;
259  }
260  }
261  }
262  break;
263  case "response_label":
264  $this->in_response = true;
265  break;
266  }
267  }
268 
272  public function handlerCharacterData($a_xml_parser, $a_data)
273  {
274  $this->texts++;
275  $this->text_size+=strlen($a_data);
276  $this->characterbuffer .= $a_data;
277  $a_data = $this->characterbuffer;
278  }
279 
283  public function handlerEndTag($a_xml_parser, $a_name)
284  {
285  switch ($a_name) {
286  case "questestinterop":
287  if (is_object($this->survey)) {
288  // write constraints
289  if (count($this->constraints)) {
290  $relations = $this->survey->getAllRelations(true);
291  foreach ($this->constraints as $constraint) {
292  $this->survey->addConstraint($this->questions[$constraint["sourceref"]], $this->questions[$constraint["destref"]], $relations[$constraint["relation"]]["id"], $constraint["value"]);
293  }
294  }
295  // write question blocks
296  if (count($this->questionblocks)) {
297  foreach ($this->questionblocks as $data) {
298  $questionblock = $data["questions"];
299  $title = $data["title"];
300  $qblock = array();
301  foreach ($questionblock as $question_id) {
302  array_push($qblock, $this->questions[$question_id]);
303  }
304  $this->survey->createQuestionblock($title, true, $qblock);
305  }
306  }
307  $this->survey->saveToDb();
308 
309  // write textblocks
310  if (count($this->textblocks)) {
311  foreach ($this->textblocks as $original_id => $textblock) {
312  $this->survey->saveHeading($textblock, $this->questions[$original_id]);
313  }
314  }
315  }
316  break;
317  case "survey":
318  $this->in_survey = false;
319  break;
320  case "item":
321  if (is_object($this->activequestion)) {
322  $this->activequestion->setTitle($this->question_title);
323  $this->activequestion->setDescription($this->question_description);
324  $this->activequestion->saveToDb();
325  if (is_object($this->survey)) {
326  // duplicate the question for the survey
327  $question_id = $this->activequestion->duplicate(true);
328  $this->survey->addQuestion($question_id);
329  $this->questions[$this->original_question_id] = $question_id;
330  }
331  }
332  $this->in_question = false;
333  $this->question_description = "";
334  $this->activequestion = null;
335  break;
336  case "qticomment":
337  if (strcmp($this->getParent($a_xml_parser), "item") == 0) {
338  if (preg_match("/Questiontype\=(.*)/", $this->characterbuffer, $matches)) {
339  $questiontype = $matches[1];
340  switch ($matches[1]) {
342  $questiontype = "SurveyMetricQuestion";
343  break;
345  $questiontype = "SurveyMultipleChoiceQuestion";
346  break;
348  $questiontype = "SurveySingleChoiceQuestion";
349  break;
351  $questiontype = "SurveyTextQuestion";
352  break;
353  }
354  if (strlen($questiontype)) {
355  include_once "./Modules/SurveyQuestionPool/classes/class.$questiontype.php";
356  $this->activequestion = new $questiontype();
357  $this->activequestion->setObjId($this->spl_id);
358  } else {
359  $this->activequestion = null;
360  }
361  } elseif (preg_match("/Author\=(.*)/", $this->characterbuffer, $matches)) {
362  if (is_object($this->activequestion)) {
363  $this->activequestion->setAuthor($matches[1]);
364  }
365  } elseif (preg_match("/ILIAS Version\=(.*)/", $this->characterbuffer, $matches)) {
366  } else {
367  $this->question_description = $this->characterbuffer;
368  }
369  }
370  if (strcmp($this->getParent($a_xml_parser), "survey") == 0) {
371  if (preg_match("/Author\=(.*)/", $this->characterbuffer, $matches)) {
372  if (is_object($this->survey)) {
373  $this->survey->setAuthor($matches[1]);
374  }
375  } elseif (preg_match("/ILIAS Version\=(.*)/", $this->characterbuffer, $matches)) {
376  } else {
377  if (is_object($this->survey)) {
378  $this->survey->setDescription($this->characterbuffer);
379  }
380  }
381  }
382  break;
383  case "fieldlabel":
384  $this->metadata[count($this->metadata)-1]["label"] = $this->characterbuffer;
385  break;
386  case "fieldentry":
387  $this->metadata[count($this->metadata)-1]["entry"] = $this->characterbuffer;
388  break;
389  case "qtimetadata":
390  if (strcmp($this->getParent($a_xml_parser), "section") == 0) {
391  foreach ($this->metadata as $meta) {
392  switch ($meta["label"]) {
393  case "SCORM":
394  if (!$this->spl_exists) {
395  include_once "./Services/MetaData/classes/class.ilMDSaxParser.php";
396  include_once "./Services/MetaData/classes/class.ilMD.php";
397  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
398  $md_sax_parser = new ilMDSaxParser();
399  $md_sax_parser->setXMLContent($value["entry"]);
400  $md_sax_parser->setMDObject($tmp = new ilMD($this->spl_id, 0, "spl"));
401  $md_sax_parser->enableMDParsing(true);
402  $md_sax_parser->startParsing();
403  $spl = new ilObjSurveyQuestionPool($this->spl_id, false);
404  $spl->MDUpdateListener("General");
405  break;
406  }
407  }
408  }
409  }
410  if (is_object($this->activequestion)) {
411  foreach ($this->metadata as $meta) {
412  switch ($this->activequestion->getQuestionType()) {
413  case "SurveyMultipleQuestion":
414  switch ($meta["label"]) {
415  case "obligatory":
416  $this->activequestion->setObligatory($meta["entry"]);
417  break;
418  case "orientation":
419  $this->activequestion->setOrientation($meta["entry"]);
420  break;
421  }
422  break;
423  case "SurveySingleChoiceQuestion":
424  switch ($meta["label"]) {
425  case "obligatory":
426  $this->activequestion->setObligatory($meta["entry"]);
427  break;
428  case "orientation":
429  $this->activequestion->setOrientation($meta["entry"]);
430  break;
431  }
432  break;
433  case "SurveyMetricQuestion":
434  switch ($meta["label"]) {
435  case "obligatory":
436  $this->activequestion->setObligatory($meta["entry"]);
437  break;
438  case "subtype":
439  $this->activequestion->setSubtype($meta["entry"]);
440  break;
441  }
442  break;
443  case "SurveyTextQuestion":
444  switch ($meta["label"]) {
445  case "obligatory":
446  $this->activequestion->setObligatory($meta["entry"]);
447  break;
448  case "maxchars":
449  if (strlen($meta["entry"])) {
450  $this->activequestion->setMaxChars($meta["entry"]);
451  }
452  break;
453  }
454  break;
455  }
456  }
457  }
458  if (is_object($this->survey)) {
459  foreach ($this->metadata as $meta) {
460  switch ($meta["label"]) {
461  case "SCORM":
462  if (strcmp($this->getParent($a_xml_parser), "survey") == 0) {
463  include_once "./Services/MetaData/classes/class.ilMDSaxParser.php";
464  include_once "./Services/MetaData/classes/class.ilMD.php";
465  $md_sax_parser = new ilMDSaxParser();
466  $md_sax_parser->setXMLContent($meta["entry"]);
467  $md_sax_parser->setMDObject($tmp = new ilMD($this->survey->getId(), 0, "svy"));
468  $md_sax_parser->enableMDParsing(true);
469  $md_sax_parser->startParsing();
470  $this->survey->MDUpdateListener("General");
471  }
472  break;
473  case "author":
474  $this->survey->setAuthor($meta["entry"]);
475  break;
476  case "description":
477  $this->survey->setDescription($meta["entry"]);
478  break;
479  case "evaluation_access":
480  $this->survey->setEvaluationAccess($meta["entry"]);
481  break;
482  case "evaluation_access":
483  $this->survey->setEvaluationAccess($meta["entry"]);
484  break;
485  case "anonymize":
486  $this->survey->setAnonymize($meta["entry"]);
487  break;
488  case "status":
489  $this->survey->setStatus($meta["entry"]);
490  break;
491  case "startdate":
492  if (preg_match("/P(\d+)Y(\d+)M(\d+)DT0H0M0S/", $meta["entry"], $matches)) {
493  $this->survey->setStartDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
494  $this->survey->setStartDateEnabled(1);
495  }
496  break;
497  case "enddate":
498  if (preg_match("/P(\d+)Y(\d+)M(\d+)DT0H0M0S/", $meta["entry"], $matches)) {
499  $this->survey->setEndDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
500  $this->survey->setEndDateEnabled(1);
501  }
502  break;
503  case "display_question_titles":
504  if ($meta["entry"]) {
505  $this->survey->showQuestionTitles();
506  }
507  break;
508  }
509  if (preg_match("/questionblock_(\d+)/", $meta["label"], $matches)) {
510  // handle questionblocks
511  $qb = $meta["entry"];
512  preg_match("/<title>(.*?)<\/title>/", $qb, $matches);
513  $qb_title = $matches[1];
514  preg_match("/<questions>(.*?)<\/questions>/", $qb, $matches);
515  $qb_questions = $matches[1];
516  $qb_questions_array = explode(",", $qb_questions);
517  array_push($this->questionblocks, array(
518  "title" => $qb_title,
519  "questions" => $qb_questions_array
520  ));
521  }
522  if (preg_match("/constraint_(\d+)/", $meta["label"], $matches)) {
523  $constraint = $meta["entry"];
524  $constraint_array = explode(",", $constraint);
525  if (count($constraint_array) == 3) {
526  array_push($this->constraints, array(
527  "sourceref" => $matches[1],
528  "destref" => $constraint_array[0],
529  "relation" => $constraint_array[1],
530  "value" => $constraint_array[2]
531  ));
532  }
533  }
534  if (preg_match("/heading_(\d+)/", $meta["label"], $matches)) {
535  $heading = $meta["entry"];
536  $this->textblocks[$matches[1]] = $heading;
537  }
538  }
539  }
540  break;
541  case "mattext":
542  $this->material[count($this->material)-1]["text"] = $this->characterbuffer;
543  break;
544  case "matimage":
545  $this->material[count($this->material)-1]["image"] = $this->characterbuffer;
546  break;
547  case "material":
548  if ($this->in_survey) {
549  if (strcmp($this->getParent($a_xml_parser), "objectives") == 0) {
550  if (strcmp($this->material[0]["label"], "introduction") == 0) {
551  if (is_object($this->survey)) {
552  $this->survey->setIntroduction($this->material[0]["text"]);
553  }
554  }
555  if (strcmp($this->material[0]["label"], "outro") == 0) {
556  if (is_object($this->survey)) {
557  $this->survey->setOutro($this->material[0]["text"]);
558  }
559  }
560  $this->material = array();
561  }
562  } else {
563  switch ($this->getParent($a_xml_parser)) {
564  case "response_num":
565  case "response_lid":
566  case "response_str":
567  if (is_object($this->activequestion)) {
568  $this->activequestion->setMaterial($this->material[0]["text"], true, $this->material[0]["label"]);
569  }
570  break;
571  case "flow":
572  if (is_object($this->activequestion)) {
573  $this->activequestion->setQuestiontext($this->material[0]["text"]);
574  }
575  break;
576  }
577  if (is_object($this->activequestion)) {
578  if ($this->in_response) {
579  switch ($this->activequestion->getQuestiontype()) {
580  case "SurveyMultipleChoiceQuestion":
581  case "SurveySingleChoiceQuestion":
582  $this->activequestion->categories->addCategory($this->material[0]["text"]);
583  break;
584  }
585  }
586  }
587  }
588  break;
589  case "response_label":
590  $this->in_response = false;
591  break;
592  }
593  $this->depth[$a_xml_parser]--;
594  }
595 
596  public function getErrorCode()
597  {
598  return $this->error_code;
599  }
600 
601  public function getErrorLine()
602  {
603  return $this->error_line;
604  }
605 
606  public function getErrorColumn()
607  {
608  return $this->error_col;
609  }
610 
611  public function getErrorMessage()
612  {
613  return $this->error_msg;
614  }
615 
616  public function getFullError()
617  {
618  return "Error: " . $this->error_msg . " at line:" . $this->error_line . " column:" . $this->error_col;
619  }
620 
621  public function getXMLSize()
622  {
623  return $this->size;
624  }
625 
626  public function getXMLElements()
627  {
628  return $this->elements;
629  }
630 
631  public function getXMLAttributes()
632  {
633  return $this->attributes;
634  }
635 
636  public function getXMLTextSections()
637  {
638  return $this->texts;
639  }
640 
641  public function getXMLTextSize()
642  {
643  return $this->text_size;
644  }
645 
646  public function hasError()
647  {
648  return $this->has_error;
649  }
650 }
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
const NOMINAL_QUESTION_IDENTIFIER
const TEXT_QUESTION_IDENTIFIER
parse($a_xml_parser, $a_fp=null)
parse xml file
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
const METRIC_QUESTION_IDENTIFIER
Old survey question import/export identifiers.
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class private
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
Create styles array
The data for the language used.
const ORDINAL_QUESTION_IDENTIFIER
__construct($a_spl_id, $a_xml_file='', $spl_exists=false)
Constructor.
Survey Question Import Parser.
setSurveyObject($a_svy)
Sets a reference to a survey object public.