MDL-24955 improved detection of available self enrol plugins
authorPetr Skoda <skodak@moodle.org>
Wed, 10 Nov 2010 02:14:27 +0000 (02:14 +0000)
committerPetr Skoda <skodak@moodle.org>
Wed, 10 Nov 2010 02:14:27 +0000 (02:14 +0000)
lib/enrollib.php
mod/forum/post.php

index 0d930a7..8e7741c 100644 (file)
@@ -768,6 +768,33 @@ function enrol_try_internal_enrol($courseid, $userid, $roleid = null, $timestart
     return true;
 }
 
     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.
 /**
  * All enrol plugins should be based on this class,
  * this is also the main source of documentation.
index 6cb638c..3778eee 100644 (file)
@@ -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)) {
     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'));
                     $SESSION->wantsurl = $FULLME;
                     $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
                     redirect($CFG->wwwroot.'/enrol/index.php?id='.$course->id, get_string('youneedtoenrol'));