Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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  namespace core_courseformat\external;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/course/dnduploadlib.php');
  23  
  24  use core_external\external_api;
  25  use core_external\external_function_parameters;
  26  use core_external\external_multiple_structure;
  27  use core_external\external_single_structure;
  28  use core_external\external_value;
  29  use dndupload_handler;
  30  
  31  /**
  32   * Class for exporting a course file handlers.
  33   *
  34   * @package    core_courseformat
  35   * @copyright  2022 Ferran Recio <ferran@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @since      Moodle 4.2
  38   */
  39  class file_handlers extends external_api {
  40  
  41      /**
  42       * Webservice parameters.
  43       *
  44       * @return external_function_parameters
  45       */
  46      public static function execute_parameters(): external_function_parameters {
  47          return new external_function_parameters(
  48              [
  49                  'courseid' => new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
  50              ]
  51          );
  52      }
  53  
  54      /**
  55       * Return the list of available file handlers.
  56       *
  57       * @param int $courseid the course id
  58       * @return array of file hanlders.
  59       */
  60      public static function execute(int $courseid): array {
  61          global $CFG;
  62  
  63          require_once($CFG->dirroot . '/course/lib.php');
  64  
  65          $params = external_api::validate_parameters(self::execute_parameters(), [
  66              'courseid' => $courseid,
  67          ]);
  68          $courseid = $params['courseid'];
  69  
  70          self::validate_context(\context_course::instance($courseid));
  71  
  72          $format = course_get_format($courseid);
  73          $course = $format->get_course();
  74  
  75          $handler = new dndupload_handler($course, null);
  76  
  77          $data = $handler->get_js_data();
  78          return $data->filehandlers ?? [];
  79      }
  80  
  81      /**
  82       * Webservice returns.
  83       *
  84       * @return external_multiple_structure
  85       */
  86      public static function execute_returns(): external_multiple_structure {
  87          return new external_multiple_structure(
  88              new external_single_structure([
  89                  'extension' => new external_value(PARAM_TEXT, 'File extension'),
  90                  'module' => new external_value(PARAM_TEXT, 'Target module'),
  91                  'message' => new external_value(PARAM_TEXT, 'Output message'),
  92              ])
  93          );
  94      }
  95  }