. //
// //
///////////////////////////////////////////////////////////////////////////
/**
* This file generate a web service documentation in HTML
* This documentation describe how to call a Moodle Web Service
*/
require_once('../config.php');
require_once('lib.php');
$protocol = optional_param('protocol',"soap",PARAM_ALPHA);
/// PAGE settings
$PAGE->set_course($COURSE);
$PAGE->set_url('webservice/wsdoc.php');
$PAGE->set_title(get_string('wspagetitle', 'webservice'));
$PAGE->set_heading(get_string('wspagetitle', 'webservice'));
$PAGE->set_generaltype("form");
echo $OUTPUT->header();
webservice_lib::display_webservices_availability($protocol);
generate_documentation($protocol);
generate_functionlist();
echo $OUTPUT->footer();
/**
* Generate documentation specific to a protocol
* @param string $protocol
*/
function generate_documentation($protocol) {
switch ($protocol) {
case "soap":
$documentation = get_string('soapdocumentation','webservice');
break;
case "xmlrpc":
$documentation = get_string('xmlrpcdocumentation','webservice');
break;
default:
break;
}
echo $documentation;
echo "".get_string('wsuserreminder','webservice')."";
}
/**
* Generate web service function list
* @global object $CFG
*/
function generate_functionlist () {
global $CFG;
$documentation = "
".get_string('functionlist','webservice')."
";
//retrieve all external file
$externalfiles = array();
$externalfunctions = array();
webservice_lib::setListApiFiles($externalfiles, $CFG->dirroot);
foreach ($externalfiles as $file) {
require($file);
$classpath = substr($file,strlen($CFG->dirroot)+1); //remove the dir root + / from the file path
$classpath = substr($classpath,0,strlen($classpath) - 13); //remove /external.php from the classpath
$classpath = str_replace('/','_',$classpath); //convert all / into _
$classname = $classpath."_external";
$api = new $classname();
$documentation .= "".get_string('moodlepath','webservice').": ".$classpath."
";
if ($classname == "user_external") {
$description = $api->get_descriptions();
var_dump("
");
convertDescriptionType($description);
var_dump("");
foreach ($description as $functionname => $functiondescription) {
$documentation .= <<{$functionname}(
EOF;
$arrayparams = array();
$comma="";
//TODO: this is not an array anymore!!! (all algo function)
foreach($functiondescription['params'] as $param => $type) {
$documentation .= <<{$comma} {$type} {$param}
EOF;
if (empty($comma)) {
$comma = ',';
}
}
$documentation .= <<) :
EOF;
if (array_key_exists('return', $functiondescription)) {
foreach($functiondescription['return'] as $return => $type) {
$documentation .= <<
{$type}
EOF;
if (is_array($type)) {
$arraytype = "".print_r($type, true)."
";
$documentation .= <<{$return}
{$return} {$arraytype}
EOF;
}
}
}
$documentation .= <<
EOF;
foreach($functiondescription['params'] as $param => $type) {
if (is_array($type)) {
$arraytype = "".print_r($type, true)."
";
$documentation .= <<{$param} : {$arraytype}
EOF;
}
else {
$documentation .= <<{$param} : {$type}
EOF;
}
}
$documentation .= <<
EOF;
}
}
$documentation .= <<
EOF;
}
echo $documentation;
}
function convertDescriptionType(&$description) {
foreach ($description as &$type) {
if (is_array($type)) { //is it a List ?
convertDescriptionType($type);
}
else {
if (is_object($type)) { //is it a object
convertObjectTypes($type);
}
else { //it's a primary type
$type = converterMoodleParamIntoWsParam($type);
}
}
}
}
function convertObjectTypes(&$type) {
foreach (get_object_vars($type) as $propertyname => $propertytype) { //browse all properties of the object
if (is_array($propertytype)) { //the property is an array
convertDescriptionType($propertytype);
$type->$propertyname = $propertytype;
} else if (is_object($propertytype)) { //the property is an object
convertObjectTypes($propertytype);
$type->$propertyname = $propertytype;
}
else { //the property is a primary type
$type->$propertyname = converterMoodleParamIntoWsParam($propertytype);
}
}
}
/**
* Convert a Moodle type (PARAM_ALPHA, PARAM_NUMBER,...) as a SOAP type (string, interger,...)
* @param integer $moodleparam
* @return string SOAP type
*/
function converterMoodleParamIntoWsParam($moodleparam) {
switch ($moodleparam) {
case PARAM_NUMBER:
return "integer";
break;
case PARAM_INT:
return "integer";
break;
case PARAM_BOOL:
return "boolean";
break;
case PARAM_ALPHANUM:
return "string";
break;
case PARAM_ALPHA:
return "string";
break;
case PARAM_RAW:
return "string";
break;
case PARAM_ALPHANUMEXT:
return "string";
break;
case PARAM_NOTAGS:
return "string";
break;
case PARAM_TEXT:
return "string";
break;
//here we check that the value has not already been changed
//the algo could want to do it in the case two functions of the web description use the
//same object ($params or $return could be the same for two functions, so the guy
//writing the web description use the same object)
//as the convertDescriptionType function passes parameter in reference
case "integer":
return "integer";
break;
case "boolean":
return "boolean";
break;
case "string":
return "string";
break;
default:
return "object";
break;
}
}