From 5ac62e7763dbc950aaf2bc86b1a959f3f4017d63 Mon Sep 17 00:00:00 2001
From: Khoa Nguyen <knd39@open.ac.uk>
Date: Fri, 1 Nov 2024 16:34:01 +0700
Subject: [PATCH] MDL-83606 Question bank: Delete questions to fit one page
 causes errors.

---
 .../tests/quiz_question_bank_view_test.php    | 89 +++++++++++++++++++
 question/classes/local/bank/view.php          | 11 ++-
 2 files changed, 96 insertions(+), 4 deletions(-)

diff --git a/mod/quiz/tests/quiz_question_bank_view_test.php b/mod/quiz/tests/quiz_question_bank_view_test.php
index 442bc1b1a2f..370d2688e75 100644
--- a/mod/quiz/tests/quiz_question_bank_view_test.php
+++ b/mod/quiz/tests/quiz_question_bank_view_test.php
@@ -31,6 +31,7 @@ require_once($CFG->dirroot . '/question/editlib.php');
  * @category   test
  * @copyright  2018 the Open University
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @covers     \core_question\local\bank\view
  */
 class quiz_question_bank_view_test extends \advanced_testcase {
 
@@ -80,4 +81,92 @@ class quiz_question_bank_view_test extends \advanced_testcase {
         // Verify the question has not been loaded into the cache.
         $this->assertFalse($cache->has($questiondata->id));
     }
+
+    public function test_viewing_question_bank_when_paging_out_of_limit(): void {
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        $generator = $this->getDataGenerator();
+        /** @var core_question_generator $questiongenerator */
+        $questiongenerator = $generator->get_plugin_generator('core_question');
+
+        // Create a course and a quiz.
+        $course = $generator->create_course();
+        $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
+        $context = \context_module::instance($quiz->cmid);
+
+        // Create a question in the default category.
+        $contexts = new question_edit_contexts($context);
+        question_make_default_categories($contexts->all());
+        $cm = get_coursemodule_from_instance('quiz', $quiz->id);
+        $cat = question_get_default_category($context->id);
+
+        // Create three questions.
+        $questiongenerator->create_question('numerical', null,
+            ['name' => 'Example question 1', 'category' => $cat->id]);
+        $questiongenerator->create_question('numerical', null,
+            ['name' => 'Example question 2', 'category' => $cat->id]);
+        $question3 = $questiongenerator->create_question('numerical', null,
+            ['name' => 'Example question 3', 'category' => $cat->id]);
+
+        // Retrieve the question bank view on page 3 with 1 questions per page.
+        $params = [
+            'qpage' => 3,
+            'qperpage' => 1,
+            'cat' => $cat->id . ',' . $context->id,
+            'recurse' => false,
+            'showhidden' => false,
+            'qbshowtext' => false,
+            'tabname' => 'editq',
+        ];
+
+        // Load the question bank view.
+        $view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $params, ['cmid' => $cm->id]);
+        ob_start();
+        $view->display();
+        $html = ob_get_clean();
+
+        // Verify that questions exist in the view.
+        $this->assertStringNotContainsString('Example question 1', $html);
+        $this->assertStringNotContainsString('Example question 2', $html);
+        $this->assertStringContainsString('Example question 3', $html);
+
+        // Set the param per page is 2.
+        // The view only has 2 pages.
+        $params['qperpage'] = 2;
+
+        // Reload the question bank view on page 3.
+        $view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $params, ['cmid' => $cm->id]);
+        ob_start();
+        $view->display();
+        $html = ob_get_clean();
+
+        // Since the view has only 2 pages and the requested page is out of range,
+        // the view will move to the nearest available page.
+        // Verify that the view is in the page 2.
+        $this->assertEquals(1, $view->get_pagevars('qpage'));
+        // Verify that questions exist in the view.
+        $this->assertStringNotContainsString('Example question 1', $html);
+        $this->assertStringNotContainsString('Example question 2', $html);
+        $this->assertStringContainsString('Example question 3', $html);
+
+        // Create a new category.
+        $newcategory = $generator->create_category();
+        $newcontext = \context_coursecat::instance($newcategory->id);
+        $newquestioncat = $questiongenerator->create_question_category([
+            'contextid' => $newcontext->id,
+        ]);
+        // Move question 3 to a new category.
+        question_move_questions_to_category([$question3->id], $newquestioncat->id);
+        // Load the question bank view from the new category.
+        $params['cat'] = $newquestioncat->id . ',' . $newcontext->id;
+        $view = new custom_view(new question_edit_contexts($newcontext),
+            new \moodle_url('/'), $course, $cm, $params, ['cmid' => $cm->id]);
+        ob_start();
+        $view->display();
+        $html = ob_get_clean();
+        // Verify that the view is in the page 1 and exist only one question.
+        $this->assertEquals(0, $view->get_pagevars('qpage'));
+        $this->assertStringContainsString('Example question 3', $html);
+    }
 }
diff --git a/question/classes/local/bank/view.php b/question/classes/local/bank/view.php
index 30d4f5354fc..507e95a030e 100644
--- a/question/classes/local/bank/view.php
+++ b/question/classes/local/bank/view.php
@@ -800,10 +800,13 @@ class view {
         global $DB;
         $questions = $DB->get_recordset_sql($this->loadsql, $this->sqlparams,
             (int)$this->pagevars['qpage'] * (int)$this->pagevars['qperpage'], $this->pagevars['qperpage']);
-        if (empty($questions)) {
+        if (!$questions->valid()) {
             $questions->close();
-            // No questions on this page. Reset to page 0.
-            $questions = $DB->get_recordset_sql($this->loadsql, $this->sqlparams, 0, $this->pagevars['qperpage']);
+            // No questions on this page. Reset to the nearest page that contains questions.
+            $this->pagevars['qpage'] = max(0,
+                ceil($this->totalcount / $this->pagevars['qperpage']) - 1);
+            $questions = $DB->get_recordset_sql($this->loadsql, $this->sqlparams,
+                $this->pagevars['qpage'] * (int) $this->pagevars['qperpage'], $this->pagevars['qperpage']);
         }
         return $questions;
     }
@@ -1155,8 +1158,8 @@ class view {
         echo $this->get_plugin_controls($catcontext, $categoryid);
 
         $this->build_query();
-        $questionsrs = $this->load_page_questions();
         $totalquestions = $this->get_question_count();
+        $questionsrs = $this->load_page_questions();
         $questions = [];
         foreach ($questionsrs as $question) {
             if (!empty($question->id)) {