MDL-41571 move forced_choices_selection_strategy
authorJamie Pratt <me@jamiep.org>
Tue, 3 Sep 2013 12:11:47 +0000 (19:11 +0700)
committerJamie Pratt <me@jamiep.org>
Tue, 10 Sep 2013 07:29:01 +0000 (14:29 +0700)
out of question/engine/tests/helpers.php to
question/engine/lib.php

question/engine/lib.php
question/engine/tests/helpers.php

index 29c6366..510be36 100644 (file)
@@ -912,3 +912,69 @@ class question_variant_pseudorandom_no_repeats_strategy
         return ($randint + $this->attemptno) % $maxvariants + 1;
     }
 }
+
+/**
+ * A {@link question_variant_selection_strategy} designed ONLY for testing.
+ * For selected questions it wil return a specific variants. For the other
+ * slots it will use a fallback strategy.
+ *
+ * @copyright  2013 The Open University
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class question_variant_forced_choices_selection_strategy
+    implements question_variant_selection_strategy {
+
+    /** @var array seed => variant to select. */
+    protected $forcedchoices;
+
+    /** @var question_variant_selection_strategy strategy used to make the non-forced choices. */
+    protected $basestrategy;
+
+    /**
+     * Constructor.
+     * @param array $forcedchoices array seed => variant to select.
+     * @param question_variant_selection_strategy $basestrategy strategy used
+     *      to make the non-forced choices.
+     */
+    public function __construct(array $forcedchoices, question_variant_selection_strategy $basestrategy) {
+        $this->forcedchoices = $forcedchoices;
+        $this->basestrategy  = $basestrategy;
+    }
+
+    public function choose_variant($maxvariants, $seed) {
+        if (array_key_exists($seed, $this->forcedchoices)) {
+            if ($this->forcedchoices[$seed] > $maxvariants) {
+                throw new coding_exception('Forced variant out of range.');
+            }
+            return $this->forcedchoices[$seed];
+        } else {
+            return $this->basestrategy->choose_variant($maxvariants, $seed);
+        }
+    }
+
+    /**
+     * Helper method for preparing the $forcedchoices array.
+     * @param array                      $variantsbyslot slot number => variant to select.
+     * @param question_usage_by_activity $quba           the question usage we need a strategy for.
+     * @throws coding_exception when variant cannot be forced as doesn't work.
+     * @return array that can be passed to the constructor as $forcedchoices.
+     */
+    public static function prepare_forced_choices_array(array $variantsbyslot,
+                                                        question_usage_by_activity $quba) {
+
+        $forcedchoices = array();
+
+        foreach ($variantsbyslot as $slot => $varianttochoose) {
+            $question = $quba->get_question($slot);
+            $seed = $question->get_variants_selection_seed();
+            if (array_key_exists($seed, $forcedchoices) && $forcedchoices[$seed] != $varianttochoose) {
+                throw new coding_exception('Inconsistent forced variant detected at slot ' . $slot);
+            }
+            if ($varianttochoose > $question->get_num_variants()) {
+                throw new coding_exception('Forced variant out of range at slot ' . $slot);
+            }
+            $forcedchoices[$seed] = $varianttochoose;
+        }
+        return $forcedchoices;
+    }
+}
index 303746b..5542014 100644 (file)
@@ -1202,68 +1202,3 @@ class question_test_recordset extends moodle_recordset {
         $this->records = null;
     }
 }
-
-/**
- * A {@link question_variant_selection_strategy} designed for testing.
- * For selected. questions it wil return a specific variants. In for the other
- * slots it will use a fallback strategy.
- *
- * @copyright  2013 The Open University
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class question_variant_forced_choices_selection_strategy
-    implements question_variant_selection_strategy {
-
-    /** @var array seed => variant to select. */
-    protected $forcedchoices;
-
-    /** @var question_variant_selection_strategy strategy used to make the non-forced choices. */
-    protected $basestrategy;
-
-    /**
-     * Constructor.
-     * @param array $forcedchoice array seed => variant to select.
-     * @param question_variant_selection_strategy $basestrategy strategy used
-     *      to make the non-forced choices.
-     */
-    public function __construct(array $forcedchoices, question_variant_selection_strategy $basestrategy) {
-        $this->forcedchoices = $forcedchoices;
-        $this->basestrategy  = $basestrategy;
-    }
-
-    public function choose_variant($maxvariants, $seed) {
-        if (array_key_exists($seed, $this->forcedchoices)) {
-            if ($this->forcedchoices[$seed] > $maxvariants) {
-                throw new coding_exception('Forced variant out of range.');
-            }
-            return $this->forcedchoices[$seed];
-        } else {
-            return $this->basestrategy->choose_variant($maxvariants, $seed);
-        }
-    }
-
-    /**
-     * Helper method for preparing the $forcedchoices array.
-     * @param array $variantsbyslot slot number => variant to select.
-     * @param question_usage_by_activity $quba the question usage we need a strategy for.
-     * @return array that can be passed to the constructor as $forcedchoices.
-     */
-    public static function prepare_forced_choices_array(array $variantsbyslot,
-                                                        question_usage_by_activity $quba) {
-
-        $forcedchoices = array();
-
-        foreach ($variantsbyslot as $slot => $varianttochoose) {
-            $question = $quba->get_question($slot);
-            $seed = $question->get_variants_selection_seed();
-            if (array_key_exists($seed, $forcedchoices) && $forcedchoices[$seed] != $varianttochoose) {
-                throw new coding_exception('Inconsistent forced variant detected at slot ' . $slot);
-            }
-            if ($varianttochoose > $question->get_num_variants()) {
-                throw new coding_exception('Forced variant out of range at slot ' . $slot);
-            }
-            $forcedchoices[$seed] = $varianttochoose;
-        }
-        return $forcedchoices;
-    }
-}