Skip to content

Commit

Permalink
MDL-37717 warn teachers before disabling or deleting their enrolment …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
skodak committed Oct 4, 2013
1 parent 56cc9b3 commit 12c92bc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
23 changes: 23 additions & 0 deletions enrol/instances.php
Expand Up @@ -28,6 +28,7 @@
$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);
Expand Down Expand Up @@ -96,6 +97,17 @@
$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);
}
Expand All @@ -117,6 +129,17 @@
$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);
}
Expand Down
4 changes: 4 additions & 0 deletions enrol/manual/edit_form.php
Expand Up @@ -65,6 +65,10 @@ function definition() {
$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);
Expand Down
4 changes: 4 additions & 0 deletions enrol/paypal/edit_form.php
Expand Up @@ -79,6 +79,10 @@ function definition() {
$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);
Expand Down
4 changes: 4 additions & 0 deletions enrol/self/edit_form.php
Expand Up @@ -147,6 +147,10 @@ function definition() {
$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);
Expand Down
4 changes: 4 additions & 0 deletions lang/en/enrol.php
Expand Up @@ -38,7 +38,9 @@
$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';
Expand Down Expand Up @@ -80,6 +82,8 @@
$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';
Expand Down
23 changes: 23 additions & 0 deletions lib/enrollib.php
Expand Up @@ -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,
Expand Down

0 comments on commit 12c92bc

Please sign in to comment.