Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-30766 mod_quiz: fix edge cases of repaginate function.

  • Loading branch information...
commit 85be8375c7483898cefb4fd6dbbb752922dde573 1 parent ddb7ae2
@timhunt timhunt authored
Showing with 49 additions and 10 deletions.
  1. +17 −10 mod/quiz/locallib.php
  2. +32 −0 mod/quiz/simpletest/testlocallib.php
View
27 mod/quiz/locallib.php
@@ -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 //////////////////////////////////////////
View
32 mod/quiz/simpletest/testlocallib.php
@@ -96,6 +96,38 @@ public function test_quiz_clean_layout() {
$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;

0 comments on commit 85be837

Please sign in to comment.
Something went wrong with that request. Please try again.