diff --git a/mod/quiz/addrandomform.php b/mod/quiz/addrandomform.php index eec332d0268..6121d874662 100644 --- a/mod/quiz/addrandomform.php +++ b/mod/quiz/addrandomform.php @@ -106,7 +106,8 @@ class quiz_add_random_form extends moodleform { // Add the javascript required to enhance this mform. $PAGE->requires->js_call_amd('mod_quiz/add_random_form', 'init', [ $mform->getAttribute('id'), - $contexts->lowest()->id + $contexts->lowest()->id, + $tops ]); } diff --git a/mod/quiz/amd/build/add_random_form.min.js b/mod/quiz/amd/build/add_random_form.min.js index 03d7365bec4..977a623131c 100644 --- a/mod/quiz/amd/build/add_random_form.min.js +++ b/mod/quiz/amd/build/add_random_form.min.js @@ -1 +1 @@ -define(["jquery","mod_quiz/random_question_form_preview"],function(a,b){var c=2e3,d={PREVIEW_CONTAINER:'[data-region="random-question-preview-container"]',CATEGORY_FORM_ELEMENT:'[name="category"]',SUBCATEGORY_FORM_ELEMENT:'[name="includesubcategories"]',TAG_IDS_FORM_ELEMENT:'[name="fromtags[]"]'},e=function(a){var b=a.find(d.CATEGORY_FORM_ELEMENT).val(),c=b.split(",");return c[0]},f=function(a){return a.find(d.SUBCATEGORY_FORM_ELEMENT).is(":checked")},g=function(a){var b=a.find(d.TAG_IDS_FORM_ELEMENT).val();return b.map(function(a){var b=a.split(",");return b[0]})},h=function(a,c){var h=a.find(d.PREVIEW_CONTAINER);b.reload(h,e(a),f(a),g(a),c)},i=function(a){return a.closest(d.CATEGORY_FORM_ELEMENT).length>0||(a.closest(d.SUBCATEGORY_FORM_ELEMENT).length>0||a.closest(d.TAG_IDS_FORM_ELEMENT).length>0)},j=function(d,e){var f=null;d.on("change",function(g){i(a(g.target))&&(b.showLoadingIcon(d),f&&clearTimeout(f),f=setTimeout(function(){h(d,e)},c))})},k=function(b,c){var d=a("#"+b);h(d,c),j(d,c)};return{init:k}}); \ No newline at end of file +define(["jquery","mod_quiz/random_question_form_preview"],function(a,b){var c=2e3,d={PREVIEW_CONTAINER:'[data-region="random-question-preview-container"]',CATEGORY_FORM_ELEMENT:'[name="category"]',SUBCATEGORY_FORM_ELEMENT:'[name="includesubcategories"]',TAG_IDS_FORM_ELEMENT:'[name="fromtags[]"]'},e=function(a){return a.find(d.CATEGORY_FORM_ELEMENT).val()},f=function(a){var b=e(a),c=b.split(",");return c[0]},g=function(a,b){var c=e(a);return b.indexOf(c)>-1},h=function(a,b){return!!g(a,b)||a.find(d.SUBCATEGORY_FORM_ELEMENT).is(":checked")},i=function(a){var b=a.find(d.TAG_IDS_FORM_ELEMENT).val();return b.map(function(a){var b=a.split(",");return b[0]})},j=function(a,c,e){var g=a.find(d.PREVIEW_CONTAINER);b.reload(g,f(a),h(a,e),i(a),c)},k=function(a){return a.closest(d.CATEGORY_FORM_ELEMENT).length>0||(a.closest(d.SUBCATEGORY_FORM_ELEMENT).length>0||a.closest(d.TAG_IDS_FORM_ELEMENT).length>0)},l=function(d,e,f){var g=null;d.on("change",function(h){k(a(h.target))&&(b.showLoadingIcon(d),g&&clearTimeout(g),g=setTimeout(function(){j(d,e,f)},c))})},m=function(b,c,d){var e=a("#"+b);j(e,c,d),l(e,c,d)};return{init:m}}); \ No newline at end of file diff --git a/mod/quiz/amd/src/add_random_form.js b/mod/quiz/amd/src/add_random_form.js index b2a4b308835..35ebd32d0f7 100644 --- a/mod/quiz/amd/src/add_random_form.js +++ b/mod/quiz/amd/src/add_random_form.js @@ -41,6 +41,16 @@ define( TAG_IDS_FORM_ELEMENT: '[name="fromtags[]"]' }; + /** + * Get the selected category value from the form. + * + * @param {jquery} form The form element. + * @return {string} The category value. + */ + var getCategorySelectValue = function(form) { + return form.find(SELECTORS.CATEGORY_FORM_ELEMENT).val(); + }; + /** * Get the category id from the form. * @@ -50,22 +60,39 @@ define( var getCategoryId = function(form) { // The value string is the category id and category context id joined // by a comma. - var valueString = form.find(SELECTORS.CATEGORY_FORM_ELEMENT).val(); + var valueString = getCategorySelectValue(form); // Split the two ids. var values = valueString.split(','); // Return just the category id. return values[0]; }; + /** + * Check if a top level category is selected in the form. + * + * @param {jquery} form The form element. + * @param {string[]} topCategories List of top category values (matching the select box values) + * @return {bool} + */ + var isTopLevelCategorySelected = function(form, topCategories) { + var selectedValue = getCategorySelectValue(form); + return (topCategories.indexOf(selectedValue) > -1); + }; + /** * Check if the form indicates we should include include subcategories in * the filter. * * @param {jquery} form The form element. + * @param {string[]} topCategories List of top category values (matching the select box values) * @return {bool} */ - var shouldIncludeSubcategories = function(form) { - return form.find(SELECTORS.SUBCATEGORY_FORM_ELEMENT).is(':checked'); + var shouldIncludeSubcategories = function(form, topCategories) { + if (isTopLevelCategorySelected(form, topCategories)) { + return true; + } else { + return form.find(SELECTORS.SUBCATEGORY_FORM_ELEMENT).is(':checked'); + } }; /** @@ -89,13 +116,14 @@ define( * * @param {jquery} form The form element. * @param {int} contextId The current context id. + * @param {string[]} topCategories List of top category values (matching the select box values) */ - var reloadQuestionPreview = function(form, contextId) { + var reloadQuestionPreview = function(form, contextId, topCategories) { var previewContainer = form.find(SELECTORS.PREVIEW_CONTAINER); RandomQuestionFormPreview.reload( previewContainer, getCategoryId(form), - shouldIncludeSubcategories(form), + shouldIncludeSubcategories(form, topCategories), getTagIds(form), contextId ); @@ -136,8 +164,9 @@ define( * * @param {jquery} form The form element. * @param {int} contextId The current context id. + * @param {string[]} topCategories List of top category values (matching the select box values) */ - var addEventListeners = function(form, contextId) { + var addEventListeners = function(form, contextId, topCategories) { var reloadTimerId = null; form.on('change', function(e) { @@ -160,7 +189,7 @@ define( // in case the user is still modifying the form. We don't want to // spam reload requests. reloadTimerId = setTimeout(function() { - reloadQuestionPreview(form, contextId); + reloadQuestionPreview(form, contextId, topCategories); }, RELOAD_DELAY); }); }; @@ -171,12 +200,13 @@ define( * * @param {jquery} formId The form element id. * @param {int} contextId The current context id. + * @param {string[]} topCategories List of top category values (matching the select box values) */ - var init = function(formId, contextId) { + var init = function(formId, contextId, topCategories) { var form = $('#' + formId); - reloadQuestionPreview(form, contextId); - addEventListeners(form, contextId); + reloadQuestionPreview(form, contextId, topCategories); + addEventListeners(form, contextId, topCategories); }; return {