webservice MDL-21510 added three defines for the description of ws optional value:

VALUE_REQUIRED - if the parameter is not supplied, there is an error
VALUE_OPTIONAL - if the parameter is not supplied, then the param has no value
VALUE_DEFAULT - if the parameter is not supplied, then the default value is used
This commit is contained in:
jerome mouneyrac
2010-02-05 02:58:24 +00:00
parent 2e08c9822b
commit 382b9ceab3
4 changed files with 59 additions and 9 deletions
+1
View File
@@ -28,6 +28,7 @@ $string['createtokenforuser'] = 'Create a token for a user';
$string['createtokenforuserdescription'] = 'On the <strong>Manage token</strong> page, click on \'Add\'. Then select the created user and service.';
$string['createuser'] = 'Create a specific user';
$string['createuserdescription'] = 'You need to create a specific user for the system controlling Moodle.';
$string['default'] = 'Default to \"$a\"';
$string['deleteservice'] = 'Delete the service: $a->name (id: $a->id)';
$string['deleteaservice'] = 'Delete service';
$string['deleteserviceconfirm'] = 'Do you really want to delete external service \"$a\"?';
+9 -7
View File
@@ -155,7 +155,7 @@ class external_api {
}
}
return validate_param($params, $description->type, $description->allownull, 'Invalid external api parameter');
} else if ($description instanceof external_single_structure) {
if (!is_array($params)) {
throw new invalid_parameter_exception('Only arrays accepted.');
@@ -163,12 +163,14 @@ class external_api {
$result = array();
foreach ($description->keys as $key=>$subdesc) {
if (!array_key_exists($key, $params)) {
if ($subdesc->required) {
if ($subdesc->required == VALUE_REQUIRED) {
throw new invalid_parameter_exception('Missing required key in single structure.');
}
if ($subdesc instanceof external_value) {
$result[$key] = self::validate_parameters($subdesc, $subdesc->default);
}
if ($subdesc->required == VALUE_DEFAULT) {
$result[$key] = self::validate_parameters($subdesc, $subdesc->default);
}
}
} else {
$result[$key] = self::validate_parameters($subdesc, $params[$key]);
}
@@ -275,7 +277,7 @@ class external_value extends external_description {
* @param mixed $default
* @param bool $allownull
*/
public function __construct($type, $desc='', $required=true, $default=null, $allownull=true) {
public function __construct($type, $desc='', $required=VALUE_REQUIRED, $default=null, $allownull=true) {
parent::__construct($desc, $required);
$this->type = $type;
$this->default = $default;
@@ -296,7 +298,7 @@ class external_single_structure extends external_description {
* @param string $desc
* @param bool $required
*/
public function __construct(array $keys, $desc='', $required=true) {
public function __construct(array $keys, $desc='', $required=VALUE_REQUIRED) {
parent::__construct($desc, $required);
$this->keys = $keys;
}
@@ -315,7 +317,7 @@ class external_multiple_structure extends external_description {
* @param string $desc
* @param bool $required
*/
public function __construct(external_description $content, $desc='', $required=true) {
public function __construct(external_description $content, $desc='', $required=VALUE_REQUIRED) {
parent::__construct($desc, $required);
$this->content = $content;
}
+15
View File
@@ -264,7 +264,22 @@ define('PARAM_MULTILANG', 'text');
*/
define('PARAM_CLEANFILE', 'file');
/// Web Services ///
/**
* VALUE_REQUIRED - if the parameter is not supplied, there is an error
*/
define('VALUE_REQUIRED', 1);
/**
* VALUE_OPTIONAL - if the parameter is not supplied, then the param has no value
*/
define('VALUE_OPTIONAL', 2);
/**
* VALUE_DEFAULT - if the parameter is not supplied, then the default value is used
*/
define('VALUE_DEFAULT', 0);
/// Page types ///
+34 -2
View File
@@ -48,9 +48,26 @@ class core_webservice_renderer extends plugin_renderer_base {
$paramdesc = "";
if (!empty($params->desc)) {
$paramdesc .= html_writer::start_tag('span', array('style' => "color:#2A33A6"));
if ($params->required == VALUE_REQUIRED) {
$required = '';
}
if ($params->required == VALUE_DEFAULT) {
if (empty($params->default)) {
$params->default = "null";
}
$required = html_writer::start_tag('b', array()).get_string('default', 'webservice', $params->default).html_writer::end_tag('b');
}
if ($params->required == VALUE_OPTIONAL) {
$required = html_writer::start_tag('b', array()).get_string('optional', 'webservice').html_writer::end_tag('b');
}
$paramdesc .= " ".$required." ";
$paramdesc .= html_writer::start_tag('i', array());
$paramdesc .= "//".$params->desc;
$paramdesc .= "//";
$paramdesc .= $params->desc;
$paramdesc .= html_writer::end_tag('i');
$paramdesc .= html_writer::end_tag('span');
$paramdesc .= html_writer::empty_tag('br', array());
}
@@ -313,7 +330,22 @@ EOF;
foreach ($description->parameters_desc->keys as $paramname => $paramdesc) {
/// a argument documentation
$documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%'));
$required = $paramdesc->required?get_string('required', 'webservice'):get_string('optional', 'webservice');
if ($paramdesc->required == VALUE_REQUIRED) {
$required = get_string('required', 'webservice');
}
if ($paramdesc->required == VALUE_DEFAULT) {
if (empty($paramdesc->default)) {
$default = "null";
} else {
$default = $paramdesc->default;
}
$required = get_string('default', 'webservice', $default);
}
if ($paramdesc->required == VALUE_OPTIONAL) {
$required = get_string('optional', 'webservice');
}
$documentationhtml .= html_writer::start_tag('b', array());
$documentationhtml .= $paramname;
$documentationhtml .= html_writer::end_tag('b');