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