1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* A module to handle Share operations of the MoodleNet.
|
|
|
18 |
*
|
|
|
19 |
* @module core/moodlenet/send_resource
|
|
|
20 |
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @since 4.2
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
import Config from 'core/config';
|
|
|
26 |
import {addNotification, exception as displayException} from 'core/notification';
|
|
|
27 |
import {getString} from 'core/str';
|
|
|
28 |
import Prefetch from "core/prefetch";
|
|
|
29 |
import * as Templates from 'core/templates';
|
|
|
30 |
import {publish} from 'core/pubsub';
|
|
|
31 |
import * as MoodleNetService from 'core/moodlenet/service';
|
|
|
32 |
import SendActivityModal from 'core/moodlenet/send_activity_modal';
|
|
|
33 |
import * as MoodleNetAuthorize from 'core/moodlenet/authorize';
|
|
|
34 |
import MoodleNetEvents from 'core/moodlenet/events';
|
|
|
35 |
|
|
|
36 |
const TYPE_ACTIVITY = "activity";
|
|
|
37 |
const TYPE_COURSE = "course";
|
|
|
38 |
const TYPE_PARTIAL_COURSE = "partial";
|
|
|
39 |
|
|
|
40 |
let listenersRegistered = false;
|
|
|
41 |
let currentModal;
|
|
|
42 |
let siteSupportUrl;
|
|
|
43 |
let issuerId;
|
|
|
44 |
let courseId;
|
|
|
45 |
let resourceId;
|
|
|
46 |
let shareFormat;
|
|
|
47 |
let type;
|
|
|
48 |
let selectedCmIds;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Handle send to MoodleNet.
|
|
|
52 |
*
|
|
|
53 |
* @param {int} issuerId The OAuth 2 issuer ID.
|
|
|
54 |
* @param {int} resourceId The resource ID, it can be a course or an activity.
|
|
|
55 |
* @param {int} shareFormat The share format.
|
|
|
56 |
*/
|
|
|
57 |
export const sendToMoodleNet = (issuerId, resourceId, shareFormat) => {
|
|
|
58 |
const $modal = currentModal.getModal();
|
|
|
59 |
const modal = $modal[0];
|
|
|
60 |
modal.querySelector('.modal-header').classList.remove('no-border');
|
|
|
61 |
modal.querySelector('.modal-header').classList.add('no-header-text');
|
|
|
62 |
|
|
|
63 |
currentModal.setBody(Templates.render('core/moodlenet/send_activity_modal_packaging', {}));
|
|
|
64 |
currentModal.hideFooter();
|
|
|
65 |
|
|
|
66 |
let infoPromise;
|
|
|
67 |
if (type === TYPE_ACTIVITY) {
|
|
|
68 |
infoPromise = MoodleNetService.sendActivity(issuerId, resourceId, shareFormat);
|
|
|
69 |
} else if (type === TYPE_COURSE) {
|
|
|
70 |
infoPromise = MoodleNetService.sendCourse(issuerId, resourceId, shareFormat);
|
|
|
71 |
} else if (type === TYPE_PARTIAL_COURSE) {
|
|
|
72 |
if (selectedCmIds.length > 1) {
|
|
|
73 |
infoPromise = MoodleNetService.sendPartialCourse(issuerId, resourceId, selectedCmIds, shareFormat);
|
|
|
74 |
} else {
|
|
|
75 |
infoPromise = MoodleNetService.sendActivity(issuerId, selectedCmIds[0], shareFormat);
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
infoPromise.then(async(data) => {
|
|
|
79 |
const status = data.status;
|
|
|
80 |
const resourceUrl = data.resourceurl;
|
|
|
81 |
return responseFromMoodleNet(status, resourceUrl);
|
|
|
82 |
}).catch(displayException);
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Handle response from MoodleNet.
|
|
|
87 |
*
|
|
|
88 |
* @param {boolean} status Response status. True if successful.
|
|
|
89 |
* @param {String} resourceUrl Resource URL.
|
|
|
90 |
*/
|
|
|
91 |
const responseFromMoodleNet = (status, resourceUrl = '') => {
|
|
|
92 |
const $modal = currentModal.getModal();
|
|
|
93 |
const modal = $modal[0];
|
|
|
94 |
modal.querySelector('.modal-header').classList.add('no-border');
|
|
|
95 |
currentModal.setBody(Templates.render('core/moodlenet/send_activity_modal_done', {
|
|
|
96 |
success: status,
|
|
|
97 |
sitesupporturl: siteSupportUrl,
|
|
|
98 |
}));
|
|
|
99 |
|
|
|
100 |
if (status) {
|
|
|
101 |
currentModal.setFooter(Templates.render('core/moodlenet/send_activity_modal_footer_view', {
|
|
|
102 |
resourceurl: resourceUrl,
|
|
|
103 |
}));
|
|
|
104 |
currentModal.showFooter();
|
|
|
105 |
}
|
|
|
106 |
};
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Render the modal to send resource to MoodleNet.
|
|
|
110 |
*
|
|
|
111 |
* @param {object} data The data of the resource to be shared.
|
|
|
112 |
* @param {array} selectedActivities Selected activities.
|
|
|
113 |
*/
|
|
|
114 |
const renderModal = async(data, selectedActivities) => {
|
|
|
115 |
if (data.status) {
|
|
|
116 |
siteSupportUrl = data.supportpageurl;
|
|
|
117 |
issuerId = data.issuerid;
|
|
|
118 |
let modalConfig = {
|
|
|
119 |
templateContext: {
|
|
|
120 |
'activitytype': data.type,
|
|
|
121 |
'activityname': data.name,
|
|
|
122 |
'server': data.server,
|
|
|
123 |
}
|
|
|
124 |
};
|
|
|
125 |
if (selectedActivities.length > 0) {
|
|
|
126 |
selectedCmIds = selectedActivities;
|
|
|
127 |
}
|
|
|
128 |
if (selectedActivities.length > 1) {
|
|
|
129 |
modalConfig.templateContext.fullsharing = false;
|
|
|
130 |
modalConfig.templateContext.selectedactivitiesnotice = await getString('moodlenet:sharenoticepartialactivitynumber',
|
|
|
131 |
'moodle', selectedActivities.length);
|
|
|
132 |
modalConfig.templateContext.sharenotice = await getString('moodlenet:sharenoticepartial', 'moodle');
|
|
|
133 |
} else {
|
|
|
134 |
modalConfig.templateContext.fullsharing = true;
|
|
|
135 |
if (type === TYPE_ACTIVITY || (type === TYPE_PARTIAL_COURSE && selectedActivities.length == 1)) {
|
|
|
136 |
modalConfig.templateContext.sharenotice = await getString('moodlenet:sharenoticeactivity', 'moodle');
|
|
|
137 |
} else {
|
|
|
138 |
modalConfig.templateContext.sharenotice = await getString('moodlenet:sharenoticecourse', 'moodle');
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
return SendActivityModal.create(modalConfig);
|
|
|
143 |
} else {
|
|
|
144 |
return addNotification({
|
|
|
145 |
message: data.warnings[0].message,
|
|
|
146 |
type: 'error'
|
|
|
147 |
});
|
|
|
148 |
}
|
|
|
149 |
};
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Handle modal.
|
|
|
153 |
* @param {string} shareActionType Share action type.
|
|
|
154 |
* @param {array} selectedActivities Selected activities.
|
|
|
155 |
*/
|
|
|
156 |
export const handleModal = (shareActionType, selectedActivities = []) => {
|
|
|
157 |
const resourceId = Config.contextInstanceId;
|
|
|
158 |
type = shareActionType;
|
|
|
159 |
|
|
|
160 |
Promise.resolve(type)
|
|
|
161 |
.then((type) => {
|
|
|
162 |
if (type === TYPE_ACTIVITY) {
|
|
|
163 |
return MoodleNetService.getActivityInformation(resourceId);
|
|
|
164 |
} else if (type === TYPE_COURSE) {
|
|
|
165 |
return MoodleNetService.getCourseInformation(resourceId);
|
|
|
166 |
} else if (type === TYPE_PARTIAL_COURSE) {
|
|
|
167 |
if (selectedActivities.length > 1) {
|
|
|
168 |
// Selected more than one activity.
|
|
|
169 |
return MoodleNetService.getCourseInformation(resourceId);
|
|
|
170 |
} else {
|
|
|
171 |
// Select only one activity. Switch to activity mode.
|
|
|
172 |
return MoodleNetService.getActivityInformation(selectedActivities[0]);
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
throw new Error(`Unknown type ${type}`);
|
|
|
176 |
})
|
|
|
177 |
.then((data) => {
|
|
|
178 |
return renderModal(data, selectedActivities);
|
|
|
179 |
})
|
|
|
180 |
.then((modal) => {
|
|
|
181 |
currentModal = modal;
|
|
|
182 |
return currentModal;
|
|
|
183 |
})
|
|
|
184 |
.catch(displayException);
|
|
|
185 |
};
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Register events.
|
|
|
189 |
*/
|
|
|
190 |
const registerEventListeners = () => {
|
|
|
191 |
document.addEventListener('click', (e) => {
|
|
|
192 |
const shareAction = e.target.closest('[data-action="sendtomoodlenet"]');
|
|
|
193 |
const sendAction = e.target.closest('.moodlenet-action-buttons [data-action="share"]');
|
|
|
194 |
if (shareAction) {
|
|
|
195 |
e.preventDefault();
|
|
|
196 |
type = shareAction.getAttribute('data-type');
|
|
|
197 |
handleModal(shareAction.getAttribute('data-type'));
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if (sendAction) {
|
|
|
201 |
e.preventDefault();
|
|
|
202 |
publish(MoodleNetEvents.MOODLENET_SHARE_STARTED, {});
|
|
|
203 |
courseId = Config.courseId;
|
|
|
204 |
resourceId = Config.contextInstanceId;
|
|
|
205 |
shareFormat = 0;
|
|
|
206 |
MoodleNetAuthorize.handleAuthorization(issuerId, courseId, resourceId, shareFormat);
|
|
|
207 |
}
|
|
|
208 |
});
|
|
|
209 |
};
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
* Initialize.
|
|
|
214 |
*/
|
|
|
215 |
export const init = () => {
|
|
|
216 |
if (!listenersRegistered) {
|
|
|
217 |
Prefetch.prefetchTemplates([
|
|
|
218 |
'core/moodlenet/send_activity_modal_base',
|
|
|
219 |
'core/moodlenet/send_activity_modal_packaging',
|
|
|
220 |
'core/moodlenet/send_activity_modal_done',
|
|
|
221 |
'core/moodlenet/send_activity_modal_footer_view',
|
|
|
222 |
'core/moodlenet/send_activity_modal_footer_share',
|
|
|
223 |
]);
|
|
|
224 |
registerEventListeners();
|
|
|
225 |
listenersRegistered = true;
|
|
|
226 |
}
|
|
|
227 |
};
|