2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2009 */
6 /* Inclusive Design Institute */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /****************************************************************/
16 define('AT_QTI_REPONSE_GRP', 1);
17 define('AT_QTI_REPONSE_LID', 2);
18 define('AT_QTI_REPONSE_STR', 3);
22 * Class for parsing XML language info and returning a QTI Object
28 var $parser; // the XML handler
29 var $qti_type; // QTI specification versoin, imsqti_xmlv1p1, imsqti_item_xmlv2p1, imsqti_xmlv1p2
30 var $character_data; // tmp variable for storing the data
31 var $element_path; // array of element paths (basically a stack)
32 var $title; //title for this question test
33 var $q_identifiers = array(); //The identifier of the choice. This identifier must not be used by any other choice or item variable.
34 var $question = ''; //question of this QTI
35 var $response_type = array(); //detects what type of question this would be.
36 var $relative_path = ''; //the relative path to all resources in this xml.
39 var $choices = array(); //answers array that keep tracks of all the correct answers
40 var $groups = array(); //groups for matching, the left handside to match with the different choices
41 var $attributes = array(); //tag attribute
42 var $answers = array(); //correct answers
43 var $response_label = array(); //temporary holders for response labels
44 var $field_label = array(); //fields label
45 var $field_entry = array(); //fields entry
46 var $feedback = array(); //question feedback
47 var $item_num = 0; //item number
48 var $items = array(); //stacks of media items, ie. img, embed, ahref etc.
49 var $qmd_itemtype = -1; //qmd flag
50 var $temp_answer = array(); //store the temp answer stack
51 var $answers_for_matching = array();
52 var $weights = array(); //the weight of each question
55 function QTIParser($qti_type='') {
56 $this->qti_type = $qti_type;
57 $this->parser = xml_parser_create();
59 xml_set_object($this->parser, $this);
60 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
61 xml_set_element_handler($this->parser, 'startElement', 'endElement');
62 xml_set_character_data_handler($this->parser, 'characterData');
66 // @return true if parsed successfully, false otherwise
67 function parse($xml_data) {
68 $this->element_path = array();
69 $this->character_data = '';
70 xml_parse($this->parser, $xml_data, TRUE);
72 //Loop thru each item and replace if existed
73 foreach ($this->answers_for_matching as $afm_k => $afk_v){
74 if (!empty($this->answers_for_matching[$afm_k])){
75 $this->answers[$afm_k] = $afk_v;
79 if(in_array('questestinterop', $this->element_path) ||
80 in_array('assessment', $this->element_path)){
81 //this is a v2.1+ package
89 function startElement($parser, $name, $attributes) {
91 // debug($attributes, $name );
95 $this->title = $attributes['title'];
98 if ($this->response_type[$this->item_num] <= 0) {
99 $this->response_type[$this->item_num] = AT_QTI_REPONSE_LID;
102 if ($this->response_type[$this->item_num] <= 0) {
103 $this->response_type[$this->item_num] = AT_QTI_REPONSE_GRP;
106 $this->attributes[$this->item_num][$name]['ident'] = $attributes['ident'];
107 $this->attributes[$this->item_num][$name]['rcardinality'] = $attributes['rcardinality'];
108 if ($this->response_type[$this->item_num] <= 0) {
109 $this->response_type[$this->item_num] = AT_QTI_REPONSE_STR;
112 case 'response_label':
113 if(!isset($this->choices[$this->item_num][$attributes['ident']])){
114 if (!is_array($this->response_label[$this->item_num])){
115 $this->response_label[$this->item_num] = array();
117 array_push($this->response_label[$this->item_num], $attributes['ident']);
121 $this->attributes[$this->item_num][$name]['respident'] = $attributes['respident'];
124 $this->attributes[$this->item_num][$name]['varname'] = $attributes['varname'];
126 case 'render_choice':
127 $this->attributes[$this->item_num][$name]['shuffle'] = $attributes['shuffle'];
128 $this->attributes[$this->item_num][$name]['minnumber'] = $attributes['minnumber'];
129 $this->attributes[$this->item_num][$name]['maxnumber'] = $attributes['maxnumber'];
132 $rows = intval($attributes['rows']);
135 //1,2,3,4 according to tools/tests/create_question_long.php
138 } elseif ($rows > 1 && $rows <= 5){
140 } elseif ($rows > 5){
143 $this->attributes[$this->item_num][$name]['property'] = $property;
146 $this->attributes[$this->item_num][$name]['imagtype'] = $attributes['imagtype'];
147 $this->attributes[$this->item_num][$name]['uri'] = $attributes['uri'];
150 $this->attributes[$this->item_num][$name]['audiotype'] = $attributes['audiotype'];
151 $this->attributes[$this->item_num][$name]['uri'] = $attributes['uri'];
154 $this->attributes[$this->item_num][$name]['videotype'] = $attributes['videotype'];
155 $this->attributes[$this->item_num][$name]['uri'] = $attributes['uri'];
158 $this->attributes[$this->item_num][$name]['uri'] = $attributes['uri'];
159 $this->attributes[$this->item_num][$name]['width'] = intval($attributes['width']);
160 $this->attributes[$this->item_num][$name]['height'] = intval($attributes['height']);
163 $this->attributes[$this->item_num][$name]['varname'] = $attributes['varname'];
164 $this->attributes[$this->item_num][$name]['action'] = $attributes['action'];
166 case 'itemproc_extension':
167 if (preg_match('/imsqti_xmlv1p2\/imscc_xmlv1p0(.*)/', $this->qti_type)){
168 $msg->addError('QTI_WRONG_PACKAGE');
172 array_push($this->element_path, $name);
176 /* called when an element ends */
177 /* removed the current element from the $path */
178 function endElement($parser, $name) {
181 $current_pos = count($this->element_path) - 1;
182 $last_element = $this->element_path[$current_pos - 1];
189 $this->mat_content[$this->item_num] .= $this->reconstructRelativePath($this->character_data);
192 $this->mat_content[$this->item_num] .= '<img src="'.$this->attributes[$this->item_num][$name]['uri'].'" alt="Image Not loaded:'.$this->attributes[$this->item_num][$name]['uri'].'" />';
195 $this->mat_content[$this->item_num] .= '<embed SRC="'.$this->attributes[$this->item_num][$name]['uri'].'" autostart="false" width="145" height="60"><noembed><bgsound src="'.$this->attributes[$this->item_num][$name]['uri'].'"></noembed></embed>';
198 if ($this->attributes[$this->item_num][$name]['videotype'] == 'type/swf'){
199 $this->mat_content[$this->item_num] .= '<object type="application/x-shockwave-flash" data="' . $this->attributes[$this->item_num][$name]['uri'] . '" width="550" height="400"><param name="movie" value="'. $this->attributes[$this->item_num][$name]['uri'] .'" /></object>';
200 } elseif ($this->attributes[$this->item_num][$name]['videotype'] == 'type/mov'){
201 $this->mat_content[$this->item_num] .= '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="550" height="400" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="'. $this->attributes[$this->item_num][$name]['uri'] . '" /><param name="autoplay" value="true" /><param name="controller" value="true" /><embed src="' . $this->attributes[$this->item_num][$name]['uri'] .'" width="550" height="400" controller="true" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>';
205 (($this->attributes[$this->item_num][$name]['width'] != 0)? $width = $this->attributes[$this->item_num][$name]['width'] : $width = 460);
206 (($this->attributes[$this->item_num][$name]['height'] != 0)? $height = $this->attributes[$this->item_num][$name]['height'] : $height = 160);
207 $this->mat_content[$this->item_num] .= '<applet code="'.$this->attributes[$this->item_num][$name]['uri'].'" width="'.$width.'" height="'.$height.'" alt="Applet not loaded."></applet>';
210 //check who is mattext's ancestor, started from the most known inner layer
211 if (in_array('response_label', $this->element_path)){
212 if(!in_array($this->mat_content, $this->choices)){
213 //This is one of the choices.
214 if (!empty($this->response_label[$this->item_num])){
215 $this->choices[$this->item_num][array_pop($this->response_label[$this->item_num])] = $this->mat_content[$this->item_num];
218 } elseif (in_array('response_grp', $this->element_path) || in_array('response_lid', $this->element_path)){
219 //for matching, where there are groups
220 //keep in mind that Respondus handles this by using response_lid
221 $this->groups[$this->item_num][] = $this->reconstructRelativePath($this->mat_content[$this->item_num]);
222 // debug($this->character_data, 'harris - groups');
223 } elseif (in_array('presentation', $this->element_path)){
224 $this->question[$this->item_num] = $this->reconstructRelativePath($this->mat_content[$this->item_num]);
225 } elseif (in_array('itemfeedback', $this->element_path)){
226 $this->feedback[$this->item_num] = $this->mat_content[$this->item_num];
228 //once material is closed, reset the mat_content variable.
229 $this->mat_content[$this->item_num] = '';
232 //stores the answers (either correct or incorrect) into a stack
233 $this->temp_answer[$this->attributes[$this->item_num][$name]['respident']]['name'][] = $this->character_data;
234 //responses handling, remember to save the answers or match them up
235 if (!is_array($this->answers[$this->item_num])){
236 $this->answers[$this->item_num] = array();
238 array_push($this->answers[$this->item_num], $this->reconstructRelativePath($this->character_data));
241 $this->temp_answer[$this->attributes[$this->item_num]['varequal']['respident']]['value'][] = $this->character_data;
242 $this->temp_answer[$this->attributes[$this->item_num]['varequal']['respident']]['attribute'][] = $this->attributes[$this->item_num]['setvar']['varname'];
244 case 'respcondition':
245 if (empty($this->temp_answer)) {
249 //closing this tag means a selection of choices have ended. Assign the correct answer in this case.
250 $tv = $this->temp_answer[$this->attributes[$this->item_num]['varequal']['respident']];
251 //debug($tv, 'harris'.$this->item_num);
252 //debug($this->choices[$this->item_num], 'choices');
253 //debug($this->answers_for_matching[$this->item_num], 'answers');
255 //If matching, then attribute = 'Respondus_correct'; otherwise it is 'que_score'
256 if ($this->getQuestionType($this->item_num) == 5){
257 if ($tv['answerAdded']!=true && !empty($tv['attribute'])){
258 foreach ($tv['attribute'] as $att_id => $att_value){
259 //Handles Respondus' (and blakcboard, angels, etc) responses schemas
260 if (strtolower($att_value)=='respondus_correct'){
261 //Then this is the right answer
262 if (!is_array($this->answers_for_matching[$this->item_num])){
263 $this->answers_for_matching[$this->item_num] = array();
265 //The condition here is to check rather the answers have been duplicated, otherwise the indexing won't be right.
266 //sizeof[answers] != sizeof[questions], then the index matching is wrong.
267 //Created a problem though, which is then many-to-1 matching fails, cuz answers will be repeated.
268 //Sep 2,08, Fixed by adding a flag into the array
269 // if (!in_array($tv['name'][$att_id], $this->answers_for_matching[$this->item_num])){
270 array_push($this->answers_for_matching[$this->item_num], $tv['name'][$att_id]);
271 $this->temp_answer[$this->attributes[$this->item_num]['varequal']['respident']]['answerAdded'] = true;
274 $this->weights[$this->item_num] = floatval($tv['value'][$att_id]);
281 $pos = sizeof($tv['value']) - 1; //position of the last entry of the "temp answer's value" array
282 //Retrieve the last entry of the "temp answer's value" array
283 $current_answer = $tv['value'][$pos];
284 if (floatval($current_answer) > 0){
285 if (!is_array($this->answers_for_matching[$this->item_num])){
286 $this->answers_for_matching[$this->item_num] = array();
288 // if (!in_array($tv['name'][$val_id], $this->answers_for_matching[$this->item_num])){
289 array_push($this->answers_for_matching[$this->item_num], $tv['name'][$this->item_num]);
292 $this->weights[$this->item_num] += floatval($current_answer);
298 $this->field_label[$this->item_num] = $this->character_data;
301 $this->field_entry[$this->item_num][$this->field_label[$this->item_num]] = $this->character_data;
304 //Deprecated as of QTI 1.2.
305 if (empty($this->field_entry[$this->item_num][$name])){
306 $this->field_entry[$this->item_num][$name] = $this->character_data;
312 // debug($this->element_path, "Ele Path");
314 //pop stack and reset character data, o/w it will stack up
315 array_pop($this->element_path);
316 $this->character_data = '';
320 function characterData($parser, $data){
322 if (trim($data)!=''){
323 $this->character_data .= $addslashes(preg_replace('/[\t\0\x0B(\r\n)]*/', '', $data));
324 // $this->character_data .= trim($data);
329 * This function returns the question type of this XML.
331 * @param the item_num
334 * 3: open ended question
340 * false for not found.
342 function getQuestionType($item_num){
343 switch ($this->field_entry[$item_num]['qmd_questiontype']){
344 case 'Multiple-choice':
346 //likert have no answers
347 if (empty($this->answers)){
358 case 'Drag-and-drop':
361 case 'Multiple-response':
366 switch ($this->field_entry[$item_num]['qmd_itemtype']){
373 //handles CC packages
374 switch ($this->field_entry[$item_num]['cc_profile']){
375 case 'cc.multiple_choice.v0p1':
378 case 'cc.true_false.v0p1':
384 case 'cc.multiple_response.v0p1':
390 //Check if this is an ordering, or matching
392 switch ($this->response_type[$item_num]){
393 case AT_QTI_REPONSE_LID:
394 $response_obj = $this->attributes[$item_num]['response_lid'];
396 case AT_QTI_REPONSE_GRP:
397 $response_obj = $this->attributes[$item_num]['response_grp'];
399 case AT_QTI_REPONSE_STR:
400 $response_obj = $this->attributes[$item_num]['response_str'];
401 return 3; //no need to parse the rcardinality?
404 if ($response_obj['rcardinality'] == 'Ordered'){
406 } elseif ($response_obj['rcardinality'] == 'Multiple'){
407 //TODO Multiple answers, Simple matching and Graphical matching
408 if (empty($this->field_entry[$item_num])){
412 } elseif ($response_obj['rcardinality'] == 'Single'){
413 return 1; //assume mc
422 //must be used before calling parse. Otherwise it will be null.
424 function setRelativePath($path){
426 if ($path[-1] != '/'){
429 $this->relative_path = $path;
435 //when importing, the path of the images are all changed. Have to parse them out and add the extra path in.
436 //No longer needed to reconstruct, just needed to save the path, as of Aug 25th, 08. Decided to overwrite files if the same name exist.
437 function reconstructRelativePath($path){
438 //match img tag, all.
439 // if (preg_match_all('/\<img(\s[^\>])*\ssrc\=[\\\\]?\"([^\\\\^\"]+)[\\\\]?\".*\/?\>/i', $path, $matches) > 0){
440 //fixes multiple image tags within a $path
441 if (preg_match_all('/\<img(\s[\w^img]+\=[\\\\]?\"[^\\\\^\"]+[\\\\]?\")*\ssrc\=[\\\\]?\"([^\\\\^\"]+)[\\\\]?\"/i', $path, $matches) > 0){
442 foreach ($matches[2] as $k=>$v){
443 if(strpos($v, 'http://')===false && !in_array($v, $this->items)) {
444 $this->items[] = $v; //save the url of this media.
445 // $path = str_replace($v, $this->relative_path.$v, $path);
449 } elseif (preg_match_all('/\<embed(\s[^\>])*\ssrc\=[\\\\]?\"([^\\\\^\"]+)[\\\\]?\".*/i', $path, $matches) > 0){
450 foreach ($matches[2] as $k=>$v){
451 if(strpos($v, 'http://')===false && !in_array($v, $this->items)) {
452 $this->items[] = $v; //save the url of this media.
453 // $path = str_replace($v, $this->relative_path.$v, $path);
465 //Free the XML parser
466 unset($this->response_label);
467 unset($this->field_label);
468 unset($this->temp_answer);
469 xml_parser_free($this->parser);