Merge branch 'MDL-26250-final6' of github.com:mouneyrac/moodle
authorSam Hemelryk <sam@moodle.com>
Wed, 4 May 2011 10:04:10 +0000 (18:04 +0800)
committerSam Hemelryk <sam@moodle.com>
Wed, 4 May 2011 10:04:10 +0000 (18:04 +0800)
enrol/manual/db/services.php [new file with mode: 0644]
enrol/manual/externallib.php [new file with mode: 0644]
enrol/manual/lang/en/enrol_manual.php
enrol/manual/version.php
webservice/simpletest/testwebservice.php

diff --git a/enrol/manual/db/services.php b/enrol/manual/db/services.php
new file mode 100644 (file)
index 0000000..cd49d1b
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Manual plugin external functions and service definitions.
+ *
+ * @package    enrol
+ * @subpackage manual
+ * @author 2011 Jerome Mouneyrac
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+$functions = array(
+
+    // === enrol related functions ===
+    'moodle_enrol_manual_enrol_users' => array(
+        'classname'   => 'moodle_enrol_manual_external',
+        'methodname'  => 'manual_enrol_users',
+        'classpath'   => 'enrol/manual/externallib.php',
+        'description' => 'Manual enrol users',
+        'capabilities'=> 'enrol/manual:enrol',
+        'type'        => 'write',
+    ),
+
+);
diff --git a/enrol/manual/externallib.php b/enrol/manual/externallib.php
new file mode 100644 (file)
index 0000000..2edc992
--- /dev/null
@@ -0,0 +1,143 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * External course participation api.
+ *
+ * This api is mostly read only, the actual enrol and unenrol
+ * support is in each enrol plugin.
+ *
+ * @package    enrol
+ * @subpackage manual
+ * @copyright  2011 Moodle Pty Ltd (http://moodle.com)
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+defined('MOODLE_INTERNAL') || die();
+
+require_once("$CFG->libdir/externallib.php");
+
+class moodle_enrol_manual_external extends external_api {
+
+    /**
+     * Returns description of method parameters
+     * @return external_function_parameters
+     */
+    public static function manual_enrol_users_parameters() {
+        return new external_function_parameters(
+                array(
+                    'enrolments' => new external_multiple_structure(
+                            new external_single_structure(
+                                    array(
+                                        'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
+                                        'userid' => new external_value(PARAM_INT, 'The user that is going to be enrolled'),
+                                        'courseid' => new external_value(PARAM_INT, 'The course to enrol the user role in'),
+                                        'timestart' => new external_value(PARAM_INT, 'Timestamp when the enrolment start', VALUE_OPTIONAL),
+                                        'timeend' => new external_value(PARAM_INT, 'Timestamp when the enrolment end', VALUE_OPTIONAL),
+                                        'suspend' => new external_value(PARAM_INT, 'set to 1 to suspend the enrolment', VALUE_OPTIONAL)
+                                    )
+                            )
+                    )
+                )
+        );
+    }
+
+    /**
+     * Enrolment of users
+     * Function throw an exception at the first error encountered.
+     * @param array $enrolments  An array of user enrolment
+     * @return null
+     */
+    public static function manual_enrol_users($enrolments) {
+        global $DB, $CFG;
+
+        require_once($CFG->libdir . '/enrollib.php');
+
+        $params = self::validate_parameters(self::manual_enrol_users_parameters(),
+                array('enrolments' => $enrolments));
+
+        $transaction = $DB->start_delegated_transaction(); //rollback all enrolment if an error occurs
+                                                           //(except if the DB doesn't support it)
+
+        //retrieve the manual enrolment plugin
+        $enrol = enrol_get_plugin('manual');
+        if (empty($enrol)) {
+            throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual');
+        }
+
+        foreach ($params['enrolments'] as $enrolment) {
+            // Ensure the current user is allowed to run this function in the enrolment context
+            $context = get_context_instance(CONTEXT_COURSE, $enrolment['courseid']);
+            self::validate_context($context);
+
+            //check that the user has the permission to manual enrol
+            require_capability('enrol/manual:enrol', $context);
+
+            //throw an exception if user is not able to assign the role
+            $roles = get_assignable_roles($context);
+            if (!key_exists($enrolment['roleid'], $roles)) {
+                $errorparams = new stdClass();
+                $errorparams->roleid = $enrolment['roleid'];
+                $errorparams->courseid = $enrolment['courseid'];
+                $errorparams->userid = $enrolment['userid'];
+                throw new moodle_exception('wsusercannotassign', 'enrol_manual', '', $errorparams);
+            }
+
+            //check manual enrolment plugin instance is enabled/exist
+            $enrolinstances = enrol_get_instances($enrolment['courseid'], true);
+            foreach ($enrolinstances as $courseenrolinstance) {
+              if ($courseenrolinstance->enrol == "manual") {
+                  $instance = $courseenrolinstance;
+                  break;
+              }
+            }
+            if (empty($instance)) {
+              $errorparams = new stdClass();
+              $errorparams->courseid = $enrolment['courseid'];
+              throw new moodle_exception('wsnoinstance', 'enrol_manual', $errorparams);
+            }
+
+            //check that the plugin accept enrolment (it should always the case, it's hard coded in the plugin)
+            if (!$enrol->allow_enrol($instance)) {
+                $errorparams = new stdClass();
+                $errorparams->roleid = $enrolment['roleid'];
+                $errorparams->courseid = $enrolment['courseid'];
+                $errorparams->userid = $enrolment['userid'];
+                throw new moodle_exception('wscannotenrol', 'enrol_manual', '', $errorparams);
+            }
+
+            //finally proceed the enrolment
+            $enrolment['timestart'] = isset($enrolment['timestart']) ? $enrolment['timestart'] : 0;
+            $enrolment['timeend'] = isset($enrolment['timeend']) ? $enrolment['timeend'] : 0;
+            $enrolment['status'] = (isset($enrolment['suspend']) && !empty($enrolment['suspend'])) ?
+                    ENROL_USER_SUSPENDED : ENROL_USER_ACTIVE;
+
+            $enrol->enrol_user($instance, $enrolment['userid'], $enrolment['roleid'],
+                    $enrolment['timestart'], $enrolment['timeend'], $enrolment['status']);
+
+        }
+
+        $transaction->allow_commit();
+    }
+
+    /**
+     * Returns description of method result value
+     * @return external_description
+     */
+    public static function manual_enrol_users_returns() {
+        return null;
+    }
+
+}
index 84c647f..aead384 100644 (file)
@@ -53,3 +53,6 @@ $string['unenrolselectedusers'] = 'Unenrol selected users';
 $string['unenrolselfconfirm'] = 'Do you really want to unenrol yourself from course "{$a}"?';
 $string['unenroluser'] = 'Do you really want to unenrol "{$a->user}" from course "{$a->course}"?';
 $string['unenrolusers'] = 'Unenrol users';
+$string['wscannotenrol'] = 'Plugin instance cannot manually enrol a user in the course id = {$a->courseid}';
+$string['wsnoinstance'] = 'Manual enrolment plugin instance doesn\'t exist or is disabled for the course (id = {$a->courseid})';
+$string['wsusercannotassign'] = 'You don\'t have the permission to assign this role ({$a->roleid}) to this user ({$a->userid}) in this course({$a->courseid}).';
index 764b660..5f2e9e6 100644 (file)
@@ -26,4 +26,4 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version = 2010071200;
+$plugin->version = 2010071201;
index ff96cd2..cbc299c 100644 (file)
@@ -59,7 +59,7 @@ class webservice_test extends UnitTestCase {
 
     function setUp() {
         //token to test
-        $this->testtoken = '72d338d58ff881cc293f8cd1d96d7a57';
+        $this->testtoken = 'acabec9d20933913f14309785324f579';
 
         //protocols to test
         $this->testrest = false; //Does not work till XML => PHP is implemented (MDL-22965)
@@ -87,7 +87,8 @@ class webservice_test extends UnitTestCase {
             'moodle_group_add_groupmembers' => false,
             'moodle_group_delete_groupmembers' => false,
             'moodle_group_create_groups' => false,
-            'moodle_group_delete_groups' => false
+            'moodle_group_delete_groups' => false,
+            'moodle_enrol_manual_enrol_users' => false
         );
 
         //performance testing: number of time the web service are run
@@ -231,6 +232,88 @@ class webservice_test extends UnitTestCase {
         $this->assertEqual(count($users), count($userids));
     }
 
+    /**
+     * This test will:
+     * 1- create a user (core call)
+     * 2- enrol this user in the courses supporting enrolment
+     * 3- unenrol this user (core call)
+     */
+    function moodle_enrol_manual_enrol_users($client) {
+        global $DB, $CFG;
+
+        require_once($CFG->dirroot . "/user/lib.php");
+        require_once($CFG->dirroot . "/user/profile/lib.php");
+        require_once($CFG->dirroot . "/lib/enrollib.php");
+
+        //Delete some previous test data
+        if ($user = $DB->get_record('user', array('username' => 'veryimprobabletestusername2'))) {
+            $DB->delete_records('user', array('id' => $user->id));
+        }
+        if ($role = $DB->get_record('role', array('shortname' => 'role1thatshouldnotexist'))) {
+            set_role_contextlevels($role->id, array(CONTEXT_COURSE));
+            delete_role($role->id);
+        }
+
+        //create a user
+        $user = new stdClass();
+        $user->username = 'veryimprobabletestusername2';
+        $user->password = 'testpassword2';
+        $user->firstname = 'testfirstname2';
+        $user->lastname = 'testlastname2';
+        $user->email = 'testemail1@moodle.com';
+        $user->id = user_create_user($user);
+
+        $roleid = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
+        set_role_contextlevels($roleid, array(CONTEXT_COURSE));
+
+        $enrolments = array();
+        $courses = $DB->get_records('course');
+
+        foreach ($courses as $course) {
+            if ($course->id > 1) {
+                $enrolments[] = array('roleid' => $roleid,
+                    'userid' => $user->id, 'courseid' => $course->id);
+                $enrolledcourses[] = $course;
+            }
+        }
+
+        //web service call
+        $function = 'moodle_enrol_manual_enrol_users';
+        $wsparams = array('enrolments' => $enrolments);
+        $enrolmentsresult = $client->call($function, $wsparams);
+
+        //get instance that can unenrol
+        $enrols = enrol_get_plugins(true);
+        $enrolinstances = enrol_get_instances($course->id, true);
+        $unenrolled = false;
+        foreach ($enrolinstances as $instance) {
+            if (!$unenrolled and $enrols[$instance->enrol]->allow_unenrol($instance)) {
+                $unenrolinstance = $instance;
+                $unenrolled = true;
+            }
+        }
+
+        //test and unenrol the user
+        $enrolledusercourses = enrol_get_users_courses($user->id);
+        foreach ($enrolledcourses as $course) {
+            //test
+            $this->assertEqual(true, isset($enrolledusercourses[$course->id]));
+
+            //unenrol the user
+            $enrols[$unenrolinstance->enrol]->unenrol_user($unenrolinstance, $user->id, $roleid);
+        }
+
+        //delete user
+        $DB->delete_records('user', array('id' => $user->id));
+
+        //delete the context level
+        set_role_contextlevels($roleid, array(CONTEXT_COURSE));
+
+        //delete role
+        delete_role($roleid);
+    }
+
+
     function moodle_enrol_get_enrolled_users($client) {
         global $DB;