Skip to content

Commit

Permalink
Merge branch 'MDL-80630_main' of https://github.com/PhilippImhof/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Feb 13, 2024
2 parents 22b0e01 + 5ec21f8 commit 043c829
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
18 changes: 18 additions & 0 deletions mod/quiz/lib.php
Expand Up @@ -2606,3 +2606,21 @@ function mod_quiz_calculate_question_stats(context $context): ?all_calculated_fo
$report = new quiz_statistics_report();
return $report->calculate_questions_stats_for_question_bank($cm->instance, false, false);
}

/**
* Return a list of all the user preferences used by mod_quiz.
*
* @uses core_user::is_current_user
*
* @return array[]
*/
function mod_quiz_user_preferences(): array {
$preferences = [];
$preferences['quiz_timerhidden'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => '0',
'permissioncallback' => [core_user::class, 'is_current_user'],
];
return $preferences;
}
67 changes: 53 additions & 14 deletions mod/quiz/module.js
Expand Up @@ -78,38 +78,72 @@ M.mod_quiz.timer = {
M.mod_quiz.timer.endtime = M.pageloadstarttime.getTime() + start*1000;
M.mod_quiz.timer.preview = preview;
M.mod_quiz.timer.update();

Y.one('#quiz-timer-wrapper').setStyle('display', 'flex');
require(['core_form/changechecker'], function(FormChangeChecker) {
M.mod_quiz.timer.FormChangeChecker = FormChangeChecker;
});
Y.one('#toggle-timer').on('click', function() {
M.mod_quiz.timer.toggleVisibility();
});

// We store the visibility as a user preference. If the value is not '1',
// i. e. it is '0' or the item does not exist, the timer must be shown.
require(['core_user/repository'], function(UserRepository) {
UserRepository.getUserPreference('quiz_timerhidden')
.then((response) => {
M.mod_quiz.timer.setVisibility(response !== '1', false);
return;
})
// If there is an error, we catch and ignore it, because (i) no matter what we do,
// we do not have the stored value, so we will need to take a reasonable default
// and (ii) the student who is currently taking the quiz is probably not interested
// in the technical details why the fetch failed, even less, because they can hardly
// do anything to solve the problem. However, we still log that there was an error
// to leave a trace, e. g. for debugging.
.catch((error) => {
M.mod_quiz.timer.setVisibility(true, false);
Y.log(error, 'error', 'moodle-mod_quiz');
});
});
},

/**
* Toggle the timer's visibility.
*/
toggleVisibility: function() {
var Y = M.mod_quiz.timer.Y;
var timer = Y.one('#quiz-time-left');

// If the timer is currently hidden, the visibility should be set to true and vice versa.
this.setVisibility(timer.getAttribute('hidden') === 'hidden');
},

/**
* Hide or show the timer.
* @param {boolean} whether we are ultimately displaying the timer and disabling the button
* Set visibility of the timer.
* @param visible whether the timer should be visible
* @param updatePref whether the new status should be stored as a preference
*/
toggleVisibility: function(finalShow = false) {
setVisibility: function(visible, updatePref = true) {
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();
if (visible) {
button.setContent(M.util.get_string('hide', 'moodle'));
button.setAttribute('disabled', true);
return;
timer.show();
} else {
button.setContent(M.util.get_string('show', 'moodle'));
timer.hide();
}

timer.toggleView();
if (timer.getAttribute('hidden') === 'hidden') {
button.setContent(M.util.get_string('show', 'moodle'));
} else {
button.setContent(M.util.get_string('hide', 'moodle'));
// Only update the user preference if this has been requested.
if (updatePref) {
require(['core_user/repository'], function(UserRepository) {
UserRepository.setUserPreference('quiz_timerhidden', (visible ? '0' : '1'));
});
}

},

/**
Expand Down Expand Up @@ -157,7 +191,12 @@ 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);

// From now on, the timer should be visible and should not be hideable anymore.
// We use the second (optional) parameter in order to leave the user preference
// unchanged.
M.mod_quiz.timer.setVisibility(true, false);
Y.one('#toggle-timer').setAttribute('disabled', true);
}

// Update the time display.
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/templates/timer.mustache
Expand Up @@ -28,7 +28,7 @@
{{#str}} timeleft, quiz {{/str}}
<span id="quiz-time-left"></span>
</div>
<button type="button" class="btn btn-secondary btn-small" id="toggle-timer" aria-controls="quiz-timer" aria-describedby="quiz-timer">
<button type="button" class="btn btn-secondary btn-small ml-1" id="toggle-timer" aria-controls="quiz-timer" aria-describedby="quiz-timer">
{{#str}} hide, core {{/str}}
</button>
</div>

0 comments on commit 043c829

Please sign in to comment.