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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * This is the external API for the filter component.
  19   *
  20   * @package    core_filters
  21   * @copyright  2017 Juan Leyva
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_filters;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir . '/filterlib.php');
  29  
  30  use core_external\external_api;
  31  use core_external\external_function_parameters;
  32  use core_external\external_multiple_structure;
  33  use core_external\external_single_structure;
  34  use core_external\external_value;
  35  use core_external\external_warnings;
  36  use Exception;
  37  
  38  /**
  39   * This is the external API for the filter component.
  40   *
  41   * @copyright  2017 Juan Leyva
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class external extends external_api {
  45  
  46      /**
  47       * Returns description of get_available_in_context() parameters.
  48       *
  49       * @return external_function_parameters
  50       * @since  Moodle 3.4
  51       */
  52      public static function get_available_in_context_parameters() {
  53          return new external_function_parameters (
  54              array(
  55                  'contexts' => new external_multiple_structure(
  56                      new external_single_structure(
  57                          array(
  58                              'contextlevel' => new external_value(PARAM_ALPHA, 'The context level where the filters are:
  59                                  (coursecat, course, module)'),
  60                              'instanceid' => new external_value(PARAM_INT, 'The instance id of item associated with the context.')
  61                          )
  62                      ), 'The list of contexts to check.'
  63                  ),
  64              )
  65          );
  66      }
  67  
  68      /**
  69       * Returns the filters available in the given contexts.
  70       *
  71       * @param array $contexts the list of contexts to check
  72       * @return array with the filters information and warnings
  73       * @since Moodle 3.4
  74       */
  75      public static function get_available_in_context($contexts) {
  76          $params = self::validate_parameters(self::get_available_in_context_parameters(), array('contexts' => $contexts));
  77          $filters = $warnings = array();
  78  
  79          foreach ($params['contexts'] as $contextinfo) {
  80              try {
  81                  $context = self::get_context_from_params($contextinfo);
  82                  self::validate_context($context);
  83                  $contextinfo['contextid'] = $context->id;
  84              } catch (Exception $e) {
  85                  $warnings[] = array(
  86                      'item' => 'context',
  87                      'itemid' => $contextinfo['instanceid'],
  88                      'warningcode' => $e->getCode(),
  89                      'message' => $e->getMessage(),
  90                  );
  91                  continue;
  92              }
  93              $contextfilters = filter_get_available_in_context($context);
  94  
  95              foreach ($contextfilters as $filter) {
  96                  $filters[] = array_merge($contextinfo, (array) $filter);
  97              }
  98          }
  99  
 100          return array(
 101              'filters' => $filters,
 102              'warnings' => $warnings,
 103          );
 104      }
 105  
 106      /**
 107       * Returns description of get_available_in_context() result value.
 108       *
 109       * @return external_single_structure
 110       * @since  Moodle 3.4
 111       */
 112      public static function get_available_in_context_returns() {
 113          return new external_single_structure(
 114              array(
 115                  'filters' => new external_multiple_structure(
 116                      new external_single_structure(
 117                          array(
 118                              'contextlevel' => new external_value(PARAM_ALPHA, 'The context level where the filters are:
 119                                  (coursecat, course, module).'),
 120                              'instanceid' => new external_value(PARAM_INT, 'The instance id of item associated with the context.'),
 121                              'contextid' => new external_value(PARAM_INT, 'The context id.'),
 122                              'filter'  => new external_value(PARAM_PLUGIN, 'Filter plugin name.'),
 123                              'localstate' => new external_value(PARAM_INT, 'Filter state: 1 for on, -1 for off, 0 if inherit.'),
 124                              'inheritedstate' => new external_value(PARAM_INT, '1 or 0 to use when localstate is set to inherit.'),
 125                          )
 126                      ),
 127                      'Available filters'
 128                  ),
 129                  'warnings' => new external_warnings(),
 130              )
 131          );
 132      }
 133  }