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