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
 * AMD module for model actions confirmation.
18
 *
19
 * @module     tool_analytics/model
20
 * @copyright  2017 David Monllao
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define([
24
    'jquery', 'core/str', 'core/log', 'core/notification', 'core/modal_save_cancel',
25
    'core/modal_cancel', 'core/modal_events', 'core/templates'],
26
    function($, Str, log, Notification, ModalSaveCancel, ModalCancel, ModalEvents, Templates) {
27
 
28
    /**
29
     * List of actions that require confirmation and confirmation message.
30
     */
31
    var actionsList = {
32
        clear: {
33
            title: {
34
                key: 'clearpredictions',
35
                component: 'tool_analytics'
36
            }, body: {
37
                key: 'clearmodelpredictions',
38
                component: 'tool_analytics'
39
            }
40
 
41
        },
42
        'delete': {
43
            title: {
44
                key: 'delete',
45
                component: 'tool_analytics'
46
            }, body: {
47
                key: 'deletemodelconfirmation',
48
                component: 'tool_analytics'
49
            }
50
        }
51
    };
52
 
53
    /**
54
     * Returns the model name.
55
     *
56
     * @param {Object} actionItem The action item DOM node.
57
     * @return {String}
58
     */
59
    var getModelName = function(actionItem) {
60
        var wrap = $(actionItem).closest('[data-model-name]');
61
 
62
        if (wrap.length) {
63
            return wrap.attr('data-model-name');
64
 
65
        } else {
66
            log.error('Unexpected DOM error - unable to obtain the model name');
67
            return '';
68
        }
69
    };
70
 
71
    /** @alias module:tool_analytics/model */
72
    return {
73
 
74
        /**
75
         * Displays a confirm modal window before executing the action.
76
         *
77
         * @param {String} actionId
78
         * @param {String} actionType
79
         */
80
        confirmAction: function(actionId, actionType) {
81
            $('[data-action-id="' + actionId + '"]').on('click', function(ev) {
82
                ev.preventDefault();
83
 
84
                var a = $(ev.currentTarget);
85
 
86
                if (typeof actionsList[actionType] === "undefined") {
87
                    log.error('Action "' + actionType + '" is not allowed.');
88
                    return;
89
                }
90
 
91
                var reqStrings = [
92
                    actionsList[actionType].title,
93
                    actionsList[actionType].body
94
                ];
95
                reqStrings[1].param = getModelName(a);
96
 
97
                var stringsPromise = Str.get_strings(reqStrings);
98
                var modalPromise = ModalSaveCancel.create({});
99
 
100
                $.when(stringsPromise, modalPromise).then(function(strings, modal) {
101
                    modal.setTitle(strings[0]);
102
                    modal.setBody(strings[1]);
103
                    modal.setSaveButtonText(strings[0]);
104
                    modal.getRoot().on(ModalEvents.save, function() {
105
                        window.location.href = a.attr('href');
106
                    });
107
                    modal.show();
108
                    return modal;
109
                }).fail(Notification.exception);
110
            });
111
        },
112
 
113
        /**
114
         * Displays evaluation mode and time-splitting method choices.
115
         *
116
         * @param  {String}  actionId
117
         * @param  {Boolean} trainedOnlyExternally
118
         */
119
        selectEvaluationOptions: function(actionId, trainedOnlyExternally) {
120
            $('[data-action-id="' + actionId + '"]').on('click', function(ev) {
121
                ev.preventDefault();
122
 
123
                var a = $(ev.currentTarget);
124
 
125
                var timeSplittingMethods = $(this).attr('data-timesplitting-methods');
126
 
127
                ModalSaveCancel.create({
128
                    title: Str.get_string('evaluatemodel', 'tool_analytics'),
129
                    body: Templates.render('tool_analytics/evaluation_options', {
130
                        trainedexternally: trainedOnlyExternally,
131
                        timesplittingmethods: JSON.parse(timeSplittingMethods)
132
                    }),
133
                    removeOnClose: true,
134
                    buttons: {
135
                        save: Str.get_string('evaluate', 'tool_analytics'),
136
                    },
137
                    show: true,
138
                })
139
                .then((modal) => {
140
                    modal.getRoot().on(ModalEvents.save, function() {
141
                        // Evaluation mode.
142
                        var evaluationMode = $("input[name='evaluationmode']:checked").val();
143
                        if (evaluationMode == 'trainedmodel') {
144
                            a.attr('href', a.attr('href') + '&mode=trainedmodel');
145
                        }
146
 
147
                        // Selected time-splitting id.
148
                        var timeSplittingMethod = $("#id-evaluation-timesplitting").val();
149
                        a.attr('href', a.attr('href') + '&timesplitting=' + timeSplittingMethod);
150
 
151
                        window.location.href = a.attr('href');
152
                        return;
153
                    });
154
 
155
                    return modal;
156
                }).catch(Notification.exception);
157
            });
158
        },
159
 
160
        /**
161
         * Displays export options.
162
         *
163
         * We have two main options: export training data and export configuration.
164
         * The 2nd option has an extra option: include the trained algorithm weights.
165
         *
166
         * @param  {String}  actionId
167
         * @param  {Boolean} isTrained
168
         */
169
        selectExportOptions: function(actionId, isTrained) {
170
            $('[data-action-id="' + actionId + '"]').on('click', function(ev) {
171
                ev.preventDefault();
172
 
173
                var a = $(ev.currentTarget);
174
 
175
                if (!isTrained) {
176
                    // Export the model configuration if the model is not trained. We can't export anything else.
177
                    a.attr('href', a.attr('href') + '&action=exportmodel&includeweights=0');
178
                    window.location.href = a.attr('href');
179
                    return;
180
                }
181
 
182
                var stringsPromise = Str.get_strings([
183
                    {
184
                        key: 'export',
185
                        component: 'tool_analytics'
186
                    }
187
                ]);
188
                var modalPromise = ModalSaveCancel.create({
189
                    body: Templates.render('tool_analytics/export_options', {}),
190
                    removeOnClose: true,
191
                });
192
 
193
                $.when(stringsPromise, modalPromise).then(function(strings, modal) {
194
                    modal.setTitle(strings[0]);
195
                    modal.setSaveButtonText(strings[0]);
196
 
197
                    modal.getRoot().on(ModalEvents.save, function() {
198
 
199
                        var exportOption = $("input[name='exportoption']:checked").val();
200
 
201
                        if (exportOption == 'exportdata') {
202
                            a.attr('href', a.attr('href') + '&action=exportdata');
203
 
204
                        } else {
205
                            a.attr('href', a.attr('href') + '&action=exportmodel');
206
                            if ($("#id-includeweights").is(':checked')) {
207
                                a.attr('href', a.attr('href') + '&includeweights=1');
208
                            } else {
209
                                a.attr('href', a.attr('href') + '&includeweights=0');
210
                            }
211
                        }
212
 
213
                        window.location.href = a.attr('href');
214
                        return;
215
                    });
216
 
217
                    modal.show();
218
                    return modal;
219
                }).fail(Notification.exception);
220
            });
221
        }
222
    };
223
});