]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-12886 refactoring: improved test client forms support; fixing external group...
authorPetr Skoda <skodak@moodle.org>
Tue, 27 Oct 2009 09:27:34 +0000 (09:27 +0000)
committerPetr Skoda <skodak@moodle.org>
Tue, 27 Oct 2009 09:27:34 +0000 (09:27 +0000)
group/externallib.php
lib/db/services.php
version.php
webservice/testclient.php
webservice/testclient_forms.php

index c6fec61b6422df2b81d537cec8af88dc823f47ca..e58bf70edd793f3519ea9e621efa0d9f0f8f57fe 100644 (file)
@@ -237,7 +237,7 @@ class moodle_group_external extends external_api {
      * @return void
      */
     public static function delete_groups($groupids) {
-        global $CFG;
+        global $CFG, $DB;
         require_once("$CFG->dirroot/group/lib.php");
 
         $params = self::validate_parameters(self::delete_groups_parameters(), array('groupids'=>$groupids));
@@ -352,7 +352,7 @@ class moodle_group_external extends external_api {
      * @return void
      */
     public static function add_groupmembers($members) {
-        global $CFG;
+        global $CFG, $DB;
         require_once("$CFG->dirroot/group/lib.php");
 
         $params = self::validate_parameters(self::add_groupmembers_parameters(), array('members'=>$members));
@@ -412,7 +412,7 @@ class moodle_group_external extends external_api {
      * @return void
      */
     public static function delete_groupmembers($members) {
-        global $CFG;
+        global $CFG, $DB;
         require_once("$CFG->dirroot/group/lib.php");
 
         $params = self::validate_parameters(self::delete_groupmembers_parameters(), array($members=>'members'));
index 18111ccb7c7162dec50c4c146f7e5ce28dc226a3..c0acfbd57dce22bac4474a127539915810ef3b0e 100644 (file)
@@ -51,7 +51,7 @@ $functions = array(
         'description' => 'Returns all groups in specified course.',
         'type'        => 'read',
     ),
-/*
+
     'moodle_group_delete_groups' => array(
         'classname'   => 'moodle_group_external',
         'methodname'  => 'delete_groups',
@@ -59,7 +59,7 @@ $functions = array(
         'description' => 'Deletes all specified groups.',
         'type'        => 'delete',
     ),
-
+/*
     'moodle_group_get_groupmembers' => array(
         'classname'   => 'moodle_group_external',
         'methodname'  => 'get_groupmembers',
index 667a99b778db24d98afb2b532dacbd071299e34b..b8e5c7b55b63942f23befc5e1930bf2cb326af90 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 = 2009102600;  // YYYYMMDD   = date of the last version bump
+    $version = 2009102700;  // YYYYMMDD   = date of the last version bump
                             //         XX = daily increments
 
     $release = '2.0 dev (Build: 20091027)';  // Human-friendly version name
index 22126378abbd75cebd3026f9b1d6ef8a0c009bbc..f4f0cb3e6d9415d25753012a6d3d45a2943127b0 100644 (file)
@@ -20,6 +20,7 @@
  *
  * @package   webservice
  * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
+ * @author    Petr Skoda (skodak)
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
@@ -35,12 +36,19 @@ $PAGE->set_url('webservice/testclient.php');
 require_login();
 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); // TODO: do we need some new capability?
 
-// 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
-// TODO: automate this list by fetching all known functiosn from db and looking if client form defined
-$functions = array('moodle_group_create_groups', 'moodle_group_get_groups', 'moodle_group_get_course_groups');
-$functions = array_combine($functions, $functions);
-if (!isset($functions[$function])) { // whitelisting security
+// list of all available functions for testing
+$allfunctions = $DB->get_records('external_functions', array(), 'name ASC');
+$functions = array();
+foreach ($allfunctions as $f) {
+    $class = $f->name.'_form';
+    if (class_exists($class)) {
+        $functions[$f->name] = $f->name;
+        continue;
+    }
+}
+
+// whitelisting security
+if (!isset($functions[$function])) {
     $function = '';
 }
 
@@ -83,11 +91,6 @@ if ($mform->is_cancelled()) {
 } else if ($data = $mform->get_data()) {
 
     $functioninfo = external_function_info($function);
-    
-    // remove unused from form data
-    unset($data->submitbutton);
-    unset($data->protocol);
-    unset($data->function);
 
     // first load lib of selected protocol
     require_once("$CFG->dirroot/webservice/$protocol/locallib.php");
@@ -100,31 +103,10 @@ if ($mform->is_cancelled()) {
 
     $serverurl = "$CFG->wwwroot/webservice/$protocol/simpleserver.php";
     $serverurl .= '?wsusername='.urlencode($data->wsusername);
-    unset($data->wsusername);
     $serverurl .= '&wspassword='.urlencode($data->wspassword);
-    unset($data->wspassword);
-
-    // now get the function parameters - each functions processing must be hardcoded here
-    $params = array();
-    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])) {
-                continue;
-            }
-            $params['groupids'][] = $data->groupids[$i];
-        }
-
-    } else if ($function === 'moodle_group_get_course_groups') {
-        $params['courseid'] = $data->courseid;
-
-    } else {
-        throw new coding_exception('Testing of function '.$function.' not implemented yet!');
-    }
+
+    // now get the function parameters
+    $params = $mform->get_params();
 
     // now test the parameters, this also fixes PHP data types
     $params = external_api::validate_parameters($functioninfo->parameters_desc, $params);
index 08ddef3a3a04e81fd822e941cfb584007663e9f3..687db3cedd2d72541922c8105f68d6b0bc0ece78 100644 (file)
@@ -48,6 +48,24 @@ class moodle_group_create_groups_form extends moodleform {
 
         $this->add_action_buttons(true, get_string('execute', 'webservice'));
     }
+
+    public function get_params() {
+        if (!$data = $this->get_data()) {
+            return null;
+        }
+        // remove unused from form data
+        unset($data->submitbutton);
+        unset($data->protocol);
+        unset($data->function);
+        unset($data->wsusername);
+        unset($data->wspassword);
+
+        $params = array();
+        $params['groups'] = array();
+        $params['groups'][] = (array)$data;
+
+        return $params;
+    }
 }
 
 class moodle_group_get_groups_form extends moodleform {
@@ -74,6 +92,29 @@ class moodle_group_get_groups_form extends moodleform {
 
         $this->add_action_buttons(true, get_string('execute', 'webservice'));
     }
+
+    public function get_params() {
+        if (!$data = $this->get_data()) {
+            return null;
+        }
+        // remove unused from form data
+        unset($data->submitbutton);
+        unset($data->protocol);
+        unset($data->function);
+        unset($data->wsusername);
+        unset($data->wspassword);
+
+        $params = array();
+        $params['groupids'] = array();
+        for ($i=0; $i<10; $i++) {
+            if (empty($data->groupids[$i])) {
+                continue;
+            }
+            $params['groupids'][] = $data->groupids[$i];
+        }
+
+        return $params;
+    }
 }
 
 class moodle_group_get_course_groups_form extends moodleform {
@@ -97,5 +138,72 @@ class moodle_group_get_course_groups_form extends moodleform {
 
         $this->add_action_buttons(true, get_string('execute', 'webservice'));
     }
+
+    public function get_params() {
+        if (!$data = $this->get_data()) {
+            return null;
+        }
+        // remove unused from form data
+        unset($data->submitbutton);
+        unset($data->protocol);
+        unset($data->function);
+        unset($data->wsusername);
+        unset($data->wspassword);
+
+        $params = array();
+        $params['courseid'] = $data->courseid;
+
+        return $params;
+    }
 }
 
+class moodle_group_delete_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', 'groupids[0]', 'groupids[0]');
+        $mform->addElement('text', 'groupids[1]', 'groupids[1]');
+        $mform->addElement('text', 'groupids[2]', 'groupids[2]');
+        $mform->addElement('text', 'groupids[3]', 'groupids[3]');
+
+        $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'));
+    }
+
+    public function get_params() {
+        if (!$data = $this->get_data()) {
+            return null;
+        }
+        // remove unused from form data
+        unset($data->submitbutton);
+        unset($data->protocol);
+        unset($data->function);
+        unset($data->wsusername);
+        unset($data->wspassword);
+
+        $params = array();
+        $params['groupids'] = array();
+        for ($i=0; $i<10; $i++) {
+            if (empty($data->groupids[$i])) {
+                continue;
+            }
+            $params['groupids'][] = $data->groupids[$i];
+        }
+
+        return $params;
+    }
+}