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
import Notification from 'core/notification';
17
import {getString} from 'core/str';
18
 
19
/**
20
 * A javascript module for the time in the assign module.
21
 *
22
 * @copyright  2020 Matt Porritt <mattp@catalyst-au.net>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
/**
27
 * Timestamp at which time runs out.
28
 *
29
 * @property {Number} endTime
30
 */
31
let endTime = 0;
32
 
33
/**
34
 * ID of the timeout that updates the clock.
35
 *
36
 * @property {Number} timeoutId
37
 */
38
let timeoutId = null;
39
 
40
/**
41
 * The timer element.
42
 *
43
 * @property {Element} timer
44
 */
45
let timer = null;
46
 
47
/**
48
 * Helper method to convert time remaining in seconds into HH:MM:SS format.
49
 *
50
 * @method formatSeconds
51
 * @param {Number} secs Time remaining in seconds to get value for.
52
 * @return {String} Time remaining in HH:MM:SS format.
53
 */
54
const formatSeconds = (secs) => {
55
    const hours = Math.floor(secs / 3600);
56
    const minutes = Math.floor(secs / 60) % 60;
57
    const seconds = secs % 60;
58
 
59
    return [hours, minutes, seconds]
60
        // Remove the hours column if there is less than 1 hour left.
61
        .filter((value, index) => value !== 0 || index > 0)
62
        // Ensure that all fields are two digit numbers.
63
        .map(value => `${value}`.padStart(2, '0'))
64
        .join(":");
65
};
66
 
67
/**
68
 * Stop the timer, if it is running.
69
 *
70
 * @method stop
71
 */
72
const stop = () => {
73
    if (timeoutId) {
74
        clearTimeout(timeoutId);
75
    }
76
};
77
 
78
/**
79
 * Function to update the clock with the current time left.
80
 *
81
 * @method update
82
 */
83
const update = () => {
84
    const now = new Date().getTime();
85
    const secondsLeft = Math.floor((endTime - now) / 1000);
86
 
87
    // If time has expired, set the hidden form field that says time has expired.
88
    if (secondsLeft <= 0) {
89
        timer.classList.add('alert', 'alert-danger');
90
        timer.innerHTML = '00:00:00';
91
 
92
        // Only add a notification on the assign submission page.
93
        if (document.getElementById("mod_assign_timelimit_block")) {
94
            getString('caneditsubmission', 'mod_assign')
95
                .then(message => Notification.addNotification({message}))
96
                .catch(Notification.exception);
97
        }
98
 
99
        stop();
100
        return;
101
    } else if (secondsLeft < 300) { // Add danger style when less than 5 minutes left.
102
        timer.classList.remove('alert-warning');
103
        timer.classList.add('alert', 'alert-danger');
104
    } else if (secondsLeft < 900) { // Add warning style when less than 15 minutes left.
105
        timer.classList.remove('alert-danger');
106
        timer.classList.add('alert', 'alert-warning');
107
    }
108
 
109
    // Update the time display.
110
    timer.innerHTML = formatSeconds(secondsLeft);
111
 
112
    // Arrange for this method to be called again soon.
113
    timeoutId = setTimeout(update, 500);
114
};
115
 
116
/**
117
 * Set up the submission timer.
118
 *
119
 * @method init
120
 * @param {Number} timerId Unique ID of the timer element.
121
 */
122
export const init = (timerId) => {
123
    timer = document.getElementById(timerId);
124
    endTime = M.pageloadstarttime.getTime() + (timer.dataset.starttime * 1000);
125
    update();
126
};