Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

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