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
 * Handle discussion subscription toggling on a discussion list in
18
 * the forum view.
19
 *
20
 * @module     mod_forum/favourite_toggle
21
 * @copyright  2019 Peter Dias <peter@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
define([
25
        'jquery',
26
        'core/templates',
27
        'core/notification',
28
        'mod_forum/repository',
29
        'mod_forum/selectors',
30
        'core/str',
31
    ], function(
32
        $,
33
        Templates,
34
        Notification,
35
        Repository,
36
        Selectors,
37
        String
38
    ) {
39
 
40
    /**
41
     * Register event listeners for the subscription toggle.
42
     *
43
     * @param {object} root The discussion list root element
44
     * @param {boolean} preventDefault Should the default action of the event be prevented
45
     * @param {function} callback Success callback
46
     */
47
    var registerEventListeners = function(root, preventDefault, callback) {
48
        root.on('click', Selectors.favourite.toggle, function(e) {
49
            var toggleElement = $(this);
50
            var forumId = toggleElement.data('forumid');
51
            var discussionId = toggleElement.data('discussionid');
52
            var subscriptionState = toggleElement.data('targetstate');
53
 
54
            Repository.setFavouriteDiscussionState(forumId, discussionId, subscriptionState)
55
                .then(function(context) {
56
                    return callback(toggleElement, context);
57
                })
58
                .then(function() {
59
                    return String.get_string("favouriteupdated", "forum")
60
                        .done(function(s) {
61
                            return Notification.addNotification({
62
                                message: s,
63
                                type: "info"
64
                            });
65
                        });
66
                })
67
                .catch(Notification.exception);
68
 
69
            if (preventDefault) {
70
                e.preventDefault();
71
            }
72
        });
73
    };
74
 
75
    return {
76
        init: registerEventListeners
77
    };
78
});