MDL-37717 warn teachers before disabling or deleting their enrolment method

This commit is contained in:
Petr Škoda 2013-09-28 17:26:40 +02:00
parent 56cc9b387e
commit 12c92bca22
6 changed files with 62 additions and 0 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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';

View File

@ -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,