web services MDL-12886 ws params: when the algo cleans a list of param, only the first element of the list is cleaned (fixed) + web service description includes service name

This commit is contained in:
jerome
2009-09-02 07:19:26 +00:00
parent 72b3485eab
commit 46b02712fd
2 changed files with 63 additions and 42 deletions
+49 -37
View File
@@ -1,30 +1,39 @@
<?php
/*
* Created on 01/12/2008
*
* Moodle base webservice api
*
* @author Jerome Mouneyrac
*/
// 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/>.
/**
* DO NOT USE ANYTHING FROM THIS FILE - WORK IN PROGRESS
* moodleexternal.php - parent class of any external.php file into Moodle
*
* @package moodlecore
* @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class moodle_external {
protected $descriptions;
protected $descriptions; //the web service description of the external.php functions
/**
* Constructor - We set the description of this API in order to be access by Web service
*/
function __construct () {
$this->descriptions = array();
}
/**
*
* Return web service description for a specific function
* @param string $functionname
* @return array
* @return array the web service description of the function, return false if the function name doesn't exist
*/
public function get_function_webservice_description($functionname) {
if (key_exists($functionname, $this->descriptions)) {
@@ -36,7 +45,7 @@ abstract class moodle_external {
}
/**
*
* Return web service description for all web service functions of the external class
* @return array
*/
public function get_descriptions() {
@@ -44,7 +53,8 @@ abstract class moodle_external {
}
/**
* This function clean params, because it should only be called by external itself, it has to be protected (server should not call it)
* This function clean params,
* It's protected because we should only clean the params into external files (not server layer)
* @param array $params
*/
protected function clean_function_params($functionname, &$params) {
@@ -52,31 +62,33 @@ abstract class moodle_external {
$this->clean_object($description['params'], $params);
}
/**
*
* @param <type> $params
*/
/**
* Clean an array param
* @param array $description - an array with only one element !
* @param array $params - an array with one or several elements
*/
protected function clean_params($description, &$params) {
if (is_array($params) ) { //it's a list
$nextdescriptionkey = key($description);
if (isset($nextdescriptionkey)) {
$this->clean_params($description[$nextdescriptionkey], $params[key($params)]);
} else {
throw new moodle_exception('wswrongparams');
foreach ($params as &$param) {
if (is_array($param) ) { //it's a list
$this->clean_params($description[0], $param);
}
else {
if (is_object($param)) { //it's an object
$this->clean_object($description[0], $param);
}
else { //it's a primary type
$param = clean_param($param, $description[0]);
}
}
}
else {
if (is_object($params)) { //it's an object
$this->clean_object($description, $params);
}
else { //it's a primary type
$params = clean_param($params, $description);
}
}
}
/**
* Clean an object param
* @param object $objectdescription
* @param object $paramobject
*/
protected function clean_object($objectdescription, &$paramobject) {
foreach (get_object_vars($paramobject) as $propertyname => $propertyvalue) {
if (!isset($objectdescription->$propertyname)) { //if the param is not defined into the web service description
@@ -101,4 +113,4 @@ abstract class moodle_external {
}
}
?>
+14 -5
View File
@@ -37,7 +37,6 @@ final class user_external extends moodle_external {
*/
function __construct () {
$this->descriptions = array();
///The desciption of the web service
$user = new object();
$user->password = PARAM_ALPHANUMEXT;
@@ -62,7 +61,9 @@ final class user_external extends moodle_external {
$return->userids = array(PARAM_NUMBER);
$this->descriptions['create_users'] = array( 'params' => $params,
'optionalinformation' => 'Username, password, firstname, and username are the only mandatory',
'return' => $return);
'return' => $return,
'service' => 'user',
'requiredlogin' => 0);
$user = new object();
$user->id = PARAM_NUMBER;
@@ -87,7 +88,9 @@ final class user_external extends moodle_external {
$return->users = array($user);
$this->descriptions['get_users'] = array( 'params' => $params,
'optionalparams' => 'All params are not mandatory',
'return' => $return);
'return' => $return,
'service' => 'user',
'requiredlogin' => 0);
$params = new object();
$params->usernames = array(PARAM_ALPHANUMEXT);
@@ -95,14 +98,18 @@ final class user_external extends moodle_external {
$return->result = PARAM_BOOL;
$this->descriptions['delete_users'] = array( 'params' => $params,
'optionalparams' => 'All params are not mandatory',
'return' => $return);
'return' => $return,
'service' => 'user',
'requiredlogin' => 0);
$user->newusername = PARAM_ALPHANUMEXT;
$params = new object();
$params->users = array($user);
$this->descriptions['update_users'] = array( 'params' => $params,
'optionalparams' => 'All params are not mandatory',
'return' => $return);
'return' => $return,
'service' => 'user',
'requiredlogin' => 0);
}
/**
@@ -160,8 +167,10 @@ final class user_external extends moodle_external {
if (has_capability('moodle/user:delete', get_context_instance(CONTEXT_SYSTEM))) {
$this->clean_function_params('delete_users', $params);
foreach ($params->usernames as $username) {
$user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>1));
if (empty($user)) {
throw new moodle_exception('wscouldnotdeletenoexistinguser');
}