Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
11 efrain 1
{"version":3,"file":"modal_add_random_question.min.js","sources":["../src/modal_add_random_question.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 <http://www.gnu.org/licenses/>.\n\n/**\n * Contain the logic for the add random question modal.\n *\n * @module     mod_quiz/modal_add_random_question\n * @copyright  2018 Ryan Wyllie <ryan@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport $ from 'jquery';\nimport Modal from './add_question_modal';\nimport * as Notification from 'core/notification';\nimport * as Fragment from 'core/fragment';\nimport * as Templates from 'core/templates';\nimport * as FormChangeChecker from 'core_form/changechecker';\nimport {call as fetchMany} from 'core/ajax';\nimport Pending from 'core/pending';\n\nconst SELECTORS = {\n    EXISTING_CATEGORY_CONTAINER: '[data-region=\"existing-category-container\"]',\n    EXISTING_CATEGORY_TAB: '#id_existingcategoryheader',\n    NEW_CATEGORY_CONTAINER: '[data-region=\"new-category-container\"]',\n    NEW_CATEGORY_TAB: '#id_newcategoryheader',\n    TAB_CONTENT: '[data-region=\"tab-content\"]',\n    ADD_ON_PAGE_FORM_ELEMENT: '[name=\"addonpage\"]',\n    ADD_RANDOM_BUTTON: 'input[type=\"submit\"][name=\"addrandom\"]',\n    ADD_NEW_CATEGORY_BUTTON: 'input[type=\"submit\"][name=\"newcategory\"]',\n    SUBMIT_BUTTON_ELEMENT: 'input[type=\"submit\"][name=\"addrandom\"], input[type=\"submit\"][name=\"newcategory\"]',\n    FORM_HEADER: 'legend',\n    SELECT_NUMBER_TO_ADD: '#menurandomcount',\n    NEW_CATEGORY_ELEMENT: '#categoryname',\n    PARENT_CATEGORY_ELEMENT: '#parentcategory',\n    FILTER_CONDITION_ELEMENT: '[data-filtercondition]',\n    FORM_ELEMENT: '#add_random_question_form',\n    MESSAGE_INPUT: '[name=\"message\"]',\n};\n\nexport default class ModalAddRandomQuestion extends Modal {\n    static TYPE = 'mod_quiz-quiz-add-random-question';\n    static TEMPLATE = 'mod_quiz/modal_add_random_question';\n\n    /**\n     * Create the add random question modal.\n     *\n     * @param  {Number} contextId Current context id.\n     * @param  {string} category Category id and category context id comma separated.\n     * @param  {string} returnUrl URL to return to after form submission.\n     * @param  {Number} cmid Current course module id.\n     * @param  {boolean} showNewCategory Display the New category tab when selecting random questions.\n     */\n    static init(contextId, category, returnUrl, cmid, showNewCategory = true) {\n        const selector = '.menu [data-action=\"addarandomquestion\"]';\n        document.addEventListener('click', (e) => {\n            const trigger = e.target.closest(selector);\n            if (!trigger) {\n                return;\n            }\n            e.preventDefault();\n\n            ModalAddRandomQuestion.create({\n                contextId,\n                category,\n                returnUrl,\n                cmid,\n\n                title: trigger.dataset.header,\n                addOnPage: trigger.dataset.addonpage,\n\n                templateContext: {\n                    hidden: showNewCategory,\n                },\n            });\n        });\n    }\n\n    /**\n     * Constructor for the Modal.\n     *\n     * @param {object} root The root jQuery element for the modal\n     */\n    constructor(root) {\n        super(root);\n        this.category = null;\n        this.returnUrl = null;\n        this.cmid = null;\n        this.loadedForm = false;\n    }\n\n    configure(modalConfig) {\n        modalConfig.removeOnClose = true;\n\n        this.setCategory(modalConfig.category);\n        this.setReturnUrl(modalConfig.returnUrl);\n        this.setCMID(modalConfig.cmid);\n\n        super.configure(modalConfig);\n    }\n\n    /**\n     * Set the id of the page that the question should be added to\n     * when the user clicks the add to quiz link.\n     *\n     * @method setAddOnPageId\n     * @param {int} id\n     */\n    setAddOnPageId(id) {\n        super.setAddOnPageId(id);\n        this.getBody().find(SELECTORS.ADD_ON_PAGE_FORM_ELEMENT).val(id);\n    }\n\n    /**\n     * Set the category for this form. The category is a comma separated\n     * category id and category context id.\n     *\n     * @method setCategory\n     * @param {string} category\n     */\n    setCategory(category) {\n        this.category = category;\n    }\n\n    /**\n     * Returns the saved category.\n     *\n     * @method getCategory\n     * @return {string}\n     */\n    getCategory() {\n        return this.category;\n    }\n\n    /**\n     * Set the return URL for the form.\n     *\n     * @method setReturnUrl\n     * @param {string} url\n     */\n    setReturnUrl(url) {\n        this.returnUrl = url;\n    }\n\n    /**\n     * Returns the return URL for the form.\n     *\n     * @method getReturnUrl\n     * @return {string}\n     */\n    getReturnUrl() {\n        return this.returnUrl;\n    }\n\n    /**\n     * Set the course module id for the form.\n     *\n     * @method setCMID\n     * @param {Number} id\n     */\n    setCMID(id) {\n        this.cmid = id;\n    }\n\n    /**\n     * Returns the course module id for the form.\n     *\n     * @method getCMID\n     * @return {Number}\n     */\n    getCMID() {\n        return this.cmid;\n    }\n\n    /**\n     * Moves a given form element inside (a child of) a given tab element.\n     *\n     * Hides the 'legend' (e.g. header) element of the form element because the\n     * tab has the name.\n     *\n     * Moves the submit button into a footer element at the bottom of the form\n     * element for styling purposes.\n     *\n     * @method moveContentIntoTab\n     * @param  {jquery} tabContent The form element to move into the tab.\n     * @param  {jquey} tabElement The tab element for the form element to move into.\n     */\n    moveContentIntoTab(tabContent, tabElement) {\n        // Hide the header because the tabs show us which part of the form we're\n        // looking at.\n        tabContent.find(SELECTORS.FORM_HEADER).addClass('hidden');\n        // Move the element inside a tab.\n        tabContent.wrap(tabElement);\n    }\n\n    /**\n     * Empty the tab content container and move all tabs from the form into the\n     * tab container element.\n     *\n     * @method moveTabsIntoTabContent\n     * @param  {jquery} form The form element.\n     */\n    moveTabsIntoTabContent(form) {\n        // Empty it to remove the loading icon.\n        const tabContent = this.getBody().find(SELECTORS.TAB_CONTENT).empty();\n        // Make sure all tabs are inside the tab content element.\n        form.find('[role=\"tabpanel\"]').wrapAll(tabContent);\n    }\n\n    /**\n     * Make sure all of the tabs have a cancel button in their fotter to sit along\n     * side the submit button.\n     *\n     * @method moveCancelButtonToTabs\n     * @param  {jquey} form The form element.\n     */\n    moveCancelButtonToTabs(form) {\n        const cancelButton = form.find(SELECTORS.CANCEL_BUTTON_ELEMENT).addClass('ml-1');\n        const tabFooters = form.find('[data-region=\"footer\"]');\n        // Remove the buttons container element.\n        cancelButton.closest(SELECTORS.BUTTON_CONTAINER).remove();\n        cancelButton.clone().appendTo(tabFooters);\n    }\n\n    /**\n     * Load the add random question form in a fragement and perform some transformation\n     * on the HTML to convert it into tabs for rendering in the modal.\n     *\n     * @method loadForm\n     * @return {promise} Resolved with form HTML and JS.\n     */\n    loadForm() {\n        const cmid = this.getCMID();\n        const cat = this.getCategory();\n        const addonpage = this.getAddOnPageId();\n        const returnurl = this.getReturnUrl();\n\n        return Fragment.loadFragment(\n            'mod_quiz',\n            'add_random_question_form',\n            this.getContextId(),\n            {\n                addonpage,\n                cat,\n                returnurl,\n                cmid,\n            }\n        )\n        .then((html, js) =>{\n            const form = $(html);\n            const existingCategoryTabContent = form.find(SELECTORS.EXISTING_CATEGORY_TAB);\n            const existingCategoryTab = this.getBody().find(SELECTORS.EXISTING_CATEGORY_CONTAINER);\n            const newCategoryTabContent = form.find(SELECTORS.NEW_CATEGORY_TAB);\n            const newCategoryTab = this.getBody().find(SELECTORS.NEW_CATEGORY_CONTAINER);\n\n            // Transform the form into tabs for better rendering in the modal.\n            this.moveContentIntoTab(existingCategoryTabContent, existingCategoryTab);\n            this.moveContentIntoTab(newCategoryTabContent, newCategoryTab);\n            this.moveTabsIntoTabContent(form);\n\n            Templates.replaceNode(this.getBody().find(SELECTORS.TAB_CONTENT), form, js);\n            return;\n        })\n        .then(() => {\n            // Make sure the form change checker is disabled otherwise it'll stop the user from navigating away from the\n            // page once the modal is hidden.\n            FormChangeChecker.disableAllChecks();\n\n            // Add question to quiz.\n            this.getBody()[0].addEventListener('click', (e) => {\n                const button = e.target.closest(SELECTORS.SUBMIT_BUTTON_ELEMENT);\n                if (!button) {\n                    return;\n                }\n                e.preventDefault();\n\n                // Add Random questions if the add random button was clicked.\n                const addRandomButton = e.target.closest(SELECTORS.ADD_RANDOM_BUTTON);\n                if (addRandomButton) {\n                    const randomcount = document.querySelector(SELECTORS.SELECT_NUMBER_TO_ADD).value;\n                    const filtercondition = document.querySelector(SELECTORS.FILTER_CONDITION_ELEMENT).dataset?.filtercondition;\n\n                    this.addQuestions(cmid, addonpage, randomcount, filtercondition, '', '');\n                    return;\n                }\n                // Add new category if the add category button was clicked.\n                const addCategoryButton = e.target.closest(SELECTORS.ADD_NEW_CATEGORY_BUTTON);\n                if (addCategoryButton) {\n                    this.addQuestions(\n                        cmid,\n                        addonpage,\n                        1,\n                        '',\n                        document.querySelector(SELECTORS.NEW_CATEGORY_ELEMENT).value,\n                        document.querySelector(SELECTORS.PARENT_CATEGORY_ELEMENT).value\n                    );\n                    return;\n                }\n            });\n        })\n        .catch(Notification.exception);\n    }\n\n    /**\n     * Call web service function to add random questions\n     *\n     * @param {number} cmid course module id\n     * @param {number} addonpage the page where random questions will be added to\n     * @param {number} randomcount Number of random questions\n     * @param {string} filtercondition Filter condition\n     * @param {string} newcategory add new category\n     * @param {string} parentcategory parent category of new category\n     */\n    async addQuestions(\n        cmid,\n        addonpage,\n        randomcount,\n        filtercondition,\n        newcategory,\n        parentcategory\n    ) {\n        // We do not need to resolve this Pending because the form submission will result in a page redirect.\n        new Pending('mod-quiz/modal_add_random_questions');\n        const call = {\n            methodname: 'mod_quiz_add_random_questions',\n            args: {\n                cmid,\n                addonpage,\n                randomcount,\n                filtercondition,\n                newcategory,\n                parentcategory,\n            }\n        };\n        try {\n            const response = await fetchMany([call])[0];\n            const form = document.querySelector(SELECTORS.FORM_ELEMENT);\n            const messageInput = form.querySelector(SELECTORS.MESSAGE_INPUT);\n            messageInput.value = response.message;\n            form.submit();\n        } catch (e) {\n            Notification.exception(e);\n        }\n    }\n\n    /**\n     * Override the modal show function to load the form when this modal is first\n     * shown.\n     *\n     * @method show\n     */\n    show() {\n        super.show(this);\n\n        if (!this.loadedForm) {\n            this.loadForm(window.location.search);\n            this.loadedForm = true;\n        }\n    }\n}\n\nModalAddRandomQuestion.registerModalType();\n"],"names":["SELECTORS","EXISTING_CATEGORY_CONTAINER","EXISTING_CATEGORY_TAB","NEW_CATEGORY_CONTAINER","NEW_CATEGORY_TAB","TAB_CONTENT","ADD_ON_PAGE_FORM_ELEMENT","ADD_RANDOM_BUTTON","ADD_NEW_CATEGORY_BUTTON","SUBMIT_BUTTON_ELEMENT","FORM_HEADER","SELECT_NUMBER_TO_ADD","NEW_CATEGORY_ELEMENT","PARENT_CATEGORY_ELEMENT","FILTER_CONDITION_ELEMENT","FORM_ELEMENT","MESSAGE_INPUT","ModalAddRandomQuestion","Modal","contextId","category","returnUrl","cmid","showNewCategory","document","addEventListener","e","trigger","target","closest","preventDefault","create","title","dataset","header","addOnPage","addonpage","templateContext","hidden","constructor","root","loadedForm","configure","modalConfig","removeOnClose","setCategory","setReturnUrl","setCMID","setAddOnPageId","id","getBody","find","val","getCategory","this","url","getReturnUrl","getCMID","moveContentIntoTab","tabContent","tabElement","addClass","wrap","moveTabsIntoTabContent","form","empty","wrapAll","moveCancelButtonToTabs","cancelButton","CANCEL_BUTTON_ELEMENT","tabFooters","BUTTON_CONTAINER","remove","clone","appendTo","loadForm","cat","getAddOnPageId","returnurl","Fragment","loadFragment","getContextId","then","html","js","existingCategoryTabContent","existingCategoryTab","newCategoryTabContent","newCategoryTab","Templates","replaceNode","FormChangeChecker","disableAllChecks","randomcount","querySelector","value","filtercondition","_document$querySelect","addQuestions","catch","Notification","exception","newcategory","parentcategory","Pending","call","methodname","args","response","message","submit","show","window","location","search","registerModalType"],"mappings":"g5DAgCMA,UAAY,CACdC,4BAA6B,8CAC7BC,sBAAuB,6BACvBC,uBAAwB,yCACxBC,iBAAkB,wBAClBC,YAA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