From f7f596a2a7eb434bd8c5bf7987fd4a85e5373a2f Mon Sep 17 00:00:00 2001 From: Jamie Pratt Date: Tue, 3 Sep 2013 19:11:47 +0700 Subject: [PATCH] MDL-41571 move forced_choices_selection_strategy out of question/engine/tests/helpers.php to question/engine/lib.php --- question/engine/lib.php | 66 +++++++++++++++++++++++++++++++ question/engine/tests/helpers.php | 65 ------------------------------ 2 files changed, 66 insertions(+), 65 deletions(-) diff --git a/question/engine/lib.php b/question/engine/lib.php index 29c6366554471..510be36ee2b8b 100644 --- a/question/engine/lib.php +++ b/question/engine/lib.php @@ -912,3 +912,69 @@ public function choose_variant($maxvariants, $seed) { 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; + } +} diff --git a/question/engine/tests/helpers.php b/question/engine/tests/helpers.php index 303746beff389..5542014036e39 100644 --- a/question/engine/tests/helpers.php +++ b/question/engine/tests/helpers.php @@ -1202,68 +1202,3 @@ public function close() { $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; - } -}