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
 * Competency frameworks actions via ajax.
18
 *
19
 * @module     tool_lp/frameworkactions
20
 * @copyright  2015 Damyon Wiese <damyon@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['jquery', 'core/templates', 'core/ajax', 'core/notification', 'core/str'], function($, templates, ajax, notification, str) {
24
    // Private variables and functions.
25
 
26
    /** @var {Number} pagecontextid The id of the context */
27
    var pagecontextid = 0;
28
 
29
    /** @var {Number} frameworkid The id of the framework */
30
    var frameworkid = 0;
31
 
32
    /**
33
     * Callback to replace the dom element with the rendered template.
34
     *
35
     * @param {String} newhtml The new html to insert.
36
     * @param {String} newjs The new js to run.
37
     */
38
    var updatePage = function(newhtml, newjs) {
39
        $('[data-region="managecompetencies"]').replaceWith(newhtml);
40
        templates.runTemplateJS(newjs);
41
    };
42
 
43
    /**
44
     * Callback to render the page template again and update the page.
45
     *
46
     * @param {Object} context The context for the template.
47
     */
48
    var reloadList = function(context) {
49
        templates.render('tool_lp/manage_competency_frameworks_page', context)
50
            .done(updatePage)
51
            .fail(notification.exception);
52
    };
53
 
54
    /**
55
     * Duplicate a framework and reload the page.
56
     * @method doDuplicate
57
     * @param {Event} e
58
     */
59
    var doDuplicate = function(e) {
60
        e.preventDefault();
61
 
62
        frameworkid = $(this).attr('data-frameworkid');
63
 
64
        // We are chaining ajax requests here.
65
        var requests = ajax.call([{
66
            methodname: 'core_competency_duplicate_competency_framework',
67
            args: {id: frameworkid}
68
        }, {
69
            methodname: 'tool_lp_data_for_competency_frameworks_manage_page',
70
            args: {
71
                pagecontext: {
72
                    contextid: pagecontextid
73
                }
74
            }
75
        }]);
76
        requests[1].done(reloadList).fail(notification.exception);
77
    };
78
    /**
79
     * Delete a framework and reload the page.
80
     */
81
    var doDelete = function() {
82
 
83
        // We are chaining ajax requests here.
84
        var requests = ajax.call([{
85
            methodname: 'core_competency_delete_competency_framework',
86
            args: {id: frameworkid}
87
        }, {
88
            methodname: 'tool_lp_data_for_competency_frameworks_manage_page',
89
            args: {
90
                pagecontext: {
91
                    contextid: pagecontextid
92
                }
93
            }
94
        }]);
95
        requests[0].done(function(success) {
96
            if (success === false) {
97
                var req = ajax.call([{
98
                    methodname: 'core_competency_read_competency_framework',
99
                    args: {id: frameworkid}
100
                }]);
101
                req[0].done(function(framework) {
102
                    str.get_strings([
103
                        {key: 'frameworkcannotbedeleted', component: 'tool_lp', param: framework.shortname},
104
                        {key: 'cancel', component: 'moodle'}
105
                    ]).done(function(strings) {
106
                        notification.alert(
107
                            null,
108
                            strings[0]
109
                        );
110
                    }).fail(notification.exception);
111
                });
112
            }
113
        }).fail(notification.exception);
114
        requests[1].done(reloadList).fail(notification.exception);
115
    };
116
 
117
    /**
118
     * Handler for "Delete competency framework" actions.
119
     * @param {Event} e
120
     */
121
    var confirmDelete = function(e) {
122
        e.preventDefault();
123
 
124
        var id = $(this).attr('data-frameworkid');
125
        frameworkid = id;
126
 
127
        var requests = ajax.call([{
128
            methodname: 'core_competency_read_competency_framework',
129
            args: {id: frameworkid}
130
        }]);
131
 
132
        requests[0].done(function(framework) {
133
            str.get_strings([
134
                {key: 'confirm', component: 'moodle'},
135
                {key: 'deletecompetencyframework', component: 'tool_lp', param: framework.shortname},
136
                {key: 'delete', component: 'moodle'},
137
                {key: 'cancel', component: 'moodle'}
138
            ]).done(function(strings) {
139
                notification.confirm(
140
                    strings[0], // Confirm.
141
                    strings[1], // Delete competency framework X?
142
                    strings[2], // Delete.
143
                    strings[3], // Cancel.
144
                    doDelete
145
                );
146
            }).fail(notification.exception);
147
        }).fail(notification.exception);
148
 
149
    };
150
 
151
 
152
    return /** @alias module:tool_lp/frameworkactions */ {
153
        // Public variables and functions.
154
 
155
        /**
156
         * Expose the event handler for delete.
157
         * @method deleteHandler
158
         * @param {Event} e
159
         */
160
        deleteHandler: confirmDelete,
161
 
162
        /**
163
         * Expose the event handler for duplicate.
164
         * @method duplicateHandler
165
         * @param {Event} e
166
         */
167
        duplicateHandler: doDuplicate,
168
 
169
        /**
170
         * Initialise the module.
171
         * @method init
172
         * @param {Number} contextid The context id of the page.
173
         */
174
        init: function(contextid) {
175
            pagecontextid = contextid;
176
        }
177
    };
178
});