Skip to content

Commit e10e3c6

Browse files
SimonThornettmarxjohnson
authored andcommitted
MDL-87177 question: Update page types returns for 5.2
As part of the qbank work the page types were incorrect. This has been updated. Additionally we now check the context that the quesion is in and load the appropriate module page types as well.
1 parent 8f545ba commit e10e3c6

2 files changed

Lines changed: 99 additions & 12 deletions

File tree

public/lib/questionlib.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,27 +1840,39 @@ function core_question_question_preview_pluginfile($previewcontext, $questionid,
18401840
}
18411841

18421842
/**
1843-
* Return a list of page types
1843+
* Return a list of page types for questions and the page types for the current module/context.
1844+
*
1845+
* This list is used when displaying blocks on a question page, to provide the list of possible page type patterns for the block.
1846+
*
18441847
* @param string $pagetype current page type
18451848
* @param stdClass $parentcontext Block's parent context
18461849
* @param stdClass $currentcontext Current context of block
18471850
* @return array
18481851
*/
18491852
function question_page_type_list($pagetype, $parentcontext, $currentcontext): array {
1850-
global $CFG;
18511853
$types = [
18521854
'question-*' => get_string('page-question-x', 'question'),
18531855
'question-edit' => get_string('page-question-edit', 'question'),
1854-
'question-category' => get_string('page-question-category', 'question'),
1855-
'question-export' => get_string('page-question-export', 'question'),
1856-
'question-import' => get_string('page-question-import', 'question')
1856+
'question-bank-managecategories-category' => get_string('page-question-category', 'question'),
1857+
'question-bank-exportquestions-export' => get_string('page-question-export', 'question'),
1858+
'question-bank-importquestions-import' => get_string('page-question-import', 'question'),
18571859
];
1858-
if ($currentcontext && $currentcontext->contextlevel == CONTEXT_COURSE) {
1859-
require_once($CFG->dirroot . '/course/lib.php');
1860-
return array_merge(course_page_type_list($pagetype, $parentcontext, $currentcontext), $types);
1861-
} else {
1862-
return $types;
1860+
// If current page is in a module context, include the list of page types for that module, if it provides one.
1861+
if ($currentcontext && $currentcontext->contextlevel == CONTEXT_MODULE) {
1862+
[, $cm] = get_course_and_cm_from_cmid($currentcontext->instanceid);
1863+
$directory = core_component::get_plugin_directory('mod', $cm->modname);
1864+
if (!empty($directory)) {
1865+
$libfile = $directory . '/lib.php';
1866+
if (file_exists($libfile)) {
1867+
require_once($libfile);
1868+
$function = $cm->modname . '_page_type_list';
1869+
if (function_exists($function)) {
1870+
return array_merge($function($pagetype, $parentcontext, $currentcontext), $types);
1871+
}
1872+
}
1873+
}
18631874
}
1875+
return $types;
18641876
}
18651877

18661878
/**

public/lib/tests/questionlib_test.php

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
namespace core;
1818

19+
use core\context\course;
20+
use core\context\module;
1921
use core_question\local\bank\question_bank_helper;
2022
use mod_quiz\quiz_settings;
23+
use PHPUnit\Framework\Attributes\DataProvider;
2124
use question_bank;
2225

2326
defined('MOODLE_INTERNAL') || die();
@@ -53,9 +56,9 @@ public function setUp(): void {
5356

5457
/**
5558
* Generate a course and question bank module instance for use in test cases, and return the bank context.
56-
* @return \core\context\module
59+
* @return module
5760
*/
58-
protected function create_course_and_question_bank(): \core\context\module {
61+
protected function create_course_and_question_bank(): module {
5962
$course = self::getDataGenerator()->create_course();
6063
$qbank = self::getDataGenerator()->create_module('qbank', ['course' => $course->id]);
6164
return \context_module::instance($qbank->cmid);
@@ -1777,4 +1780,76 @@ public function test_move_question_set_references_category(): void {
17771780
$this->assertEquals($randomquestion->contextid, $context2->id);
17781781
$this->assertEquals($randomquestion->filtercondition['filter']['category']['values'][0], $topcategory2->id);
17791782
}
1783+
1784+
/**
1785+
* Provide examples of different modules and the page types returned by their _page_type_list function.
1786+
*
1787+
* @return array[] Module name, and a list of page types.
1788+
*/
1789+
public static function module_page_types(): array {
1790+
return [
1791+
'quiz' => [
1792+
'module' => 'quiz',
1793+
'pagetypes' => [
1794+
'mod-quiz-*',
1795+
'mod-quiz-view',
1796+
'mod-quiz-attempt',
1797+
'mod-quiz-summary',
1798+
'mod-quiz-review',
1799+
'mod-quiz-edit',
1800+
'mod-quiz-report',
1801+
],
1802+
],
1803+
'forum' => [
1804+
'module' => 'forum',
1805+
'pagetypes' => [
1806+
'mod-forum-*',
1807+
'mod-forum-view',
1808+
'mod-forum-discuss',
1809+
],
1810+
],
1811+
'scorm' => [
1812+
'module' => 'scorm',
1813+
'pagetypes' => [
1814+
'mod-scorm-*',
1815+
],
1816+
],
1817+
'lti' => [ // LTI has no additional page types.
1818+
'module' => 'lti',
1819+
'pagetypes' => [],
1820+
],
1821+
];
1822+
}
1823+
1824+
/**
1825+
* Calling question_page_type_list should return the question page types, plus the page types for the module context.
1826+
*
1827+
* @covers ::question_page_type_list
1828+
* @param string $module The module name.
1829+
* @param string[] $pagetypes A list of the page type patterns this module should add.
1830+
*/
1831+
#[DataProvider('module_page_types')]
1832+
public function test_question_page_type_list(string $module, array $pagetypes): void {
1833+
$this->resetAfterTest();
1834+
$this->setAdminUser();
1835+
1836+
$course = $this->getDataGenerator()->create_course();
1837+
$coursecontext = course::instance($course->id);
1838+
1839+
$modulerecord = $this->getDataGenerator()->create_module($module, ['course' => $course->id]);
1840+
$modulecontext = module::instance($modulerecord->cmid);
1841+
1842+
$questionpagetypes = [
1843+
'question-*',
1844+
'question-edit',
1845+
'question-bank-managecategories-category',
1846+
'question-bank-exportquestions-export',
1847+
'question-bank-importquestions-import',
1848+
];
1849+
1850+
$expectedpagetypes = array_merge($pagetypes, $questionpagetypes);
1851+
$actualpagetypes = question_page_type_list('question-edit', $coursecontext, $modulecontext);
1852+
// Check that the expected page type patterns match those returned for this module.
1853+
$this->assertEquals($expectedpagetypes, array_keys($actualpagetypes));
1854+
}
17801855
}

0 commit comments

Comments
 (0)