PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/form/classes/external.php

https://bitbucket.org/moodle/moodle
PHP | 108 lines | 45 code | 15 blank | 48 comment | 1 complexity | b110cdeb4de3d7741212b78b7ab4a5ff MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Provides the {@link core_form\external} class.
  18. *
  19. * @package core_form
  20. * @category external
  21. * @copyright 2017 David Mudrák <david@moodle.com>
  22. * @copyright 2016 Jonathon Fowler <fowlerj@usq.edu.au>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. namespace core_form;
  26. use external_api;
  27. use external_function_parameters;
  28. use external_multiple_structure;
  29. use external_single_structure;
  30. use external_value;
  31. defined('MOODLE_INTERNAL') || die();
  32. require_once($CFG->libdir.'/externallib.php');
  33. /**
  34. * Implements the external functions provided by the core_form subsystem.
  35. *
  36. * @copyright 2017 David Mudrak <david@moodle.com>
  37. * @copyright 2016 Jonathon Fowler <fowlerj@usq.edu.au>
  38. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39. */
  40. class external extends external_api {
  41. /**
  42. * Describes the input paramaters of the get_filetypes_browser_data external function.
  43. *
  44. * @return external_description
  45. */
  46. public static function get_filetypes_browser_data_parameters() {
  47. return new external_function_parameters([
  48. 'onlytypes' => new external_value(PARAM_RAW, 'Limit the browser to the given groups and extensions', VALUE_DEFAULT, ''),
  49. 'allowall' => new external_value(PARAM_BOOL, 'Allows to select All file types, does not apply with onlytypes are set.',
  50. VALUE_DEFAULT, true),
  51. 'current' => new external_value(PARAM_RAW, 'Current types that should be selected.', VALUE_DEFAULT, ''),
  52. ]);
  53. }
  54. /**
  55. * Implements the get_filetypes_browser_data external function.
  56. *
  57. * @param string $onlytypes Allow selection from these file types only; for example 'web_image'.
  58. * @param bool $allowall Allow to select 'All file types'. Does not apply if onlytypes is set.
  59. * @param string $current Current values that should be selected.
  60. * @return object
  61. */
  62. public static function get_filetypes_browser_data($onlytypes, $allowall, $current) {
  63. $params = self::validate_parameters(self::get_filetypes_browser_data_parameters(),
  64. compact('onlytypes', 'allowall', 'current'));
  65. $util = new filetypes_util();
  66. return ['groups' => $util->data_for_browser($params['onlytypes'], $params['allowall'], $params['current'])];
  67. }
  68. /**
  69. * Describes the output of the get_filetypes_browser_data external function.
  70. *
  71. * @return external_description
  72. */
  73. public static function get_filetypes_browser_data_returns() {
  74. $type = new external_single_structure([
  75. 'key' => new external_value(PARAM_RAW, 'The file type identifier'),
  76. 'name' => new external_value(PARAM_RAW, 'The file type name'),
  77. 'selected' => new external_value(PARAM_BOOL, 'Should it be marked as selected'),
  78. 'ext' => new external_value(PARAM_RAW, 'The file extension associated with the file type'),
  79. ]);
  80. $group = new external_single_structure([
  81. 'key' => new external_value(PARAM_RAW, 'The file type group identifier'),
  82. 'name' => new external_value(PARAM_RAW, 'The file type group name'),
  83. 'selectable' => new external_value(PARAM_BOOL, 'Can it be marked as selected'),
  84. 'selected' => new external_value(PARAM_BOOL, 'Should it be marked as selected'),
  85. 'ext' => new external_value(PARAM_RAW, 'The list of file extensions associated with the group'),
  86. 'expanded' => new external_value(PARAM_BOOL, 'Should the group start as expanded or collapsed'),
  87. 'types' => new external_multiple_structure($type, 'List of file types in the group'),
  88. ]);
  89. return new external_single_structure([
  90. 'groups' => new external_multiple_structure($group, 'List of file type groups'),
  91. ]);
  92. }
  93. }