on-demand release 4.0dev+
[moodle.git] / report / insights / amd / src / actions.js
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/>.
16 /**
17  * Module to manage report insights actions that are executed using AJAX.
18  *
19  * @copyright  2017 David Monllao {@link http://www.davidmonllao.com}
20  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21  */
23 /**
24  * This module manages prediction actions that require AJAX requests.
25  *
26  * @module report_insights/actions
27  */
28 define(['jquery', 'core/str', 'core/ajax', 'core/notification', 'core/url', 'core/modal_factory', 'core/modal_events'],
29         function($, Str, Ajax, Notification, Url, ModalFactory, ModalEvents) {
31     return {
33         /**
34          * Attach on click handlers for bulk actions.
35          *
36          * @param {String} rootNode
37          * @access public
38          */
39         initBulk: function(rootNode) {
41             /**
42              * Executes the provided action.
43              *
44              * @param  {Array}  predictionIds
45              * @param  {Array}  predictionContainers
46              * @param  {String} actionName
47              * @return {Promise}
48              */
49             var executeAction = function(predictionIds, predictionContainers, actionName) {
51                 return Ajax.call([
52                     {
53                         methodname: 'report_insights_action_executed',
54                         args: {
55                             predictionids: predictionIds,
56                             actionname: actionName
57                         }
58                     }
59                 ])[0].then(function() {
60                     // Remove the selected elements from the list.
62                     var tableNode = false;
63                     predictionContainers.forEach(function(el) {
64                         if (tableNode === false) {
65                             tableNode = el.closest('table');
66                         }
67                         el.remove();
68                     });
70                     if (tableNode.find('tbody > tr').length === 0) {
71                         let params = {
72                             contextid: tableNode.closest('div.insight-container').data('context-id'),
73                             modelid: tableNode.closest('div.insight-container').data('model-id')
74                         };
75                         window.location.assign(Url.relativeUrl("report/insights/insights.php", params, false));
76                     }
77                     return;
78                 }).catch(Notification.exception);
79             };
81             $(rootNode + ' [data-bulk-actionname]').on('click', function(e) {
82                 e.preventDefault();
83                 var action = $(e.currentTarget);
84                 var actionName = action.data('bulk-actionname');
85                 var actionVisibleName = action.text().trim();
87                 var predictionIds = [];
88                 var predictionContainers = [];
90                 $('.insights-list input[data-togglegroup^="insight-bulk-action-"][data-toggle="slave"]:checked').each(function() {
91                     var container = $(this).closest('tr[data-prediction-id]');
92                     predictionContainers.push(container);
93                     predictionIds.push(container.data('prediction-id'));
94                 });
96                 if (predictionIds.length === 0) {
97                     // No items selected message.
98                     return this;
99                 }
101                 var strings = [];
102                 Str.get_strings([{
103                     key: 'confirmbulkaction',
104                     component: 'report_insights',
105                     param: {
106                         action: actionVisibleName,
107                         nitems: predictionIds.length
108                     }
109                 }, {
110                     key: 'confirm',
111                     component: 'moodle'
112                 }]
113                 ).then(function(strs) {
114                     strings = strs;
115                     return ModalFactory.create({
116                         type: ModalFactory.types.SAVE_CANCEL,
117                         title: actionVisibleName,
118                         body: strings[0],
119                     });
120                 }).then(function(modal) {
121                     modal.setSaveButtonText(strings[1]);
122                     modal.show();
123                     modal.getRoot().on(ModalEvents.save, function() {
124                         // The action is now confirmed, sending an action for it.
125                         return executeAction(predictionIds, predictionContainers, actionName);
126                     });
128                     return modal;
129                 }).catch(Notification.exception);
131                 return this;
132             });
133         },
134     };
135 });