MDL-58835 report_insights: Not useful and ack prediction actions
[moodle.git] / report / insights / amd / src / actions.js
CommitLineData
0c0a72e7
DM
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 * @package report_insights
20 * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24/**
25 * This module manages prediction actions that require AJAX requests.
26 *
27 * @module report_insights/actions
28 */
29define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notification) {
30
31 return {
32
33 /**
34 * Attach on click handlers to hide predictions.
35 *
36 * @param {Number} predictionId The prediction id.
37 * @access public
38 */
39 init: function(predictionId) {
40
41 // Select the prediction with the provided id ensuring that an external function is set as method name.
42 $('a[data-prediction-methodname][data-prediction-id=' + predictionId + ']').on('click', function(e) {
43 e.preventDefault();
44 var action = $(e.currentTarget);
45 var methodname = action.attr('data-prediction-methodname');
46 var predictionContainers = action.closest('tr');
47
48 if (predictionContainers.length > 0) {
49 var promise = Ajax.call([
50 {
51 methodname: methodname,
52 args: {predictionid: predictionId}
53 }
54 ])[0];
55 promise.done(function() {
56 predictionContainers[0].remove();
57
58 // Move back if no remaining predictions.
59 if ($('.insights-list tr').length === 0) {
60 window.history.back();
61 }
62 }).fail(Notification.exception);
63 }
64 });
65 }
66 };
67});