MDL-83970 core_ai: Updated get_name() to return the provider's name

The changes also affect the AI usage report. The provider column will show
the provider plugin name instead of the plugin class in the database.

Co-authored-by: Stevani Andolo <stevani.andolo@moodle.com>
This commit is contained in:
meirzamoodle 2025-02-25 13:56:31 +07:00
parent 7a318d5c85
commit a5b90c1969
5 changed files with 36 additions and 4 deletions

View File

@ -109,8 +109,7 @@ abstract class provider {
* @return string The name of the provider.
*/
public function get_name(): string {
$component = \core\component::get_component_from_classname(get_class($this));
return get_string('pluginname', $component);
return \core\component::get_component_from_classname(get_class($this));
}
/**

View File

@ -90,6 +90,16 @@ class usage extends system_report {
$this->add_columns_from_entities($columns);
$this->get_column('ai_action_register:provider')
->add_callback(static function(string $output): string {
if (get_string_manager()->string_exists('pluginname', $output)) {
return get_string('pluginname', $output);
} else {
// Return as is if the lang string does not exist.
return $output;
}
});
// It's possible to set a default initial sort direction for one column.
$this->set_initial_sort_column('ai_action_register:timecreated', SORT_DESC);
}

View File

@ -1544,7 +1544,7 @@ final class provider_test extends \advanced_testcase {
* Test get_name.
*/
public function test_get_name(): void {
$this->assertEquals(get_string('pluginname', 'aiprovider_openai'), $this->provider->get_name());
$this->assertEquals('aiprovider_openai', $this->provider->get_name());
}
/**

View File

@ -1412,5 +1412,28 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2025013100.01);
}
if ($oldversion < 2025022100.01) {
// Due to a code restriction on the upgrade, invoking any core functions is not permitted.
// Thus, to acquire the list of provider plugins,
// we should extract them from the `config_plugins` database table.
$condition = $DB->sql_like('plugin', ':pattern');
$params = ['pattern' => 'aiprovider_%', 'name' => 'version'];
$sql = "SELECT plugin FROM {config_plugins} WHERE {$condition} AND name = :name";
$providers = $DB->get_fieldset_sql($sql, $params);
foreach ($providers as $provider) {
// Replace the provider's language string with the provider component's name.
if (get_string_manager()->string_exists('pluginname', $provider)) {
$providername = get_string('pluginname', $provider);
$sql = 'UPDATE {ai_action_register}
SET provider = :provider
WHERE LOWER(provider) = :providername';
$DB->execute($sql, ['provider' => $provider, 'providername' => strtolower($providername)]);
}
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2025022100.01);
}
return true;
}

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2025022100.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2025022100.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '5.0dev (Build: 20250221)'; // Human-friendly version name