MDL-69552 Quiz: Fix tag filter does not work when adding random question

This commit is contained in:
Huong Nguyen 2022-03-30 15:30:42 +07:00
parent 7ce003b666
commit b307f3df02
4 changed files with 36 additions and 3 deletions

View File

@ -5,6 +5,6 @@
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("mod_quiz/add_random_form",["jquery","mod_quiz/random_question_form_preview"],(function($,RandomQuestionFormPreview){var SELECTORS_PREVIEW_CONTAINER='[data-region="random-question-preview-container"]',SELECTORS_CATEGORY_FORM_ELEMENT='[name="category"]',SELECTORS_SUBCATEGORY_FORM_ELEMENT='[name="includesubcategories"]',SELECTORS_TAG_IDS_FORM_ELEMENT='[name="fromtags[]"]',getCategorySelectValue=function(form){return form.find(SELECTORS_CATEGORY_FORM_ELEMENT).val()},shouldIncludeSubcategories=function(form,topCategories){return!!function(form,topCategories){var selectedValue=getCategorySelectValue(form);return topCategories.indexOf(selectedValue)>-1}(form,topCategories)||form.find(SELECTORS_SUBCATEGORY_FORM_ELEMENT).is(":checked")},reloadQuestionPreview=function(form,contextId,topCategories){var previewContainer=form.find(SELECTORS_PREVIEW_CONTAINER);RandomQuestionFormPreview.reload(previewContainer,function(form){return getCategorySelectValue(form).split(",")[0]}(form),shouldIncludeSubcategories(form,topCategories),function(form){return form.find(SELECTORS_TAG_IDS_FORM_ELEMENT).val().map((function(value){return value.split(",")[0]}))}(form),contextId)},addEventListeners=function(form,contextId,topCategories){var reloadTimerId=null;form.on("change",(function(e){var element;((element=$(e.target)).closest(SELECTORS_CATEGORY_FORM_ELEMENT).length>0||element.closest(SELECTORS_SUBCATEGORY_FORM_ELEMENT).length>0||element.closest(SELECTORS_TAG_IDS_FORM_ELEMENT).length>0)&&(RandomQuestionFormPreview.showLoadingIcon(form),reloadTimerId&&clearTimeout(reloadTimerId),reloadTimerId=setTimeout((function(){reloadQuestionPreview(form,contextId,topCategories)}),2e3))}))};return{init:function(formId,contextId,topCategories,isTagsEnabled){if(1==isTagsEnabled){var form=$("#"+formId);reloadQuestionPreview(form,contextId,topCategories),addEventListeners(form,contextId,topCategories)}}}}));
define("mod_quiz/add_random_form",["jquery","mod_quiz/random_question_form_preview"],(function($,RandomQuestionFormPreview){var SELECTORS_PREVIEW_CONTAINER='[data-region="random-question-preview-container"]',SELECTORS_CATEGORY_FORM_ELEMENT='[name="category"]',SELECTORS_SUBCATEGORY_FORM_ELEMENT='[name="includesubcategories"]',SELECTORS_TAG_IDS_FORM_ELEMENT='[name="fromtags[]"]',getCategorySelectValue=function(form){return form.find(SELECTORS_CATEGORY_FORM_ELEMENT).val()},shouldIncludeSubcategories=function(form,topCategories){return!!function(form,topCategories){var selectedValue=getCategorySelectValue(form);return topCategories.indexOf(selectedValue)>-1}(form,topCategories)||form.find(SELECTORS_SUBCATEGORY_FORM_ELEMENT).is(":checked")},reloadQuestionPreview=function(form,contextId,topCategories){var previewContainer=form.find(SELECTORS_PREVIEW_CONTAINER);RandomQuestionFormPreview.reload(previewContainer,function(form){return getCategorySelectValue(form).split(",")[0]}(form),shouldIncludeSubcategories(form,topCategories),function(form){return form.find(SELECTORS_TAG_IDS_FORM_ELEMENT).val().map((function(value){return value.split(",")[0]}))}(form),contextId)},addEventListeners=function(form,contextId,topCategories){var reloadTimerId=null;const tagsFilter=form.find(SELECTORS_TAG_IDS_FORM_ELEMENT);form.add(tagsFilter).on("change",(function(e){var element;((element=$(e.target)).closest(SELECTORS_CATEGORY_FORM_ELEMENT).length>0||element.closest(SELECTORS_SUBCATEGORY_FORM_ELEMENT).length>0||element.closest(SELECTORS_TAG_IDS_FORM_ELEMENT).length>0)&&(RandomQuestionFormPreview.showLoadingIcon(form),reloadTimerId&&clearTimeout(reloadTimerId),reloadTimerId=setTimeout((function(){reloadQuestionPreview(form,contextId,topCategories)}),2e3))}))};return{init:function(formId,contextId,topCategories,isTagsEnabled){if(1==isTagsEnabled){var form=$("#"+formId);reloadQuestionPreview(form,contextId,topCategories),addEventListeners(form,contextId,topCategories)}}}}));
//# sourceMappingURL=add_random_form.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -167,8 +167,9 @@ define(
*/
var addEventListeners = function(form, contextId, topCategories) {
var reloadTimerId = null;
const tagsFilter = form.find(SELECTORS.TAG_IDS_FORM_ELEMENT);
form.on('change', function(e) {
form.add(tagsFilter).on('change', function(e) {
// Only reload the preview when elements that will change the result
// are modified.
if (!isInterestingElement($(e.target))) {

View File

@ -21,14 +21,21 @@ Feature: Adding random questions to a quiz based on category and tags
| contextlevel | reference | name |
| Course | C1 | Questions Category 1 |
| Course | C1 | Questions Category 2 |
And the following "question categories" exist:
| contextlevel | reference | name | questioncategory |
| Course | C1 | Subcategory | Questions Category 1 |
And the following "questions" exist:
| questioncategory | qtype | name | user | questiontext |
| Questions Category 1 | essay | question 1 name | admin | Question 1 text |
| Questions Category 1 | essay | question 2 name | teacher1 | Question 2 text |
| Subcategory | essay | question 3 name | teacher1 | Question 3 text |
| Subcategory | essay | question 4 name | teacher1 | Question 4 text |
And the following "core_question > Tags" exist:
| question | tag |
| question 1 name | foo |
| question 2 name | bar |
| question 3 name | foo |
| question 4 name | bar |
Scenario: Available tags are shown in the autocomplete tag field
Given I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher1"
@ -38,6 +45,31 @@ Feature: Adding random questions to a quiz based on category and tags
Then "foo" "autocomplete_suggestions" should exist
And "bar" "autocomplete_suggestions" should exist
Scenario: Questions can be filtered by tags
Given I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher1"
When I open the "last" add to quiz menu
And I follow "a random question"
And I set the field "Category" to "Top for Course 1"
And I wait until the page is ready
And I open the autocomplete suggestions list
And I click on "foo" item in the autocomplete list
Then I should see "question 1 name"
And I should see "question 3 name"
And I should not see "question 2 name"
And I should not see "question 4 name"
And I set the field "Category" to "Questions Category 1"
And I wait until the page is ready
And I should see "question 1 name"
And I should not see "question 3 name"
And I should not see "question 2 name"
And I should not see "question 4 name"
And I click on "Include questions from subcategories too" "checkbox"
And I wait until the page is ready
And I should see "question 1 name"
And I should see "question 3 name"
And I should not see "question 2 name"
And I should not see "question 4 name"
Scenario: A random question can be added to the quiz
Given I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher1"
When I open the "last" add to quiz menu