MDL-30766 mod_quiz: fix edge cases of repaginate function.
This commit is contained in:
+17
-10
@@ -246,22 +246,29 @@ function quiz_number_of_questions_in_quiz($layout) {
|
||||
* @return string the new layout string
|
||||
*/
|
||||
function quiz_repaginate($layout, $perpage, $shuffle = false) {
|
||||
$layout = str_replace(',0', '', $layout); // remove existing page breaks
|
||||
$questions = explode(',', $layout);
|
||||
$questions = quiz_questions_in_quiz($layout);
|
||||
if (!$questions) {
|
||||
return '0';
|
||||
}
|
||||
|
||||
$questions = explode(',', quiz_questions_in_quiz($layout));
|
||||
if ($shuffle) {
|
||||
shuffle($questions);
|
||||
}
|
||||
$i = 1;
|
||||
$layout = '';
|
||||
|
||||
$onthispage = 0;
|
||||
$layout = array();
|
||||
foreach ($questions as $question) {
|
||||
if ($perpage and $i > $perpage) {
|
||||
$layout .= '0,';
|
||||
$i = 1;
|
||||
if ($perpage and $onthispage >= $perpage) {
|
||||
$layout[] = 0;
|
||||
$onthispage = 0;
|
||||
}
|
||||
$layout .= $question.',';
|
||||
$i++;
|
||||
$layout[] = $question;
|
||||
$onthispage += 1;
|
||||
}
|
||||
return $layout.'0';
|
||||
|
||||
$layout[] = 0;
|
||||
return implode(',', $layout);
|
||||
}
|
||||
|
||||
/// Functions to do with quiz grades //////////////////////////////////////////
|
||||
|
||||
@@ -96,6 +96,38 @@ class quiz_locallib_test extends UnitTestCase {
|
||||
$this->assertEqual(quiz_clean_layout('0,1,0,0,2,0', true), '1,0,2,0');
|
||||
}
|
||||
|
||||
public function test_quiz_repaginate() {
|
||||
// Test starting with 1 question per page.
|
||||
$this->assertEqual(quiz_repaginate('1,0,2,0,3,0', 0), '1,2,3,0');
|
||||
$this->assertEqual(quiz_repaginate('1,0,2,0,3,0', 3), '1,2,3,0');
|
||||
$this->assertEqual(quiz_repaginate('1,0,2,0,3,0', 2), '1,2,0,3,0');
|
||||
$this->assertEqual(quiz_repaginate('1,0,2,0,3,0', 1), '1,0,2,0,3,0');
|
||||
|
||||
// Test starting with all on one page page.
|
||||
$this->assertEqual(quiz_repaginate('1,2,3,0', 0), '1,2,3,0');
|
||||
$this->assertEqual(quiz_repaginate('1,2,3,0', 3), '1,2,3,0');
|
||||
$this->assertEqual(quiz_repaginate('1,2,3,0', 2), '1,2,0,3,0');
|
||||
$this->assertEqual(quiz_repaginate('1,2,3,0', 1), '1,0,2,0,3,0');
|
||||
|
||||
// Test single question case.
|
||||
$this->assertEqual(quiz_repaginate('100,0', 0), '100,0');
|
||||
$this->assertEqual(quiz_repaginate('100,0', 1), '100,0');
|
||||
|
||||
// No questions case.
|
||||
$this->assertEqual(quiz_repaginate('0', 0), '0');
|
||||
|
||||
// Test empty pages are removed.
|
||||
$this->assertEqual(quiz_repaginate('1,2,3,0,0,0', 0), '1,2,3,0');
|
||||
$this->assertEqual(quiz_repaginate('1,0,0,0,2,3,0', 0), '1,2,3,0');
|
||||
$this->assertEqual(quiz_repaginate('0,0,0,1,2,3,0', 0), '1,2,3,0');
|
||||
|
||||
// Test shuffle option.
|
||||
$this->assertTrue(in_array(quiz_repaginate('1,2,0', 0, true),
|
||||
array('1,2,0', '2,1,0')));
|
||||
$this->assertTrue(in_array(quiz_repaginate('1,2,0', 1, true),
|
||||
array('1,0,2,0', '2,0,1,0')));
|
||||
}
|
||||
|
||||
public function test_quiz_rescale_grade() {
|
||||
$quiz = new stdClass();
|
||||
$quiz->decimalpoints = 2;
|
||||
|
||||
Reference in New Issue
Block a user