From 45ff8a80d31b0506c29149a58b4bac2d774da96b Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Wed, 10 Nov 2010 02:14:27 +0000 Subject: [PATCH 1/1] MDL-24955 improved detection of available self enrol plugins --- lib/enrollib.php | 27 +++++++++++++++++++++++++++ mod/forum/post.php | 15 +-------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/enrollib.php b/lib/enrollib.php index 0d930a751f7..8e7741c915b 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -768,6 +768,33 @@ function enrol_try_internal_enrol($courseid, $userid, $roleid = null, $timestart return true; } +/** + * Is there a chance users might self enrol + * @param int $courseid + * @return bool + */ +function enrol_selfenrol_available($courseid) { + $result = false; + + $plugins = enrol_get_plugins(true); + $enrolinstances = enrol_get_instances($courseid, true); + foreach($enrolinstances as $instance) { + if (!isset($plugins[$instance->enrol])) { + continue; + } + if ($instance->enrol === 'guest') { + // blacklist known temporary guest plugins + continue; + } + if ($plugins[$instance->enrol]->show_enrolme_link($instance)) { + $result = true; + break; + } + } + + return $result; +} + /** * All enrol plugins should be based on this class, * this is also the main source of documentation. diff --git a/mod/forum/post.php b/mod/forum/post.php index 6cb638cf5d1..3778eeef844 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -111,20 +111,7 @@ if (!empty($forum)) { // User is starting a new discussion in a forum if (! forum_user_can_post_discussion($forum, $groupid, -1, $cm)) { if (!isguestuser()) { if (!is_enrolled($coursecontext)) { - //note: this is a bloody hack, make sure there is at least one enrol - // plugin that allows them to self enrol... - $enrolinstances = enrol_get_instances($course->id, true); - - $somethingprobablyusefulforselfenrol = false; - foreach($enrolinstances as $instance) { - if ($instance->enrol === 'self' or $instance->enrol === 'paypal') { - $somethingprobablyusefulforselfenrol = true; - break; - } - } - unset($enrolinstances); - - if ($somethingprobablyusefulforselfenrol) { + if (enrol_selfenrol_available($course->id)) { $SESSION->wantsurl = $FULLME; $SESSION->enrolcancel = $_SERVER['HTTP_REFERER']; redirect($CFG->wwwroot.'/enrol/index.php?id='.$course->id, get_string('youneedtoenrol')); -- 2.43.0