Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Quiz Item Analysis of Multianswers Question Types

From MoodleDocs

This page described a code proposal to access the embedded questions data in multianswers questiion types so that they can be displayed in the Item analysis report. See http://moodle.org/mod/forum/discuss.php?d=86598#387154 The code is experimental and need more testing and is not optimized.

All question types to be analyzed contain at least one embedded question

If we set this as a general framework, the code will be valid for all question types.

We need a question type function to retrieve the embedded questions

There is actually no question type function to know the number the embedded questions in a question. The default function number_of_embedded_questions() returns 1;

This is not the same as  actual_number_of_questions().

We need a convention of how to store the responses from the different embedded questions

The same convention should be used in the $session states and in the get_responses function. The actual responses convention used an array to contains the different answers and put in [''] element the actual response. an example for a numerical question with the tolerance limit

stdClass Object
(
   [id] => 2312
   [responses] => Array
       (
           [6837] => stdClass Object
               (
                   [answer] => 1822 (1817..1827)
                   [credit] => 1
               )
       )
)

In a multianswers qtype the different answers responses are stored in an array as.

stdClass Object
(
   [id] => 2815
   [responses] => Array
       (
            [1] => stdClass Object
               (
                   [id] => 2816
                   [responses] => Array
                       (
                           [7300] => stdClass Object
                               (
                                   [answer] => Wrong answer
                                   [credit] => 0
                               )
                           [7302] => stdClass Object
                               (
                                   [answer] => Correct answer
                                   [credit] => 1
                               )
                           [7305] => stdClass Object
                               (
                                   [answer] => Answer that gives half the credit
                                   [credit] => 0.5
                               )
                       )
               )
           [2] => stdClass Object
               (
                   [id] => 2817
                   [responses] => Array
                       (
                           [7303] => stdClass Object
                               (
                                   [answer] => Wrong answer
                                   [credit] => 0
                               )
                           [7304] => stdClass Object
                               (
                                   [answer] => Correct answer
                                   [credit] => 1
                               )
                       )
               )
           [3] => stdClass Object
               (
                   [id] => 2818
                   [responses] => Array
                       (
                           [7307] => stdClass Object
                               (
                                   [answer] => 65 (64.9..65.1)
                                   [credit] => 1
                               )
                       )
               )
       )
)

So the one question question types responses is tranformed in a similar structure.

The upper example becomes

stdClass Object
(
   [id] => 2312
   [responses] => Array
       (
           [0] => stdClass Object
               (
                   [id] => 2312
                   [responses] => Array
                       (
                           [6837] => stdClass Object
                               (
                                   [answer] => 1822 (1817..1827)
                                   [credit] => 1
                               )
                       )
               )
       )
)

The new array elements the defined the responses of the embedded questions and are put in the questions array as subquestions

                 foreach ($q->responses as $subquestion => $qresponses ){ 
                   foreach ($qresponses->responses as $answer => $r) {
                       $r->count = 0;
                       $questions[$qid]['subquestion'][$subquestion]['responses'][$answer] = $r->answer;
                       $questions[$qid]['subquestion'][$subquestion]['rcounts'][$answer] = 0;
                       $questions[$qid]['subquestion'][$subquestion]['credits'][$answer] = $r->credit;
                       $statsrow[$qid]['subquestion'][$subquestion] = 0;
                   }

For a 1 embedded question question type there is just one subquestion with a $subquestion value of 0.

Check response of each subquestion instead of each question

  foreach ($responses as $subquestion => $resp ){ 
      if ($resp) {
          if ($key = array_search($resp, $questions[$qid]['subquestion'][$subquestion]['responses'])) {                            
               $questions[$qid]['subquestion'][$subquestion]['rcounts'][$key]++;
           } else {
                $test = new stdClass;
                $test->responses =  $QTYPES[$quizquestions[$i]->qtype]->get_correct_responses($quizquestions[$i], $states[$i]);
                $test->resp = $resp ;
                $test->keyresp = $subquestion ;
                           $test->question_qid = &$questions[$qid] ;
                           if ($key = $QTYPES[$quizquestions[$i]->qtype]->check_response($quizquestions[$i], $states[$i], $test)) {
                               $questions[$qid]['subquestion'][$subquestion]['rcounts'][$key]++;

if (!$this->debug){ echo "

test cheeck response search OK $key

rcounts

";print_r($questions[$qid][$subquestion]['rcounts']); echo " 

";}

                           } else {
                               $questions[$qid]['subquestion'][$subquestion]['responses'][] = $resp;
                               $questions[$qid]['subquestion'][$subquestion]['rcounts'][] = 1;
                               $questions[$qid]['subquestion'][$subquestion]['credits'][] = 0;
                           }
                       }
                   }
               }

To distinguish