From f85aa04def2b9447b9a837bf8a6e13ec7187a8e6 Mon Sep 17 00:00:00 2001 From: Philipp Imhof <52650214+PhilippImhof@users.noreply.github.com> Date: Mon, 11 Sep 2023 21:30:25 +0200 Subject: [PATCH] MDL-77127 quiz: allow students to hide timer --- mod/quiz/locallib.php | 2 ++ mod/quiz/module.js | 29 +++++++++++++++++++++++++++++ mod/quiz/templates/timer.mustache | 3 +++ 3 files changed, 34 insertions(+) diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 83636df5476..d7bd98b71f9 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -1620,6 +1620,8 @@ function quiz_get_js_module() { ['functiondisabledbysecuremode', 'quiz'], ['startattempt', 'quiz'], ['timesup', 'quiz'], + ['show', 'moodle'], + ['hide', 'moodle'], ], ]; } diff --git a/mod/quiz/module.js b/mod/quiz/module.js index db7d3eda872..5514afe76ed 100644 --- a/mod/quiz/module.js +++ b/mod/quiz/module.js @@ -82,6 +82,34 @@ M.mod_quiz.timer = { require(['core_form/changechecker'], function(FormChangeChecker) { M.mod_quiz.timer.FormChangeChecker = FormChangeChecker; }); + Y.one('#toggle-timer').on('click', function() { + M.mod_quiz.timer.toggleVisibility(); + }); + }, + + /** + * Hide or show the timer. + * @param {boolean} whether we are ultimately displaying the timer and disabling the button + */ + toggleVisibility: function(finalShow = false) { + var Y = M.mod_quiz.timer.Y; + var timer = Y.one('#quiz-time-left'); + var button = Y.one('#toggle-timer'); + + // When time is running out, we show the timer and disable the button. + if (finalShow) { + timer.show(); + button.setContent(M.util.get_string('hide', 'moodle')); + button.setAttribute('disabled', true); + return; + } + + timer.toggleView(); + if (timer.getAttribute('hidden') === 'hidden') { + button.setContent(M.util.get_string('show', 'moodle')); + } else { + button.setContent(M.util.get_string('hide', 'moodle')); + } }, /** @@ -129,6 +157,7 @@ M.mod_quiz.timer = { Y.one('#quiz-timer').removeClass('timeleft' + (secondsleft + 2)) .removeClass('timeleft' + (secondsleft + 1)) .addClass('timeleft' + secondsleft); + M.mod_quiz.timer.toggleVisibility(true); } // Update the time display. diff --git a/mod/quiz/templates/timer.mustache b/mod/quiz/templates/timer.mustache index 7a825558085..2e414b48b78 100644 --- a/mod/quiz/templates/timer.mustache +++ b/mod/quiz/templates/timer.mustache @@ -28,4 +28,7 @@ {{#str}} timeleft, quiz {{/str}} +