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 this component.
  19   *
  20   * @package    tool_analytics
  21   * @copyright  2019 David Monllao {@link http://www.davidmonllao.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_analytics;
  26  
  27  use core_external\external_api;
  28  use core_external\external_function_parameters;
  29  use core_external\external_value;
  30  use core_external\external_single_structure;
  31  use core_external\external_multiple_structure;
  32  
  33  /**
  34   * This is the external API for this component.
  35   *
  36   * @copyright  2019 David Monllao {@link http://www.davidmonllao.com}
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class external extends external_api {
  40  
  41      const MAX_CONTEXTS_RETURNED = 100;
  42  
  43      /**
  44       * potential_contexts parameters.
  45       *
  46       * @since  Moodle 3.8
  47       * @return external_function_parameters
  48       */
  49      public static function potential_contexts_parameters() {
  50          return new external_function_parameters(
  51              array(
  52                  'query' => new external_value(PARAM_NOTAGS, 'The model id', VALUE_DEFAULT),
  53                  'modelid' => new external_value(PARAM_INT, 'The model id', VALUE_DEFAULT)
  54              )
  55          );
  56      }
  57  
  58      /**
  59       * Return the contexts that match the provided query.
  60       *
  61       * @since  Moodle 3.8
  62       * @param  string|null $query
  63       * @param  int|null $modelid
  64       * @return array an array of contexts
  65       */
  66      public static function potential_contexts(?string $query = null, ?int $modelid = null) {
  67  
  68          $params = self::validate_parameters(self::potential_contexts_parameters(), ['modelid' => $modelid, 'query' => $query]);
  69  
  70          \core_analytics\manager::check_can_manage_models();
  71  
  72          if ($params['modelid']) {
  73              $model = new \core_analytics\model($params['modelid']);
  74              $contexts = ($model->get_analyser(['notimesplitting' => true]))::potential_context_restrictions($params['query']);
  75          } else {
  76              $contexts = \core_analytics\manager::get_potential_context_restrictions(null, $params['query']);
  77          }
  78  
  79          $contextoptions = [];
  80          $i = 0;
  81          foreach ($contexts as $contextid => $contextname) {
  82  
  83              if ($i === self::MAX_CONTEXTS_RETURNED) {
  84                  // Limited to MAX_CONTEXTS_RETURNED items.
  85                  break;
  86              }
  87  
  88              $contextoptions[] = ['id' => $contextid, 'name' => $contextname];
  89              $i++;
  90          }
  91  
  92          return $contextoptions;
  93      }
  94  
  95      /**
  96       * potential_contexts return
  97       *
  98       * @since  Moodle 3.8
  99       * @return \core_external\external_description
 100       */
 101      public static function potential_contexts_returns() {
 102          return new external_multiple_structure(
 103              new external_single_structure([
 104                  'id'    => new external_value(PARAM_INT, 'ID of the context'),
 105                  'name'  => new external_value(PARAM_NOTAGS, 'The context name')
 106              ])
 107          );
 108      }
 109  }