From 5d9761c65572e7bdc489dddadf3c6319e8ddb7c6 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 18 Aug 2023 14:30:35 +0800 Subject: [PATCH] MDL-78324 mod_survey: Convert validation to esm This module is converted to ESM in preparation for simplification of Modal instantiation. --- mod/survey/amd/build/validation.min.js | 16 +++--- mod/survey/amd/build/validation.min.js.map | 2 +- mod/survey/amd/src/validation.js | 57 +++++++------------ .../tests/behat/survey_validation.feature | 24 ++++++++ 4 files changed, 54 insertions(+), 45 deletions(-) create mode 100644 mod/survey/tests/behat/survey_validation.feature diff --git a/mod/survey/amd/build/validation.min.js b/mod/survey/amd/build/validation.min.js index 6bd15d17e7f..b3f85da8360 100644 --- a/mod/survey/amd/build/validation.min.js +++ b/mod/survey/amd/build/validation.min.js @@ -1,11 +1,11 @@ +define("mod_survey/validation",["exports","core/str","core/notification"],(function(_exports,_str,_notification){var obj; /** - * Javascript to handle survey validation. - * - * @module mod_survey/validation - * @copyright 2017 Dan Poltawski - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since 3.3 - */ -define("mod_survey/validation",["jquery","core/str","core/modal_factory","core/notification"],(function($,Str,ModalFactory,Notification){return{ensureRadiosChosen:function(formid){var modalPromise=Str.get_strings([{key:"error",component:"moodle"},{key:"questionsnotanswered",component:"survey"}]).then((function(strings){return ModalFactory.create({type:ModalFactory.types.CANCEL,title:strings[0],body:strings[1]})})).catch(Notification.exception),form=$("#"+formid);form.submit((function(e){return 0===form.find('input:radio[data-survey-default="true"]:checked').length||(e.preventDefault(),modalPromise.then((function(modal){return modal.show(),!1})))}))}}})); + * Javascript to handle survey validation. + * + * @module mod_survey/validation + * @copyright 2017 Dan Poltawski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since 3.3 + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.ensureRadiosChosen=void 0,_notification=(obj=_notification)&&obj.__esModule?obj:{default:obj};_exports.ensureRadiosChosen=formid=>{const form=document.getElementById(formid);form.addEventListener("submit",(e=>{0!==form.querySelectorAll('input[type="radio"][data-survey-default="true"]:checked').length&&(_notification.default.alert((0,_str.get_string)("error"),(0,_str.get_string)("questionsnotanswered","survey"),(0,_str.get_string)("ok")),e.preventDefault())}))}})); //# sourceMappingURL=validation.min.js.map \ No newline at end of file diff --git a/mod/survey/amd/build/validation.min.js.map b/mod/survey/amd/build/validation.min.js.map index bc86d1fa35a..b2004df7b11 100644 --- a/mod/survey/amd/build/validation.min.js.map +++ b/mod/survey/amd/build/validation.min.js.map @@ -1 +1 @@ -{"version":3,"file":"validation.min.js","sources":["../src/validation.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Javascript to handle survey validation.\n *\n * @module mod_survey/validation\n * @copyright 2017 Dan Poltawski \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 3.3\n */\ndefine(['jquery', 'core/str', 'core/modal_factory', 'core/notification'], function($, Str, ModalFactory, Notification) {\n return {\n /**\n * Prevents form submission until all radio buttons are chosen, displays\n * modal error if any choices are missing.\n *\n * @param {String} formid HTML id of form\n */\n ensureRadiosChosen: function(formid) {\n // Prepare modal for display in case of problems.\n var modalPromise = Str.get_strings([\n {key: 'error', component: 'moodle'},\n {key: 'questionsnotanswered', component: 'survey'},\n ]).then(function(strings) {\n return ModalFactory.create({\n type: ModalFactory.types.CANCEL,\n title: strings[0],\n body: strings[1],\n });\n }).catch(Notification.exception);\n\n var form = $('#' + formid);\n form.submit(function(e) {\n // Look for unanswered questions..\n if (form.find('input:radio[data-survey-default=\"true\"]:checked').length !== 0) {\n e.preventDefault();\n // Display the modal error.\n return modalPromise.then(function(modal) {\n modal.show();\n return false;\n });\n }\n\n return true;\n });\n }\n };\n});\n"],"names":["define","$","Str","ModalFactory","Notification","ensureRadiosChosen","formid","modalPromise","get_strings","key","component","then","strings","create","type","types","CANCEL","title","body","catch","exception","form","submit","e","find","length","preventDefault","modal","show"],"mappings":";;;;;;;;AAuBAA,+BAAO,CAAC,SAAU,WAAY,qBAAsB,sBAAsB,SAASC,EAAGC,IAAKC,aAAcC,oBAC9F,CAOHC,mBAAoB,SAASC,YAErBC,aAAeL,IAAIM,YAAY,CAC/B,CAACC,IAAK,QAASC,UAAW,UAC1B,CAACD,IAAK,uBAAwBC,UAAW,YAC1CC,MAAK,SAASC,gBACNT,aAAaU,OAAO,CACvBC,KAAMX,aAAaY,MAAMC,OACzBC,MAAOL,QAAQ,GACfM,KAAMN,QAAQ,QAEnBO,MAAMf,aAAagB,WAElBC,KAAOpB,EAAE,IAAMK,QACnBe,KAAKC,QAAO,SAASC,UAE2D,IAAxEF,KAAKG,KAAK,mDAAmDC,SAC7DF,EAAEG,iBAEKnB,aAAaI,MAAK,SAASgB,cAC9BA,MAAMC,QACC"} \ No newline at end of file +{"version":3,"file":"validation.min.js","sources":["../src/validation.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Javascript to handle survey validation.\n *\n * @module mod_survey/validation\n * @copyright 2017 Dan Poltawski \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 3.3\n */\nimport {get_string as getString} from 'core/str';\nimport Notification from 'core/notification';\n\n/**\n * Prevents form submission until all radio buttons are chosen, displays\n * modal error if any choices are missing.\n *\n * @param {String} formid HTML id of form\n */\nexport const ensureRadiosChosen = (formid) => {\n const form = document.getElementById(formid);\n form.addEventListener('submit', (e) => {\n const optionsToSet = form.querySelectorAll('input[type=\"radio\"][data-survey-default=\"true\"]:checked');\n if (optionsToSet.length !== 0) {\n Notification.alert(\n getString('error'),\n getString('questionsnotanswered', 'survey'),\n getString('ok'),\n );\n e.preventDefault();\n }\n });\n};\n"],"names":["formid","form","document","getElementById","addEventListener","e","querySelectorAll","length","alert","preventDefault"],"mappings":";;;;;;;;gMAgCmCA,eACzBC,KAAOC,SAASC,eAAeH,QACrCC,KAAKG,iBAAiB,UAAWC,IAED,IADPJ,KAAKK,iBAAiB,2DAC1BC,+BACAC,OACT,mBAAU,UACV,mBAAU,uBAAwB,WAClC,mBAAU,OAEdH,EAAEI"} \ No newline at end of file diff --git a/mod/survey/amd/src/validation.js b/mod/survey/amd/src/validation.js index a3bcb2d7b8f..a4eb9b89bae 100644 --- a/mod/survey/amd/src/validation.js +++ b/mod/survey/amd/src/validation.js @@ -21,41 +21,26 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since 3.3 */ -define(['jquery', 'core/str', 'core/modal_factory', 'core/notification'], function($, Str, ModalFactory, Notification) { - return { - /** - * Prevents form submission until all radio buttons are chosen, displays - * modal error if any choices are missing. - * - * @param {String} formid HTML id of form - */ - ensureRadiosChosen: function(formid) { - // Prepare modal for display in case of problems. - var modalPromise = Str.get_strings([ - {key: 'error', component: 'moodle'}, - {key: 'questionsnotanswered', component: 'survey'}, - ]).then(function(strings) { - return ModalFactory.create({ - type: ModalFactory.types.CANCEL, - title: strings[0], - body: strings[1], - }); - }).catch(Notification.exception); +import {get_string as getString} from 'core/str'; +import Notification from 'core/notification'; - var form = $('#' + formid); - form.submit(function(e) { - // Look for unanswered questions.. - if (form.find('input:radio[data-survey-default="true"]:checked').length !== 0) { - e.preventDefault(); - // Display the modal error. - return modalPromise.then(function(modal) { - modal.show(); - return false; - }); - } - - return true; - }); +/** + * Prevents form submission until all radio buttons are chosen, displays + * modal error if any choices are missing. + * + * @param {String} formid HTML id of form + */ +export const ensureRadiosChosen = (formid) => { + const form = document.getElementById(formid); + form.addEventListener('submit', (e) => { + const optionsToSet = form.querySelectorAll('input[type="radio"][data-survey-default="true"]:checked'); + if (optionsToSet.length !== 0) { + Notification.alert( + getString('error'), + getString('questionsnotanswered', 'survey'), + getString('ok'), + ); + e.preventDefault(); } - }; -}); + }); +}; diff --git a/mod/survey/tests/behat/survey_validation.feature b/mod/survey/tests/behat/survey_validation.feature new file mode 100644 index 00000000000..addc41cac8f --- /dev/null +++ b/mod/survey/tests/behat/survey_validation.feature @@ -0,0 +1,24 @@ +@mod @mod_survey @javascript +Feature: When some answers are not selected, the survey should not be submitted + In order to submit valid responses + As a student + I need to fill values + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | + | Course 1 | C1 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C1 | student | + + Scenario: Require survey view + Given the following "activities" exist: + | activity | name | course | + | survey | Test survey name | C1 | + And I am on the "Test survey name" "survey activity" page logged in as "student1" + When I press "Submit" + Then I should see "Some of the multiple choice questions have not been answered." in the "Error" "dialogue"