From 12c92bca22827a127d3ae06732e298e67219d43e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sat, 28 Sep 2013 17:26:40 +0200 Subject: [PATCH] MDL-37717 warn teachers before disabling or deleting their enrolment method --- enrol/instances.php | 23 +++++++++++++++++++++++ enrol/manual/edit_form.php | 4 ++++ enrol/paypal/edit_form.php | 4 ++++ enrol/self/edit_form.php | 4 ++++ lang/en/enrol.php | 4 ++++ lib/enrollib.php | 23 +++++++++++++++++++++++ 6 files changed, 62 insertions(+) diff --git a/enrol/instances.php b/enrol/instances.php index 0ad5d2c427d..09ed54922ad 100644 --- a/enrol/instances.php +++ b/enrol/instances.php @@ -28,6 +28,7 @@ $id = required_param('id', PARAM_INT); // course id $action = optional_param('action', '', PARAM_ALPHANUMEXT); $instanceid = optional_param('instance', 0, PARAM_INT); $confirm = optional_param('confirm', 0, PARAM_BOOL); +$confirm2 = optional_param('confirm2', 0, PARAM_BOOL); $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); $context = context_course::instance($course->id, MUST_EXIST); @@ -96,6 +97,17 @@ if ($canconfig and $action and confirm_sesskey()) { $plugin = $plugins[$instance->enrol]; if ($confirm) { + if (enrol_accessing_via_instance($instance)) { + if (!$confirm2) { + $yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'delete', 'instance'=>$instance->id, 'confirm'=>1, 'confirm2'=>1, 'sesskey'=>sesskey())); + $displayname = $plugin->get_instance_name($instance); + $message = markdown_to_html(get_string('deleteinstanceconfirmself', 'enrol', array('name'=>$displayname))); + echo $OUTPUT->header(); + echo $OUTPUT->confirm($message, $yesurl, $PAGE->url); + echo $OUTPUT->footer(); + die(); + } + } $plugin->delete_instance($instance); redirect($PAGE->url); } @@ -117,6 +129,17 @@ if ($canconfig and $action and confirm_sesskey()) { $instance = $instances[$instanceid]; $plugin = $plugins[$instance->enrol]; if ($instance->status != ENROL_INSTANCE_DISABLED) { + if (enrol_accessing_via_instance($instance)) { + if (!$confirm2) { + $yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'disable', 'instance'=>$instance->id, 'confirm2'=>1, 'sesskey'=>sesskey())); + $displayname = $plugin->get_instance_name($instance); + $message = markdown_to_html(get_string('disableinstanceconfirmself', 'enrol', array('name'=>$displayname))); + echo $OUTPUT->header(); + echo $OUTPUT->confirm($message, $yesurl, $PAGE->url); + echo $OUTPUT->footer(); + die(); + } + } $plugin->update_status($instance, ENROL_INSTANCE_DISABLED); redirect($PAGE->url); } diff --git a/enrol/manual/edit_form.php b/enrol/manual/edit_form.php index 834aee726d3..bfc09a7848f 100644 --- a/enrol/manual/edit_form.php +++ b/enrol/manual/edit_form.php @@ -65,6 +65,10 @@ class enrol_manual_edit_form extends moodleform { $mform->addElement('hidden', 'courseid'); $mform->setType('courseid', PARAM_INT); + if (enrol_accessing_via_instance($instance)) { + $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), get_string('instanceeditselfwarningtext', 'core_enrol')); + } + $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol'))); $this->set_data($instance); diff --git a/enrol/paypal/edit_form.php b/enrol/paypal/edit_form.php index 857cc2ceecc..6a511382ec2 100644 --- a/enrol/paypal/edit_form.php +++ b/enrol/paypal/edit_form.php @@ -79,6 +79,10 @@ class enrol_paypal_edit_form extends moodleform { $mform->addElement('hidden', 'courseid'); $mform->setType('courseid', PARAM_INT); + if (enrol_accessing_via_instance($instance)) { + $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), get_string('instanceeditselfwarningtext', 'core_enrol')); + } + $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol'))); $this->set_data($instance); diff --git a/enrol/self/edit_form.php b/enrol/self/edit_form.php index dc324f28af6..5abbc192a0f 100644 --- a/enrol/self/edit_form.php +++ b/enrol/self/edit_form.php @@ -147,6 +147,10 @@ class enrol_self_edit_form extends moodleform { $mform->addElement('hidden', 'courseid'); $mform->setType('courseid', PARAM_INT); + if (enrol_accessing_via_instance($instance)) { + $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), get_string('instanceeditselfwarningtext', 'core_enrol')); + } + $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol'))); $this->set_data($instance); diff --git a/lang/en/enrol.php b/lang/en/enrol.php index 50c6573e122..ac01907560b 100644 --- a/lang/en/enrol.php +++ b/lang/en/enrol.php @@ -38,7 +38,9 @@ $string['defaultenrol_desc'] = 'It is possible to add this plugin to all new cou $string['deleteinstanceconfirm'] = 'You are about to delete the enrolment method "{$a->name}". All {$a->users} users currently enrolled using this method will be unenrolled and any course-related data such as users\' grades, group membership or forum subscriptions will be deleted. Are you sure you want to continue?'; +$string['deleteinstanceconfirmself'] = 'Are you really sure you want to delete instance "{$a->name}" that gives you access to this course? It is possible that you will not be able to access this course if you continue.'; $string['deleteinstancenousersconfirm'] = 'You are about to delete the enrolment method "{$a->name}". Are you sure you want to continue?'; +$string['disableinstanceconfirmself'] = 'Are you really sure you want to disable instance "{$a->name}" that gives you access to this course? It is possible that you will not be able to access this course if you continue.'; $string['durationdays'] = '{$a} days'; $string['editenrolment'] = 'Edit enrolment'; $string['enrol'] = 'Enrol'; @@ -80,6 +82,8 @@ $string['expirynotifyhour'] = 'Hour to send enrolment expiry notifications'; $string['expirythreshold'] = 'Notification threshold'; $string['expirythreshold_help'] = 'How long before expiration should be users notified?'; $string['finishenrollingusers'] = 'Finish enrolling users'; +$string['instanceeditselfwarning'] = 'Warning:'; +$string['instanceeditselfwarningtext'] = 'You are enrolled into this course through this enrolment method, changes may affect your access to this course.'; $string['invalidenrolinstance'] = 'Invalid enrolment instance'; $string['invalidrole'] = 'Invalid role'; $string['manageenrols'] = 'Manage enrol plugins'; diff --git a/lib/enrollib.php b/lib/enrollib.php index dc7aa9224c9..df880d877f0 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -1039,6 +1039,29 @@ function enrol_get_enrolment_end($courseid, $userid) { } } +/** + * Is current user accessing course via this enrolment method? + * + * This is intended for operations that are going to affect enrol instances. + * + * @param stdClass $instance enrol instance + * @return bool + */ +function enrol_accessing_via_instance(stdClass $instance) { + global $DB, $USER; + + if (empty($instance->id)) { + return false; + } + + if (is_siteadmin()) { + // Admins may go anywhere. + return false; + } + + return $DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id)); +} + /** * All enrol plugins should be based on this class,