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
 * Send bulk message to the given user ids.
18
 *
19
 * @module     core_message/message_send_bulk
20
 * @copyright  2019 Shamim Rezaie <shamim@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
import {get_string} from 'core/str';
24
import ModalSaveCancel from 'core/modal_save_cancel';
25
import Templates from 'core/templates';
26
import ModalEvents from 'core/modal_events';
27
import Ajax from 'core/ajax';
28
import Notification from 'core/notification';
29
 
30
/**
31
 * Show the send message popup.
32
 *
33
 * @method showModal
34
 * @param {int[]} users
35
 * @param {Function} callback A callback to apply after the form is closed.
36
 * @returns {Promise}
37
 */
38
export const showModal = (users, callback = null) => {
39
    if (!users.length) {
40
        // Nothing to do.
41
        return Promise.resolve();
42
    }
43
    let titlePromise = null;
44
    if (users.length == 1) {
45
        titlePromise = get_string('sendbulkmessagesingle', 'core_message');
46
    } else {
47
        titlePromise = get_string('sendbulkmessage', 'core_message', users.length);
48
    }
49
 
50
    return ModalSaveCancel.create({
51
        body: Templates.render('core_message/send_bulk_message', {}),
52
        title: titlePromise,
53
        show: true,
54
        buttons: {
55
            save: titlePromise,
56
        },
57
    })
58
    .then(function(modal) {
59
        // When the dialog is closed, perform the callback (if provided).
60
        modal.getRoot().on(ModalEvents.hidden, function() {
61
            if (callback) {
62
                callback();
63
            }
64
            modal.getRoot().remove();
65
        });
66
 
67
        modal.getRoot().on(ModalEvents.save, function() {
68
            let messageText = modal.getRoot().find('form textarea').val();
69
            sendMessage(messageText, users);
70
        });
71
 
72
        return modal;
73
    });
74
};
75
 
76
/**
77
 * Send a message to these users.
78
 *
79
 * @method sendMessage
80
 * @param {String} messageText
81
 * @param {Number[]} users
82
 * @returns {Promise}
83
 */
84
export const sendMessage = (messageText, users) => {
85
    let messages = [];
86
 
87
    users.forEach(user => {
88
        messages.push({
89
            touserid: user,
90
            text: messageText
91
        });
92
    });
93
 
94
    return Ajax.call([{
95
        methodname: 'core_message_send_instant_messages',
96
        args: {messages: messages}
97
    }])[0]
98
    .then(function(messageIds) {
99
        if (messageIds.length == 1) {
100
            return get_string('sendbulkmessagesentsingle', 'core_message');
101
        } else {
102
            return get_string('sendbulkmessagesent', 'core_message', messageIds.length);
103
        }
104
    })
105
    .then(function(msg) {
106
        Notification.addNotification({
107
            message: msg,
108
            type: "success"
109
        });
110
        return true;
111
    })
112
    .catch(Notification.exception);
113
};