From 217d039722900892eabd2612d6fe7df4694f3e35 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Tue, 13 Jul 2010 17:04:17 +0000 Subject: [PATCH] MDL-23253 plugin now may indicate if the want "Enrol me" link in navigation --- enrol/self/lib.php | 4 +++ lib/enrollib.php | 61 +++++++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/enrol/self/lib.php b/enrol/self/lib.php index 4383537fbaf..fa29bee6ced 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -67,6 +67,10 @@ class enrol_self_plugin extends enrol_plugin { return true; } + public function show_enrolme_link(stdClass $instance) { + return ($instance->status == ENROL_INSTANCE_ENABLED); + } + /** * Returns link to page which may be used to add new instance of enrolment plugin in course. * @param int $courseid diff --git a/lib/enrollib.php b/lib/enrollib.php index b681ec444c6..4900195a5d2 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -374,23 +374,35 @@ function enrol_add_course_navigation(navigation_node $coursenode, $course) { if ($course->id != SITEID) { // Unenrol link - $unenrolprinted = false; - foreach ($instances as $instance) { - if (!isset($plugins[$instance->enrol])) { - continue; + if (is_enrolled($coursecontext)) { + foreach ($instances as $instance) { + if (!isset($plugins[$instance->enrol])) { + continue; + } + $plugin = $plugins[$instance->enrol]; + if ($unenrollink = $plugin->get_unenrolself_link($instance)) { + $coursenode->add(get_string('unenrolme', 'core_enrol', format_string($course->shortname)), $unenrollink, navigation_node::TYPE_SETTING, null, 'unenrolself', new pix_icon('i/user', '')); + break; + //TODO. deal with multiple unenrol links - not likely case, but still... + } } - $plugin = $plugins[$instance->enrol]; - if ($unenrollink = $plugin->get_unenrolself_link($instance)) { - $coursenode->add(get_string('unenrolme', 'core_enrol', format_string($course->shortname)), $unenrollink, navigation_node::TYPE_SETTING, null, 'unenrolself', new pix_icon('i/user', '')); - $unenrolprinted = true; - //TODO. deal with multiple unenrol links - not likely case, but still... + } else { + if (is_viewing($coursecontext)) { + // better not show any enrol link, this is intended for managers and inspectors + } else { + foreach ($instances as $instance) { + if (!isset($plugins[$instance->enrol])) { + continue; + } + $plugin = $plugins[$instance->enrol]; + if ($plugin->show_enrolme_link($instance)) { + $url = new moodle_url('/enrol/index.php', array('id'=>$course->id)); + $coursenode->add(get_string('enrolme', 'core_enrol', format_string($course->shortname)), $url, navigation_node::TYPE_SETTING, null, 'enrolself', new pix_icon('i/user', '')); + break; + } + } } } - // Enrol link - if (!$unenrolprinted and !is_viewing($coursecontext) and !is_enrolled($coursecontext)) { - $url = new moodle_url('/enrol/index.php', array('id'=>$course->id)); - $coursenode->add(get_string('enrolme', 'core_enrol', format_string($course->shortname)), $url, navigation_node::TYPE_SETTING, null, 'enrolself', new pix_icon('i/user', '')); - } } } @@ -785,6 +797,17 @@ abstract class enrol_plugin { return false; } + /** + * Does this plugin support some way to user to self enrol? + * + * @param stdClass $instance course enrol instance + * + * @return bool - true means show "Enrol me in this course" link in course UI + */ + public function show_enrolme_link(stdClass $instance) { + return false; + } + /** * Attempt to automatically enrol current user in course without any interaction, * calling code has to make sure the plugin and instance are active. @@ -1058,8 +1081,8 @@ abstract class enrol_plugin { /** * Returns list of unenrol links for all enrol instances in course. * - * @param int $courseid - * @return moodle_url + * @param int $instance + * @return moodle_url or NULL if self unernolmnet not supported */ public function get_unenrolself_link($instance) { global $USER, $CFG, $DB; @@ -1081,14 +1104,12 @@ abstract class enrol_plugin { return NULL; } - $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST); - $courseid = $instance->courseid; - if (!file_exists("$CFG->dirroot/enrol/$name/unenrolself.php")) { return NULL; } - $context = get_context_instance(CONTEXT_COURSE, $courseid); + $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST); + if (!has_capability("enrol/$name:unenrolself", $context)) { return NULL; } -- 2.43.0