Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"module.min.js","sources":["../src/module.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 * Manager for the accessreview block.\n *\n * @module block_accessreview/module\n * @author      Max Larkin <max@brickfieldlabs.ie>\n * @copyright   2020 Brickfield Education Labs <max@brickfieldlabs.ie>\n * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport {call as fetchMany} from 'core/ajax';\nimport * as Templates from 'core/templates';\nimport {exception as displayError} from 'core/notification';\n\n/**\n * The number of colours used to represent the heatmap. (Indexed on 0.)\n * @type {number}\n */\nconst numColours = 2;\n\n/**\n * The toggle state of the heatmap.\n * @type {boolean}\n */\nlet toggleState = true;\n\n/**\n * Renders the HTML template onto a particular HTML element.\n * @param {HTMLElement} element The element to attach the HTML to.\n * @param {number} errorCount The number of errors on this module/section.\n * @param {number} checkCount The number of checks triggered on this module/section.\n * @param {String} displayFormat\n * @param {Number} minViews\n * @param {Number} viewDelta\n * @returns {Promise}\n */\nconst renderTemplate = (element, errorCount, checkCount, displayFormat, minViews, viewDelta) => {\n    // Calculate a weight?\n    const weight = parseInt((errorCount - minViews) / viewDelta * numColours);\n\n    const context = {\n        resultPassed: !errorCount,\n        classList: '',\n        passRate: {\n            errorCount,\n            checkCount,\n            failureRate: Math.round(errorCount / checkCount * 100),\n        },\n    };\n\n    if (!element) {\n        return Promise.resolve();\n    }\n\n    const elementClassList = ['block_accessreview'];\n    if (context.resultPassed) {\n        elementClassList.push('block_accessreview_success');\n    } else if (weight) {\n        elementClassList.push('block_accessreview_danger');\n    } else {\n        elementClassList.push('block_accessreview_warning');\n    }\n\n    const showIcons = (displayFormat == 'showicons') || (displayFormat == 'showboth');\n    const showBackground = (displayFormat == 'showbackground') || (displayFormat == 'showboth');\n\n    if (showBackground && !showIcons) {\n        // Only the background is displayed.\n        // No need to display the template.\n        // Note: The case where both the background and icons are shown is handled later to avoid jankiness.\n        element.classList.add(...elementClassList, 'alert');\n\n        return Promise.resolve();\n    }\n\n    if (showIcons && !showBackground) {\n        context.classList = elementClassList.join(' ');\n    }\n\n    // The icons are displayed either with, or without, the background.\n    return Templates.renderForPromise('block_accessreview/status', context)\n    .then(({html, js}) => {\n        Templates.appendNodeContents(element, html, js);\n\n        if (showBackground) {\n            element.classList.add(...elementClassList, 'alert');\n        }\n\n        return;\n    })\n    .catch();\n};\n\n/**\n * Applies the template to all sections and modules on the course page.\n *\n * @param {Number} courseId\n * @param {String} displayFormat\n * @param {Boolean} updatePreference\n * @returns {Promise}\n */\nconst showAccessMap = (courseId, displayFormat, updatePreference = false) => {\n    // Get error data.\n    return Promise.all(fetchReviewData(courseId, updatePreference))\n    .then(([sectionData, moduleData]) => {\n        // Get total data.\n        const {minViews, viewDelta} = getErrorTotals(sectionData, moduleData);\n\n        sectionData.forEach(section => {\n            const element = document.querySelector(`#section-${section.section} .summary`);\n            if (!element) {\n                return;\n            }\n\n            renderTemplate(element, section.numerrors, section.numchecks, displayFormat, minViews, viewDelta);\n        });\n\n        moduleData.forEach(module => {\n            const element = document.getElementById(`module-${module.cmid}`);\n            if (!element) {\n                return;\n            }\n\n            renderTemplate(element, module.numerrors, module.numchecks, displayFormat, minViews, viewDelta);\n        });\n\n        // Change the icon display.\n        document.querySelector('.icon-accessmap').classList.remove(...['fa-eye-slash']);\n        document.querySelector('.icon-accessmap').classList.add(...['fa-eye']);\n\n        return {\n            sectionData,\n            moduleData,\n        };\n    })\n    .catch(displayError);\n};\n\n\n/**\n * Hides or removes the templates from the HTML of the current page.\n *\n * @param {Boolean} updatePreference\n */\nconst hideAccessMap = (updatePreference = false) => {\n    // Removes the added elements.\n    document.querySelectorAll('.block_accessreview_view').forEach(node => node.remove());\n\n    const classList = [\n        'block_accessreview',\n        'block_accessreview_success',\n        'block_accessreview_warning',\n        'block_accessreview_danger',\n        'block_accessreview_view',\n        'alert',\n    ];\n\n    // Removes the added classes.\n    document.querySelectorAll('.block_accessreview').forEach(node => node.classList.remove(...classList));\n\n    if (updatePreference) {\n        setToggleStatePreference(false);\n    }\n\n    // Change the icon display.\n    document.querySelector('.icon-accessmap').classList.remove(...['fa-eye']);\n    document.querySelector('.icon-accessmap').classList.add(...['fa-eye-slash']);\n};\n\n\n/**\n * Toggles the heatmap on/off.\n *\n * @param {Number} courseId\n * @param {String} displayFormat\n */\nconst toggleAccessMap = (courseId, displayFormat) => {\n    toggleState = !toggleState;\n    if (!toggleState) {\n        hideAccessMap(true);\n    } else {\n        showAccessMap(courseId, displayFormat, true);\n    }\n};\n\n/**\n * Parses information on the errors, generating the min, max and totals.\n *\n * @param {Object[]} sectionData The error data for course sections.\n * @param {Object[]} moduleData The error data for course modules.\n * @returns {Object} An object representing the extra error information.\n */\nconst getErrorTotals = (sectionData, moduleData) => {\n    const totals = {\n        totalErrors: 0,\n        totalUsers: 0,\n        minViews: 0,\n        maxViews: 0,\n        viewDelta: 0,\n    };\n\n    [].concat(sectionData, moduleData).forEach(item => {\n        totals.totalErrors += item.numerrors;\n        if (item.numerrors < totals.minViews) {\n            totals.minViews = item.numerrors;\n        }\n\n        if (item.numerrors > totals.maxViews) {\n            totals.maxViews = item.numerrors;\n        }\n        totals.totalUsers += item.numchecks;\n    });\n\n    totals.viewDelta = totals.maxViews - totals.minViews + 1;\n\n    return totals;\n};\n\nconst registerEventListeners = (courseId, displayFormat) => {\n    document.addEventListener('click', e => {\n        if (e.target.closest('#toggle-accessmap')) {\n            e.preventDefault();\n            toggleAccessMap(courseId, displayFormat);\n        }\n    });\n};\n\n/**\n * Set the user preference for the toggle value.\n *\n * @param   {Boolean} toggleState\n * @returns {Promise}\n */\nconst getTogglePreferenceParams = toggleState => {\n    return {\n        methodname: 'core_user_update_user_preferences',\n        args: {\n            preferences: [{\n                type: 'block_accessreviewtogglestate',\n                value: toggleState,\n            }],\n        }\n    };\n};\n\nconst setToggleStatePreference = toggleState => fetchMany([getTogglePreferenceParams(toggleState)]);\n\n/**\n * Fetch the review data.\n *\n * @param   {Number} courseid\n * @param {Boolean} updatePreference\n * @returns {Promise[]}\n */\nconst fetchReviewData = (courseid, updatePreference = false) => {\n    const calls = [\n        {\n            methodname: 'block_accessreview_get_section_data',\n            args: {courseid}\n        },\n        {\n            methodname: 'block_accessreview_get_module_data',\n            args: {courseid}\n        },\n    ];\n\n    if (updatePreference) {\n        calls.push(getTogglePreferenceParams(true));\n    }\n\n    return fetchMany(calls);\n};\n\n/**\n * Setting up the access review module.\n * @param {number} toggled A number represnting the state of the review toggle.\n * @param {string} displayFormat A string representing the display format for icons.\n * @param {number} courseId The course ID.\n */\nexport const init = (toggled, displayFormat, courseId) => {\n    // Settings consts.\n    toggleState = toggled == 1;\n\n    if (toggleState) {\n        showAccessMap(courseId, displayFormat);\n    }\n\n    registerEventListeners(courseId, displayFormat);\n};\n"],"names":["toggleState","renderTemplate","element","errorCount","checkCount","displayFormat","minViews","viewDelta","weight","parseInt","context","resultPassed","classList","passRate","failureRate","Math","round","Promise","resolve","elementClassList","push","showIcons","showBackground","add","join","Templates","renderForPromise","then","_ref","html","js","appendNodeContents","catch","showAccessMap","courseId","updatePreference","all","fetchReviewData","_ref2","sectionData","moduleData","getErrorTotals","forEach","section","document","querySelector","numerrors","numchecks","module","getElementById","cmid","remove","displayError","toggleAccessMap","querySelectorAll","node","setToggleStatePreference","hideAccessMap","totals","totalErrors","totalUsers","maxViews","concat","item","getTogglePreferenceParams","methodname","args","preferences","type","value","courseid","calls","toggled","addEventListener","e","target","closest","preventDefault","register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