weekly release 5.0dev
[moodle.git] / ai / classes / provider.php
blob63eb352408f3c82d248b7aa70acf12e5f419b864
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.
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/>.
17 namespace core_ai;
19 use core_ai\form\action_settings_form;
20 use Spatie\Cloneable\Cloneable;
22 /**
23 * Class provider.
25 * @package core_ai
26 * @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 abstract class provider {
30 use Cloneable;
32 /** @var string $provider The provider used to make this instance */
33 public readonly string $provider;
35 /** @var array The configuration for this instance. */
36 public readonly array $config;
38 /** @var array The action specific settings for this instance. */
39 public readonly array $actionconfig;
41 /**
42 * Create a new provider.
44 * @param bool $enabled Whether the gateway is enabled
45 * @param string $name The name of the provider config.
46 * @param string $config The configuration for this instance.
47 * @param string $actionconfig The action specific settings for this instance.
48 * @param int|null $id The id of the provider in the database.
50 public function __construct(
51 /** @var bool Whether the gateway is enabled */
52 public readonly bool $enabled,
53 /** @var string The name of the provider config. */
54 public string $name,
55 string $config,
56 string $actionconfig = '',
57 /** @var null|int The ID of the provider in the database, or null if it has not been persisted yet. */
58 public readonly ?int $id = null,
59 ) {
60 $this->provider = strstr(get_class($this), '\\', true);
61 $this->config = json_decode($config, true);
62 if ($actionconfig == '') {
63 $this->actionconfig = static::initialise_action_settings();
64 } else {
65 $this->actionconfig = json_decode($actionconfig, true);
69 /**
70 * Get the actions that this provider supports.
72 * Returns an array of action class names.
74 * @return array An array of action class names.
76 abstract public static function get_action_list(): array;
78 /**
79 * Initialise the action settings array.
81 * @return array The initialised action settings.
83 public static function initialise_action_settings(): array {
84 $actions = static::get_action_list();
85 $actionconfig = [];
86 foreach ($actions as $action) {
87 $actionconfig[$action] = [
88 'enabled' => true,
89 'settings' => static::get_action_setting_defaults($action),
92 return $actionconfig;
95 /**
96 * Given an action class name, return an array of sub actions
97 * that this provider supports.
99 * @param string $classname The action class name.
100 * @return array An array of supported sub actions.
102 public function get_sub_actions(string $classname): array {
103 return [];
107 * Get the name of the provider.
109 * @return string The name of the provider.
111 public function get_name(): string {
112 $component = \core\component::get_component_from_classname(get_class($this));
113 return get_string('pluginname', $component);
117 * Get any action settings for this provider.
119 * @param string $action The action class name.
120 * @param array $customdata The customdata for the form.
121 * @return action_settings_form|bool The settings form for this action or false in no settings.
123 public static function get_action_settings(
124 string $action,
125 array $customdata = [],
126 ): action_settings_form|bool {
127 return false;
131 * Get the default settings for an action.
133 * @param string $action The action class name.
134 * @return array The default settings for the action.
136 public static function get_action_setting_defaults(string $action): array {
137 return [];
141 * Check if the request is allowed by the rate limiter.
143 * @param aiactions\base $action The action to check.
144 * @return array|bool True on success, array of error details on failure.
146 public function is_request_allowed(aiactions\base $action): array|bool {
147 $ratelimiter = \core\di::get(rate_limiter::class);
148 $component = \core\component::get_component_from_classname(get_class($this));
150 // Check the user rate limit.
151 if (isset($this->config['enableuserratelimit']) && $this->config['enableuserratelimit']) {
152 if (!$ratelimiter->check_user_rate_limit(
153 component: $component,
154 ratelimit: $this->config['userratelimit'],
155 userid: $action->get_configuration('userid')
156 )) {
157 return [
158 'success' => false,
159 'errorcode' => 429,
160 'errormessage' => 'User rate limit exceeded',
165 // Check the global rate limit.
166 if (isset($this->config['enableglobalratelimit']) && $this->config['enableglobalratelimit']) {
167 if (!$ratelimiter->check_global_rate_limit(
168 component: $component,
169 ratelimit: $this->config['globalratelimit']
170 )) {
171 return [
172 'success' => false,
173 'errorcode' => 429,
174 'errormessage' => 'Global rate limit exceeded',
179 return true;
183 * Check if a provider has the minimal configuration to work.
185 * @return bool Return true if configured.
187 public function is_provider_configured(): bool {
188 return false;
192 * Convert this object to a stdClass, suitable for saving to the database.
194 * @return \stdClass
196 public function to_record(): \stdClass {
197 return (object) [
198 'id' => $this->id,
199 'name' => $this->name,
200 'provider' => get_class($this),
201 'enabled' => $this->enabled,
202 'config' => json_encode($this->config),
203 'actionconfig' => json_encode($this->actionconfig),