]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-12886 one more external group function, other minor fixes
authorskodak <skodak>
Thu, 22 Oct 2009 23:07:21 +0000 (23:07 +0000)
committerskodak <skodak>
Thu, 22 Oct 2009 23:07:21 +0000 (23:07 +0000)
group/externallib.php
lang/en_utf8/webservice.php
lib/db/services.php
version.php
webservice/testclient.php
webservice/testclient_forms.php

index a0af9d474153c7a21324ab68b7b8dfa6be7a4e1f..800f12ec4bbec3507a050b83cbd8a4df24d22d87 100644 (file)
@@ -30,59 +30,95 @@ class moodle_group_external extends external_api {
 
     /**
      * Returns description of method parameters
-     * @return ?
+     * @return external_function_parameters
      */
     public static function create_groups_parameters() {
-        //TODO
+        return new external_function_parameters(
+            array(
+                'groups' => new external_multiple_structure(
+                    new external_single_structure(
+                        array(
+                            'courseid' => new external_value(PARAM_INT, 'id of course'),
+                            'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
+                            'description' => new external_value(PARAM_RAW, 'group description text'),
+                            'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
+                        )
+                    )
+                )
+            )
+        );
     }
 
     /**
      * Create groups
      * @param array $groups array of group description arrays (with keys groupname and courseid)
-     * @return array of newly created group ids
+     * @return array of newly created groups
      */
     public static function create_groups($groups) {
-        global $CFG;
+        global $CFG, $DB;
         require_once("$CFG->dirroot/group/lib.php");
 
         $params = self::validate_parameters(self::create_groups_parameters(), array('groups'=>$groups));
 
-        $groups = array();
-
-        foreach ($params['groups'] as $group) {
-            $group = (object)$group;
-
-            if (trim($group->name) == '') {
-                throw new invalid_parameter_exception('Invalid group name');
-            }
-            if ($DB->get_record('groups', array('courseid'=>$group->courseid, 'name'=>$group->name))) {
-                throw new invalid_parameter_exception('Group with the same name already exists in the course');
+        // ideally create all groups or none at all, unfortunately myisam engine does not support transactions :-(
+        $DB->begin_sql();
+        try {
+            $groups = array();
+
+            foreach ($params['groups'] as $group) {
+                $group = (object)$group;
+
+                if (trim($group->name) == '') {
+                    throw new invalid_parameter_exception('Invalid group name');
+                }
+                if ($DB->get_record('groups', array('courseid'=>$group->courseid, 'name'=>$group->name))) {
+                    throw new invalid_parameter_exception('Group with the same name already exists in the course');
+                }
+
+                // now security checks
+                $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
+                self::validate_context($context);
+                require_capability('moodle/course:managegroups', $context);
+
+                // finally create the group
+                $group->id = groups_create_group($group, false);
+                $groups[] = (array)$group;
             }
-
-            // now security checks
-            $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
-            self::validate_context($context);
-            require_capability('moodle/course:managegroups', $context);
-
-            $group->id = groups_create_group($group, false);
-            $groups[] = (array)$group;
+        } catch (Exception $ex) {
+            $DB->rollback_sql();
+            throw $ex;
         }
+        $DB->commit_sql();
 
         return $groups;
     }
 
    /**
      * Returns description of method result value
-     * @return ?
+     * @return external_description
      */
     public static function create_groups_returns() {
-        //TODO
+        return new external_multiple_structure(
+            new external_single_structure(
+                array(
+                    'id' => new external_value(PARAM_INT, 'group record id'),
+                    'courseid' => new external_value(PARAM_INT, 'id of course'),
+                    'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
+                    'description' => new external_value(PARAM_RAW, 'group description text'),
+                    'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
+                )
+            )
+        );
     }
 
+    /**
+     * Returns description of method parameters
+     * @return external_function_parameters
+     */
     public static function get_groups_parameters() {
         return new external_function_parameters(
             array(
-                'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID'))
+                'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')),
             )
         );
     }
@@ -97,9 +133,6 @@ class moodle_group_external extends external_api {
 
         $params = self::validate_parameters(self::get_groups_parameters(), array('groupids'=>$groupids));
 
-        //TODO: we do need to search for groups in courses too,
-        //      fetching by id is not enough!
-
         foreach ($params['groupids'] as $groupid) {
             // validate params
             $group = groups_get_group($groupid, 'id, courseid, name, description, enrolmentkey', MUST_EXIST);
@@ -115,14 +148,19 @@ class moodle_group_external extends external_api {
         return $groups;
     }
 
+   /**
+     * Returns description of method result value
+     * @return external_description
+     */
     public static function get_groups_returns() {
         return new external_multiple_structure(
             new external_single_structure(
                 array(
-                    'id' => new external_value(PARAM_INT, 'some group id'),
+                    'id' => new external_value(PARAM_INT, 'group record id'),
+                    'courseid' => new external_value(PARAM_INT, 'id of course'),
                     'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
-                    'description' => new external_value(PARAM_RAW, 'just some text'),
-                    'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase')
+                    'description' => new external_value(PARAM_RAW, 'group description text'),
+                    'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
                 )
             )
         );
index ae122ab3a0c6dd37ce3a138bfe7a50124056bc42..624fc9894410d6c4f724f5861bddbe1d9e30463d 100644 (file)
@@ -9,6 +9,8 @@ $string['configwebserviceplugins'] = 'For security reasons enable only protocols
 $string['deleteserviceconfirm'] = 'Do you really want to delete external service \"$a\"?';
 $string['disabledwarning'] = 'All webs service protocols are disabled, the \Enable web services\" setting can be found in the \"Advanced features\" section.';
 $string['enabled'] = 'Enabled';
+$string['execute'] = 'Execute';
+$string['executewarnign'] = 'WARNING: if you press execute your database will be modified and changes can not be reverted automatically!';
 $string['externalservices'] = 'External services';
 $string['externalservice'] = 'External service';
 $string['externalservicefunctions'] = 'External service functions';
@@ -30,7 +32,6 @@ $string['servicescustom'] = 'Custom services';
 $string['serviceusers'] = 'Authorised users';
 $string['serviceusersmatching'] = 'Authorised users matching';
 $string['serviceuserssettings'] = 'Change settings for the authorised users';
-$string['test'] = 'Test';
 $string['testclient'] = 'Web service test client';
 $string['validuntil'] = 'Valid until';
 $string['webservices'] = 'Web services';
index 25e582430e64b768a038c3818ae8bcac2aa9fb89..72a30600c38fe83298ddec8119c96ae15052efad 100644 (file)
 $functions = array(
 
     // === group related functions ===
-/*
+
     'moodle_group_create_groups' => array(
         'classname'   => 'moodle_group_external',
         'methodname'  => 'create_groups',
         'classpath'   => 'group/externallib.php',
+        'description' => 'Creates new groups.',
+        'type'        => 'write',
     ),
-*/
+
     'moodle_group_get_groups' => array(
         'classname'   => 'moodle_group_external',
         'methodname'  => 'get_groups',
         'classpath'   => 'group/externallib.php',
         'description' => 'Returns group details.',
+        'type'        => 'read',
     ),
 /*
     'moodle_group_delete_groups' => array(
index 635b87e79f2765629ed5b66d0e0d934bd059d94c..8ef71103da9bbef922c4d5e3603cc23c9e36317c 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2009102100;  // YYYYMMDD   = date of the last version bump
+    $version = 2009102200;  // YYYYMMDD   = date of the last version bump
                             //         XX = daily increments
 
     $release = '2.0 dev (Build: 20091022)';  // Human-friendly version name
index 38a715ce0553a67907c30665c0d78e12db84cbd8..c2e6829fff277f61db9fc3003234415a1446cb36 100644 (file)
@@ -36,7 +36,7 @@ require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
 
 // list of all available functions for testing - please note there must be explicit
 // support for testing of each functions, the parameter conversion and form is hardcoded
-$functions = array('moodle_group_get_groups');
+$functions = array('moodle_group_create_groups', 'moodle_group_get_groups');
 $functions = array_combine($functions, $functions);
 if (!isset($functions[$function])) { // whitelisting security
     $function = '';
@@ -96,7 +96,11 @@ if ($mform->is_cancelled()) {
 
     // now get the function parameters - each functions processing must be hardcoded here
     $params = array();
-    if ($function === 'moodle_group_get_groups') {
+    if ($function === 'moodle_group_create_groups') {
+        $params['groups'] = array();
+        $params['groups'][] = (array)$data;
+
+    } else if ($function === 'moodle_group_get_groups') {
         $params['groupids'] = array();
         for ($i=0; $i<10; $i++) {
             if (empty($data->groupids[$i])) {
@@ -118,7 +122,7 @@ if ($mform->is_cancelled()) {
 
     try {
         $response = $testclient->simpletest($serverurl, $function, $params);
-        echo str_replace("\n", '<br />', s(var_export($response, true)));
+        echo str_replace("\n", '<br />', s($response));
     } catch (Exception $ex) {
         //TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces!
         echo str_replace("\n", '<br />', s($ex));
index ea0a6ca8d2618ba0077ecab8f14a8f270f8e025e..0221e9974c432ea91c0849b5e91e4663b00c146e 100644 (file)
@@ -20,6 +20,8 @@ class webservice_test_client_form extends moodleform {
     }
 }
 
+// === Test client forms ===
+
 class moodle_group_get_groups_form extends moodleform {
     public function definition() {
         global $CFG;
@@ -42,6 +44,34 @@ class moodle_group_get_groups_form extends moodleform {
         $mform->addElement('hidden', 'protocol');
         $mform->setType('protocol', PARAM_SAFEDIR);
 
-        $this->add_action_buttons(true, get_string('test', 'webservice'));
+        $this->add_action_buttons(true, get_string('execute', 'webservice'));
     }
-}
\ No newline at end of file
+}
+
+class moodle_group_create_groups_form extends moodleform {
+    public function definition() {
+        global $CFG;
+
+        $mform = $this->_form;
+
+        $mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
+
+        //note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters
+        $mform->addElement('text', 'wsusername', 'wsusername');
+        $mform->addElement('text', 'wspassword', 'wspassword');
+        $mform->addElement('text', 'courseid', 'courseid');
+        $mform->addElement('text', 'name', 'name');
+        $mform->addElement('text', 'description', 'description');
+        $mform->addElement('text', 'enrolmentkey', 'enrolmentkey');
+
+        $mform->addElement('hidden', 'function');
+        $mform->setType('function', PARAM_SAFEDIR);
+
+        $mform->addElement('hidden', 'protocol');
+        $mform->setType('protocol', PARAM_SAFEDIR);
+
+        $mform->addElement('static', 'warning', '', get_string('executewarnign', 'webservice'));
+
+        $this->add_action_buttons(true, get_string('execute', 'webservice'));
+    }
+}