Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'MDL-30766_22' of git://github.com/timhunt/moodle into M…

…OODLE_22_STABLE
  • Loading branch information...
commit e972cdc5adf7cac492d4b426c746b4e20a31be27 2 parents dc8d309 + 4587d4f
Sam Hemelryk authored January 03, 2012
27  mod/quiz/locallib.php
@@ -237,22 +237,29 @@ function quiz_number_of_questions_in_quiz($layout) {
237 237
  * @return string the new layout string
238 238
  */
239 239
 function quiz_repaginate($layout, $perpage, $shuffle = false) {
240  
-    $layout = str_replace(',0', '', $layout); // remove existing page breaks
241  
-    $questions = explode(',', $layout);
  240
+    $questions = quiz_questions_in_quiz($layout);
  241
+    if (!$questions) {
  242
+        return '0';
  243
+    }
  244
+
  245
+    $questions = explode(',', quiz_questions_in_quiz($layout));
242 246
     if ($shuffle) {
243 247
         shuffle($questions);
244 248
     }
245  
-    $i = 1;
246  
-    $layout = '';
  249
+
  250
+    $onthispage = 0;
  251
+    $layout = array();
247 252
     foreach ($questions as $question) {
248  
-        if ($perpage and $i > $perpage) {
249  
-            $layout .= '0,';
250  
-            $i = 1;
  253
+        if ($perpage and $onthispage >= $perpage) {
  254
+            $layout[] = 0;
  255
+            $onthispage = 0;
251 256
         }
252  
-        $layout .= $question.',';
253  
-        $i++;
  257
+        $layout[] = $question;
  258
+        $onthispage += 1;
254 259
     }
255  
-    return $layout.'0';
  260
+
  261
+    $layout[] = 0;
  262
+    return implode(',', $layout);
256 263
 }
257 264
 
258 265
 /// Functions to do with quiz grades //////////////////////////////////////////
32  mod/quiz/simpletest/testlocallib.php
@@ -96,6 +96,38 @@ public function test_quiz_clean_layout() {
96 96
         $this->assertEqual(quiz_clean_layout('0,1,0,0,2,0', true), '1,0,2,0');
97 97
     }
98 98
 
  99
+    public function test_quiz_repaginate() {
  100
+        // Test starting with 1 question per page.
  101
+        $this->assertEqual(quiz_repaginate('1,0,2,0,3,0', 0), '1,2,3,0');
  102
+        $this->assertEqual(quiz_repaginate('1,0,2,0,3,0', 3), '1,2,3,0');
  103
+        $this->assertEqual(quiz_repaginate('1,0,2,0,3,0', 2), '1,2,0,3,0');
  104
+        $this->assertEqual(quiz_repaginate('1,0,2,0,3,0', 1), '1,0,2,0,3,0');
  105
+
  106
+        // Test starting with all on one page page.
  107
+        $this->assertEqual(quiz_repaginate('1,2,3,0', 0), '1,2,3,0');
  108
+        $this->assertEqual(quiz_repaginate('1,2,3,0', 3), '1,2,3,0');
  109
+        $this->assertEqual(quiz_repaginate('1,2,3,0', 2), '1,2,0,3,0');
  110
+        $this->assertEqual(quiz_repaginate('1,2,3,0', 1), '1,0,2,0,3,0');
  111
+
  112
+        // Test single question case.
  113
+        $this->assertEqual(quiz_repaginate('100,0', 0), '100,0');
  114
+        $this->assertEqual(quiz_repaginate('100,0', 1), '100,0');
  115
+
  116
+        // No questions case.
  117
+        $this->assertEqual(quiz_repaginate('0', 0), '0');
  118
+
  119
+        // Test empty pages are removed.
  120
+        $this->assertEqual(quiz_repaginate('1,2,3,0,0,0', 0), '1,2,3,0');
  121
+        $this->assertEqual(quiz_repaginate('1,0,0,0,2,3,0', 0), '1,2,3,0');
  122
+        $this->assertEqual(quiz_repaginate('0,0,0,1,2,3,0', 0), '1,2,3,0');
  123
+
  124
+        // Test shuffle option.
  125
+        $this->assertTrue(in_array(quiz_repaginate('1,2,0', 0, true),
  126
+                array('1,2,0', '2,1,0')));
  127
+        $this->assertTrue(in_array(quiz_repaginate('1,2,0', 1, true),
  128
+                array('1,0,2,0', '2,0,1,0')));
  129
+    }
  130
+
99 131
     public function test_quiz_rescale_grade() {
100 132
         $quiz = new stdClass();
101 133
         $quiz->decimalpoints = 2;

0 notes on commit e972cdc

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