예제 #1
0
 /**
  * store_details
  *
  * @param xxx $attempt (passed by reference)
  */
 public static function store_details($attempt)
 {
     // encode ampersands so that HTML entities are preserved in the XML parser
     // N.B. ampersands inside <![CDATA[ ]]> blocks do NOT need to be encoded
     // disabled 2008.11.20
     // $attempt->details = self::pre_xmlize($attempt->details);
     // parse the attempt details as xml
     $details = xmlize($attempt->details);
     $question_number;
     // initially unset
     $question = false;
     $response = false;
     $i = 0;
     while (isset($details[self::xmlresultstag]['#']['fields']['0']['#']['field'][$i]['#'])) {
         // shortcut to field
         $field =& $details[self::xmlresultstag]['#']['fields']['0']['#']['field'][$i]['#'];
         // extract field name and data
         if (isset($field['fieldname'][0]['#']) && is_string($field['fieldname'][0]['#'])) {
             $name = $field['fieldname'][0]['#'];
         } else {
             $name = '';
         }
         if (isset($field['fielddata'][0]['#']) && is_string($field['fielddata'][0]['#'])) {
             $data = $field['fielddata'][0]['#'];
         } else {
             $data = '';
         }
         // parse the field name into $matches
         //  [1] quiz type
         //  [2] attempt detail name
         if (preg_match('/^(\\w+?)_(\\w+)$/', $name, $matches)) {
             $quiztype = strtolower($matches[1]);
             $name = strtolower($matches[2]);
             // parse the attempt detail $name into $matches
             //  [1] question number
             //  [2] question detail name
             if (preg_match('/^q(\\d+)_(\\w+)$/', $name, $matches)) {
                 $num = $matches[1];
                 $name = strtolower($matches[2]);
                 // not needed Moodle 2.0 and later
                 // $data = addslashes($data);
                 // adjust JCross question numbers
                 if (preg_match('/^(across|down)(.*)$/', $name, $matches)) {
                     $num .= '_' . $matches[1];
                     // e.g. 01_across, 02_down
                     $name = $matches[2];
                     if (substr($name, 0, 1) == '_') {
                         $name = substr($name, 1);
                         // remove leading '_'
                     }
                 }
                 if (isset($question_number) && $question_number == $num) {
                     // do nothing - this response is for the same question as the previous response
                 } else {
                     // store previous question / response (if any)
                     self::add_response($attempt, $question, $response);
                     // initialize question object
                     $question = new stdClass();
                     $question->name = '';
                     $question->text = '';
                     $question->hotpotid = $attempt->hotpotid;
                     // initialize response object
                     $response = new stdClass();
                     $response->attemptid = $attempt->id;
                     // update question number
                     $question_number = $num;
                 }
                 // adjust field name and value, and set question type
                 // (may not be necessary one day)
                 // hotpot_adjust_response_field($quiztype, $question, $num, $name, $data);
                 // add $data to the question/response details
                 switch ($name) {
                     case 'name':
                     case 'type':
                         $question->{$name} = $data;
                         break;
                     case 'text':
                         $question->{$name} = hotpot::string_id($data);
                         break;
                     case 'correct':
                     case 'ignored':
                     case 'wrong':
                         $response->{$name} = hotpot::string_ids($data);
                         break;
                     case 'score':
                     case 'weighting':
                     case 'hints':
                     case 'clues':
                     case 'checks':
                         $response->{$name} = intval($data);
                         break;
                 }
             } else {
                 // attempt details
                 // adjust field name and value
                 //hotpot_adjust_response_field($quiztype, $question, $num='', $name, $data);
                 // add $data to the attempt details
                 if ($name == 'penalties') {
                     $attempt->{$name} = intval($data);
                 }
             }
         }
         $i++;
     }
     // end while
     // add the final question and response, if any
     self::add_response($attempt, $question, $response);
 }