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 |
* 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 |
*/
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* This module manages prediction actions that require AJAX requests.
|
|
|
25 |
*
|
|
|
26 |
* @module report_insights/actions
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
import {get_string as getString} from 'core/str';
|
|
|
30 |
import Ajax from 'core/ajax';
|
|
|
31 |
import Notification from 'core/notification';
|
|
|
32 |
import Url from 'core/url';
|
|
|
33 |
import ModalEvents from 'core/modal_events';
|
|
|
34 |
import ModalSaveCancel from 'core/modal_save_cancel';
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Executes the provided action.
|
|
|
39 |
*
|
|
|
40 |
* @param {Array} predictionids
|
|
|
41 |
* @param {String} actionname
|
|
|
42 |
* @return {Promise}
|
|
|
43 |
*/
|
|
|
44 |
const markActionExecuted = (predictionids, actionname) => Ajax.call([
|
|
|
45 |
{
|
|
|
46 |
methodname: 'report_insights_action_executed',
|
|
|
47 |
args: {
|
|
|
48 |
actionname,
|
|
|
49 |
predictionids,
|
|
|
50 |
},
|
|
|
51 |
}
|
|
|
52 |
])[0];
|
|
|
53 |
|
|
|
54 |
const getPredictionTable = (predictionContainers) => {
|
|
|
55 |
for (const el of predictionContainers) {
|
|
|
56 |
if (el.closest('table')) {
|
|
|
57 |
return el.closest('table');
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
return null;
|
|
|
62 |
};
|
|
|
63 |
|
|
|
64 |
const executeAction = (predictionIds, predictionContainers, actionName) => {
|
|
|
65 |
markActionExecuted(predictionIds, actionName).then(() => {
|
|
|
66 |
// Remove the selected elements from the list.
|
|
|
67 |
const tableNode = getPredictionTable(predictionContainers);
|
|
|
68 |
predictionContainers.forEach((el) => el.remove());
|
|
|
69 |
|
|
|
70 |
if (!tableNode.querySelector('tbody > tr')) {
|
|
|
71 |
const params = {
|
|
|
72 |
contextid: tableNode.closest('div.insight-container').dataset.contextId,
|
|
|
73 |
modelid: tableNode.closest('div.insight-container').dataset.modelId,
|
|
|
74 |
};
|
|
|
75 |
window.location.assign(Url.relativeUrl("report/insights/insights.php", params, false));
|
|
|
76 |
}
|
|
|
77 |
return;
|
|
|
78 |
}).catch(Notification.exception);
|
|
|
79 |
};
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Attach on click handlers for bulk actions.
|
|
|
83 |
*
|
|
|
84 |
* @param {String} rootNode
|
|
|
85 |
* @access public
|
|
|
86 |
*/
|
|
|
87 |
export const initBulk = (rootNode) => {
|
|
|
88 |
document.addEventListener('click', (e) => {
|
|
|
89 |
const action = e.target.closest(`${rootNode} [data-bulk-actionname]`);
|
|
|
90 |
if (!action) {
|
|
|
91 |
return;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
e.preventDefault();
|
|
|
95 |
const actionName = action.dataset.bulkActionname;
|
|
|
96 |
const actionVisibleName = action.textContent.trim();
|
|
|
97 |
|
|
|
98 |
const predictionContainers = Array.from(document.querySelectorAll(
|
|
|
99 |
'.insights-list input[data-togglegroup^="insight-bulk-action-"][data-toggle="slave"]:checked',
|
|
|
100 |
)).map((checkbox) => checkbox.closest('tr[data-prediction-id]'));
|
|
|
101 |
const predictionIds = predictionContainers.map((el) => el.dataset.predictionId);
|
|
|
102 |
|
|
|
103 |
if (predictionIds.length === 0) {
|
|
|
104 |
// No items selected message.
|
|
|
105 |
return;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
const stringParams = {
|
|
|
109 |
action: actionVisibleName,
|
|
|
110 |
nitems: predictionIds.length,
|
|
|
111 |
};
|
|
|
112 |
|
|
|
113 |
ModalSaveCancel.create({
|
|
|
114 |
title: actionVisibleName,
|
|
|
115 |
body: getString('confirmbulkaction', 'report_insights', stringParams),
|
|
|
116 |
buttons: {
|
|
|
117 |
save: getString('confirm'),
|
|
|
118 |
},
|
|
|
119 |
show: true,
|
|
|
120 |
}).then((modal) => {
|
|
|
121 |
modal.getRoot().on(ModalEvents.save, function() {
|
|
|
122 |
// The action is now confirmed, sending an action for it.
|
|
|
123 |
return executeAction(predictionIds, predictionContainers, actionName);
|
|
|
124 |
});
|
|
|
125 |
|
|
|
126 |
return modal;
|
|
|
127 |
}).catch(Notification.exception);
|
|
|
128 |
});
|
|
|
129 |
};
|