From 5ebb8172a3029f422148324284eca06a3df7006f Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 3 May 2024 09:40:41 +0200 Subject: [PATCH] #129 fix problem adding questions from question bank form --- amd/build/edit.min.js | 2 +- amd/build/edit.min.js.map | 2 +- amd/src/edit.js | 23 +++++++++++++---------- edit.php | 2 +- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/amd/build/edit.min.js b/amd/build/edit.min.js index ce0e0e6..1454fe8 100644 --- a/amd/build/edit.min.js +++ b/amd/build/edit.min.js @@ -5,6 +5,6 @@ define("mod_jazzquiz/edit",["exports","jquery","mod_jazzquiz/selectors"],(functi * @copyright 2015 University of Wisconsin - Madison * @copyright 2018 NTNU * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */function submitQuestionOrder(order,courseModuleId){_jquery.default.post("edit.php",{id:courseModuleId,action:"order",order:JSON.stringify(order)},(()=>location.reload()))}function getQuestionOrder(){return[...document.querySelectorAll(".questionlist li")].map((question=>question.dataset.questionId))}function offsetQuestion(questionId,offset){let order=getQuestionOrder(),originalIndex=order.indexOf(questionId);if(-1===originalIndex)return order;for(let i=0;i{const editQuestionAction=event.target.closest(_selectors.default.edit.editQuestionAction);if(editQuestionAction){let order=[];switch(editQuestionAction.dataset.action){case"up":order=offsetQuestion(editQuestionAction.dataset.questionId,1);break;case"down":order=offsetQuestion(editQuestionAction.dataset.questionId,-1);break;case"delete":{order=getQuestionOrder();const index=order.indexOf(editQuestionAction.dataset.questionId);-1!==index&&order.splice(index,1);break}default:return}submitQuestionOrder(order,courseModuleId)}}));let questionList=document.getElementsByClassName("questionlist")[0];"undefined"!=typeof Sortable&&Sortable.create(questionList,{handle:".dragquestion",onSort:()=>submitQuestionOrder(getQuestionOrder(),courseModuleId)});!function(courseModuleId){document.querySelector(_selectors.default.edit.addSelectedQuestions).addEventListener("click",(function(){let questionIds="";for(const checkbox of document.querySelectorAll(_selectors.default.edit.questionCheckedCheckbox))questionIds+=checkbox.getAttribute("name").slice(1)+",";_jquery.default.post("edit.php",{id:courseModuleId,action:"addquestion",questionids:questionIds},(()=>location.reload()))}))}(courseModuleId)},_jquery=_interopRequireDefault(_jquery),_selectors=_interopRequireDefault(_selectors)})); + */function submitQuestionOrder(order,courseModuleId){_jquery.default.post("edit.php",{id:courseModuleId,action:"order",order:JSON.stringify(order)},(()=>location.reload()))}function getQuestionOrder(){return[...document.querySelectorAll(".questionlist li")].map((question=>question.dataset.questionId))}function offsetQuestion(questionId,offset){let order=getQuestionOrder(),originalIndex=order.indexOf(questionId);if(-1===originalIndex)return order;for(let i=0;i{const editQuestionAction=event.target.closest(_selectors.default.edit.editQuestionAction);if(editQuestionAction){let order=[];switch(editQuestionAction.dataset.action){case"up":order=offsetQuestion(editQuestionAction.dataset.questionId,1);break;case"down":order=offsetQuestion(editQuestionAction.dataset.questionId,-1);break;case"delete":{order=getQuestionOrder();const index=order.indexOf(editQuestionAction.dataset.questionId);-1!==index&&order.splice(index,1);break}default:return}submitQuestionOrder(order,courseModuleId)}}));let questionList=document.getElementsByClassName("questionlist")[0];"undefined"!=typeof Sortable&&Sortable.create(questionList,{handle:".dragquestion",onSort:()=>submitQuestionOrder(getQuestionOrder(),courseModuleId)});!function(courseModuleId){document.addEventListener("click",(event=>{if(event.target.closest(_selectors.default.edit.addSelectedQuestions)){event.preventDefault();let questionIds="";for(const checkbox of document.querySelectorAll(_selectors.default.edit.questionCheckedCheckbox))questionIds+=checkbox.getAttribute("name").slice(1)+",";_jquery.default.post("edit.php",{id:courseModuleId,action:"addquestion",questionids:questionIds},(()=>location.reload()))}}))}(courseModuleId)},_jquery=_interopRequireDefault(_jquery),_selectors=_interopRequireDefault(_selectors)})); //# sourceMappingURL=edit.min.js.map \ No newline at end of file diff --git a/amd/build/edit.min.js.map b/amd/build/edit.min.js.map index 95491e4..e8b2759 100644 --- a/amd/build/edit.min.js.map +++ b/amd/build/edit.min.js.map @@ -1 +1 @@ -{"version":3,"file":"edit.min.js","sources":["../src/edit.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 * @module mod_jazzquiz\n * @author Sebastian S. Gundersen \n * @copyright 2015 University of Wisconsin - Madison\n * @copyright 2018 NTNU\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport $ from 'jquery';\nimport selectors from 'mod_jazzquiz/selectors';\n\n/**\n * Submit the question order to the server. An empty array will delete all questions.\n * @param {Array.} order\n * @param {number} courseModuleId\n */\nfunction submitQuestionOrder(order, courseModuleId) {\n $.post('edit.php', {\n id: courseModuleId,\n action: 'order',\n order: JSON.stringify(order)\n }, () => location.reload()); // TODO: Correct locally instead, but for now just refresh.\n}\n\n/**\n * @returns {Array} The current question order.\n */\nfunction getQuestionOrder() {\n const questions = document.querySelectorAll('.questionlist li');\n return [...questions].map(question => question.dataset.questionId);\n}\n\n/**\n * Move a question up or down by a specified offset.\n * @param {number} questionId\n * @param {number} offset Negative to move down, positive to move up\n * @returns {Array}\n */\nfunction offsetQuestion(questionId, offset) {\n let order = getQuestionOrder();\n let originalIndex = order.indexOf(questionId);\n if (originalIndex === -1) {\n return order;\n }\n for (let i = 0; i < order.length; i++) {\n if (i + offset === originalIndex) {\n order[originalIndex] = order[i];\n order[i] = questionId;\n break;\n }\n }\n return order;\n}\n\n/**\n * Add click-listener to a quiz by module id.\n * @param {number} courseModuleId\n */\nfunction listenAddToQuiz(courseModuleId) {\n const addSelectedQuestionsButton = document.querySelector(selectors.edit.addSelectedQuestions);\n addSelectedQuestionsButton.addEventListener('click', function() {\n let questionIds = '';\n for (const checkbox of document.querySelectorAll(selectors.edit.questionCheckedCheckbox)) {\n questionIds += checkbox.getAttribute('name').slice(1) + ',';\n }\n $.post('edit.php', {\n id: courseModuleId,\n action: 'addquestion',\n questionids: questionIds,\n }, () => location.reload());\n });\n}\n\n/**\n * Initialize edit page.\n * @param {Number} courseModuleId\n */\nexport function initialize(courseModuleId) {\n document.addEventListener('click', event => {\n const editQuestionAction = event.target.closest(selectors.edit.editQuestionAction);\n if (editQuestionAction) {\n let order = [];\n switch (editQuestionAction.dataset.action) {\n case 'up':\n order = offsetQuestion(editQuestionAction.dataset.questionId, 1);\n break;\n case 'down':\n order = offsetQuestion(editQuestionAction.dataset.questionId, -1);\n break;\n case 'delete': {\n order = getQuestionOrder();\n const index = order.indexOf(editQuestionAction.dataset.questionId);\n if (index !== -1) {\n order.splice(index, 1);\n }\n break;\n }\n default:\n return;\n }\n submitQuestionOrder(order, courseModuleId);\n }\n });\n let questionList = document.getElementsByClassName('questionlist')[0];\n if (typeof Sortable !== 'undefined') {\n // eslint-disable-next-line no-undef\n Sortable.create(questionList, {\n handle: '.dragquestion',\n onSort: () => submitQuestionOrder(getQuestionOrder(), courseModuleId)\n });\n }\n listenAddToQuiz(courseModuleId);\n}\n"],"names":["submitQuestionOrder","order","courseModuleId","post","id","action","JSON","stringify","location","reload","getQuestionOrder","document","querySelectorAll","map","question","dataset","questionId","offsetQuestion","offset","originalIndex","indexOf","i","length","addEventListener","event","editQuestionAction","target","closest","selectors","edit","index","splice","questionList","getElementsByClassName","Sortable","create","handle","onSort","querySelector","addSelectedQuestions","questionIds","checkbox","questionCheckedCheckbox","getAttribute","slice","questionids","listenAddToQuiz"],"mappings":";;;;;;;cA+BSA,oBAAoBC,MAAOC,gCAC9BC,KAAK,WAAY,CACfC,GAAIF,eACJG,OAAQ,QACRJ,MAAOK,KAAKC,UAAUN,SACvB,IAAMO,SAASC,oBAMbC,yBAEE,IADWC,SAASC,iBAAiB,qBACtBC,KAAIC,UAAYA,SAASC,QAAQC,sBASlDC,eAAeD,WAAYE,YAC5BjB,MAAQS,mBACRS,cAAgBlB,MAAMmB,QAAQJ,gBACX,IAAnBG,qBACOlB,UAEN,IAAIoB,EAAI,EAAGA,EAAIpB,MAAMqB,OAAQD,OAC1BA,EAAIH,SAAWC,cAAe,CAC9BlB,MAAMkB,eAAiBlB,MAAMoB,GAC7BpB,MAAMoB,GAAKL,wBAIZf,2FA0BgBC,gBACvBS,SAASY,iBAAiB,SAASC,cACzBC,mBAAqBD,MAAME,OAAOC,QAAQC,mBAAUC,KAAKJ,uBAC3DA,mBAAoB,KAChBxB,MAAQ,UACJwB,mBAAmBV,QAAQV,YAC1B,KACDJ,MAAQgB,eAAeQ,mBAAmBV,QAAQC,WAAY,aAE7D,OACDf,MAAQgB,eAAeQ,mBAAmBV,QAAQC,YAAa,aAE9D,UACDf,MAAQS,yBACFoB,MAAQ7B,MAAMmB,QAAQK,mBAAmBV,QAAQC,aACxC,IAAXc,OACA7B,MAAM8B,OAAOD,MAAO,wBAOhC9B,oBAAoBC,MAAOC,wBAG/B8B,aAAerB,SAASsB,uBAAuB,gBAAgB,GAC3C,oBAAbC,UAEPA,SAASC,OAAOH,aAAc,CAC1BI,OAAQ,gBACRC,OAAQ,IAAMrC,oBAAoBU,mBAAoBR,4BAlDzCA,gBACcS,SAAS2B,cAAcV,mBAAUC,KAAKU,sBAC9ChB,iBAAiB,SAAS,eAC7CiB,YAAc,OACb,MAAMC,YAAY9B,SAASC,iBAAiBgB,mBAAUC,KAAKa,yBAC5DF,aAAeC,SAASE,aAAa,QAAQC,MAAM,GAAK,oBAE1DzC,KAAK,WAAY,CACfC,GAAIF,eACJG,OAAQ,cACRwC,YAAaL,cACd,IAAMhC,SAASC,cA0CtBqC,CAAgB5C"} \ No newline at end of file +{"version":3,"file":"edit.min.js","sources":["../src/edit.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 * @module mod_jazzquiz\n * @author Sebastian S. Gundersen \n * @copyright 2015 University of Wisconsin - Madison\n * @copyright 2018 NTNU\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport $ from 'jquery';\nimport selectors from 'mod_jazzquiz/selectors';\n\n/**\n * Submit the question order to the server. An empty array will delete all questions.\n * @param {Array.} order\n * @param {number} courseModuleId\n */\nfunction submitQuestionOrder(order, courseModuleId) {\n $.post('edit.php', {\n id: courseModuleId,\n action: 'order',\n order: JSON.stringify(order)\n }, () => location.reload()); // TODO: Correct locally instead, but for now just refresh.\n}\n\n/**\n * @returns {Array} The current question order.\n */\nfunction getQuestionOrder() {\n const questions = document.querySelectorAll('.questionlist li');\n return [...questions].map(question => question.dataset.questionId);\n}\n\n/**\n * Move a question up or down by a specified offset.\n * @param {number} questionId\n * @param {number} offset Negative to move down, positive to move up\n * @returns {Array}\n */\nfunction offsetQuestion(questionId, offset) {\n let order = getQuestionOrder();\n let originalIndex = order.indexOf(questionId);\n if (originalIndex === -1) {\n return order;\n }\n for (let i = 0; i < order.length; i++) {\n if (i + offset === originalIndex) {\n order[originalIndex] = order[i];\n order[i] = questionId;\n break;\n }\n }\n return order;\n}\n\n/**\n * Add click-listener to a quiz by module id.\n * @param {number} courseModuleId\n */\nfunction listenAddToQuiz(courseModuleId) {\n document.addEventListener('click', event => {\n const addSelectedQuestionsButton = event.target.closest(selectors.edit.addSelectedQuestions);\n if (addSelectedQuestionsButton) {\n event.preventDefault();\n let questionIds = '';\n for (const checkbox of document.querySelectorAll(selectors.edit.questionCheckedCheckbox)) {\n questionIds += checkbox.getAttribute('name').slice(1) + ',';\n }\n $.post('edit.php', {\n id: courseModuleId,\n action: 'addquestion',\n questionids: questionIds,\n }, () => location.reload());\n }\n });\n}\n\n/**\n * Initialize edit page.\n * @param {Number} courseModuleId\n */\nexport function initialize(courseModuleId) {\n document.addEventListener('click', event => {\n const editQuestionAction = event.target.closest(selectors.edit.editQuestionAction);\n if (editQuestionAction) {\n let order = [];\n switch (editQuestionAction.dataset.action) {\n case 'up':\n order = offsetQuestion(editQuestionAction.dataset.questionId, 1);\n break;\n case 'down':\n order = offsetQuestion(editQuestionAction.dataset.questionId, -1);\n break;\n case 'delete': {\n order = getQuestionOrder();\n const index = order.indexOf(editQuestionAction.dataset.questionId);\n if (index !== -1) {\n order.splice(index, 1);\n }\n break;\n }\n default:\n return;\n }\n submitQuestionOrder(order, courseModuleId);\n }\n });\n let questionList = document.getElementsByClassName('questionlist')[0];\n if (typeof Sortable !== 'undefined') {\n // eslint-disable-next-line no-undef\n Sortable.create(questionList, {\n handle: '.dragquestion',\n onSort: () => submitQuestionOrder(getQuestionOrder(), courseModuleId)\n });\n }\n listenAddToQuiz(courseModuleId);\n}\n"],"names":["submitQuestionOrder","order","courseModuleId","post","id","action","JSON","stringify","location","reload","getQuestionOrder","document","querySelectorAll","map","question","dataset","questionId","offsetQuestion","offset","originalIndex","indexOf","i","length","addEventListener","event","editQuestionAction","target","closest","selectors","edit","index","splice","questionList","getElementsByClassName","Sortable","create","handle","onSort","addSelectedQuestions","preventDefault","questionIds","checkbox","questionCheckedCheckbox","getAttribute","slice","questionids","listenAddToQuiz"],"mappings":";;;;;;;cA+BSA,oBAAoBC,MAAOC,gCAC9BC,KAAK,WAAY,CACfC,GAAIF,eACJG,OAAQ,QACRJ,MAAOK,KAAKC,UAAUN,SACvB,IAAMO,SAASC,oBAMbC,yBAEE,IADWC,SAASC,iBAAiB,qBACtBC,KAAIC,UAAYA,SAASC,QAAQC,sBASlDC,eAAeD,WAAYE,YAC5BjB,MAAQS,mBACRS,cAAgBlB,MAAMmB,QAAQJ,gBACX,IAAnBG,qBACOlB,UAEN,IAAIoB,EAAI,EAAGA,EAAIpB,MAAMqB,OAAQD,OAC1BA,EAAIH,SAAWC,cAAe,CAC9BlB,MAAMkB,eAAiBlB,MAAMoB,GAC7BpB,MAAMoB,GAAKL,wBAIZf,2FA6BgBC,gBACvBS,SAASY,iBAAiB,SAASC,cACzBC,mBAAqBD,MAAME,OAAOC,QAAQC,mBAAUC,KAAKJ,uBAC3DA,mBAAoB,KAChBxB,MAAQ,UACJwB,mBAAmBV,QAAQV,YAC1B,KACDJ,MAAQgB,eAAeQ,mBAAmBV,QAAQC,WAAY,aAE7D,OACDf,MAAQgB,eAAeQ,mBAAmBV,QAAQC,YAAa,aAE9D,UACDf,MAAQS,yBACFoB,MAAQ7B,MAAMmB,QAAQK,mBAAmBV,QAAQC,aACxC,IAAXc,OACA7B,MAAM8B,OAAOD,MAAO,wBAOhC9B,oBAAoBC,MAAOC,wBAG/B8B,aAAerB,SAASsB,uBAAuB,gBAAgB,GAC3C,oBAAbC,UAEPA,SAASC,OAAOH,aAAc,CAC1BI,OAAQ,gBACRC,OAAQ,IAAMrC,oBAAoBU,mBAAoBR,4BArDzCA,gBACrBS,SAASY,iBAAiB,SAASC,WACIA,MAAME,OAAOC,QAAQC,mBAAUC,KAAKS,sBACvC,CAC5Bd,MAAMe,qBACFC,YAAc,OACb,MAAMC,YAAY9B,SAASC,iBAAiBgB,mBAAUC,KAAKa,yBAC5DF,aAAeC,SAASE,aAAa,QAAQC,MAAM,GAAK,oBAE1DzC,KAAK,WAAY,CACfC,GAAIF,eACJG,OAAQ,cACRwC,YAAaL,cACd,IAAMhC,SAASC,eA2C1BqC,CAAgB5C"} \ No newline at end of file diff --git a/amd/src/edit.js b/amd/src/edit.js index 1143f16..bb13a96 100755 --- a/amd/src/edit.js +++ b/amd/src/edit.js @@ -72,17 +72,20 @@ function offsetQuestion(questionId, offset) { * @param {number} courseModuleId */ function listenAddToQuiz(courseModuleId) { - const addSelectedQuestionsButton = document.querySelector(selectors.edit.addSelectedQuestions); - addSelectedQuestionsButton.addEventListener('click', function() { - let questionIds = ''; - for (const checkbox of document.querySelectorAll(selectors.edit.questionCheckedCheckbox)) { - questionIds += checkbox.getAttribute('name').slice(1) + ','; + document.addEventListener('click', event => { + const addSelectedQuestionsButton = event.target.closest(selectors.edit.addSelectedQuestions); + if (addSelectedQuestionsButton) { + event.preventDefault(); + let questionIds = ''; + for (const checkbox of document.querySelectorAll(selectors.edit.questionCheckedCheckbox)) { + questionIds += checkbox.getAttribute('name').slice(1) + ','; + } + $.post('edit.php', { + id: courseModuleId, + action: 'addquestion', + questionids: questionIds, + }, () => location.reload()); } - $.post('edit.php', { - id: courseModuleId, - action: 'addquestion', - questionids: questionIds, - }, () => location.reload()); }); } diff --git a/edit.php b/edit.php index eb89321..0338e62 100755 --- a/edit.php +++ b/edit.php @@ -89,7 +89,7 @@ function jazzquiz_edit_add_question(jazzquiz $jazzquiz, moodle_url $url): void { $questionids = required_param('questionids', PARAM_TEXT); $questionids = explode(',', $questionids); foreach ($questionids as $questionid) { - $jazzquiz->add_question($questionid); + $jazzquiz->add_question((int)$questionid); } // Ensure there is no action or questionid in the base url. $url->remove_params('action', 'questionids');