MDL-79531 mod_quiz: Fix exception when adding random questions

Moves the data-filtercondition property to an element that always
exists regardless of whether the selected category from the filter
has questions or not. This will effectively solve the exception
that is being thrown due to the non-existing element and also
enable creation of random questions in a category that does not
have any questions yet. Also, the data-filtercondition attribute
is no longer used when creating a random question in a new category
as this value is not relevant in this case. Instead, a default
filter condition is generated in the webservice for the random
question based on the newly created category.
This commit is contained in:
Mihail Geshoski 2023-10-03 23:36:53 +02:00
parent 43d5aec47e
commit 451614016e
6 changed files with 31 additions and 14 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -284,11 +284,12 @@ export default class ModalAddRandomQuestion extends Modal {
e.preventDefault();
const randomcount = document.querySelector(SELECTORS.SELECT_NUMBER_TO_ADD).value;
const filtercondition = document.querySelector(SELECTORS.FILTER_CONDITION_ELEMENT).dataset?.filtercondition;
// Add Random questions if the add random button was clicked.
const addRandomButton = e.target.closest(SELECTORS.ADD_RANDOM_BUTTON);
if (addRandomButton) {
const filtercondition = document.querySelector(SELECTORS.FILTER_CONDITION_ELEMENT).dataset?.filtercondition;
this.addQuestions(cmid, addonpage, randomcount, filtercondition, '', '');
return;
}
@ -299,7 +300,7 @@ export default class ModalAddRandomQuestion extends Modal {
cmid,
addonpage,
randomcount,
filtercondition,
'',
document.querySelector(SELECTORS.NEW_CATEGORY_ELEMENT).value,
document.querySelector(SELECTORS.PARENT_CATEGORY_ELEMENT).value
);

View File

@ -51,7 +51,13 @@ class add_random_questions extends external_api {
'cmid' => new external_value(PARAM_INT, 'The cmid of the quiz'),
'addonpage' => new external_value(PARAM_INT, 'The page where random questions will be added to'),
'randomcount' => new external_value(PARAM_INT, 'Number of random questions'),
'filtercondition' => new external_value(PARAM_TEXT, 'Filter condition'),
'filtercondition' => new external_value(
PARAM_TEXT,
'(Optional) The filter condition used when adding random questions from an existing category.
Not required if adding random questions from a new category.',
VALUE_DEFAULT,
'',
),
'newcategory' => new external_value(
PARAM_TEXT,
'(Optional) The name of a new question category to create and use for the random questions.',
@ -83,7 +89,7 @@ class add_random_questions extends external_api {
int $cmid,
int $addonpage,
int $randomcount,
string $filtercondition,
string $filtercondition = '',
string $newcategory = '',
string $parentcategory = '',
): array {
@ -108,7 +114,8 @@ class add_random_questions extends external_api {
self::validate_context($thiscontext);
require_capability('mod/quiz:manage', $thiscontext);
$filtercondition = json_decode($filtercondition, true);
// If filtercondition is not empty, decode it. Otherwise, set it to empty array.
$filtercondition = !empty($filtercondition) ? json_decode($filtercondition, true) : [];
// Create new category.
if (!empty($newcategory)) {
@ -131,7 +138,15 @@ class add_random_questions extends external_api {
'filteroptions' => ['includesubcategories' => false],
]
];
$filtercondition['filter'] = $filter;
// Generate default filter condition for the random question to be added in the new category.
$filtercondition = [
'qpage' => 0,
'cat' => "{$categoryid},{$thiscontext->id}",
'qperpage' => DEFAULT_QUESTIONS_PER_PAGE,
'tabname' => 'questions',
'sortdata' => [],
'filter' => $filter,
];
}
// Add random question to the quiz.

View File

@ -8,7 +8,9 @@ This files describes API changes in the quiz code.
In the distant past it was always a stdClass, then at one point there was an undocumented change making
it sometimes a stdClass and sometimes a cm_info. Now it is consistently a cm_info. Type hints have been
updated to reflect this.
* The parameter filtercondition in the web service mod_quiz_add_random_questions is now optional. This parameter is
only used when adding random questions from an existing category. It is not required when adding random questions from
a new category since it is not used in this case.
=== 4.2 ===

View File

@ -1167,8 +1167,10 @@ class view {
echo \html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
echo \html_writer::input_hidden_params($this->baseurl);
$filtercondition = json_encode($this->get_pagevars());
// Embeded filterconditon into the div.
echo \html_writer::start_tag('div',
['class' => 'categoryquestionscontainer']);
['class' => 'categoryquestionscontainer', 'data-filtercondition' => $filtercondition]);
if ($totalquestions > 0) {
// Bulk load any required statistics.
$this->load_required_statistics($questions);
@ -1365,11 +1367,8 @@ class view {
echo $OUTPUT->render($pagingbar);
// Table of questions.
// Embeded filterconditon into the div.
$filtercondition = json_encode($this->get_pagevars());
echo \html_writer::start_tag('div',
['class' => 'question_table', 'id' => 'question_table', 'data-filtercondition' => $filtercondition]);
['class' => 'question_table', 'id' => 'question_table']);
$this->print_table($questions);
echo \html_writer::end_tag('div');
echo $OUTPUT->render($pagingbar);