1 |
efrain |
1 |
import ModalFactory from 'core/modal_factory';
|
|
|
2 |
import ModalForm from 'core_form/modalform';
|
|
|
3 |
import * as Str from 'core/str';
|
|
|
4 |
|
|
|
5 |
let modalForm = null;
|
|
|
6 |
let responseModal = null;
|
|
|
7 |
let contextid = null;
|
|
|
8 |
let appendurl = false;
|
|
|
9 |
|
|
|
10 |
/**
|
|
|
11 |
* Display the form modal and load the form.
|
|
|
12 |
*
|
|
|
13 |
* @param {Event} e
|
|
|
14 |
*/
|
|
|
15 |
async function showForm(e) {
|
|
|
16 |
e.preventDefault();
|
|
|
17 |
M.util.js_pending('block_messageteacher_show');
|
|
|
18 |
|
|
|
19 |
const link = e.currentTarget;
|
|
|
20 |
modalForm = new ModalForm({
|
|
|
21 |
formClass: 'block_messageteacher\\message_form',
|
|
|
22 |
args: {
|
|
|
23 |
contextid: contextid,
|
|
|
24 |
appendurl: appendurl,
|
|
|
25 |
referurl: link.dataset.referurl,
|
|
|
26 |
courseid: link.dataset.courseid,
|
|
|
27 |
recipientid: link.dataset.recipientid
|
|
|
28 |
},
|
|
|
29 |
modalConfig: {
|
|
|
30 |
title: await Str.get_string('pluginname', 'block_messageteacher')
|
|
|
31 |
},
|
|
|
32 |
saveButtonText: await Str.get_string('send', 'block_messageteacher'),
|
|
|
33 |
returnFocus: link
|
|
|
34 |
});
|
|
|
35 |
modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, submitForm);
|
|
|
36 |
await modalForm.show();
|
|
|
37 |
M.util.js_complete('block_messageteacher_show');
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Send the message entered into the form using a core webservice.
|
|
|
42 |
*
|
|
|
43 |
* @param {Event} e
|
|
|
44 |
*/
|
|
|
45 |
async function submitForm(e) {
|
|
|
46 |
M.util.js_pending('block_messageteacher_send');
|
|
|
47 |
if (e.detail < 1) {
|
|
|
48 |
responseModal.setBody(e.errormessage);
|
|
|
49 |
} else {
|
|
|
50 |
const sent = await Str.get_string('messagesent', 'block_messageteacher');
|
|
|
51 |
responseModal.setBody(sent);
|
|
|
52 |
}
|
|
|
53 |
responseModal.show();
|
|
|
54 |
M.util.js_complete('block_messageteacher_send');
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Create modals.
|
|
|
59 |
*
|
|
|
60 |
* Create a Save/Cancel modal to display the form, and a Cancel modal for confirmation/error messages.
|
|
|
61 |
*/
|
|
|
62 |
export const init = async function() {
|
|
|
63 |
|
|
|
64 |
const title = await Str.get_string('pluginname', 'block_messageteacher');
|
|
|
65 |
|
|
|
66 |
responseModal = await ModalFactory.create({
|
|
|
67 |
type: ModalFactory.types.DEFAULT,
|
|
|
68 |
title: title
|
|
|
69 |
});
|
|
|
70 |
|
|
|
71 |
var links = document.querySelectorAll('.messageteacher_link');
|
|
|
72 |
for (var i = 0; i < links.length; i++) {
|
|
|
73 |
if (contextid === null) {
|
|
|
74 |
contextid = links[i].parentElement.parentElement.dataset.contextid;
|
|
|
75 |
appendurl = links[i].parentElement.parentElement.dataset.appendurl;
|
|
|
76 |
}
|
|
|
77 |
links[i].addEventListener('click', showForm);
|
|
|
78 |
}
|
|
|
79 |
};
|