1441 |
ariadna |
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 |
* Script to update stored_progress progress bars on the screen.
|
|
|
18 |
*
|
|
|
19 |
* @module core/stored_progress
|
|
|
20 |
* @copyright 2023 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @author Conn Warwicker <conn.warwicker@catalyst-eu.net>
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/* global updateProgressBar */
|
|
|
26 |
|
|
|
27 |
import * as Ajax from 'core/ajax';
|
|
|
28 |
import Notification from 'core/notification';
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* @var bool This AMD script is loaded multiple times, for each progress bar on a page.
|
|
|
32 |
* So this stops it running multiple times.
|
|
|
33 |
* */
|
|
|
34 |
var STORED_PROGRESS_LOADED = false;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Poll a given stored progress record.
|
|
|
38 |
*
|
|
|
39 |
* @param {array} ids
|
|
|
40 |
* @param {integer} timeout
|
|
|
41 |
*/
|
|
|
42 |
function poll(ids, timeout) {
|
|
|
43 |
|
|
|
44 |
// Call AJAX request.
|
|
|
45 |
let promise = Ajax.call([{
|
|
|
46 |
methodname: 'core_output_poll_stored_progress', args: {'ids': ids}
|
|
|
47 |
}]);
|
|
|
48 |
|
|
|
49 |
let repollids = [];
|
|
|
50 |
|
|
|
51 |
// When AJAX request returns, handle the results.
|
|
|
52 |
promise[0].then(function(results) {
|
|
|
53 |
|
|
|
54 |
results.forEach(function(data) {
|
|
|
55 |
|
|
|
56 |
// Update the progress bar percentage and message using the core method from the javascript-static.js.
|
|
|
57 |
updateProgressBar(data.uniqueid, data.progress, data.message, data.estimated, data.error);
|
|
|
58 |
|
|
|
59 |
// Add the bar for re-polling if it's not completed.
|
|
|
60 |
if (data.progress < 100 && !data.error) {
|
|
|
61 |
repollids.push(data.id);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// If a different timeout came back from the script, use that instead.
|
|
|
65 |
if (data.timeout && data.timeout > 0) {
|
|
|
66 |
timeout = data.timeout;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
});
|
|
|
70 |
|
|
|
71 |
// If we still want to poll any of them, do it again.
|
|
|
72 |
if (repollids.length > 0) {
|
|
|
73 |
return setTimeout(() => poll(repollids, timeout), timeout * 1000);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
return false;
|
|
|
77 |
|
|
|
78 |
}).catch(Notification.exception);
|
|
|
79 |
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Initialise the polling process.
|
|
|
84 |
*
|
|
|
85 |
* @param {integer} timeout Timeout to use (seconds).
|
|
|
86 |
*/
|
|
|
87 |
export const init = (timeout) => {
|
|
|
88 |
|
|
|
89 |
if (STORED_PROGRESS_LOADED === false) {
|
|
|
90 |
|
|
|
91 |
let ids = [];
|
|
|
92 |
|
|
|
93 |
// Find any stored progress bars we want to poll.
|
|
|
94 |
document.querySelectorAll('.stored-progress-bar').forEach(el => {
|
|
|
95 |
|
|
|
96 |
// Get its id and add to array.
|
|
|
97 |
let id = el.dataset.recordid;
|
|
|
98 |
ids.push(id);
|
|
|
99 |
|
|
|
100 |
});
|
|
|
101 |
|
|
|
102 |
// Poll for updates from these IDs.
|
|
|
103 |
poll(ids, timeout);
|
|
|
104 |
|
|
|
105 |
// Script has run, we don't want it to run again.
|
|
|
106 |
STORED_PROGRESS_LOADED = true;
|
|
|
107 |
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
};
|