ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
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("./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  var $path;
46  var $depth;
48  var $spl;
54  var $size;
55  var $elements;
57  var $texts;
61  var $material;
62  var $metadata;
65  var $matrix;
70  var $survey;
82 
87 
95  function SurveyImportParserPre38(&$a_spl, $a_xml_file = '', $spl_exists = FALSE)
96  {
97  parent::ilSaxParser($a_xml_file);
98  $this->spl =& $a_spl;
99  $this->has_error = FALSE;
100  $this->characterbuffer = "";
101  $this->activetag = "";
102  $this->material = array();
103  $this->depth = array();
104  $this->path = array();
105  $this->metadata = array();
106  $this->responses = array();
107  $this->response_id = "";
108  $this->matrix = array();
109  $this->is_matrix = FALSE;
110  $this->adjectives = array();
111  $this->spl_exists = $spl_exists;
112  $this->survey = NULL;
113  $this->in_survey = FALSE;
114  $this->anonymisation = 0;
115  $this->surveyaccess = "restricted";
116  $this->questions = array();
117  $this->original_question_id = "";
118  $this->constraints = array();
119  $this->textblock = "";
120  $this->textblocks = array();
121  $this->in_questionblock = FALSE;
122  $this->questionblocks = array();
123  $this->questionblock = array();
124  $this->questionblocktitle = "";
125  $this->question_title = "";
126  $this->in_question = FALSE;
127  $this->in_response = FALSE;
128  $this->question_description = "";
129  }
130 
135  function setSurveyObject(&$a_svy)
136  {
137  $this->survey =& $a_svy;
138  }
139 
145  function setHandlers($a_xml_parser)
146  {
147  xml_set_object($a_xml_parser,$this);
148  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
149  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
150  }
151 
155  function startParsing()
156  {
158  }
159 
165  function parse($a_xml_parser,$a_fp = null)
166  {
167  switch($this->getInputType())
168  {
169  case 'file':
170 
171  while($data = fread($a_fp,4096))
172  {
173  $parseOk = xml_parse($a_xml_parser,$data,feof($a_fp));
174  }
175  break;
176 
177  case 'string':
178  $parseOk = xml_parse($a_xml_parser,$this->getXMLContent());
179  break;
180  }
181  if(!$parseOk
182  && (xml_get_error_code($a_xml_parser) != XML_ERROR_NONE))
183  {
184  $this->error_code = xml_get_error_code($a_xml_parser);
185  $this->error_line = xml_get_current_line_number($a_xml_parser);
186  $this->error_col = xml_get_current_column_number($a_xml_parser);
187  $this->error_msg = xml_error_string($a_xml_parser);
188  $this->has_error = TRUE;
189  return false;
190  }
191  return true;
192  }
193 
194  function getParent($a_xml_parser)
195  {
196  if ($this->depth[$a_xml_parser] > 0)
197  {
198  return $this->path[$this->depth[$a_xml_parser]-1];
199  }
200  else
201  {
202  return "";
203  }
204  }
205 
209  function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
210  {
211  $this->depth[$a_xml_parser]++;
212  $this->path[$this->depth[$a_xml_parser]] = strtolower($a_name);
213  $this->characterbuffer = "";
214  $this->activetag = $a_name;
215  $this->elements++;
216  $this->attributes+=count($a_attribs);
217  switch ($a_name)
218  {
219  case "survey":
220  $this->in_survey = TRUE;
221  if (is_object($this->survey))
222  {
223  if (strlen($a_attribs["title"]))
224  {
225  $this->survey->setTitle($a_attribs["title"]);
226  }
227  }
228  break;
229  case "section":
230  $this->spl->setTitle($a_attribs["title"]);
231  break;
232  case "item":
233  $this->original_question_id = $a_attribs["ident"];
234  $this->question_title = $a_attribs["title"];
235  $this->in_question = TRUE;
236  break;
237  case "qtimetadata":
238  $this->metadata = array();
239  break;
240  case "qtimetadatafield":
241  array_push($this->metadata, array("label" => "", "entry" => ""));
242  break;
243  case "material":
244  $this->material = array();
245  array_push($this->material, array("text" => "", "image" => "", "label" => $a_attribs["label"]));
246  break;
247  case "render_fib":
248  if (is_object($this->activequestion))
249  {
250  if (strlen($a_attribs["minnumber"]))
251  {
252  $this->activequestion->setMinimum($a_attribs["minnumber"]);
253  }
254  if (strlen($a_attribs["maxnumber"]))
255  {
256  $this->activequestion->setMaximum($a_attribs["maxnumber"]);
257  }
258  }
259  break;
260  case "response_lid":
261  if (is_object($this->activequestion))
262  {
263  if (strcmp($this->activequestion->getQuestiontype(), "SurveyNominalQuestion") == 0)
264  {
265  switch (strtolower($a_attribs["rcardinality"]))
266  {
267  case "single":
268  $this->activequestion->setSubtype(1);
269  break;
270  case "multiple":
271  $this->activequestion->setSubtype(2);
272  break;
273  }
274  }
275  }
276  break;
277  case "response_label":
278  $this->in_response = TRUE;
279  break;
280  }
281  }
282 
286  function handlerCharacterData($a_xml_parser, $a_data)
287  {
288  $this->texts++;
289  $this->text_size+=strlen($a_data);
290  $this->characterbuffer .= $a_data;
291  $a_data = $this->characterbuffer;
292  }
293 
297  function handlerEndTag($a_xml_parser, $a_name)
298  {
299  switch ($a_name)
300  {
301  case "questestinterop":
302  if (is_object($this->survey))
303  {
304  // write constraints
305  if (count($this->constraints))
306  {
307  $relations = $this->survey->getAllRelations(TRUE);
308  foreach ($this->constraints as $constraint)
309  {
310  $this->survey->addConstraint($this->questions[$constraint["sourceref"]], $this->questions[$constraint["destref"]], $relations[$constraint["relation"]]["id"], $constraint["value"]);
311  }
312  }
313  // write question blocks
314  if (count($this->questionblocks))
315  {
316  foreach ($this->questionblocks as $data)
317  {
318  $questionblock = $data["questions"];
319  $title = $data["title"];
320  $qblock = array();
321  foreach ($questionblock as $question_id)
322  {
323  array_push($qblock, $this->questions[$question_id]);
324  }
325  $this->survey->createQuestionblock($title, TRUE, $qblock);
326  }
327  }
328  $this->survey->saveToDb();
329 
330  // write textblocks
331  if (count($this->textblocks))
332  {
333  foreach ($this->textblocks as $original_id => $textblock)
334  {
335  $this->survey->saveHeading($textblock, $this->questions[$original_id]);
336  }
337  }
338  }
339  break;
340  case "survey":
341  $this->in_survey = FALSE;
342  break;
343  case "item":
344  if (is_object($this->activequestion))
345  {
346  $this->activequestion->setTitle($this->question_title);
347  $this->activequestion->setDescription($this->question_description);
348  $this->activequestion->saveToDb();
349  if (is_object($this->survey))
350  {
351  // duplicate the question for the survey
352  $question_id = $this->activequestion->duplicate(TRUE);
353  $this->survey->addQuestion($question_id);
354  $this->questions[$this->original_question_id] = $question_id;
355  }
356  }
357  $this->in_question = FALSE;
358  $this->question_description = "";
359  $this->activequestion = NULL;
360  break;
361  case "qticomment":
362  if (strcmp($this->getParent($a_xml_parser), "item") == 0)
363  {
364  if (preg_match("/Questiontype\=(.*)/", $this->characterbuffer, $matches))
365  {
366  $questiontype = $matches[1];
367  switch ($matches[1])
368  {
370  $questiontype = "SurveyMetricQuestion";
371  break;
373  $questiontype = "SurveyMultipleChoiceQuestion";
374  break;
376  $questiontype = "SurveySingleChoiceQuestion";
377  break;
379  $questiontype = "SurveyTextQuestion";
380  break;
381  }
382  if (strlen($questiontype))
383  {
384  include_once "./Modules/SurveyQuestionPool/classes/class.$questiontype.php";
385  $this->activequestion = new $questiontype();
386  $this->activequestion->setObjId($this->spl->getId());
387  }
388  else
389  {
390  $this->activequestion = NULL;
391  }
392  }
393  else if (preg_match("/Author\=(.*)/", $this->characterbuffer, $matches))
394  {
395  if (is_object($this->activequestion))
396  {
397  $this->activequestion->setAuthor($matches[1]);
398  }
399  }
400  else if (preg_match("/ILIAS Version\=(.*)/", $this->characterbuffer, $matches))
401  {
402  }
403  else
404  {
405  $this->question_description = $this->characterbuffer;
406  }
407  }
408  if (strcmp($this->getParent($a_xml_parser), "survey") == 0)
409  {
410  if (preg_match("/Author\=(.*)/", $this->characterbuffer, $matches))
411  {
412  if (is_object($this->survey))
413  {
414  $this->survey->setAuthor($matches[1]);
415  }
416  }
417  else if (preg_match("/ILIAS Version\=(.*)/", $this->characterbuffer, $matches))
418  {
419  }
420  else
421  {
422  if (is_object($this->survey))
423  {
424  $this->survey->setDescription($this->characterbuffer);
425  }
426  }
427 
428  }
429  break;
430  case "fieldlabel":
431  $this->metadata[count($this->metadata)-1]["label"] = $this->characterbuffer;
432  break;
433  case "fieldentry":
434  $this->metadata[count($this->metadata)-1]["entry"] = $this->characterbuffer;
435  break;
436  case "qtimetadata":
437  if (strcmp($this->getParent($a_xml_parser), "section") == 0)
438  {
439  foreach ($this->metadata as $meta)
440  {
441  switch ($meta["label"])
442  {
443  case "SCORM":
444  if (!$this->spl_exists)
445  {
446  include_once "./Services/MetaData/classes/class.ilMDSaxParser.php";
447  include_once "./Services/MetaData/classes/class.ilMD.php";
448  $md_sax_parser = new ilMDSaxParser();
449  $md_sax_parser->setXMLContent($meta["entry"]);
450  $md_sax_parser->setMDObject($tmp = new ilMD($this->spl->getId(), 0, "spl"));
451  $md_sax_parser->enableMDParsing(TRUE);
452  $md_sax_parser->startParsing();
453  $this->spl->MDUpdateListener("General");
454  break;
455  }
456  }
457  }
458  }
459  if (is_object($this->activequestion))
460  {
461  foreach ($this->metadata as $meta)
462  {
463  switch ($this->activequestion->getQuestionType())
464  {
465  case "SurveyMultipleQuestion":
466  switch ($meta["label"])
467  {
468  case "obligatory":
469  $this->activequestion->setObligatory($meta["entry"]);
470  break;
471  case "orientation":
472  $this->activequestion->setOrientation($meta["entry"]);
473  break;
474  }
475  break;
476  case "SurveySingleChoiceQuestion":
477  switch ($meta["label"])
478  {
479  case "obligatory":
480  $this->activequestion->setObligatory($meta["entry"]);
481  break;
482  case "orientation":
483  $this->activequestion->setOrientation($meta["entry"]);
484  break;
485  }
486  break;
487  case "SurveyMetricQuestion":
488  switch ($meta["label"])
489  {
490  case "obligatory":
491  $this->activequestion->setObligatory($meta["entry"]);
492  break;
493  case "subtype":
494  $this->activequestion->setSubtype($meta["entry"]);
495  break;
496  }
497  break;
498  case "SurveyTextQuestion":
499  switch ($meta["label"])
500  {
501  case "obligatory":
502  $this->activequestion->setObligatory($meta["entry"]);
503  break;
504  case "maxchars":
505  if (strlen($meta["entry"]))
506  {
507  $this->activequestion->setMaxChars($meta["entry"]);
508  }
509  break;
510  }
511  break;
512  }
513  }
514  }
515  if (is_object($this->survey))
516  {
517  foreach ($this->metadata as $meta)
518  {
519  switch ($meta["label"])
520  {
521  case "SCORM":
522  if (strcmp($this->getParent($a_xml_parser), "survey") == 0)
523  {
524  include_once "./Services/MetaData/classes/class.ilMDSaxParser.php";
525  include_once "./Services/MetaData/classes/class.ilMD.php";
526  $md_sax_parser = new ilMDSaxParser();
527  $md_sax_parser->setXMLContent($meta["entry"]);
528  $md_sax_parser->setMDObject($tmp = new ilMD($this->survey->getId(), 0, "svy"));
529  $md_sax_parser->enableMDParsing(TRUE);
530  $md_sax_parser->startParsing();
531  $this->survey->MDUpdateListener("General");
532  }
533  break;
534  case "author":
535  $this->survey->setAuthor($meta["entry"]);
536  break;
537  case "description":
538  $this->survey->setDescription($meta["entry"]);
539  break;
540  case "evaluation_access":
541  $this->survey->setEvaluationAccess($meta["entry"]);
542  break;
543  case "evaluation_access":
544  $this->survey->setEvaluationAccess($meta["entry"]);
545  break;
546  case "anonymize":
547  $this->survey->setAnonymize($meta["entry"]);
548  break;
549  case "status":
550  $this->survey->setStatus($meta["entry"]);
551  break;
552  case "startdate":
553  if (preg_match("/P(\d+)Y(\d+)M(\d+)DT0H0M0S/", $meta["entry"], $matches))
554  {
555  $this->survey->setStartDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
556  $this->survey->setStartDateEnabled(1);
557  }
558  break;
559  case "enddate":
560  if (preg_match("/P(\d+)Y(\d+)M(\d+)DT0H0M0S/", $meta["entry"], $matches))
561  {
562  $this->survey->setEndDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
563  $this->survey->setEndDateEnabled(1);
564  }
565  break;
566  case "display_question_titles":
567  if ($meta["entry"])
568  {
569  $this->survey->showQuestionTitles();
570  }
571  break;
572  }
573  if (preg_match("/questionblock_(\d+)/", $meta["label"], $matches))
574  {
575  // handle questionblocks
576  $qb = $meta["entry"];
577  preg_match("/<title>(.*?)<\/title>/", $qb, $matches);
578  $qb_title = $matches[1];
579  preg_match("/<questions>(.*?)<\/questions>/", $qb, $matches);
580  $qb_questions = $matches[1];
581  $qb_questions_array = explode(",", $qb_questions);
582  array_push($this->questionblocks, array(
583  "title" => $qb_title,
584  "questions" => $qb_questions_array
585  ));
586  }
587  if (preg_match("/constraint_(\d+)/", $meta["label"], $matches))
588  {
589  $constraint = $meta["entry"];
590  $constraint_array = explode(",", $constraint);
591  if (count($constraint_array) == 3)
592  {
593  array_push($this->constraints, array(
594  "sourceref" => $matches[1],
595  "destref" => $constraint_array[0],
596  "relation" => $constraint_array[1],
597  "value" => $constraint_array[2]
598  ));
599  }
600  }
601  if (preg_match("/heading_(\d+)/", $meta["label"], $matches))
602  {
603  $heading = $meta["entry"];
604  $this->textblocks[$matches[1]] = $heading;
605  }
606  }
607  }
608  break;
609  case "mattext":
610  $this->material[count($this->material)-1]["text"] = $this->characterbuffer;
611  break;
612  case "matimage":
613  $this->material[count($this->material)-1]["image"] = $this->characterbuffer;
614  break;
615  case "material":
616  if ($this->in_survey)
617  {
618  if (strcmp($this->getParent($a_xml_parser), "objectives") == 0)
619  {
620  if (strcmp($this->material[0]["label"], "introduction") == 0)
621  {
622  if (is_object($this->survey))
623  {
624  $this->survey->setIntroduction($this->material[0]["text"]);
625  }
626  }
627  if (strcmp($this->material[0]["label"], "outro") == 0)
628  {
629  if (is_object($this->survey))
630  {
631  $this->survey->setOutro($this->material[0]["text"]);
632  }
633  }
634  $this->material = array();
635  }
636  }
637  else
638  {
639  switch ($this->getParent($a_xml_parser))
640  {
641  case "response_num":
642  case "response_lid":
643  case "response_str":
644  if (is_object($this->activequestion))
645  {
646  $this->activequestion->setMaterial($this->material[0]["text"], TRUE, $this->material[0]["label"]);
647  }
648  break;
649  case "flow":
650  if (is_object($this->activequestion))
651  {
652  $this->activequestion->setQuestiontext($this->material[0]["text"]);
653  }
654  break;
655  }
656  if (is_object($this->activequestion))
657  {
658  if ($this->in_response)
659  {
660  switch ($this->activequestion->getQuestiontype())
661  {
662  case "SurveyMultipleChoiceQuestion":
663  case "SurveySingleChoiceQuestion":
664  $this->activequestion->categories->addCategory($this->material[0]["text"]);
665  break;
666  }
667  }
668  }
669  }
670  break;
671  case "response_label":
672  $this->in_response = FALSE;
673  break;
674  }
675  $this->depth[$a_xml_parser]--;
676  }
677 
678  function getErrorCode()
679  {
680  return $this->error_code;
681  }
682 
683  function getErrorLine()
684  {
685  return $this->error_line;
686  }
687 
688  function getErrorColumn()
689  {
690  return $this->error_col;
691  }
692 
693  function getErrorMessage()
694  {
695  return $this->error_msg;
696  }
697 
698  function getFullError()
699  {
700  return "Error: ".$this->error_msg." at line:".$this->error_line ." column:".$this->error_col;
701  }
702 
703  function getXMLSize()
704  {
705  return $this->size;
706  }
707 
708  function getXMLElements()
709  {
710  return $this->elements;
711  }
712 
713  function getXMLAttributes()
714  {
715  return $this->attributes;
716  }
717 
718  function getXMLTextSections()
719  {
720  return $this->texts;
721  }
722 
723  function getXMLTextSize()
724  {
725  return $this->text_size;
726  }
727 
728  function hasError()
729  {
730  return $this->has_error;
731  }
732 
733 }
734 ?>