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\reportbuilder\local\entities;
|
|
|
18 |
|
|
|
19 |
use core\di;
|
|
|
20 |
use core_ai\manager;
|
|
|
21 |
use core\component;
|
|
|
22 |
use core_reportbuilder\local\entities\base;
|
|
|
23 |
use core_reportbuilder\local\filters\boolean_select;
|
|
|
24 |
use core_reportbuilder\local\filters\date;
|
|
|
25 |
use core_reportbuilder\local\filters\number;
|
|
|
26 |
use core_reportbuilder\local\filters\select;
|
|
|
27 |
use core_reportbuilder\local\helpers\format;
|
|
|
28 |
use core_reportbuilder\local\report\column;
|
|
|
29 |
use core_reportbuilder\local\report\filter;
|
|
|
30 |
use core\output\help_icon;
|
|
|
31 |
use lang_string;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* AI action register entity.
|
|
|
35 |
*
|
|
|
36 |
* Defines all the columns and filters that can be added to reports that use this entity.
|
|
|
37 |
*
|
|
|
38 |
* @package core_ai
|
|
|
39 |
* @copyright 2024 David Woloszyn <david.woloszyn@moodle.com>
|
|
|
40 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
41 |
*/
|
|
|
42 |
class ai_action_register extends base {
|
|
|
43 |
|
|
|
44 |
#[\Override]
|
|
|
45 |
protected function get_default_tables(): array {
|
|
|
46 |
return [
|
|
|
47 |
'ai_action_register',
|
|
|
48 |
];
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
#[\Override]
|
|
|
52 |
protected function get_default_entity_title(): lang_string {
|
|
|
53 |
return new lang_string('aiactionregister', 'core_ai');
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
#[\Override]
|
|
|
57 |
public function initialise(): base {
|
|
|
58 |
$columns = $this->get_all_columns();
|
|
|
59 |
foreach ($columns as $column) {
|
|
|
60 |
$this->add_column($column);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// All the filters defined by the entity can also be used as conditions.
|
|
|
64 |
$filters = $this->get_all_filters();
|
|
|
65 |
foreach ($filters as $filter) {
|
|
|
66 |
$this
|
|
|
67 |
->add_filter($filter)
|
|
|
68 |
->add_condition($filter);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
return $this;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Returns list of all available columns.
|
|
|
76 |
*
|
|
|
77 |
* @return column[]
|
|
|
78 |
*/
|
|
|
79 |
protected function get_all_columns(): array {
|
|
|
80 |
$mainalias = $this->get_table_alias('ai_action_register');
|
|
|
81 |
$generatetextalias = 'aagt';
|
|
|
82 |
$summarisetextalias = 'aast';
|
|
|
83 |
$explaintextalias = 'aaet';
|
|
|
84 |
|
|
|
85 |
// Action name column.
|
|
|
86 |
$columns[] = (new column(
|
|
|
87 |
'actionname',
|
|
|
88 |
new lang_string('action', 'core_ai'),
|
|
|
89 |
$this->get_entity_name(),
|
|
|
90 |
))
|
|
|
91 |
->add_joins($this->get_joins())
|
|
|
92 |
->set_type(column::TYPE_TEXT)
|
|
|
93 |
->add_field("{$mainalias}.actionname")
|
|
|
94 |
->set_is_sortable(true)
|
|
|
95 |
->add_callback(static function(string $actionname): string {
|
|
|
96 |
return get_string("action_{$actionname}", 'core_ai');
|
|
|
97 |
});
|
|
|
98 |
|
|
|
99 |
// Provider column.
|
|
|
100 |
$columns[] = (new column(
|
|
|
101 |
'provider',
|
|
|
102 |
new lang_string('provider', 'core_ai'),
|
|
|
103 |
$this->get_entity_name(),
|
|
|
104 |
))
|
|
|
105 |
->add_joins($this->get_joins())
|
|
|
106 |
->set_type(column::TYPE_TEXT)
|
|
|
107 |
->add_field("{$mainalias}.provider")
|
|
|
108 |
->set_is_sortable(true)
|
|
|
109 |
->add_callback(static function(string $provider): string {
|
|
|
110 |
if (get_string_manager()->string_exists('pluginname', $provider)) {
|
|
|
111 |
return get_string('pluginname', $provider);
|
|
|
112 |
} else {
|
|
|
113 |
// Return as is if the lang string does not exist.
|
|
|
114 |
return $provider;
|
|
|
115 |
}
|
|
|
116 |
});
|
|
|
117 |
|
|
|
118 |
// Success column.
|
|
|
119 |
$columns[] = (new column(
|
|
|
120 |
'success',
|
|
|
121 |
new lang_string('success', 'moodle'),
|
|
|
122 |
$this->get_entity_name(),
|
|
|
123 |
))
|
|
|
124 |
->add_joins($this->get_joins())
|
|
|
125 |
->set_type(column::TYPE_BOOLEAN)
|
|
|
126 |
->add_field("{$mainalias}.success")
|
|
|
127 |
->set_is_sortable(true)
|
|
|
128 |
->set_callback([format::class, 'boolean_as_text']);
|
|
|
129 |
|
|
|
130 |
// Time created column.
|
|
|
131 |
$columns[] = (new column(
|
|
|
132 |
'timecreated',
|
|
|
133 |
new lang_string('timegenerated', 'core_ai'),
|
|
|
134 |
$this->get_entity_name(),
|
|
|
135 |
))
|
|
|
136 |
->add_joins($this->get_joins())
|
|
|
137 |
->set_type(column::TYPE_TIMESTAMP)
|
|
|
138 |
->add_field("{$mainalias}.timecreated")
|
|
|
139 |
->set_is_sortable(true)
|
|
|
140 |
->add_callback([format::class, 'userdate']);
|
|
|
141 |
|
|
|
142 |
// Prompt tokens column.
|
|
|
143 |
// Only available for summarise_text, generate_text actions and explain_text actions.
|
|
|
144 |
$columns[] = (new column(
|
|
|
145 |
'prompttokens',
|
|
|
146 |
new lang_string('prompttokens', 'core_ai'),
|
|
|
147 |
$this->get_entity_name(),
|
|
|
148 |
))
|
|
|
149 |
->add_joins($this->get_joins())
|
|
|
150 |
->add_join("
|
|
|
151 |
LEFT JOIN {ai_action_generate_text} {$generatetextalias}
|
|
|
152 |
ON {$mainalias}.actionid = {$generatetextalias}.id
|
|
|
153 |
AND {$mainalias}.actionname = 'generate_text'")
|
|
|
154 |
->add_join("
|
|
|
155 |
LEFT JOIN {ai_action_summarise_text} {$summarisetextalias}
|
|
|
156 |
ON {$mainalias}.actionid = {$summarisetextalias}.id
|
|
|
157 |
AND {$mainalias}.actionname = 'summarise_text'")
|
|
|
158 |
->add_join("
|
|
|
159 |
LEFT JOIN {ai_action_explain_text} {$explaintextalias}
|
|
|
160 |
ON {$mainalias}.actionid = {$explaintextalias}.id
|
|
|
161 |
AND {$mainalias}.actionname = 'explain_text'")
|
|
|
162 |
->set_type(column::TYPE_INTEGER)
|
|
|
163 |
->add_field("COALESCE({$generatetextalias}.prompttokens, {$summarisetextalias}.prompttokens,
|
|
|
164 |
{$explaintextalias}.prompttokens)", 'prompttokens')
|
|
|
165 |
->set_is_sortable(true)
|
|
|
166 |
->set_help_icon(new help_icon('prompttokens', 'core_ai'))
|
|
|
167 |
->add_callback(static function(?int $value): string {
|
|
|
168 |
return $value ?? get_string('unknownvalue', 'core_ai');
|
|
|
169 |
});
|
|
|
170 |
|
|
|
171 |
// Completion tokens column.
|
|
|
172 |
// Only available for summarise_text, generate_text actions and explain_text actions.
|
|
|
173 |
$columns[] = (new column(
|
|
|
174 |
'completiontokens',
|
|
|
175 |
new lang_string('completiontokens', 'core_ai'),
|
|
|
176 |
$this->get_entity_name(),
|
|
|
177 |
))
|
|
|
178 |
->add_joins($this->get_joins())
|
|
|
179 |
->add_join("
|
|
|
180 |
LEFT JOIN {ai_action_generate_text} {$generatetextalias}
|
|
|
181 |
ON {$mainalias}.actionid = {$generatetextalias}.id
|
|
|
182 |
AND {$mainalias}.actionname = 'generate_text'")
|
|
|
183 |
->add_join("
|
|
|
184 |
LEFT JOIN {ai_action_summarise_text} {$summarisetextalias}
|
|
|
185 |
ON {$mainalias}.actionid = {$summarisetextalias}.id
|
|
|
186 |
AND {$mainalias}.actionname = 'summarise_text'")
|
|
|
187 |
->add_join("
|
|
|
188 |
LEFT JOIN {ai_action_explain_text} {$explaintextalias}
|
|
|
189 |
ON {$mainalias}.actionid = {$explaintextalias}.id
|
|
|
190 |
AND {$mainalias}.actionname = 'explain_text'")
|
|
|
191 |
->set_type(column::TYPE_INTEGER)
|
|
|
192 |
->add_field("COALESCE({$generatetextalias}.completiontoken, {$summarisetextalias}.completiontoken,
|
|
|
193 |
{$explaintextalias}.completiontoken)", 'completiontokens')
|
|
|
194 |
->set_is_sortable(true)
|
|
|
195 |
->set_help_icon(new help_icon('completiontokens', 'core_ai'))
|
|
|
196 |
->add_callback(static function(?int $value): string {
|
|
|
197 |
return $value ?? get_string('unknownvalue', 'core_ai');
|
|
|
198 |
});
|
|
|
199 |
|
|
|
200 |
return $columns;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Return list of all available filters.
|
|
|
205 |
*
|
|
|
206 |
* @return filter[]
|
|
|
207 |
*/
|
|
|
208 |
protected function get_all_filters(): array {
|
|
|
209 |
$mainalias = $this->get_table_alias('ai_action_register');
|
|
|
210 |
$generatetextalias = 'aagt';
|
|
|
211 |
$summarisetextalias = 'aast';
|
|
|
212 |
$explaintextalias = 'aaet';
|
|
|
213 |
|
|
|
214 |
// Action name filter.
|
|
|
215 |
$filters[] = (new filter(
|
|
|
216 |
select::class,
|
|
|
217 |
'actionname',
|
|
|
218 |
new lang_string('action', 'core_ai'),
|
|
|
219 |
$this->get_entity_name(),
|
|
|
220 |
"{$mainalias}.actionname",
|
|
|
221 |
))
|
|
|
222 |
->add_joins($this->get_joins())
|
|
|
223 |
->set_options([
|
|
|
224 |
'explain_text' => new lang_string('action_explain_text', 'core_ai'),
|
|
|
225 |
'generate_image' => new lang_string('action_generate_image', 'core_ai'),
|
|
|
226 |
'generate_text' => new lang_string('action_generate_text', 'core_ai'),
|
|
|
227 |
'summarise_text' => new lang_string('action_summarise_text', 'core_ai'),
|
|
|
228 |
]);
|
|
|
229 |
|
|
|
230 |
// Provider filter.
|
|
|
231 |
$filters[] = (new filter(
|
|
|
232 |
select::class,
|
|
|
233 |
'provider',
|
|
|
234 |
new lang_string('provider', 'core_ai'),
|
|
|
235 |
$this->get_entity_name(),
|
|
|
236 |
"{$mainalias}.provider",
|
|
|
237 |
))
|
|
|
238 |
->add_joins($this->get_joins())
|
|
|
239 |
->set_options_callback(static function(): array {
|
|
|
240 |
$providers = [];
|
|
|
241 |
|
|
|
242 |
$records = di::get(manager::class)->get_provider_records();
|
|
|
243 |
foreach ($records as $record) {
|
|
|
244 |
$component = component::get_component_from_classname($record->provider);
|
|
|
245 |
$providers[$component] = get_string('pluginname', $component);
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
return $providers;
|
|
|
249 |
});
|
|
|
250 |
|
|
|
251 |
// Time created filter.
|
|
|
252 |
$filters[] = (new filter(
|
|
|
253 |
date::class,
|
|
|
254 |
'timecreated',
|
|
|
255 |
new lang_string('timegenerated', 'core_ai'),
|
|
|
256 |
$this->get_entity_name(),
|
|
|
257 |
"{$mainalias}.timecreated",
|
|
|
258 |
))
|
|
|
259 |
->add_joins($this->get_joins())
|
|
|
260 |
->set_limited_operators([
|
|
|
261 |
date::DATE_ANY,
|
|
|
262 |
date::DATE_RANGE,
|
|
|
263 |
date::DATE_PREVIOUS,
|
|
|
264 |
date::DATE_CURRENT,
|
|
|
265 |
]);
|
|
|
266 |
|
|
|
267 |
// Prompt tokens filter.
|
|
|
268 |
$filters[] = (new filter(
|
|
|
269 |
number::class,
|
|
|
270 |
'prompttokens',
|
|
|
271 |
new lang_string('prompttokens', 'core_ai'),
|
|
|
272 |
$this->get_entity_name(),
|
|
|
273 |
"COALESCE({$generatetextalias}.prompttokens, {$summarisetextalias}.prompttokens,
|
|
|
274 |
{$explaintextalias}.prompttokens)",
|
|
|
275 |
))
|
|
|
276 |
->add_joins($this->get_joins());
|
|
|
277 |
|
|
|
278 |
// Completion tokens filter.
|
|
|
279 |
$filters[] = (new filter(
|
|
|
280 |
number::class,
|
|
|
281 |
'completiontokens',
|
|
|
282 |
new lang_string('completiontokens', 'core_ai'),
|
|
|
283 |
$this->get_entity_name(),
|
|
|
284 |
"COALESCE({$generatetextalias}.completiontoken, {$summarisetextalias}.completiontoken,
|
|
|
285 |
{$explaintextalias}.completiontoken)",
|
|
|
286 |
))
|
|
|
287 |
->add_joins($this->get_joins());
|
|
|
288 |
|
|
|
289 |
// Success filter.
|
|
|
290 |
$filters[] = (new filter(
|
|
|
291 |
boolean_select::class,
|
|
|
292 |
'success',
|
|
|
293 |
new lang_string('success', 'moodle'),
|
|
|
294 |
$this->get_entity_name(),
|
|
|
295 |
"{$mainalias}.success",
|
|
|
296 |
))
|
|
|
297 |
->add_joins($this->get_joins());
|
|
|
298 |
|
|
|
299 |
return $filters;
|
|
|
300 |
}
|
|
|
301 |
}
|