MDL-40127 Enrolments: Abstracted self_enrolment code so as to be used by webservices
authorRajesh Taneja <rajesh@moodle.com>
Fri, 14 Jun 2013 07:35:53 +0000 (15:35 +0800)
committerDan Poltawski <dan@moodle.com>
Tue, 2 Jul 2013 06:35:33 +0000 (14:35 +0800)
enrol/self/lang/en/enrol_self.php
enrol/self/lib.php
enrol/self/locallib.php
lib/enrollib.php

index 4203cde..fce9339 100644 (file)
@@ -22,6 +22,7 @@
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+$string['canntenrol'] = 'Enrolment is disabled or inactive';
 $string['cohortnonmemberinfo'] = 'Only members of cohort \'{$a}\' can self-enrol.';
 $string['cohortonly'] = 'Only cohort members';
 $string['cohortonly_help'] = 'Self enrolment may be restricted to members of a specified cohort only. Note that changing this setting has no effect on existing enrolments.';
index e026bab..6dee05e 100644 (file)
@@ -182,6 +182,50 @@ class enrol_self_plugin extends enrol_plugin {
         return new moodle_url('/enrol/self/edit.php', array('courseid'=>$courseid));
     }
 
+    /**
+     * Self enrol user to course
+     *
+     * @param stdClass $instance enrolment instance
+     * @param stdClass $data data needed for enrolment.
+     * @return bool|array true if enroled else eddor code and messege
+     */
+    public function enrol_self(stdClass $instance, $data = null) {
+        global $DB, $USER;
+
+        // Don't enrol user if password is not passed when required.
+        if ($instance->password && !isset($data->enrolpassword)) {
+            return;
+        }
+
+        $timestart = time();
+        if ($instance->enrolperiod) {
+            $timeend = $timestart + $instance->enrolperiod;
+        } else {
+            $timeend = 0;
+        }
+
+        $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
+        add_to_log($instance->courseid, 'course', 'enrol', '../enrol/users.php?id='.$instance->courseid, $instance->courseid); //TODO: There should be userid somewhere!
+
+        if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) {
+            // it must be a group enrolment, let's assign group too
+            $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey');
+            foreach ($groups as $group) {
+                if (empty($group->enrolmentkey)) {
+                    continue;
+                }
+                if ($group->enrolmentkey === $data->enrolpassword) {
+                    groups_add_member($group->id, $USER->id);
+                    break;
+                }
+            }
+        }
+        // Send welcome message.
+        if ($instance->customint4) {
+            $this->email_welcome_message($instance, $USER);
+        }
+    }
+
     /**
      * Creates course enrol form, checks if form submitted
      * and enrols user if necessary. It can also redirect.
@@ -190,88 +234,105 @@ class enrol_self_plugin extends enrol_plugin {
      * @return string html text, usually a form in a text box
      */
     public function enrol_page_hook(stdClass $instance) {
-        global $CFG, $OUTPUT, $SESSION, $USER, $DB;
+        global $CFG, $OUTPUT, $USER;
+
+        require_once("$CFG->dirroot/enrol/self/locallib.php");
+
+        $enrolstatus = $this->can_self_enrol($instance);
+
+        $form = new enrol_self_enrol_form(NULL, array('instance' => $instance, 'enrolstatus' => $enrolstatus));
+        $instanceid = optional_param('instance', 0, PARAM_INT);
+        if ($instance->id == $instanceid) {
+            if ($data = $form->get_data()) {
+                $this->enrol_self($instance, $data);
+            }
+        }
+
+        ob_start();
+        $form->display();
+        $output = ob_get_clean();
+
+        return $OUTPUT->box($output);
+    }
+
+    /**
+     * Checks if user can self enrol.
+     *
+     * @param stdClass $instance enrolment instance
+     * @return bool|array true if successful, else error code and message.
+     */
+    public function can_self_enrol(stdClass $instance) {
+        global $DB, $USER;
 
         if (isguestuser()) {
             // Can not enrol guest!!
-            return null;
-        }
-        if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
-            //TODO: maybe we should tell them they are already enrolled, but can not access the course
-            return null;
+            return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
         }
 
         if ($instance->enrolstartdate != 0 and $instance->enrolstartdate > time()) {
             //TODO: inform that we can not enrol yet
-            return null;
+            return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
         }
 
         if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) {
             //TODO: inform that enrolment is not possible any more
-            return null;
+            return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
         }
 
         if (!$instance->customint6) {
             // New enrols not allowed.
-            return null;
+            return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
+        }
+
+        if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
+            return array('canntenrol' => get_string('canntenrol', 'enrol_self'));
+        }
+
+        if ($instance->customint3 > 0) {
+            // Max enrol limit specified.
+            $count = $DB->count_records('user_enrolments', array('enrolid' => $instance->id));
+            if ($count >= $instance->customint3) {
+                // Bad luck, no more self enrolments here.
+                return array('maxenrolledreached' => get_string('maxenrolledreached', 'enrol_self'));
+            }
         }
 
         if ($instance->customint5) {
             require_once("$CFG->dirroot/cohort/lib.php");
             if (!cohort_is_member($instance->customint5, $USER->id)) {
-                $cohort = $DB->get_record('cohort', array('id'=>$instance->customint5));
+                $cohort = $DB->get_record('cohort', array('id' => $instance->customint5));
                 if (!$cohort) {
                     return null;
                 }
-                $a = format_string($cohort->name, true, array('context'=>context::instance_by_id($cohort->contextid)));
-                return $OUTPUT->box(markdown_to_html(get_string('cohortnonmemberinfo', 'enrol_self', $a)));
+                $a = format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid)));
+                return array('cohortnonmemberinfo' => markdown_to_html(get_string('cohortnonmemberinfo', 'enrol_self', $a)));
             }
         }
 
-        require_once("$CFG->dirroot/enrol/self/locallib.php");
-        require_once("$CFG->dirroot/group/lib.php");
+        return true;
+    }
 
-        $form = new enrol_self_enrol_form(NULL, $instance);
-        $instanceid = optional_param('instance', 0, PARAM_INT);
+    /**
+     * Return information for enrolment instance containing list of parameters required
+     * for enrolment, name of enrolment plugin etc.
+     *
+     * @param stdClass $instance enrolment instance
+     * @return stdClass instance info.
+     */
+    public function get_enrol_info(stdClass $instance) {
 
-        if ($instance->id == $instanceid) {
-            if ($data = $form->get_data()) {
-                $enrol = enrol_get_plugin('self');
-                $timestart = time();
-                if ($instance->enrolperiod) {
-                    $timeend = $timestart + $instance->enrolperiod;
-                } else {
-                    $timeend = 0;
-                }
+        $instanceinfo = new stdClass();
+        $instanceinfo->id = $instance->id;
+        $instanceinfo->courseid = $instance->courseid;
+        $instanceinfo->type = $this->get_name();
+        $instanceinfo->name = $this->get_instance_name($instance);
 
-                $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
-                add_to_log($instance->courseid, 'course', 'enrol', '../enrol/users.php?id='.$instance->courseid, $instance->courseid); //TODO: There should be userid somewhere!
-
-                if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) {
-                    // it must be a group enrolment, let's assign group too
-                    $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey');
-                    foreach ($groups as $group) {
-                        if (empty($group->enrolmentkey)) {
-                            continue;
-                        }
-                        if ($group->enrolmentkey === $data->enrolpassword) {
-                            groups_add_member($group->id, $USER->id);
-                            break;
-                        }
-                    }
-                }
-                // Send welcome message.
-                if ($instance->customint4) {
-                    $this->email_welcome_message($instance, $USER);
-                }
-            }
+        if ($instance->password) {
+            $instanceinfo->requiredparam = new stdClass();
+            $instanceinfo->requiredparam->enrolpassword = get_string('password', 'enrol_self');
         }
 
-        ob_start();
-        $form->display();
-        $output = ob_get_clean();
-
-        return $OUTPUT->box($output);
+        return $instanceinfo;
     }
 
     /**
index df78a2c..e007738 100644 (file)
@@ -36,30 +36,24 @@ class enrol_self_enrol_form extends moodleform {
      * @return string form identifier
      */
     protected function get_form_identifier() {
-        $formid = $this->_customdata->id.'_'.get_class($this);
+        $formid = $this->_customdata['instance']->id.'_'.get_class($this);
         return $formid;
     }
 
     public function definition() {
-        global $DB;
-
         $mform = $this->_form;
-        $instance = $this->_customdata;
+        $instance = $this->_customdata['instance'];
         $this->instance = $instance;
         $plugin = enrol_get_plugin('self');
 
         $heading = $plugin->get_instance_name($instance);
         $mform->addElement('header', 'selfheader', $heading);
 
-        if ($instance->customint3 > 0) {
-            // Max enrol limit specified.
-            $count = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
-            if ($count >= $instance->customint3) {
-                // Bad luck, no more self enrolments here.
-                $this->toomany = true;
-                $mform->addElement('static', 'notice', '', get_string('maxenrolledreached', 'enrol_self'));
-                return;
-            }
+        if (true !== $this->_customdata['enrolstatus']) {
+            // If enrol status then show message, else show general error message.
+            $statusmsg = array_values($this->_customdata['enrolstatus']);
+            $mform->addElement('static', 'notice', '', $statusmsg[0]);
+            return;
         }
 
         if ($instance->password) {
index db808f6..35fe766 100644 (file)
@@ -1710,6 +1710,27 @@ abstract class enrol_plugin {
         return null;
     }
 
+    /**
+     * Checks if user can self enrol.
+     *
+     * @param stdClass $instance enrolment instance
+     * @return bool true if current user can self enrol, else error code and message.
+     */
+    public function can_self_enrol(stdClass $instance) {
+        return false;
+    }
+
+    /**
+     * Return information for enrolment instance containing list of parameters required
+     * for enrolment, name of enrolment plugin etc.
+     *
+     * @param stdClass $instance enrolment instance
+     * @return array instance info.
+     */
+    public function get_enrol_info(stdClass $instance) {
+        return null;
+    }
+
     /**
      * Adds navigation links into course admin block.
      *