MDL-66115 qtype_ddimageortext: Fixing choice deletion.
authorDavid Castro <davidcastro00@gmail.com>
Wed, 10 Jul 2019 14:50:11 +0000 (09:50 -0500)
committerDavid Castro <davidcastro00@gmail.com>
Fri, 12 Jul 2019 19:36:32 +0000 (14:36 -0500)
Summarising responses fails when question choices have been removed.

AMOS BEGIN
CPY [deletedchoice,qtype_match],[deletedchoice,qtype_ddimageortext]
AMOS END

(cherry picked from commit f0265e6db16caa6d13f8bce291e7da91b0eabb53)

question/type/ddimageortext/lang/en/qtype_ddimageortext.php
question/type/ddimageortext/questionbase.php
question/type/ddimageortext/tests/question_test.php
question/type/gapselect/questionbase.php

index 4143d01..7c8c59f 100644 (file)
@@ -70,3 +70,4 @@ $string['summarisechoiceno'] = 'Item {$a}';
 $string['summariseplaceno'] = 'Drop zone {$a}';
 $string['xleft'] = 'Left';
 $string['ytop'] = 'Top';
+$string['deletedchoice'] = '[Deleted choice]';
index bbaf9bf..4754bf5 100644 (file)
@@ -62,7 +62,11 @@ class qtype_ddtoimage_question_base extends qtype_gapselect_question_base {
                                                                 $response[$this->field($placeno)]) {
                 $selected = $this->get_selected_choice($place->group,
                                                                 $response[$this->field($placeno)]);
-                $summarisechoice = $selected->summarise();
+                if (isset($selected)) {
+                    $summarisechoice = $selected->summarise();
+                } else {
+                    $summarisechoice = get_string('deletedchoice', 'qtype_ddimageortext');
+                }
                 $allblank = false;
             } else {
                 $summarisechoice = '';
index 08e64b1..b741bf4 100644 (file)
@@ -266,4 +266,19 @@ class qtype_ddimageortext_question_test extends basic_testcase {
             4 => new question_classified_response(4, '4. dog', 1)
         ), $dd->classify_response(array('p1' => '', 'p2' => '1', 'p3' => '2', 'p4' => '2')));
     }
+
+    public function test_summarise_response_choice_deleted() {
+        /** @var qtype_ddtoimage_question_base $dd */
+        $dd = test_question_maker::make_question('ddimageortext');
+        $dd->shufflechoices = false;
+        $dd->start_attempt(new question_attempt_step(), 1);
+        // Simulation of an instructor deleting 1 choice after an attempt has been made.
+        unset($dd->choices[1][1]);
+        $delquestionstr = get_string('deletedchoice', 'qtype_ddimageortext');
+        $this->assertEquals("Drop zone 1 -> {{$delquestionstr}} ".
+            "Drop zone 2 -> {{$delquestionstr}} ".
+            'Drop zone 3 -> {3. lazy} '.
+            'Drop zone 4 -> {3. lazy}',
+            $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
+    }
 }
index 76a3882..a64e67c 100644 (file)
@@ -127,7 +127,7 @@ abstract class qtype_gapselect_question_base extends question_graded_automatical
 
     protected function get_selected_choice($group, $shuffledchoicenumber) {
         $choiceno = $this->choiceorder[$group][$shuffledchoicenumber];
-        return $this->choices[$group][$choiceno];
+        return isset($this->choices[$group][$choiceno]) ? $this->choices[$group][$choiceno] : null;
     }
 
     public function summarise_response(array $response) {