Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Bulk actions for lists of participants.
18
 *
19
 * @module     core_user/local/participants/bulkactions
20
 * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import * as Repository from 'core_user/repository';
25
import * as Str from 'core/str';
26
import ModalEvents from 'core/modal_events';
27
import SaveCancelModal from 'core/modal_save_cancel';
28
import Notification from 'core/notification';
29
import Templates from 'core/templates';
30
import {add as notifyUser} from 'core/toast';
31
 
32
/**
33
 * Show the add note popup
34
 *
35
 * @param {Number} courseid
36
 * @param {Number[]} users
37
 * @param {String[]} noteStateNames
38
 * @param {HTMLElement} stateHelpIcon
39
 * @return {Promise}
40
 */
41
export const showAddNote = (courseid, users, noteStateNames, stateHelpIcon) => {
42
    if (!users.length) {
43
        // No users were selected.
44
        return Promise.resolve();
45
    }
46
 
47
    const states = [];
48
    for (let key in noteStateNames) {
49
        switch (key) {
50
            case 'draft':
51
                states.push({value: 'personal', label: noteStateNames[key]});
52
                break;
53
            case 'public':
54
                states.push({value: 'course', label: noteStateNames[key], selected: 1});
55
                break;
56
            case 'site':
57
                states.push({value: key, label: noteStateNames[key]});
58
                break;
59
        }
60
    }
61
 
62
    const context = {
63
        stateNames: states,
64
        stateHelpIcon: stateHelpIcon.innerHTML,
65
    };
66
 
67
    let titlePromise = null;
68
    if (users.length === 1) {
69
        titlePromise = Str.get_string('addbulknotesingle', 'core_notes');
70
    } else {
71
        titlePromise = Str.get_string('addbulknote', 'core_notes', users.length);
72
    }
73
 
74
    return SaveCancelModal.create({
75
        body: Templates.render('core_user/add_bulk_note', context),
76
        title: titlePromise,
77
        buttons: {
78
            save: titlePromise,
79
        },
80
        removeOnClose: true,
81
        show: true,
82
    })
83
    .then(modal => {
84
        modal.getRoot().on(ModalEvents.save, () => submitAddNote(courseid, users, modal));
85
        return modal;
86
    });
87
};
88
 
89
/**
90
 * Add a note to this list of users.
91
 *
92
 * @param {Number} courseid
93
 * @param {Number[]} users
94
 * @param {Modal} modal
95
 * @return {Promise}
96
 */
97
const submitAddNote = (courseid, users, modal) => {
98
    const text = modal.getRoot().find('form textarea').val();
99
    const publishstate = modal.getRoot().find('form select').val();
100
 
101
    const notes = users.map(userid => {
102
        return {
103
            userid,
104
            text,
105
            courseid,
106
            publishstate,
107
        };
108
    });
109
 
110
    return Repository.createNotesForUsers(notes)
111
    .then(noteIds => {
112
        if (noteIds.length === 1) {
113
            return Str.get_string('addbulknotedonesingle', 'core_notes');
114
        } else {
115
            return Str.get_string('addbulknotedone', 'core_notes', noteIds.length);
116
        }
117
    })
118
    .then(msg => notifyUser(msg))
119
    .catch(Notification.exception);
120
};
121
 
122
/**
123
 * Show the send message popup.
124
 *
125
 * @param {Number[]} users
126
 * @return {Promise}
127
 */
128
export const showSendMessage = users => {
129
    if (!users.length) {
130
        // Nothing to do.
131
        return Promise.resolve();
132
    }
133
 
134
    let titlePromise;
135
    if (users.length === 1) {
136
        titlePromise = Str.get_string('sendbulkmessagesingle', 'core_message');
137
    } else {
138
        titlePromise = Str.get_string('sendbulkmessage', 'core_message', users.length);
139
    }
140
 
141
    return SaveCancelModal.create({
142
        body: Templates.render('core_user/send_bulk_message', {}),
143
        title: titlePromise,
144
        buttons: {
145
            save: titlePromise,
146
        },
147
        removeOnClose: true,
148
        show: true,
149
    })
150
    .then(modal => {
151
        modal.getRoot().on(ModalEvents.save, (e) => {
152
            const text = modal.getRoot().find('form textarea').val();
153
            if (text.trim() === '') {
154
                modal.getRoot().find('[data-role="messagetextrequired"]').removeAttr('hidden');
155
                e.preventDefault();
156
                return;
157
            }
158
 
159
            submitSendMessage(modal, users, text);
160
        });
161
 
162
        return modal;
163
    });
164
};
165
 
166
/**
167
 * Send a message to these users.
168
 *
169
 * @param {Modal} modal
170
 * @param {Number[]} users
171
 * @param {String} text
172
 * @return {Promise}
173
 */
174
const submitSendMessage = (modal, users, text) => {
175
    const messages = users.map(touserid => {
176
        return {
177
            touserid,
178
            text,
179
        };
180
    });
181
 
182
    return Repository.sendMessagesToUsers(messages)
183
    .then(messageIds => {
184
        if (messageIds.length == 1) {
185
            return Str.get_string('sendbulkmessagesentsingle', 'core_message');
186
        } else {
187
            return Str.get_string('sendbulkmessagesent', 'core_message', messageIds.length);
188
        }
189
    })
190
    .then(msg => notifyUser(msg))
191
    .catch(Notification.exception);
192
};