mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 10:26:40 +02:00
MDL-83606 Question bank: Delete questions to fit one page causes errors.
This commit is contained in:
parent
505a85eb9a
commit
69fc9048bf
2 changed files with 96 additions and 4 deletions
|
@ -31,6 +31,7 @@ require_once($CFG->dirroot . '/question/editlib.php');
|
||||||
* @category test
|
* @category test
|
||||||
* @copyright 2018 the Open University
|
* @copyright 2018 the Open University
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @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 {
|
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.
|
// Verify the question has not been loaded into the cache.
|
||||||
$this->assertFalse($cache->has($questiondata->id));
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -800,10 +800,13 @@ class view {
|
||||||
global $DB;
|
global $DB;
|
||||||
$questions = $DB->get_recordset_sql($this->loadsql, $this->sqlparams,
|
$questions = $DB->get_recordset_sql($this->loadsql, $this->sqlparams,
|
||||||
(int)$this->pagevars['qpage'] * (int)$this->pagevars['qperpage'], $this->pagevars['qperpage']);
|
(int)$this->pagevars['qpage'] * (int)$this->pagevars['qperpage'], $this->pagevars['qperpage']);
|
||||||
if (empty($questions)) {
|
if (!$questions->valid()) {
|
||||||
$questions->close();
|
$questions->close();
|
||||||
// No questions on this page. Reset to page 0.
|
// No questions on this page. Reset to the nearest page that contains questions.
|
||||||
$questions = $DB->get_recordset_sql($this->loadsql, $this->sqlparams, 0, $this->pagevars['qperpage']);
|
$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;
|
return $questions;
|
||||||
}
|
}
|
||||||
|
@ -1155,8 +1158,8 @@ class view {
|
||||||
echo $this->get_plugin_controls($catcontext, $categoryid);
|
echo $this->get_plugin_controls($catcontext, $categoryid);
|
||||||
|
|
||||||
$this->build_query();
|
$this->build_query();
|
||||||
$questionsrs = $this->load_page_questions();
|
|
||||||
$totalquestions = $this->get_question_count();
|
$totalquestions = $this->get_question_count();
|
||||||
|
$questionsrs = $this->load_page_questions();
|
||||||
$questions = [];
|
$questions = [];
|
||||||
foreach ($questionsrs as $question) {
|
foreach ($questionsrs as $question) {
|
||||||
if (!empty($question->id)) {
|
if (!empty($question->id)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue