1441 |
ariadna |
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_ai\form;
|
|
|
18 |
|
|
|
19 |
use moodleform;
|
|
|
20 |
|
|
|
21 |
defined('MOODLE_INTERNAL') || die();
|
|
|
22 |
|
|
|
23 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* AI provider instance form.
|
|
|
27 |
*
|
|
|
28 |
* @package core_ai
|
|
|
29 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class ai_provider_form extends moodleform {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Get the custom data.
|
|
|
36 |
*
|
|
|
37 |
* @return mixed The custom data.
|
|
|
38 |
*/
|
|
|
39 |
public function get_customdata(): mixed {
|
|
|
40 |
return $this->_customdata;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
#[\Override]
|
|
|
44 |
protected function definition() {
|
|
|
45 |
global $PAGE;
|
|
|
46 |
$PAGE->requires->js_call_amd('core_ai/providerchooser', 'init');
|
|
|
47 |
|
|
|
48 |
$mform = $this->_form;
|
|
|
49 |
$providerconfigs = $this->_customdata['providerconfigs'] ?? [];
|
|
|
50 |
$returnurl = $this->_customdata['returnurl'] ?? null;
|
|
|
51 |
|
|
|
52 |
// AI provider chooser.
|
|
|
53 |
// Get all enabled AI provider plugins. Users can select one of them to create a new AI provider instance.
|
|
|
54 |
$providerplugins = [];
|
|
|
55 |
$enabledproviderplugins = \core\plugininfo\aiprovider::get_enabled_plugins();
|
|
|
56 |
foreach ($enabledproviderplugins as $pluginname => $notusing) {
|
|
|
57 |
$plugin = 'aiprovider_' . $pluginname;
|
|
|
58 |
$providerplugins[$plugin] = get_string('pluginname', $plugin);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
// Provider chooser.
|
|
|
62 |
$mform->addElement(
|
|
|
63 |
'select',
|
|
|
64 |
'aiprovider',
|
|
|
65 |
get_string('providertype', 'core_ai'),
|
|
|
66 |
$providerplugins,
|
|
|
67 |
['data-aiproviderchooser-field' => 'selector'],
|
|
|
68 |
);
|
|
|
69 |
if (isset($providerconfigs['id'])) {
|
|
|
70 |
$mform->hardFreeze('aiprovider');
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// Provider instance name.
|
|
|
74 |
$mform->addElement(
|
|
|
75 |
'text',
|
|
|
76 |
'name',
|
|
|
77 |
get_string('providername', 'core_ai'),
|
|
|
78 |
'maxlength="255" size="20"',
|
|
|
79 |
);
|
|
|
80 |
$mform->setType('name', PARAM_TEXT);
|
|
|
81 |
$mform->addRule('name', get_string('required'), 'required', null, 'client');
|
|
|
82 |
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255);
|
|
|
83 |
|
|
|
84 |
$mform->registerNoSubmitButton('updateaiprovider');
|
|
|
85 |
$mform->addElement(
|
|
|
86 |
'submit',
|
|
|
87 |
'updateaiprovider',
|
|
|
88 |
'update AI provider',
|
|
|
89 |
['data-aiproviderchooser-field' => 'updateButton', 'class' => 'd-none']
|
|
|
90 |
);
|
|
|
91 |
|
|
|
92 |
// Dispatch a hook for plugins to add their fields.
|
|
|
93 |
$providerplugindefault = array_key_first($providerplugins);
|
|
|
94 |
$hook = new \core_ai\hook\after_ai_provider_form_hook(
|
|
|
95 |
mform: $mform,
|
|
|
96 |
plugin: $providerconfigs['aiprovider'] ?? $providerplugindefault,
|
|
|
97 |
);
|
|
|
98 |
\core\di::get(\core\hook\manager::class)->dispatch($hook);
|
|
|
99 |
|
|
|
100 |
// Add the provider plugin name to form customdata.
|
|
|
101 |
$this->_customdata['aiprovider'] = $hook->plugin;
|
|
|
102 |
|
|
|
103 |
// Add rate limiting settings.
|
|
|
104 |
// Setting to enable/disable global rate limiting.
|
|
|
105 |
$mform->addElement(
|
|
|
106 |
'checkbox',
|
|
|
107 |
'enableglobalratelimit',
|
|
|
108 |
get_string('enableglobalratelimit', 'core_ai'),
|
|
|
109 |
get_string('enableglobalratelimit_help', 'core_ai'),
|
|
|
110 |
);
|
|
|
111 |
// Setting to set how many requests per hour are allowed for the global rate limit.
|
|
|
112 |
// Should only be enabled when global rate limiting is enabled.
|
|
|
113 |
$mform->addElement(
|
|
|
114 |
'text',
|
|
|
115 |
'globalratelimit',
|
|
|
116 |
get_string('globalratelimit', 'core_ai'),
|
|
|
117 |
'maxlength="10" size="4"',
|
|
|
118 |
);
|
|
|
119 |
$mform->setType('globalratelimit', PARAM_INT);
|
|
|
120 |
$mform->addHelpButton('globalratelimit', 'globalratelimit', 'core_ai');
|
|
|
121 |
$mform->hideIf('globalratelimit', 'enableglobalratelimit', 'notchecked');
|
|
|
122 |
|
|
|
123 |
// Setting to enable/disable user rate limiting.
|
|
|
124 |
$mform->addElement(
|
|
|
125 |
'checkbox',
|
|
|
126 |
'enableuserratelimit',
|
|
|
127 |
get_string('enableuserratelimit', 'core_ai'),
|
|
|
128 |
get_string('enableuserratelimit_help', 'core_ai'),
|
|
|
129 |
);
|
|
|
130 |
// Setting to set how many requests per hour are allowed for the user rate limit.
|
|
|
131 |
// Should only be enabled when user rate limiting is enabled.
|
|
|
132 |
$mform->addElement(
|
|
|
133 |
'text',
|
|
|
134 |
'userratelimit',
|
|
|
135 |
get_string('userratelimit', 'core_ai'),
|
|
|
136 |
'maxlength="10" size="4"',
|
|
|
137 |
);
|
|
|
138 |
$mform->setType('userratelimit', PARAM_INT);
|
|
|
139 |
$mform->addHelpButton('userratelimit', 'userratelimit', 'core_ai');
|
|
|
140 |
$mform->hideIf('userratelimit', 'enableuserratelimit', 'notchecked');
|
|
|
141 |
|
|
|
142 |
// Form buttons.
|
|
|
143 |
$buttonarray = [];
|
|
|
144 |
// If provider config is empty this is a new instance.
|
|
|
145 |
if (empty($providerconfigs['id'])) {
|
|
|
146 |
$buttonarray[] = $mform->createElement('submit', 'createandreturn',
|
|
|
147 |
get_string('btninstancecreate', 'core_ai'));
|
|
|
148 |
} else {
|
|
|
149 |
// We're updating an existing provider.
|
|
|
150 |
$buttonarray[] = $mform->createElement('submit', 'updateandreturn',
|
|
|
151 |
get_string('btninstanceupdate', 'core_ai'));
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
$buttonarray[] = $mform->createElement('cancel');
|
|
|
155 |
$mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
|
|
|
156 |
$mform->closeHeaderBefore('buttonar');
|
|
|
157 |
|
|
|
158 |
if ($returnurl) {
|
|
|
159 |
$mform->addElement('hidden', 'returnurl', $returnurl);
|
|
|
160 |
$mform->setType('returnurl', PARAM_LOCALURL);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
if (isset($providerconfigs['id'])) {
|
|
|
164 |
$mform->addElement('hidden', 'id', $providerconfigs['id']);
|
|
|
165 |
$mform->setType('id', PARAM_INT);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
$this->set_data($providerconfigs);
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
#[\Override]
|
|
|
172 |
public function validation($data, $files) {
|
|
|
173 |
$errors = parent::validation($data, $files);
|
|
|
174 |
|
|
|
175 |
// Ensure both global/user rate limits (if enabled) contain positive values.
|
|
|
176 |
if (!empty($data['enableglobalratelimit']) && $data['globalratelimit'] <= 0) {
|
|
|
177 |
$errors['globalratelimit'] = get_string('err_positiveint', 'core_form');
|
|
|
178 |
}
|
|
|
179 |
if (!empty($data['enableuserratelimit']) && $data['userratelimit'] <= 0) {
|
|
|
180 |
$errors['userratelimit'] = get_string('err_positiveint', 'core_form');
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
return $errors;
|
|
|
184 |
}
|
|
|
185 |
}
|