From eabd31c5d321c8450dacd60fa970dd0c3542c6e2 Mon Sep 17 00:00:00 2001 From: Matt Porritt Date: Fri, 17 Jan 2025 15:35:02 +1100 Subject: [PATCH] MDL-82977 AI: Provider instances Add a new admin setting that allows rendering of a template. This means templated content can be rendered as part of admin forms, giving more flexibility to forms and their actions. --- .../admin/admin_setting_template_render.php | 73 +++++++++++++++++++ admin/settings/ai.php | 20 ++--- ai/templates/admin_add_provider.mustache | 35 +++++++++ 3 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 admin/classes/admin/admin_setting_template_render.php create mode 100644 ai/templates/admin_add_provider.mustache diff --git a/admin/classes/admin/admin_setting_template_render.php b/admin/classes/admin/admin_setting_template_render.php new file mode 100644 index 00000000000..e709163adc8 --- /dev/null +++ b/admin/classes/admin/admin_setting_template_render.php @@ -0,0 +1,73 @@ +. + +namespace core_admin\admin; + +use admin_setting; + +/** + * Render a template as part of other admin settings. + * Use for rendering additional html in settings. + * + * @package core_admin + * @subpackage admin + * @copyright 2024 Matt Porritt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class admin_setting_template_render extends admin_setting { + /** + * Constructor. + * + * @param string $name The name of the setting. + * @param string $templatename The name of the template to render. + * @param array|\stdClass $context The context to pass to the template. + */ + public function __construct( + string $name, + /** @var string The name of the template to render. */ + protected string $templatename, + /** @var array|\stdClass The context to pass to the template. */ + protected array|\stdClass $context + ) { + $this->nosave = true; + + parent::__construct($name, $templatename, '', ''); + } + + #[\Override] + public function get_setting(): bool { + return true; + } + + #[\Override] + public function get_defaultsetting(): bool { + return true; + } + + #[\Override] + // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + public function write_setting($data): string { + // Do not write any setting. + return ''; + } + + #[\Override] + public function output_html($data, $query = ''): string { + global $OUTPUT; + + return $OUTPUT->render_from_template($this->templatename, $this->context); + } +} diff --git a/admin/settings/ai.php b/admin/settings/ai.php index 42705f5955e..ecb00c8c093 100644 --- a/admin/settings/ai.php +++ b/admin/settings/ai.php @@ -30,9 +30,16 @@ if ($hassiteconfig) { // Add settings page for AI provider settings. $providers = new admin_settingpage('aiprovider', new lang_string('aiproviders', 'ai')); $providers->add(new admin_setting_heading('availableproviders', - get_string('availableproviders', 'core_ai'), - get_string('availableproviders_desc', 'core_ai'))); - $providers->add(new \core_admin\admin\admin_setting_plugin_manager( + get_string('availableproviders', 'core_ai'), + get_string('availableproviders_desc', 'core_ai'))); + // Add call to action to add a new provider. + $providers->add(new \core_admin\admin\admin_setting_template_render( + name: 'addnewprovider', + templatename: 'core_ai/admin_add_provider', + context: ['addnewproviderurl' => new moodle_url('/ai/configure.php')] + )); + + $providers->add(new \core_ai\admin\admin_setting_provider_manager( 'aiprovider', \core_ai\table\aiprovider_management_table::class, 'manageaiproviders', @@ -40,13 +47,6 @@ if ($hassiteconfig) { )); $ADMIN->add('ai', $providers); - // Load settings for all AI providers. - $plugins = core_plugin_manager::instance()->get_plugins_of_type('aiprovider'); - foreach ($plugins as $plugin) { - /** @var \core\plugininfo\aiprovider $plugin */ - $plugin->load_settings($ADMIN, 'ai', $hassiteconfig); - } - // Add settings page for AI placement settings. $placements = new admin_settingpage('aiplacement', new lang_string('aiplacements', 'ai')); $placements->add(new admin_setting_heading('availableplacements', diff --git a/ai/templates/admin_add_provider.mustache b/ai/templates/admin_add_provider.mustache new file mode 100644 index 00000000000..e99cfd02aa5 --- /dev/null +++ b/ai/templates/admin_add_provider.mustache @@ -0,0 +1,35 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template core_ai/admin_add_provider + + Template to display a call to action to create new AI providers. + + Context variables required for this template: + * addnewproviderurl - URL to the AI provider setup page. + + Example context (json): + { + "addnewproviderurl": "http://example.com" + } +}} + +