Skip to content

Commit

Permalink
Merge branch 'MDL-41571-master' of git://github.com/jamiepratt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Sep 17, 2013
2 parents 9f21d24 + f7f596a commit 81ece3f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 65 deletions.
66 changes: 66 additions & 0 deletions question/engine/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
65 changes: 0 additions & 65 deletions question/engine/tests/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 81ece3f

Please sign in to comment.