MDL-47494 gapselect: Split question_definition::init_first_step into start_attempt...
authorTim Hunt <T.J.Hunt@open.ac.uk>
Thu, 24 Feb 2011 19:12:42 +0000 (19:12 +0000)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Thu, 24 Feb 2011 19:12:42 +0000 (19:12 +0000)
question/type/gapselect/questionbase.php
question/type/gapselect/simpletest/testquestion.php

index a3725b4..13c5d48 100755 (executable)
@@ -66,27 +66,33 @@ abstract class qtype_gapselect_question_base extends question_graded_automatical
     /** @var array shuffled choice indexes. */
     protected $choiceorder;
 
-    public function init_first_step(question_attempt_step $step) {
+    public function start_attempt(question_attempt_step $step) {
         foreach ($this->choices as $group => $choices) {
-            $varname = '_choiceorder' . $group;
-
-            if ($step->has_qt_var($varname)) {
-                $choiceorder = explode(',', $step->get_qt_var($varname));
-
-            } else {
-                $choiceorder = array_keys($choices);
-                if ($this->shufflechoices) {
-                    shuffle($choiceorder);
-                }
+            $choiceorder = array_keys($choices);
+            if ($this->shufflechoices) {
+                shuffle($choiceorder);
             }
+            $step->set_qt_var('_choiceorder' . $group, implode(',', $choiceorder));
+            $this->set_choiceorder($group, $choiceorder);
+        }
+    }
 
-            foreach ($choiceorder as $key => $value) {
-                $this->choiceorder[$group][$key + 1] = $value;
-            }
+    public function apply_attempt_state(question_attempt_step $step) {
+        foreach ($this->choices as $group => $choices) {
+            $this->set_choiceorder($group, explode(',',
+                    $step->get_qt_var('_choiceorder' . $group)));
+        }
+    }
 
-            if (!$step->has_qt_var($varname)) {
-                $step->set_qt_var($varname, implode(',', $this->choiceorder[$group]));
-            }
+    /**
+     * Helper method used by both {@link start_attempt()} and
+     * {@link apply_attempt_state()}.
+     * @param int $group the group number.
+     * @param array $choiceorder the choices, in order.
+     */
+    protected function set_choiceorder($group, $choiceorder) {
+        foreach ($choiceorder as $key => $value) {
+            $this->choiceorder[$group][$key + 1] = $value;
         }
     }
 
index f5d2c2a..c5e9e20 100755 (executable)
@@ -55,7 +55,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_summarise_response() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual('{quick} {fox} {lazy}',
                 $gapselect->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
@@ -64,7 +64,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_summarise_response_maths() {
         $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual('{+} {-} {+} {-}',
                 $gapselect->summarise_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
@@ -83,7 +83,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_get_right_choice_for() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(1, $gapselect->get_right_choice_for(1));
         $this->assertEqual(1, $gapselect->get_right_choice_for(2));
@@ -92,7 +92,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_get_right_choice_for_maths() {
         $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(1, $gapselect->get_right_choice_for(1));
         $this->assertEqual(2, $gapselect->get_right_choice_for(2));
@@ -101,7 +101,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_clear_wrong_from_response() {
         $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
         $this->assertEqual(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'),
@@ -111,7 +111,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_get_num_parts_right() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(array(2, 3),
                 $gapselect->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2')));
@@ -122,7 +122,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_get_num_parts_right_maths() {
         $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(array(2, 4),
                 $gapselect->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
@@ -130,7 +130,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
 
     public function test_get_expected_data() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
                 $gapselect->get_expected_data());
@@ -139,7 +139,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_get_correct_response() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
                 $gapselect->get_correct_response());
@@ -148,7 +148,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_get_correct_response_maths() {
         $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
                 $gapselect->get_correct_response());
@@ -156,7 +156,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
 
     public function test_is_same_response() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertTrue($gapselect->is_same_response(
                 array(),
@@ -180,7 +180,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     }
     public function test_is_complete_response() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertFalse($gapselect->is_complete_response(array()));
         $this->assertFalse($gapselect->is_complete_response(
@@ -192,7 +192,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
 
     public function test_is_gradable_response() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertFalse($gapselect->is_gradable_response(array()));
         $this->assertFalse($gapselect->is_gradable_response(
@@ -207,7 +207,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_grading() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(array(1, question_state::$gradedright),
                 $gapselect->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
@@ -220,7 +220,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_grading_maths() {
         $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(array(1, question_state::$gradedright),
                 $gapselect->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
@@ -233,7 +233,7 @@ class qtype_gapselect_question_test extends UnitTestCase {
     public function test_classify_response() {
         $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
         $gapselect->shufflechoices = false;
-        $gapselect->init_first_step(new question_attempt_step());
+        $gapselect->start_attempt(new question_attempt_step());
 
         $this->assertEqual(array(
                     1 => new question_classified_response(1, 'quick', 1),