From b5a289c4e8b261187f75a49fccc243c968bca94e Mon Sep 17 00:00:00 2001 From: Daniel Neis Araujo Date: Mon, 11 Aug 2014 16:04:03 -0300 Subject: [PATCH] MDL-46509 enrol: Require 'enrol/PLUGIN:config' capabilities to show/hide enrollment instances --- enrol/category/lib.php | 11 ++++++++++ enrol/cohort/lib.php | 11 ++++++++++ enrol/database/lib.php | 11 ++++++++++ enrol/flatfile/lib.php | 11 ++++++++++ enrol/guest/lib.php | 10 +++++++++ enrol/imsenterprise/lib.php | 11 ++++++++++ enrol/instances.php | 43 +++++++++++++++++++++++-------------- enrol/ldap/lib.php | 11 ++++++++++ enrol/manual/lib.php | 11 ++++++++++ enrol/meta/lib.php | 14 ++++++++++-- enrol/mnet/lib.php | 13 ++++++++++- enrol/paypal/lib.php | 13 ++++++++++- enrol/self/lib.php | 13 ++++++++++- enrol/upgrade.txt | 2 ++ lib/enrollib.php | 13 ++++++++++- 15 files changed, 176 insertions(+), 22 deletions(-) diff --git a/enrol/category/lib.php b/enrol/category/lib.php index 3817bed452f..ef1699d42e1 100644 --- a/enrol/category/lib.php +++ b/enrol/category/lib.php @@ -53,6 +53,17 @@ class enrol_category_plugin extends enrol_plugin { 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 diff --git a/enrol/cohort/lib.php b/enrol/cohort/lib.php index e6a30266460..9146b2112be 100644 --- a/enrol/cohort/lib.php +++ b/enrol/cohort/lib.php @@ -360,6 +360,17 @@ class enrol_cohort_plugin extends enrol_plugin { // 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); + } } /** diff --git a/enrol/database/lib.php b/enrol/database/lib.php index de5db3294b1..c22c65e9313 100644 --- a/enrol/database/lib.php +++ b/enrol/database/lib.php @@ -54,6 +54,17 @@ class enrol_database_plugin extends enrol_plugin { 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... diff --git a/enrol/flatfile/lib.php b/enrol/flatfile/lib.php index fa468327e1e..41e0577a1a5 100644 --- a/enrol/flatfile/lib.php +++ b/enrol/flatfile/lib.php @@ -106,6 +106,17 @@ class enrol_flatfile_plugin extends enrol_plugin { 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. * diff --git a/enrol/guest/lib.php b/enrol/guest/lib.php index df601777d78..9336e1c2560 100644 --- a/enrol/guest/lib.php +++ b/enrol/guest/lib.php @@ -403,4 +403,14 @@ class enrol_guest_plugin extends enrol_plugin { 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); + } } diff --git a/enrol/imsenterprise/lib.php b/enrol/imsenterprise/lib.php index 8f4643697c3..bc68f99da87 100644 --- a/enrol/imsenterprise/lib.php +++ b/enrol/imsenterprise/lib.php @@ -804,4 +804,15 @@ class enrol_imsenterprise_plugin extends enrol_plugin { $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); + } } diff --git a/enrol/instances.php b/enrol/instances.php index 825a9e81ae1..8514a0c64f9 100644 --- a/enrol/instances.php +++ b/enrol/instances.php @@ -146,28 +146,39 @@ if ($canconfig and $action and confirm_sesskey()) { } 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); + } } } } @@ -235,7 +246,7 @@ foreach ($instances as $instance) { $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'))); diff --git a/enrol/ldap/lib.php b/enrol/ldap/lib.php index 075a8d24169..a806e99eaf1 100644 --- a/enrol/ldap/lib.php +++ b/enrol/ldap/lib.php @@ -124,6 +124,17 @@ class enrol_ldap_plugin extends enrol_plugin { 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. diff --git a/enrol/manual/lib.php b/enrol/manual/lib.php index 4066f9d25d0..5ccd1a3ff70 100644 --- a/enrol/manual/lib.php +++ b/enrol/manual/lib.php @@ -564,4 +564,15 @@ class enrol_manual_plugin extends enrol_plugin { $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); + } } diff --git a/enrol/meta/lib.php b/enrol/meta/lib.php index 30d2731e643..27f68b7762c 100644 --- a/enrol/meta/lib.php +++ b/enrol/meta/lib.php @@ -152,12 +152,22 @@ class enrol_meta_plugin extends enrol_plugin { /** * 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); + } +} diff --git a/enrol/mnet/lib.php b/enrol/mnet/lib.php index 2d3c55b3474..419228b6cc5 100644 --- a/enrol/mnet/lib.php +++ b/enrol/mnet/lib.php @@ -92,11 +92,22 @@ class enrol_mnet_plugin extends enrol_plugin { /** * 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); + } } diff --git a/enrol/paypal/lib.php b/enrol/paypal/lib.php index 71ac2e4a204..6dfe3cc076b 100644 --- a/enrol/paypal/lib.php +++ b/enrol/paypal/lib.php @@ -311,11 +311,22 @@ class enrol_paypal_plugin extends enrol_plugin { /** * 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); + } } diff --git a/enrol/self/lib.php b/enrol/self/lib.php index 37901e239bf..b65b473826d 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -651,11 +651,22 @@ class enrol_self_plugin extends enrol_plugin { /** * 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); + } } diff --git a/enrol/upgrade.txt b/enrol/upgrade.txt index 2bed0e78103..bd321bd4286 100644 --- a/enrol/upgrade.txt +++ b/enrol/upgrade.txt @@ -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 === diff --git a/lib/enrollib.php b/lib/enrollib.php index 580896cb545..2df68628087 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -1554,13 +1554,24 @@ abstract class enrol_plugin { /** * 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.