ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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
24require_once("./Services/Xml/classes/class.ilSaxParser.php");
25
29define("METRIC_QUESTION_IDENTIFIER", "Metric Question");
30define("NOMINAL_QUESTION_IDENTIFIER", "Nominal Question");
31define("ORDINAL_QUESTION_IDENTIFIER", "Ordinal Question");
32define("TEXT_QUESTION_IDENTIFIER", "Text Question");
33
44{
45 var $path;
46 var $depth;
48 var $spl;
54 var $size;
57 var $texts;
82
87
95 function SurveyImportParserPre38($a_spl_id, $a_xml_file = '', $spl_exists = FALSE)
96 {
97 parent::ilSaxParser($a_xml_file);
98 $this->spl_id = $a_spl_id;
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 {
157 parent::startParsing();
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_id);
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 include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
449 $md_sax_parser = new ilMDSaxParser();
450 $md_sax_parser->setXMLContent($value["entry"]);
451 $md_sax_parser->setMDObject($tmp = new ilMD($this->spl_id,0, "spl"));
452 $md_sax_parser->enableMDParsing(true);
453 $md_sax_parser->startParsing();
454 $spl = new ilObjSurveyQuestionPool($this->spl_id, false);
455 $spl->MDUpdateListener("General");
456 break;
457 }
458 }
459 }
460 }
461 if (is_object($this->activequestion))
462 {
463 foreach ($this->metadata as $meta)
464 {
465 switch ($this->activequestion->getQuestionType())
466 {
467 case "SurveyMultipleQuestion":
468 switch ($meta["label"])
469 {
470 case "obligatory":
471 $this->activequestion->setObligatory($meta["entry"]);
472 break;
473 case "orientation":
474 $this->activequestion->setOrientation($meta["entry"]);
475 break;
476 }
477 break;
478 case "SurveySingleChoiceQuestion":
479 switch ($meta["label"])
480 {
481 case "obligatory":
482 $this->activequestion->setObligatory($meta["entry"]);
483 break;
484 case "orientation":
485 $this->activequestion->setOrientation($meta["entry"]);
486 break;
487 }
488 break;
489 case "SurveyMetricQuestion":
490 switch ($meta["label"])
491 {
492 case "obligatory":
493 $this->activequestion->setObligatory($meta["entry"]);
494 break;
495 case "subtype":
496 $this->activequestion->setSubtype($meta["entry"]);
497 break;
498 }
499 break;
500 case "SurveyTextQuestion":
501 switch ($meta["label"])
502 {
503 case "obligatory":
504 $this->activequestion->setObligatory($meta["entry"]);
505 break;
506 case "maxchars":
507 if (strlen($meta["entry"]))
508 {
509 $this->activequestion->setMaxChars($meta["entry"]);
510 }
511 break;
512 }
513 break;
514 }
515 }
516 }
517 if (is_object($this->survey))
518 {
519 foreach ($this->metadata as $meta)
520 {
521 switch ($meta["label"])
522 {
523 case "SCORM":
524 if (strcmp($this->getParent($a_xml_parser), "survey") == 0)
525 {
526 include_once "./Services/MetaData/classes/class.ilMDSaxParser.php";
527 include_once "./Services/MetaData/classes/class.ilMD.php";
528 $md_sax_parser = new ilMDSaxParser();
529 $md_sax_parser->setXMLContent($meta["entry"]);
530 $md_sax_parser->setMDObject($tmp = new ilMD($this->survey->getId(), 0, "svy"));
531 $md_sax_parser->enableMDParsing(TRUE);
532 $md_sax_parser->startParsing();
533 $this->survey->MDUpdateListener("General");
534 }
535 break;
536 case "author":
537 $this->survey->setAuthor($meta["entry"]);
538 break;
539 case "description":
540 $this->survey->setDescription($meta["entry"]);
541 break;
542 case "evaluation_access":
543 $this->survey->setEvaluationAccess($meta["entry"]);
544 break;
545 case "evaluation_access":
546 $this->survey->setEvaluationAccess($meta["entry"]);
547 break;
548 case "anonymize":
549 $this->survey->setAnonymize($meta["entry"]);
550 break;
551 case "status":
552 $this->survey->setStatus($meta["entry"]);
553 break;
554 case "startdate":
555 if (preg_match("/P(\d+)Y(\d+)M(\d+)DT0H0M0S/", $meta["entry"], $matches))
556 {
557 $this->survey->setStartDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
558 $this->survey->setStartDateEnabled(1);
559 }
560 break;
561 case "enddate":
562 if (preg_match("/P(\d+)Y(\d+)M(\d+)DT0H0M0S/", $meta["entry"], $matches))
563 {
564 $this->survey->setEndDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
565 $this->survey->setEndDateEnabled(1);
566 }
567 break;
568 case "display_question_titles":
569 if ($meta["entry"])
570 {
571 $this->survey->showQuestionTitles();
572 }
573 break;
574 }
575 if (preg_match("/questionblock_(\d+)/", $meta["label"], $matches))
576 {
577 // handle questionblocks
578 $qb = $meta["entry"];
579 preg_match("/<title>(.*?)<\/title>/", $qb, $matches);
580 $qb_title = $matches[1];
581 preg_match("/<questions>(.*?)<\/questions>/", $qb, $matches);
582 $qb_questions = $matches[1];
583 $qb_questions_array = explode(",", $qb_questions);
584 array_push($this->questionblocks, array(
585 "title" => $qb_title,
586 "questions" => $qb_questions_array
587 ));
588 }
589 if (preg_match("/constraint_(\d+)/", $meta["label"], $matches))
590 {
591 $constraint = $meta["entry"];
592 $constraint_array = explode(",", $constraint);
593 if (count($constraint_array) == 3)
594 {
595 array_push($this->constraints, array(
596 "sourceref" => $matches[1],
597 "destref" => $constraint_array[0],
598 "relation" => $constraint_array[1],
599 "value" => $constraint_array[2]
600 ));
601 }
602 }
603 if (preg_match("/heading_(\d+)/", $meta["label"], $matches))
604 {
605 $heading = $meta["entry"];
606 $this->textblocks[$matches[1]] = $heading;
607 }
608 }
609 }
610 break;
611 case "mattext":
612 $this->material[count($this->material)-1]["text"] = $this->characterbuffer;
613 break;
614 case "matimage":
615 $this->material[count($this->material)-1]["image"] = $this->characterbuffer;
616 break;
617 case "material":
618 if ($this->in_survey)
619 {
620 if (strcmp($this->getParent($a_xml_parser), "objectives") == 0)
621 {
622 if (strcmp($this->material[0]["label"], "introduction") == 0)
623 {
624 if (is_object($this->survey))
625 {
626 $this->survey->setIntroduction($this->material[0]["text"]);
627 }
628 }
629 if (strcmp($this->material[0]["label"], "outro") == 0)
630 {
631 if (is_object($this->survey))
632 {
633 $this->survey->setOutro($this->material[0]["text"]);
634 }
635 }
636 $this->material = array();
637 }
638 }
639 else
640 {
641 switch ($this->getParent($a_xml_parser))
642 {
643 case "response_num":
644 case "response_lid":
645 case "response_str":
646 if (is_object($this->activequestion))
647 {
648 $this->activequestion->setMaterial($this->material[0]["text"], TRUE, $this->material[0]["label"]);
649 }
650 break;
651 case "flow":
652 if (is_object($this->activequestion))
653 {
654 $this->activequestion->setQuestiontext($this->material[0]["text"]);
655 }
656 break;
657 }
658 if (is_object($this->activequestion))
659 {
660 if ($this->in_response)
661 {
662 switch ($this->activequestion->getQuestiontype())
663 {
664 case "SurveyMultipleChoiceQuestion":
665 case "SurveySingleChoiceQuestion":
666 $this->activequestion->categories->addCategory($this->material[0]["text"]);
667 break;
668 }
669 }
670 }
671 }
672 break;
673 case "response_label":
674 $this->in_response = FALSE;
675 break;
676 }
677 $this->depth[$a_xml_parser]--;
678 }
679
680 function getErrorCode()
681 {
682 return $this->error_code;
683 }
684
685 function getErrorLine()
686 {
687 return $this->error_line;
688 }
689
690 function getErrorColumn()
691 {
692 return $this->error_col;
693 }
694
695 function getErrorMessage()
696 {
697 return $this->error_msg;
698 }
699
700 function getFullError()
701 {
702 return "Error: ".$this->error_msg." at line:".$this->error_line ." column:".$this->error_col;
703 }
704
705 function getXMLSize()
706 {
707 return $this->size;
708 }
709
710 function getXMLElements()
711 {
712 return $this->elements;
713 }
714
716 {
717 return $this->attributes;
718 }
719
721 {
722 return $this->texts;
723 }
724
725 function getXMLTextSize()
726 {
727 return $this->text_size;
728 }
729
730 function hasError()
731 {
732 return $this->has_error;
733 }
734
735}
736?>
Survey Question Import Parser.
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
parse($a_xml_parser, $a_fp=null)
parse xml file
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
SurveyImportParserPre38($a_spl_id, $a_xml_file='', $spl_exists=FALSE)
Constructor.
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class @access private
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
setSurveyObject(&$a_svy)
Sets a reference to a survey object @access public.
const ORDINAL_QUESTION_IDENTIFIER
const TEXT_QUESTION_IDENTIFIER
const METRIC_QUESTION_IDENTIFIER
Old survey question import/export identifiers.
const NOMINAL_QUESTION_IDENTIFIER
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...