1 |
efrain |
1 |
{"version":3,"file":"send_resource.min.js","sources":["../../src/moodlenet/send_resource.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 * A module to handle Share operations of the MoodleNet.\n *\n * @module core/moodlenet/send_resource\n * @copyright 2023 Huong Nguyen <huongnv13@gmail.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 4.2\n */\n\nimport Config from 'core/config';\nimport {addNotification, exception as displayException} from 'core/notification';\nimport {getString} from 'core/str';\nimport Prefetch from \"core/prefetch\";\nimport * as Templates from 'core/templates';\nimport {publish} from 'core/pubsub';\nimport * as MoodleNetService from 'core/moodlenet/service';\nimport SendActivityModal from 'core/moodlenet/send_activity_modal';\nimport * as MoodleNetAuthorize from 'core/moodlenet/authorize';\nimport MoodleNetEvents from 'core/moodlenet/events';\n\nconst TYPE_ACTIVITY = \"activity\";\nconst TYPE_COURSE = \"course\";\nconst TYPE_PARTIAL_COURSE = \"partial\";\n\nlet listenersRegistered = false;\nlet currentModal;\nlet siteSupportUrl;\nlet issuerId;\nlet courseId;\nlet resourceId;\nlet shareFormat;\nlet type;\nlet selectedCmIds;\n\n/**\n * Handle send to MoodleNet.\n *\n * @param {int} issuerId The OAuth 2 issuer ID.\n * @param {int} resourceId The resource ID, it can be a course or an activity.\n * @param {int} shareFormat The share format.\n */\nexport const sendToMoodleNet = (issuerId, resourceId, shareFormat) => {\n const $modal = currentModal.getModal();\n const modal = $modal[0];\n modal.querySelector('.modal-header').classList.remove('no-border');\n modal.querySelector('.modal-header').classList.add('no-header-text');\n\n currentModal.setBody(Templates.render('core/moodlenet/send_activity_modal_packaging', {}));\n currentModal.hideFooter();\n\n let infoPromise;\n if (type === TYPE_ACTIVITY) {\n infoPromise = MoodleNetService.sendActivity(issuerId, resourceId, shareFormat);\n } else if (type === TYPE_COURSE) {\n infoPromise = MoodleNetService.sendCourse(issuerId, resourceId, shareFormat);\n } else if (type === TYPE_PARTIAL_COURSE) {\n if (selectedCmIds.length > 1) {\n infoPromise = MoodleNetService.sendPartialCourse(issuerId, resourceId, selectedCmIds, shareFormat);\n } else {\n infoPromise = MoodleNetService.sendActivity(issuerId, selectedCmIds[0], shareFormat);\n }\n }\n infoPromise.then(async(data) => {\n const status = data.status;\n const resourceUrl = data.resourceurl;\n return responseFromMoodleNet(status, resourceUrl);\n }).catch(displayException);\n};\n\n/**\n * Handle response from MoodleNet.\n *\n * @param {boolean} status Response status. True if successful.\n * @param {String} resourceUrl Resource URL.\n */\nconst responseFromMoodleNet = (status, resourceUrl = '') => {\n const $modal = currentModal.getModal();\n const modal = $modal[0];\n modal.querySelector('.modal-header').classList.add('no-border');\n currentModal.setBody(Templates.render('core/moodlenet/send_activity_modal_done', {\n success: status,\n sitesupporturl: siteSupportUrl,\n }));\n\n if (status) {\n currentModal.setFooter(Templates.render('core/moodlenet/send_activity_modal_footer_view', {\n resourceurl: resourceUrl,\n }));\n currentModal.showFooter();\n }\n};\n\n/**\n * Render the modal to send resource to MoodleNet.\n *\n * @param {object} data The data of the resource to be shared.\n * @param {array} selectedActivities Selected activities.\n */\nconst renderModal = async(data, selectedActivities) => {\n if (data.status) {\n siteSupportUrl = data.supportpageurl;\n issuerId = data.issuerid;\n let modalConfig = {\n templateContext: {\n 'activitytype': data.type,\n 'activityname': data.name,\n 'server': data.server,\n }\n };\n if (selectedActivities.length > 0) {\n selectedCmIds = selectedActivities;\n }\n if (selectedActivities.length > 1) {\n modalConfig.templateContext.fullsharing = false;\n modalConfig.templateContext.selectedactivitiesnotice = await getString('moodlenet:sharenoticepartialactivitynumber',\n 'moodle', selectedActivities.length);\n modalConfig.templateContext.sharenotice = await getString('moodlenet:sharenoticepartial', 'moodle');\n } else {\n modalConfig.templateContext.fullsharing = true;\n if (type === TYPE_ACTIVITY || (type === TYPE_PARTIAL_COURSE && selectedActivities.length == 1)) {\n modalConfig.templateContext.sharenotice = await getString('moodlenet:sharenoticeactivity', 'moodle');\n } else {\n modalConfig.templateContext.sharenotice = await getString('moodlenet:sharenoticecourse', 'moodle');\n }\n }\n\n return SendActivityModal.create(modalConfig);\n } else {\n return addNotification({\n message: data.warnings[0].message,\n type: 'error'\n });\n }\n};\n\n/**\n * Handle modal.\n * @param {string} shareActionType Share action type.\n * @param {array} selectedActivities Selected activities.\n */\nexport const handleModal = (shareActionType, selectedActivities = []) => {\n const resourceId = Config.contextInstanceId;\n type = shareActionType;\n\n Promise.resolve(type)\n .then((type) => {\n if (type === TYPE_ACTIVITY) {\n return MoodleNetService.getActivityInformation(resourceId);\n } else if (type === TYPE_COURSE) {\n return MoodleNetService.getCourseInformation(resourceId);\n } else if (type === TYPE_PARTIAL_COURSE) {\n if (selectedActivities.length > 1) {\n // Selected more than one activity.\n return MoodleNetService.getCourseInformation(resourceId);\n } else {\n // Select only one activity. Switch to activity mode.\n return MoodleNetService.getActivityInformation(selectedActivities[0]);\n }\n }\n throw new Error(`Unknown type ${type}`);\n })\n .then((data) => {\n return renderModal(data, selectedActivities);\n })\n .then((modal) => {\n currentModal = modal;\n return currentModal;\n })\n .catch(displayException);\n};\n\n/**\n * Register events.\n */\nconst registerEventListeners = () => {\n document.addEventListener('click', (e) => {\n const shareAction = e.target.closest('[data-action=\"sendtomoodlenet\"]');\n const sendAction = e.target.closest('.moodlenet-action-buttons [data-action=\"share\"]');\n if (shareAction) {\n e.preventDefault();\n type = shareAction.getAttribute('data-type');\n handleModal(shareAction.getAttribute('data-type'));\n }\n\n if (sendAction) {\n e.preventDefault();\n publish(MoodleNetEvents.MOODLENET_SHARE_STARTED, {});\n courseId = Config.courseId;\n resourceId = Config.contextInstanceId;\n shareFormat = 0;\n MoodleNetAuthorize.handleAuthorization(issuerId, courseId, resourceId, shareFormat);\n }\n });\n};\n\n\n/**\n * Initialize.\n */\nexport const init = () => {\n if (!listenersRegistered) {\n Prefetch.prefetchTemplates([\n 'core/moodlenet/send_activity_modal_base',\n 'core/moodlenet/send_activity_modal_packaging',\n 'core/moodlenet/send_activity_modal_done',\n 'core/moodlenet/send_activity_modal_footer_view',\n 'core/moodlenet/send_activity_modal_footer_share',\n ]);\n registerEventListeners();\n listenersRegistered = true;\n }\n};\n"],"names":["currentModal","siteSupportUrl","issuerId","courseId","resourceId","shareFormat","type","selectedCmIds","listenersRegistered","modal","getModal","infoPromise","querySelector","classList","remove","add","setBody","Templates","render","hideFooter","MoodleNetService","sendActivity","sendCourse","length","sendPartialCourse","then","async","status","data","resourceUrl","resourceurl","responseFromMoodleNet","catch","displayException","$modal","success","sitesupporturl","setFooter","showFooter","renderModal","selectedActivities","supportpageurl","issuerid","modalConfig","templateContext","name","server","fullsharing","selectedactivitiesnotice","sharenotice","SendActivityModal","create","message","warnings","handleModal","shareActionType","Config","contextInstanceId","Promise","resolve","getActivityInformation","getCourseInformation","Error","prefetchTemplates","document","addEventListener","e","shareAction","target","closest","sendAction","preventDefault","getAttribute","MoodleNetEvents","MOODLENET_SHARE_STARTED","MoodleNetAuthorize","handleAuthorization"],"mappings":";;;;;;;;yeAwCIA,aACAC,eACAC,SACAC,SACAC,WACAC,YACAC,KACAC,cARAC,qBAAsB,2BAiBK,CAACN,SAAUE,WAAYC,qBAE5CI,MADST,aAAaU,WACP,OAOjBC,YANJF,MAAMG,cAAc,iBAAiBC,UAAUC,OAAO,aACtDL,MAAMG,cAAc,iBAAiBC,UAAUE,IAAI,kBAEnDf,aAAagB,QAAQC,UAAUC,OAAO,+CAAgD,KACtFlB,aAAamB,aA5BK,aA+Bdb,KACAK,YAAcS,iBAAiBC,aAAanB,SAAUE,WA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