Skip to content

Commit

Permalink
Merge pull request #179 from aneno-m-e/copy-default
Browse files Browse the repository at this point in the history
Copy default settings on course creation
  • Loading branch information
danmarsden authored Jul 16, 2024
2 parents 1179375 + 85b9bb1 commit ca925cd
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 38 deletions.
38 changes: 38 additions & 0 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,42 @@ public static function user_enrolment_deleted(\core\event\user_enrolment_deleted
}
}
}

/**
* Observer function to handle saving default settings on course creation.
* @param \core\event\course_created $event
*/
public static function course_created(\core\event\course_created $event) {
global $DB;

$defaultsettings = get_config('local_recompletion');

if (!empty($defaultsettings->recompletiontype)) {
foreach ($defaultsettings as $key => $value) {
if (is_null($value) || $key === 'forcearchivecompletiondata') {
continue;
}
if ($key === 'recompletionemailbody' && !empty($value)) {
$DB->insert_record('local_recompletion_config', [
'course' => $event->courseid,
'name' => 'recompletionemailbody_format',
'value' => FORMAT_HTML]);
}
if ($key === 'recompletionschedule' && !empty($value)) {
$nextresttime = local_recompletion_calculate_schedule_time($value);
$DB->insert_record('local_recompletion_config', [
'course' => $event->courseid,
'name' => 'nextresettime',
'value' => $nextresttime]);
}
$setting = [
'name' => $key,
'value' => $value,
'course' => $event->courseid
];

$DB->insert_record('local_recompletion_config', $setting);
}
}
}
}
8 changes: 5 additions & 3 deletions classes/plugins/mod_assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ public static function editingform($mform): void {
get_string('donothing', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING);
$cba[] = $mform->createElement('radio', 'assign', '',
get_string('extraattempt', 'local_recompletion'), LOCAL_RECOMPLETION_EXTRAATTEMPT);
$cba[] = $mform->createElement('checkbox', 'assignevent', '', get_string('assignevent', 'local_recompletion'));

$mform->addGroup($cba, 'assign', get_string('assignattempts', 'local_recompletion'), array(' '), false);
$mform->addHelpButton('assign', 'assignattempts', 'local_recompletion');
$mform->setDefault('assign', $config->assign);

$mform->setDefault('assign', $config->assignattempts);
$mform->addElement('checkbox', 'assignevent', '', get_string('assignevent', 'local_recompletion'));
$mform->setDefault('assignevent', $config->assignevent);

$mform->disabledIf('assignevent', 'enable', 'notchecked');
$mform->disabledIf('assign', 'enable', 'notchecked');
}

Expand All @@ -70,7 +72,7 @@ public static function settings($settings) {
$choices = array(LOCAL_RECOMPLETION_NOTHING => new lang_string('donothing', 'local_recompletion'),
LOCAL_RECOMPLETION_EXTRAATTEMPT => new lang_string('extraattempt', 'local_recompletion'));

$settings->add(new \admin_setting_configselect('local_recompletion/assignattempts',
$settings->add(new \admin_setting_configselect('local_recompletion/assign',
new lang_string('assignattempts', 'local_recompletion'),
new lang_string('assignattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

Expand Down
4 changes: 2 additions & 2 deletions classes/plugins/mod_choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function editingform($mform) : void {

$mform->addGroup($cba, 'choice', get_string('choiceattempts', 'local_recompletion'), array(' '), false);
$mform->addHelpButton('choice', 'choiceattempts', 'local_recompletion');
$mform->setDefault('choice', $config->choiceattempts);
$mform->setDefault('choice', $config->choice);

$mform->addElement('checkbox', 'archivechoice',
get_string('archive', 'local_recompletion'));
Expand All @@ -75,7 +75,7 @@ public static function settings($settings) {
$choices = array(LOCAL_RECOMPLETION_NOTHING => new lang_string('donothing', 'local_recompletion'),
LOCAL_RECOMPLETION_DELETE => new lang_string('delete', 'local_recompletion'));

$settings->add(new \admin_setting_configselect('local_recompletion/choiceattempts',
$settings->add(new \admin_setting_configselect('local_recompletion/choice',
new lang_string('choiceattempts', 'local_recompletion'),
new lang_string('choiceattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

Expand Down
4 changes: 2 additions & 2 deletions classes/plugins/mod_customcert.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function editingform($mform): void {

$mform->addGroup($cba, 'customcert', get_string('customcertcertificates', 'local_recompletion'), array(' '), false);
$mform->addHelpButton('customcert', 'customcertcertificates', 'local_recompletion');
$mform->setDefault('customcert', $config->customcertcertificates);
$mform->setDefault('customcert', $config->customcert);

$mform->addElement('checkbox', 'archivecustomcert',
get_string('archivecustomcertcertificates', 'local_recompletion'));
Expand Down Expand Up @@ -94,7 +94,7 @@ public static function settings($settings) {
$choices = array(LOCAL_RECOMPLETION_NOTHING => get_string('donothing', 'local_recompletion'),
LOCAL_RECOMPLETION_DELETE => get_string('customcertresetcertificates', 'local_recompletion'));

$settings->add(new \admin_setting_configselect('local_recompletion/customcertcertificates',
$settings->add(new \admin_setting_configselect('local_recompletion/customcert',
new lang_string('customcertcertificates', 'local_recompletion'),
new lang_string('customcertcertificates_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

Expand Down
8 changes: 4 additions & 4 deletions classes/plugins/mod_h5pactivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public static function editingform(MoodleQuickForm $mform): void {

$mform->addGroup($cba, 'h5pactivity', get_string('h5pattempts', 'local_recompletion'), [' '], false);
$mform->addHelpButton('h5pactivity', 'h5pattempts', 'local_recompletion');
$mform->setDefault('h5pactivity', $config->h5pattempts);
$mform->setDefault('h5pactivity', $config->h5pactivity);

$mform->addElement('checkbox', 'archiveh5pactivity', get_string('archive', 'local_recompletion'));
$mform->setDefault('archiveh5pactivity', $config->archiveh5p);
$mform->setDefault('archiveh5pactivity', $config->archiveh5pactivity);

$mform->disabledIf('archiveh5pactivity', 'enable');
$mform->hideIf('archiveh5pactivity', 'h5pactivity');
Expand All @@ -73,11 +73,11 @@ public static function settings(admin_settingpage $settings): void {
LOCAL_RECOMPLETION_DELETE => get_string('delete', 'local_recompletion')
];

$settings->add(new admin_setting_configselect('local_recompletion/h5pattempts',
$settings->add(new admin_setting_configselect('local_recompletion/h5pactivity',
new lang_string('h5pattempts', 'local_recompletion'),
new lang_string('h5pattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

$settings->add(new admin_setting_configcheckbox('local_recompletion/archiveh5p',
$settings->add(new admin_setting_configcheckbox('local_recompletion/archiveh5pactivity',
new lang_string('archiveh5p', 'local_recompletion'), '', 1));
}

Expand Down
4 changes: 2 additions & 2 deletions classes/plugins/mod_hotpot.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function editingform(MoodleQuickForm $mform): void {

$mform->addGroup($cba, 'hotpot', get_string('hotpotattempts', 'local_recompletion'), [' '], false);
$mform->addHelpButton('hotpot', 'hotpotattempts', 'local_recompletion');
$mform->setDefault('hotpot', $config->hotpotattempts);
$mform->setDefault('hotpot', $config->hotpot);

$mform->addElement('checkbox', 'archivehotpot', get_string('archive', 'local_recompletion'));
$mform->setDefault('archivehhotpot', $config->archivehotpot);
Expand All @@ -81,7 +81,7 @@ public static function settings(admin_settingpage $settings): void {
LOCAL_RECOMPLETION_DELETE => get_string('delete', 'local_recompletion')
];

$settings->add(new admin_setting_configselect('local_recompletion/hotpotattempts',
$settings->add(new admin_setting_configselect('local_recompletion/hotpot',
new lang_string('hotpotattempts', 'local_recompletion'),
new lang_string('hotpotattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

Expand Down
4 changes: 2 additions & 2 deletions classes/plugins/mod_hvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function editingform(MoodleQuickForm $mform): void {

$mform->addGroup($cba, 'hvp', get_string('hvpattempts', 'local_recompletion'), [' '], false);
$mform->addHelpButton('hvp', 'hvpattempts', 'local_recompletion');
$mform->setDefault('hvp', $config->hvpattempts);
$mform->setDefault('hvp', $config->hvp);

$mform->addElement('checkbox', 'archivehvp', get_string('archive', 'local_recompletion'));
$mform->setDefault('archivehvp', $config->archivehvp);
Expand All @@ -81,7 +81,7 @@ public static function settings(admin_settingpage $settings): void {
LOCAL_RECOMPLETION_DELETE => get_string('delete', 'local_recompletion')
];

$settings->add(new admin_setting_configselect('local_recompletion/hvpattempts',
$settings->add(new admin_setting_configselect('local_recompletion/hvp',
new lang_string('hvpattempts', 'local_recompletion'),
new lang_string('hvpattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

Expand Down
4 changes: 2 additions & 2 deletions classes/plugins/mod_lesson.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function editingform(MoodleQuickForm $mform): void {

$mform->addGroup($cba, 'lesson', get_string('lessonattempts', 'local_recompletion'), [' '], false);
$mform->addHelpButton('lesson', 'lessonattempts', 'local_recompletion');
$mform->setDefault('lesson', $config->lessonattempts);
$mform->setDefault('lesson', $config->lesson);

$mform->addElement('checkbox', 'archivelesson', get_string('archive', 'local_recompletion'));
$mform->setDefault('archivelesson', $config->archivelesson);
Expand All @@ -73,7 +73,7 @@ public static function settings(admin_settingpage $settings): void {
LOCAL_RECOMPLETION_DELETE => get_string('delete', 'local_recompletion')
];

$settings->add(new admin_setting_configselect('local_recompletion/lessonattempts',
$settings->add(new admin_setting_configselect('local_recompletion/lesson',
new lang_string('lessonattempts', 'local_recompletion'),
new lang_string('lessonattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

Expand Down
4 changes: 2 additions & 2 deletions classes/plugins/mod_pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function editingform($mform): void {

$mform->addGroup($cba, 'pulse', get_string('pulsenotifications', 'local_recompletion'), array(' '), false);
$mform->addHelpButton('pulse', 'pulsenotifications', 'local_recompletion');
$mform->setDefault('pulse', $config->pulsenotifications);
$mform->setDefault('pulse', $config->pulse);
}

/**
Expand All @@ -73,7 +73,7 @@ public static function settings($settings) {
}
$choices = array(LOCAL_RECOMPLETION_NOTHING => get_string('donothing', 'local_recompletion'),
LOCAL_RECOMPLETION_DELETE => get_string('pulseresetnotifications', 'local_recompletion'));
$settings->add(new \admin_setting_configselect('local_recompletion/pulsenotifications',
$settings->add(new \admin_setting_configselect('local_recompletion/pulse',
new lang_string('pulsenotifications', 'local_recompletion'),
new lang_string('pulsenotifications_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));
}
Expand Down
4 changes: 2 additions & 2 deletions classes/plugins/mod_questionnaire.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function editingform($mform) : void {

$mform->addGroup($cba, 'questionnaire', get_string('questionnaireattempts', 'local_recompletion'), array(' '), false);
$mform->addHelpButton('questionnaire', 'questionnaireattempts', 'local_recompletion');
$mform->setDefault('questionnaire', $config->questionnaireattempts);
$mform->setDefault('questionnaire', $config->questionnaire);

$mform->addElement('checkbox', 'archivequestionnaire',
get_string('archive', 'local_recompletion'));
Expand All @@ -81,7 +81,7 @@ public static function settings($settings) {
LOCAL_RECOMPLETION_DELETE => new lang_string('delete', 'local_recompletion'),
LOCAL_RECOMPLETION_EXTRAATTEMPT => new lang_string('extraattempt', 'local_recompletion'));

$settings->add(new \admin_setting_configselect('local_recompletion/questionnaireattempts',
$settings->add(new \admin_setting_configselect('local_recompletion/questionnaire',
new lang_string('questionnaireattempts', 'local_recompletion'),
new lang_string('questionnaireattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

Expand Down
4 changes: 2 additions & 2 deletions classes/plugins/mod_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function editingform($mform) : void {

$mform->addGroup($cba, 'quiz', get_string('quizattempts', 'local_recompletion'), array(' '), false);
$mform->addHelpButton('quiz', 'quizattempts', 'local_recompletion');
$mform->setDefault('quiz', $config->quizattempts);
$mform->setDefault('quiz', $config->quiz);

$mform->addElement('checkbox', 'archivequiz',
get_string('archive', 'local_recompletion'));
Expand All @@ -83,7 +83,7 @@ public static function settings($settings) {
LOCAL_RECOMPLETION_DELETE => new lang_string('delete', 'local_recompletion'),
LOCAL_RECOMPLETION_EXTRAATTEMPT => new lang_string('extraattempt', 'local_recompletion'));

$settings->add(new \admin_setting_configselect('local_recompletion/quizattempts',
$settings->add(new \admin_setting_configselect('local_recompletion/quiz',
new lang_string('quizattempts', 'local_recompletion'),
new lang_string('quizattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

Expand Down
4 changes: 2 additions & 2 deletions classes/plugins/mod_scorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function editingform($mform) : void {

$mform->addGroup($cba, 'scorm', get_string('scormattempts', 'local_recompletion'), array(' '), false);
$mform->addHelpButton('scorm', 'scormattempts', 'local_recompletion');
$mform->setDefault('scorm', $config->scormattempts);
$mform->setDefault('scorm', $config->scorm);

$mform->addElement('checkbox', 'archivescorm',
get_string('archive', 'local_recompletion'));
Expand All @@ -74,7 +74,7 @@ public static function editingform($mform) : void {
public static function settings($settings) {
$choices = array(LOCAL_RECOMPLETION_NOTHING => get_string('donothing', 'local_recompletion'),
LOCAL_RECOMPLETION_DELETE => get_string('delete', 'local_recompletion'));
$settings->add(new \admin_setting_configselect('local_recompletion/scormattempts',
$settings->add(new \admin_setting_configselect('local_recompletion/scorm',
new lang_string('scormattempts', 'local_recompletion'),
new lang_string('scormattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

Expand Down
10 changes: 5 additions & 5 deletions classes/recompletion_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ public function definition() {
$mform->addHelpButton('recompletiontype', 'recompletiontype', 'local_recompletion');

$mform->addElement('checkbox', 'recompletionemailenable', get_string('recompletionemailenable', 'local_recompletion'));
$mform->setDefault('recompletionemailenable', $config->emailenable);
$mform->setDefault('recompletionemailenable', $config->recompletionemailenable);
$mform->addHelpButton('recompletionemailenable', 'recompletionemailenable', 'local_recompletion');
$mform->hideIf('recompletionemailenable', 'recompletiontype', 'eq', '');

$mform->addElement('checkbox', 'recompletionunenrolenable', get_string('recompletionunenrolenable', 'local_recompletion'));
$mform->setDefault('recompletionunenrolenable', $config->unenrolenable);
$mform->setDefault('recompletionunenrolenable', $config->recompletionunenrolenable);
$mform->addHelpButton('recompletionunenrolenable', 'recompletionunenrolenable', 'local_recompletion');
$mform->hideIf('recompletionunenrolenable', 'recompletiontype', 'eq', '');

$options = ['optional' => false, 'defaultunit' => 86400];
$mform->addElement('duration', 'recompletionduration', get_string('recompletionrange', 'local_recompletion'), $options);
$mform->addHelpButton('recompletionduration', 'recompletionrange', 'local_recompletion');
$mform->setDefault('recompletionduration', $config->duration);
$mform->setDefault('recompletionduration', $config->recompletionduration);
$mform->hideif('recompletionduration', 'recompletiontype', 'neq', self::RECOMPLETION_TYPE_PERIOD);

// Schedule / cron settings.
Expand All @@ -105,11 +105,11 @@ public function definition() {
$mform->addHelpButton('recompletionemailsubject', 'recompletionemailsubject', 'local_recompletion');
$mform->disabledIf('recompletionemailsubject', 'recompletiontype', 'eq', '');
$mform->disabledIf('recompletionemailsubject', 'recompletionemailenable', 'notchecked');
$mform->setDefault('recompletionemailsubject', $config->emailsubject);
$mform->setDefault('recompletionemailsubject', $config->recompletionemailsubject);

$mform->addElement('editor', 'recompletionemailbody', get_string('recompletionemailbody', 'local_recompletion'),
$editoroptions);
$mform->setDefault('recompletionemailbody', ['text' => $config->emailbody, 'format' => FORMAT_HTML]);
$mform->setDefault('recompletionemailbody', ['text' => $config->recompletionemailbody, 'format' => FORMAT_HTML]);
$mform->addHelpButton('recompletionemailbody', 'recompletionemailbody', 'local_recompletion');
$mform->disabledIf('recompletionemailbody', 'recompletiontype', 'eq', '');
$mform->disabledIf('recompletionemailbody', 'recompletionemailenable', 'notchecked');
Expand Down
4 changes: 4 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@
'eventname' => '\core\event\user_enrolment_deleted',
'callback' => '\local_recompletion\observer::user_enrolment_deleted'
),
array(
'eventname' => '\core\event\course_created',
'callback' => '\local_recompletion\observer::course_created'
),
);
34 changes: 34 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,5 +987,39 @@ function xmldb_local_recompletion_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2023112600, 'local', 'recompletion');
}

if ($oldversion < 2023112704) {
// We renamed some site admin settings.
$update = [
'assignattempts' => 'assign',
'choiceattempts' => 'choice',
'customcertcertificates' => 'customcert',
'h5pattempts' => 'h5pactivity',
'archiveh5p' => 'archiveh5pactivity',
'hotpotattempts' => 'hotpot',
'hvpattempts' => 'hvp',
'lessonattempts' => 'lesson',
'pulsenotifications' => 'pulse',
'questionnaireattempts' => 'questionnaire',
'quizattempts' => 'quiz',
'scormattempts' => 'scorm',
'schedule' => 'recompletionschedule',
'duration' => 'recompletionduration',
'emailenable' => 'recompletionemailenable',
'emailsubject' => 'recompletionemailsubject',
'emailbody' => 'recompletionemailbody',
'unenrolenable' => 'recompletionunenrolenable'
];

foreach ($update as $old => $new) {
if ($DB->get_record('config_plugins', ['name' => $old, 'plugin' => 'local_recompletion']) != false) {
set_config($new, get_config('local_recompletion', $old), 'local_recompletion');
unset_config($old, 'local_recompletion');
}
}

// Recompletion savepoint reached.
upgrade_plugin_savepoint(true, 2023112704, 'local', 'recompletion');
}

return true;
}
Loading

0 comments on commit ca925cd

Please sign in to comment.