From: jerome Date: Wed, 2 Sep 2009 07:19:26 +0000 (+0000) Subject: web services MDL-12886 ws params: when the algo cleans a list of param, only the... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=46b02712fd47b8fa16f034918df0b6c88fb4eaa2;p=moodle.git 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 --- diff --git a/lib/moodleexternal.php b/lib/moodleexternal.php index 9bbd367322..c593b9613c 100644 --- a/lib/moodleexternal.php +++ b/lib/moodleexternal.php @@ -1,30 +1,39 @@ . /** - * 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 $params - */ - 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'); - } - } - else { - if (is_object($params)) { //it's an object - $this->clean_object($description, $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) { + foreach ($params as &$param) { + if (is_array($param) ) { //it's a list + $this->clean_params($description[0], $param); } - else { //it's a primary type - $params = clean_param($params, $description); + 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]); + } } } - } + /** + * 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 { } } -?> + diff --git a/user/external.php b/user/external.php index 4b0f5a2c5e..24cd49c981 100644 --- a/user/external.php +++ b/user/external.php @@ -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'); }