Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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_search\external;
  18  
  19  use core_external\external_api;
  20  use core_external\external_function_parameters;
  21  use core_external\external_single_structure;
  22  use core_external\external_multiple_structure;
  23  use core_external\external_value;
  24  use core_external\external_warnings;
  25  use \core_search\manager;
  26  use moodle_exception;
  27  
  28  /**
  29   * External function for return the list of search areas.
  30   *
  31   * @package    core_search
  32   * @copyright  2023 Juan Leyva <juan@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   * @since      Moodle 4.3
  35   */
  36  class get_search_areas_list extends external_api {
  37  
  38      /**
  39       * Webservice parameters.
  40       *
  41       * @return external_function_parameters
  42       */
  43      public static function execute_parameters(): external_function_parameters {
  44          return new external_function_parameters(
  45              [
  46                  'cat' => new external_value(PARAM_NOTAGS, 'category to filter areas', VALUE_DEFAULT, ''),
  47              ]
  48          );
  49      }
  50  
  51  
  52      /**
  53       * Return list of search areas.
  54       *
  55       * @param string $cat category to filter areas
  56       * @return array search areas and warnings
  57       */
  58      public static function execute(string $cat = ''): array {
  59  
  60          $params = self::validate_parameters(self::execute_parameters(), ['cat' => $cat]);
  61  
  62          $system = \context_system::instance();
  63          external_api::validate_context($system);
  64  
  65          require_capability('moodle/search:query', $system);
  66  
  67          if (manager::is_global_search_enabled() === false) {
  68              throw new moodle_exception('globalsearchdisabled', 'search');
  69          }
  70  
  71          $areas = [];
  72          $allsearchareas = manager::get_search_area_categories();
  73          $enabledsearchareas = manager::get_search_areas_list(true);
  74  
  75          foreach ($allsearchareas as $categoryid => $searchareacategory) {
  76              if (!empty($params['cat']) && $params['cat'] != $categoryid) {
  77                  continue;
  78              }
  79  
  80              $searchareas = $searchareacategory->get_areas();
  81              $catname = $searchareacategory->get_visiblename();
  82              foreach ($searchareas as $areaid => $searcharea) {
  83                  if (key_exists($areaid, $enabledsearchareas)) {
  84                      $name = $searcharea->get_visible_name();
  85                      $areas[$name] = ['id' => $areaid, 'name' => $name, 'categoryid' => $categoryid, 'categoryname' => $catname];
  86                  }
  87              }
  88          }
  89  
  90          ksort($areas);
  91  
  92          return ['areas' => $areas, 'warnings' => []];
  93      }
  94  
  95      /**
  96       * Webservice returns.
  97       *
  98       * @return external_single_structure
  99       */
 100      public static function execute_returns(): external_single_structure {
 101          return new external_single_structure(
 102              [
 103                  'areas' => new external_multiple_structure(
 104                      new external_single_structure(
 105                          [
 106                              'id' => new external_value(PARAM_ALPHANUMEXT, 'search area id'),
 107                              'categoryid' => new external_value(PARAM_NOTAGS, 'category id'),
 108                              'categoryname' => new external_value(PARAM_NOTAGS, 'category name'),
 109                              'name' => new external_value(PARAM_TEXT, 'search area name'),
 110                          ], 'Search area'
 111                      ), 'Search areas'
 112                  ),
 113                  'warnings' => new external_warnings()
 114              ]
 115          );
 116      }
 117  }