Skip to content

Commit

Permalink
Fix locking of archive retention time job preset
Browse files Browse the repository at this point in the history
  • Loading branch information
ngandrass committed Jan 8, 2024
1 parent ba5905e commit f678200
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
28 changes: 20 additions & 8 deletions classes/form/archive_quiz_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions classes/local/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')];
}

}

0 comments on commit f678200

Please sign in to comment.