Skip to content
Browse files

MDL-35997 Create web service functions to return lang strings

  • Loading branch information...
1 parent 7e8ae12 commit 11e766020bd8e05b59550d3164e1a2cd20cc8ad9 @mouneyrac mouneyrac committed Oct 31, 2012
Showing with 399 additions and 2 deletions.
  1. +1 −0 lang/en/webservice.php
  2. +26 −1 lib/db/services.php
  3. +258 −0 lib/external/externallib.php
  4. +113 −0 lib/external/tests/externallib_test.php
  5. +1 −1 version.php
View
1 lang/en/webservice.php
@@ -180,6 +180,7 @@
$string['tokencreatedbyadmin'] = 'Can only be reset by administrator (*)';
$string['tokencreator'] = 'Creator';
$string['unknownoptionkey'] = 'Unknown option key ({$a})';
+$string['unamedstringparam'] = 'A string parameter is unnamed.';
$string['updateusersettings'] = 'Update';
$string['userasclients'] = 'Users as clients with token';
$string['userasclientsdescription'] = 'The following steps help you to set up the Moodle web service for users as clients. These steps also help to set up the recommended token (security keys) authentication method. In this use case, the user will generate his token from the security keys page via My profile settings.';
View
27 lib/db/services.php
@@ -610,6 +610,30 @@
'type' => 'read',
),
+ 'core_get_string' => array(
+ 'classname' => 'core_external',
+ 'methodname' => 'get_string',
+ 'classpath' => 'lib/external/externallib.php',
+ 'description' => 'Return a translated string - similar to core get_string() call',
+ 'type' => 'read',
+ ),
+
+ 'core_get_strings' => array(
+ 'classname' => 'core_external',
+ 'methodname' => 'get_strings',
+ 'classpath' => 'lib/external/externallib.php',
+ 'description' => 'Return some translated strings - like several core get_string() calls',
+ 'type' => 'read',
+ ),
+
+ 'core_get_component_strings' => array(
+ 'classname' => 'core_external',
+ 'methodname' => 'get_component_strings',
+ 'classpath' => 'lib/external/externallib.php',
+ 'description' => 'Return all raw strings (with {$a->xxx}) for a specific component
+ - similar to core get_component_strings() call',
+ 'type' => 'read',
+ ),
);
$services = array(
@@ -623,7 +647,8 @@
'moodle_user_get_course_participants_by_id',
'moodle_user_get_users_by_courseid',
'moodle_message_send_instantmessages',
- 'core_course_get_contents'),
+ 'core_course_get_contents',
+ 'core_get_component_strings'),
'enabled' => 0,
'restrictedusers' => 0,
'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE,
View
258 lib/external/externallib.php
@@ -0,0 +1,258 @@
+<?php
+// 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/>.
+
+
+/**
+ * external API for core library
+ *
+ * @package core_webservice
+ * @category external
+ * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die;
+
+require_once("$CFG->libdir/externallib.php");
+
+/**
+ * Web service related functions
+ *
+ * @package core
+ * @category external
+ * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since Moodle 2.4
+ */
+class core_external extends external_api {
+
+
+ /**
+ * Format the received string parameters to be sent to the core get_string() function.
+ *
+ * @param array $stringparams
+ * @return object|string
+ * @since 2.4
+ */
+ public static function format_string_parameters($stringparams) {
+ // Check if there are some string params.
+ $strparams = new stdClass();
+ if (!empty($stringparams)) {
+ // There is only one string parameter.
+ if (count($stringparams) == 1) {
+ $stringparam = array_pop($stringparams);
+ if (isset($stringparam['name'])) {
+ $strparams->{$stringparam['name']} = $stringparam['value'];
+ } else {
+ // It is a not named string parameter.
+ $strparams = $stringparam['value'];
+ }
+ } else {
+ // There are more than one parameter.
+ foreach ($stringparams as $stringparam) {
+
+ // If a parameter is unnamed throw an exception
+ // unnamed param is only possible if one only param is sent.
+ if (empty($stringparam['name'])) {
+ throw new moodle_exception('unamedstringparam', 'webservice');
+ }
+
+ $strparams->{$stringparam['name']} = $stringparam['value'];
+ }
+ }
+ }
+ return $strparams;
+ }
+
+ /**
+ * Returns description of get_string parameters
+ *
+ * @return external_function_parameters
+ * @since Moodle 2.4
+ */
+ public static function get_string_parameters() {
+ return new external_function_parameters(
+ array('stringid' => new external_value(PARAM_STRINGID, 'string identifier'),
+ 'component' => new external_value(PARAM_COMPONENT,'component', VALUE_DEFAULT, 'moodle'),
+ 'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
+ 'stringparams' => new external_multiple_structure (
+ new external_single_structure(array(
+ 'name' => new external_value(PARAM_ALPHANUMEXT, 'param name
+ - if the string expect only one $a parameter then don\'t send this field, just send the value.', VALUE_OPTIONAL),
+ 'value' => new external_value(PARAM_TEXT,'param value'))),
+ 'the definition of a string param (i.e. {$a->name})', VALUE_DEFAULT, array()
+ )
+ )
+ );
+ }
+
+ /**
+ * Return a core get_string() call
+ *
+ * @param string $identifier string identifier
+ * @param string $component string component
+ * @param array $stringparams the string params
+ * @return string
+ * @since Moodle 2.4
+ */
+ public static function get_string($stringid, $component = 'moodle', $stringparams = array()) {
+ $params = self::validate_parameters(self::get_string_parameters(),
+ array('stringid'=>$stringid, 'component' => $component, 'stringparams' => $stringparams));
+
+ return get_string($params['stringid'], $params['component'],
+ core_external::format_string_parameters($params['stringparams']), $params['lang']);
+ }
+
+ /**
+ * Returns description of get_string() result value
+ *
+ * @return string
+ * @since Moodle 2.4
+ */
+ public static function get_string_returns() {
+ return new external_value(PARAM_TEXT, 'translated string');
+ }
+
+ /**
+ * Returns description of get_string parameters
+ *
+ * @return external_function_parameters
+ * @since Moodle 2.4
+ */
+ public static function get_strings_parameters() {
+ return new external_function_parameters(
+ array('strings' => new external_multiple_structure (
+ new external_single_structure (array(
+ 'stringid' => new external_value(PARAM_STRINGID, 'string identifier'),
+ 'component' => new external_value(PARAM_COMPONENT, 'component', VALUE_DEFAULT, 'moodle'),
+ 'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
+ 'stringparams' => new external_multiple_structure (
+ new external_single_structure(array(
+ 'name' => new external_value(PARAM_ALPHANUMEXT, 'param name
+ - if the string expect only one $a parameter then don\'t send this field, just send the value.', VALUE_OPTIONAL),
+ 'value' => new external_value(PARAM_TEXT, 'param value'))),
+ 'the definition of a string param (i.e. {$a->name})', VALUE_DEFAULT, array()
+ ))
+ )
+ )
+ )
+ );
+ }
+
+ /**
+ * Return multiple call to core get_string()
+ *
+ * @param array $strings strings to translate
+ * @return array
+ *
+ * @since Moodle 2.4
+ */
+ public static function get_strings($strings) {
+ $params = self::validate_parameters(self::get_strings_parameters(),
+ array('strings'=>$strings));
+
+ $translatedstrings = array();
+ foreach($params['strings'] as $string) {
+
+ if (empty($string['lang'])) {
+ $lang = $string['lang'];
+ } else {
+ $lang = current_language();
+ }
+
+ $translatedstrings[] = array(
+ 'stringid' => $string['stringid'],
+ 'component' => $string['component'],
+ 'lang' => $lang,
+ 'string' => get_string($string['stringid'], $string['component'],
+ core_external::format_string_parameters($string['stringparams']), $lang));
+ }
+
+ return $translatedstrings;
+ }
+
+ /**
+ * Returns description of get_string() result value
+ *
+ * @return array
+ * @since Moodle 2.4
+ */
+ public static function get_strings_returns() {
+ return new external_multiple_structure(
+ new external_single_structure(array(
+ 'stringid' => new external_value(PARAM_STRINGID, 'string id'),
+ 'component' => new external_value(PARAM_COMPONENT, 'string component'),
+ 'lang' => new external_value(PARAM_LANG, 'lang'),
+ 'string' => new external_value(PARAM_TEXT, 'translated string'))
+ ));
+ }
+
+ /**
+ * Returns description of get_component_strings parameters
+ *
+ * @return external_function_parameters
+ * @since Moodle 2.4
+ */
+ public static function get_component_strings_parameters() {
+ return new external_function_parameters(
+ array('component' => new external_value(PARAM_COMPONENT, 'component'),
+ 'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
+ )
+ );
+ }
+
+ /**
+ * Return all lang strings of a component - call to core get_component_strings().
+ *
+ * @param string $component component name
+ * @return array
+ *
+ * @since Moodle 2.4
+ */
+ public static function get_component_strings($component, $lang = null) {
+
+ if (empty($lang)) {
+ $lang = current_language();
+ }
+
+ $params = self::validate_parameters(self::get_component_strings_parameters(),
+ array('component'=>$component, 'lang' => $lang));
+
+ $stringmanager = get_string_manager();
+
+ $wsstrings = array();
+ $componentstrings = $stringmanager->load_component_strings($params['component'], $params['lang']);
+ foreach($componentstrings as $stringid => $string) {
+ $wsstrings[$stringid] = $string;
+ }
+
+ return $wsstrings;
+ }
+
+ /**
+ * Returns description of get_component_strings() result value
+ *
+ * @return array
+ * @since Moodle 2.4
+ */
+ public static function get_component_strings_returns() {
+ return new external_multiple_structure(
+ new external_single_structure(array(
+ 'stringid' => new external_value(PARAM_STRINGID, 'string id'),
+ 'string' => new external_value(PARAM_TEXT, 'translated string'))
+ ));
+ }
+}
View
113 lib/external/tests/externallib_test.php
@@ -0,0 +1,113 @@
+<?php
+// 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/>.
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+require_once($CFG->dirroot . '/lib/external/externallib.php');
+require_once($CFG->dirroot . '/webservice/tests/helpers.php');
+
+/**
+ * External library functions unit tests
+ *
+ * @package core
+ * @category external
+ * @copyright 2012 Jerome Mouneyrac
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class core_external_testcase extends externallib_advanced_testcase {
+
+ /**
+ * Test get_string
+ */
+ public function test_get_string() {
+ $this->resetAfterTest(true);
+
+ $service = new stdClass();
+ $service->name = 'Dummy Service';
+ $service->id = 12;
+
+ // String with two parameters.
+ $returnedstring = core_external::get_string('addservice', 'webservice',
+ array(array('name' => 'name', 'value' => $service->name),
+ array('name' => 'id', 'value' => $service->id)));
+ $corestring = get_string('addservice', 'webservice', $service);
+ $this->assertEquals($corestring, $returnedstring);
+
+ // String with one parameter.
+ $acapname = 'A capability name';
+ $returnedstring = core_external::get_string('missingrequiredcapability', 'webservice',
+ array(array('value' => $acapname)));
+ $corestring = get_string('missingrequiredcapability', 'webservice', $acapname);
+ $this->assertEquals($corestring, $returnedstring);
+
+ // String without parameters.
+ $returnedstring = core_external::get_string('missingpassword', 'webservice');
+ $corestring = get_string('missingpassword', 'webservice');
+ $this->assertEquals($corestring, $returnedstring);
+
+ // String with two parameter but one is invalid (not named).
+ $this->setExpectedException('moodle_exception');
+ $returnedstring = core_external::get_string('addservice', 'webservice',
+ array(array('value' => $service->name),
+ array('name' => 'id', 'value' => $service->id)));
+ }
+
+ /**
+ * Test get_strings
+ */
+ public function test_get_strings() {
+ $this->resetAfterTest(true);
+
+ $service = new stdClass();
+ $service->name = 'Dummy Service';
+ $service->id = 12;
+
+ $returnedstrings = core_external::get_strings(
+ array(
+ array(
+ 'stringid' => 'addservice', 'component' => 'webservice',
+ 'stringparams' => array(array('name' => 'name', 'value' => $service->name),
+ array('name' => 'id', 'value' => $service->id)
+ )
+ ),
+ array('stringid' => 'addaservice', 'component' => 'webservice')
+ ));
+
+ foreach($returnedstrings as $returnedstring) {
+ $corestring = get_string($returnedstring['stringid'], $returnedstring['component'], $service);
+ $this->assertEquals($corestring, $returnedstring['string']);
+ }
+ }
+
+ /**
+ * Test get_component_strings
+ */
+ public function test_get_component_strings() {
+ global $USER;
+ $this->resetAfterTest(true);
+
+ $stringmanager = get_string_manager();
+
+ $wsstrings = $stringmanager->load_component_strings('webservice', current_language());
+
+ $componentstrings = core_external::get_component_strings('webservice');
+ $this->assertEquals(count($componentstrings), count($wsstrings));
+ foreach($wsstrings as $name => $string) {
+ $this->assertEquals($string, $componentstrings[$name]);
+ }
+ }
+}
View
2 version.php
@@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();
-$version = 2012101800.00; // YYYYMMDD = weekly release date of this DEV branch
+$version = 2012101800.01; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

0 comments on commit 11e7660

Please sign in to comment.
Something went wrong with that request. Please try again.