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