<?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)) {
}
/**
- *
+ * Return web service description for all web service functions of the external class
* @return array
*/
public function get_descriptions() {
}
/**
- * 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) {
$this->clean_object($description['params'], $params);
}
- /**
- *
- * @param <type> $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
}
}
-?>
+
*/
function __construct () {
$this->descriptions = array();
- ///The desciption of the web service
$user = new object();
$user->password = PARAM_ALPHANUMEXT;
$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;
$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);
$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);
}
/**
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');
}