Skip to content

Commit

Permalink
MDL-46509 enrol: Require 'enrol/PLUGIN:config' capabilities to show/h…
Browse files Browse the repository at this point in the history
…ide enrollment instances
  • Loading branch information
Daniel Neis Araujo authored and danielneis committed Aug 21, 2014
1 parent 0594336 commit b5a289c
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 22 deletions.
11 changes: 11 additions & 0 deletions enrol/category/lib.php
Expand Up @@ -53,6 +53,17 @@ public function can_delete_instance($instance) {
return !$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id));
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/category:config', $context);
}

/**
* Returns link to page which may be used to add new instance of enrolment plugin in course.
* @param int $courseid
Expand Down
11 changes: 11 additions & 0 deletions enrol/cohort/lib.php
Expand Up @@ -360,6 +360,17 @@ public function restore_group_member($instance, $groupid, $userid) {
// Nothing to do here, the group members are added in $this->restore_group_restored()
return;
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/cohort:config', $context);
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions enrol/database/lib.php
Expand Up @@ -54,6 +54,17 @@ public function can_delete_instance($instance) {
return false;
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/database:config', $context);
}

/**
* Does this plugin allow manual unenrolment of a specific user?
* Yes, but only if user suspended...
Expand Down
11 changes: 11 additions & 0 deletions enrol/flatfile/lib.php
Expand Up @@ -106,6 +106,17 @@ public function can_delete_instance($instance) {
return has_capability('enrol/flatfile:manage', $context);
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/flatfile:manage', $context);
}

/**
* Gets an array of the user enrolment actions.
*
Expand Down
10 changes: 10 additions & 0 deletions enrol/guest/lib.php
Expand Up @@ -403,4 +403,14 @@ public function can_delete_instance($instance) {
return has_capability('enrol/guest:config', $context);
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/guest:config', $context);
}
}
11 changes: 11 additions & 0 deletions enrol/imsenterprise/lib.php
Expand Up @@ -804,4 +804,15 @@ public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/imsenterprise:config', $context);
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/imsenterprise:config', $context);
}
}
43 changes: 27 additions & 16 deletions enrol/instances.php
Expand Up @@ -146,28 +146,39 @@
} else if ($action === 'disable') {
$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();
if ($plugin->can_hide_show_instance($instance)) {
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);
}
$plugin->update_status($instance, ENROL_INSTANCE_DISABLED);
redirect($PAGE->url);
}

} else if ($action === 'enable') {
$instance = $instances[$instanceid];
$plugin = $plugins[$instance->enrol];
if ($instance->status != ENROL_INSTANCE_ENABLED) {
$plugin->update_status($instance, ENROL_INSTANCE_ENABLED);
redirect($PAGE->url);
if ($plugin->can_hide_show_instance($instance)) {
if ($instance->status != ENROL_INSTANCE_ENABLED) {
$plugin->update_status($instance, ENROL_INSTANCE_ENABLED);
redirect($PAGE->url);
}
}
}
}
Expand Down Expand Up @@ -235,7 +246,7 @@
$edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/delete', $strdelete, 'core', array('class' => 'iconsmall')));
}

if (enrol_is_enabled($instance->enrol)) {
if (enrol_is_enabled($instance->enrol) && $plugin->can_hide_show_instance($instance)) {
if ($instance->status == ENROL_INSTANCE_ENABLED) {
$aurl = new moodle_url($url, array('action'=>'disable', 'instance'=>$instance->id));
$edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/hide', $strdisable, 'core', array('class' => 'iconsmall')));
Expand Down
11 changes: 11 additions & 0 deletions enrol/ldap/lib.php
Expand Up @@ -124,6 +124,17 @@ public function can_delete_instance($instance) {
return false;
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/ldap:config', $context);
}

/**
* Forces synchronisation of user enrolments with LDAP server.
* It creates courses if the plugin is configured to do so.
Expand Down
11 changes: 11 additions & 0 deletions enrol/manual/lib.php
Expand Up @@ -564,4 +564,15 @@ public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/manual:config', $context);
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/manual:config', $context);
}
}
14 changes: 12 additions & 2 deletions enrol/meta/lib.php
Expand Up @@ -152,12 +152,22 @@ public function cron() {
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/meta:config', $context);
}
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/meta:config', $context);
}
}
13 changes: 12 additions & 1 deletion enrol/mnet/lib.php
Expand Up @@ -92,11 +92,22 @@ public function get_newinstance_link($courseid) {
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/mnet:config', $context);
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/mnet:config', $context);
}
}
13 changes: 12 additions & 1 deletion enrol/paypal/lib.php
Expand Up @@ -311,11 +311,22 @@ public function sync(progress_trace $trace) {
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/paypal:config', $context);
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/paypal:config', $context);
}
}
13 changes: 12 additions & 1 deletion enrol/self/lib.php
Expand Up @@ -651,11 +651,22 @@ public function restore_role_assignment($instance, $roleid, $userid, $contextid)
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/self:config', $context);
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/self:config', $context);
}
}
2 changes: 2 additions & 0 deletions enrol/upgrade.txt
Expand Up @@ -4,6 +4,8 @@ information provided here is intended especially for developers.
=== 2.8 ===

* enrol_plugin::instance_deleteable() is deprecated and has been replaced by enrol_plugin::can_delete_instance()
* enrol_plugin::can_hide_show_instance() is a new function to control who can hide/show enrolment instances.
Returns true by default but plugins must implement their own logic.

=== 2.6 ===

Expand Down
13 changes: 12 additions & 1 deletion lib/enrollib.php
Expand Up @@ -1554,13 +1554,24 @@ public function instance_deleteable($instance) {
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
return false;
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
debugging("The enrolment plugin '".$this->get_name()."' should override the function can_hide_show_instance().", DEBUG_DEVELOPER);
return true;
}

/**
* Returns link to manual enrol UI if exists.
* Does the access control tests automatically.
Expand Down

0 comments on commit b5a289c

Please sign in to comment.