/lib/external/externallib.php

https://bitbucket.org/kudutest/moodlegit · PHP · 261 lines · 128 code · 28 blank · 105 comment · 11 complexity · f57dec7e7029f5732e6a6e16d38584a2 MD5 · raw file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * external API for core library
  18. *
  19. * @package core_webservice
  20. * @category external
  21. * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die;
  25. require_once("$CFG->libdir/externallib.php");
  26. /**
  27. * Web service related functions
  28. *
  29. * @package core
  30. * @category external
  31. * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. * @since Moodle 2.4
  34. */
  35. class core_external extends external_api {
  36. /**
  37. * Format the received string parameters to be sent to the core get_string() function.
  38. *
  39. * @param array $stringparams
  40. * @return object|string
  41. * @since 2.4
  42. */
  43. public static function format_string_parameters($stringparams) {
  44. // Check if there are some string params.
  45. $strparams = new stdClass();
  46. if (!empty($stringparams)) {
  47. // There is only one string parameter.
  48. if (count($stringparams) == 1) {
  49. $stringparam = array_pop($stringparams);
  50. if (isset($stringparam['name'])) {
  51. $strparams->{$stringparam['name']} = $stringparam['value'];
  52. } else {
  53. // It is a not named string parameter.
  54. $strparams = $stringparam['value'];
  55. }
  56. } else {
  57. // There are more than one parameter.
  58. foreach ($stringparams as $stringparam) {
  59. // If a parameter is unnamed throw an exception
  60. // unnamed param is only possible if one only param is sent.
  61. if (empty($stringparam['name'])) {
  62. throw new moodle_exception('unnamedstringparam', 'webservice');
  63. }
  64. $strparams->{$stringparam['name']} = $stringparam['value'];
  65. }
  66. }
  67. }
  68. return $strparams;
  69. }
  70. /**
  71. * Returns description of get_string parameters
  72. *
  73. * @return external_function_parameters
  74. * @since Moodle 2.4
  75. */
  76. public static function get_string_parameters() {
  77. return new external_function_parameters(
  78. array('stringid' => new external_value(PARAM_STRINGID, 'string identifier'),
  79. 'component' => new external_value(PARAM_COMPONENT,'component', VALUE_DEFAULT, 'moodle'),
  80. 'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
  81. 'stringparams' => new external_multiple_structure (
  82. new external_single_structure(array(
  83. 'name' => new external_value(PARAM_ALPHANUMEXT, 'param name
  84. - if the string expect only one $a parameter then don\'t send this field, just send the value.', VALUE_OPTIONAL),
  85. 'value' => new external_value(PARAM_TEXT,'param value'))),
  86. 'the definition of a string param (i.e. {$a->name})', VALUE_DEFAULT, array()
  87. )
  88. )
  89. );
  90. }
  91. /**
  92. * Return a core get_string() call
  93. *
  94. * @param string $identifier string identifier
  95. * @param string $component string component
  96. * @param array $stringparams the string params
  97. * @return string
  98. * @since Moodle 2.4
  99. */
  100. public static function get_string($stringid, $component = 'moodle', $stringparams = array()) {
  101. $params = self::validate_parameters(self::get_string_parameters(),
  102. array('stringid'=>$stringid, 'component' => $component, 'stringparams' => $stringparams));
  103. return get_string($params['stringid'], $params['component'],
  104. core_external::format_string_parameters($params['stringparams']), $params['lang']);
  105. }
  106. /**
  107. * Returns description of get_string() result value
  108. *
  109. * @return string
  110. * @since Moodle 2.4
  111. */
  112. public static function get_string_returns() {
  113. return new external_value(PARAM_TEXT, 'translated string');
  114. }
  115. /**
  116. * Returns description of get_string parameters
  117. *
  118. * @return external_function_parameters
  119. * @since Moodle 2.4
  120. */
  121. public static function get_strings_parameters() {
  122. return new external_function_parameters(
  123. array('strings' => new external_multiple_structure (
  124. new external_single_structure (array(
  125. 'stringid' => new external_value(PARAM_STRINGID, 'string identifier'),
  126. 'component' => new external_value(PARAM_COMPONENT, 'component', VALUE_DEFAULT, 'moodle'),
  127. 'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
  128. 'stringparams' => new external_multiple_structure (
  129. new external_single_structure(array(
  130. 'name' => new external_value(PARAM_ALPHANUMEXT, 'param name
  131. - if the string expect only one $a parameter then don\'t send this field, just send the value.', VALUE_OPTIONAL),
  132. 'value' => new external_value(PARAM_TEXT, 'param value'))),
  133. 'the definition of a string param (i.e. {$a->name})', VALUE_DEFAULT, array()
  134. ))
  135. )
  136. )
  137. )
  138. );
  139. }
  140. /**
  141. * Return multiple call to core get_string()
  142. *
  143. * @param array $strings strings to translate
  144. * @return array
  145. *
  146. * @since Moodle 2.4
  147. */
  148. public static function get_strings($strings) {
  149. $params = self::validate_parameters(self::get_strings_parameters(),
  150. array('strings'=>$strings));
  151. $translatedstrings = array();
  152. foreach($params['strings'] as $string) {
  153. if (empty($string['lang'])) {
  154. $lang = $string['lang'];
  155. } else {
  156. $lang = current_language();
  157. }
  158. $translatedstrings[] = array(
  159. 'stringid' => $string['stringid'],
  160. 'component' => $string['component'],
  161. 'lang' => $lang,
  162. 'string' => get_string($string['stringid'], $string['component'],
  163. core_external::format_string_parameters($string['stringparams']), $lang));
  164. }
  165. return $translatedstrings;
  166. }
  167. /**
  168. * Returns description of get_string() result value
  169. *
  170. * @return array
  171. * @since Moodle 2.4
  172. */
  173. public static function get_strings_returns() {
  174. return new external_multiple_structure(
  175. new external_single_structure(array(
  176. 'stringid' => new external_value(PARAM_STRINGID, 'string id'),
  177. 'component' => new external_value(PARAM_COMPONENT, 'string component'),
  178. 'lang' => new external_value(PARAM_LANG, 'lang'),
  179. 'string' => new external_value(PARAM_TEXT, 'translated string'))
  180. ));
  181. }
  182. /**
  183. * Returns description of get_component_strings parameters
  184. *
  185. * @return external_function_parameters
  186. * @since Moodle 2.4
  187. */
  188. public static function get_component_strings_parameters() {
  189. return new external_function_parameters(
  190. array('component' => new external_value(PARAM_COMPONENT, 'component'),
  191. 'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
  192. )
  193. );
  194. }
  195. /**
  196. * Return all lang strings of a component - call to core get_component_strings().
  197. *
  198. * @param string $component component name
  199. * @return array
  200. *
  201. * @since Moodle 2.4
  202. */
  203. public static function get_component_strings($component, $lang = null) {
  204. if (empty($lang)) {
  205. $lang = current_language();
  206. }
  207. $params = self::validate_parameters(self::get_component_strings_parameters(),
  208. array('component'=>$component, 'lang' => $lang));
  209. $stringmanager = get_string_manager();
  210. $wsstrings = array();
  211. $componentstrings = $stringmanager->load_component_strings($params['component'], $params['lang']);
  212. foreach($componentstrings as $stringid => $string) {
  213. $wsstring = array();
  214. $wsstring['stringid'] = $stringid;
  215. $wsstring['string'] = $string;
  216. $wsstrings[] = $wsstring;
  217. }
  218. return $wsstrings;
  219. }
  220. /**
  221. * Returns description of get_component_strings() result value
  222. *
  223. * @return array
  224. * @since Moodle 2.4
  225. */
  226. public static function get_component_strings_returns() {
  227. return new external_multiple_structure(
  228. new external_single_structure(array(
  229. 'stringid' => new external_value(PARAM_STRINGID, 'string id'),
  230. 'string' => new external_value(PARAM_RAW, 'translated string'))
  231. ));
  232. }
  233. }