From f678200d8e231f041d3076fdd25dd5be20727c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Gandra=C3=9F?= Date: Mon, 8 Jan 2024 11:46:48 +0100 Subject: [PATCH] Fix locking of archive retention time job preset --- classes/form/archive_quiz_form.php | 28 ++++++++++++++++++++-------- classes/local/util.php | 24 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/classes/form/archive_quiz_form.php b/classes/form/archive_quiz_form.php index 40af6e9..75f40fd 100644 --- a/classes/form/archive_quiz_form.php +++ b/classes/form/archive_quiz_form.php @@ -25,6 +25,7 @@ namespace quiz_archiver\form; use quiz_archiver\ArchiveJob; +use quiz_archiver\local\util; use quiz_archiver\Report; defined('MOODLE_INTERNAL') || die(); @@ -200,15 +201,26 @@ public function definition() { $mform->addHelpButton('archive_autodelete', 'archive_autodelete', 'quiz_archiver'); $mform->setDefault('archive_autodelete', $config->job_preset_archive_autodelete); - $mform->addElement( - 'duration', - 'archive_retention_time', - get_string('archive_retention_time', 'quiz_archiver'), - ['optional' => false, 'defaultunit' => DAYSECS], - $config->job_preset_archive_retention_time_locked ? 'disabled' : null - ); + if ($config->job_preset_archive_retention_time_locked) { + $durationwithunit = util::duration_to_unit($config->job_preset_archive_retention_time); + $mform->addElement( + 'static', + 'archive_retention_time_static', + get_string('archive_retention_time', 'quiz_archiver'), + $durationwithunit[0].' '.$durationwithunit[1] + ); + $mform->addElement('hidden', 'archive_retention_time', $config->job_preset_archive_retention_time); + } else { + $mform->addElement( + 'duration', + 'archive_retention_time', + get_string('archive_retention_time', 'quiz_archiver'), + ['optional' => false, 'defaultunit' => DAYSECS], + ); + $mform->setDefault('archive_retention_time', $config->job_preset_archive_retention_time); + } + $mform->setType('archive_retention_time', PARAM_INT); $mform->addHelpButton('archive_retention_time', 'archive_retention_time', 'quiz_archiver'); - $mform->setDefault('archive_retention_time', $config->job_preset_archive_retention_time); $mform->hideIf('archive_retention_time', 'archive_autodelete', 'notchecked'); // Submit diff --git a/classes/local/util.php b/classes/local/util.php index a002b6f..9a07e97 100644 --- a/classes/local/util.php +++ b/classes/local/util.php @@ -64,4 +64,28 @@ public static function duration_to_human_readable(int $duration): string { return trim($humanreadable); } + /** + * Expresses a duration in seconds as the largest possible unit. + * + * @param int $duration Duration in seconds + * @return array Containing the value (idx: 0) and the unit (idx: 1) + * @throws \coding_exception + */ + public static function duration_to_unit(int $duration): array { + if (fmod($duration, WEEKSECS) == 0) { + return [$duration / WEEKSECS, get_string('weeks')]; + } + if (fmod($duration, DAYSECS) == 0) { + return [$duration / DAYSECS, get_string('days')]; + } + if (fmod($duration, HOURSECS) == 0) { + return [$duration / HOURSECS, get_string('hours')]; + } + if (fmod($duration, MINSECS) == 0) { + return [$duration / MINSECS, get_string('minutes')]; + } + + return [$duration, get_string('seconds')]; + } + } \ No newline at end of file