From 7231409e837d686f567d4e2cee12e0eab2c817c8 Mon Sep 17 00:00:00 2001 From: Matt Porritt Date: Fri, 14 Mar 2025 15:13:51 +1100 Subject: [PATCH] MDL-84862 AI: Stop provider instance creataion when no provider plugins Prevents calls to action to create an AI provider instance from being displayed to users when there are no AI provider plugins installed in the instance. Co-Authored-By: Yusuf Wibisono --- .../admin/admin_setting_notification.php | 74 +++++++++++++++++++ public/admin/settings/ai.php | 26 +++++-- public/ai/configure.php | 6 ++ public/lang/en/ai.php | 1 + 4 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 public/admin/classes/admin/admin_setting_notification.php diff --git a/public/admin/classes/admin/admin_setting_notification.php b/public/admin/classes/admin/admin_setting_notification.php new file mode 100644 index 00000000000..de4b21bf8df --- /dev/null +++ b/public/admin/classes/admin/admin_setting_notification.php @@ -0,0 +1,74 @@ +. + +namespace core_admin\admin; + +use admin_setting; + +/** + * Render a notification as part of other admin settings. + * + * @package core_admin + * @subpackage admin + * @copyright 2025 Matt Porritt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class admin_setting_notification extends admin_setting { + /** + * Constructor. + * + * @param string $name The name of the setting. + * @param string $notification The notification to display. + * @param string $type The type of notification. + * @param bool $cancelable Whether the notification can be canceled. + */ + public function __construct( + string $name, + /** @var string The notification to display. */ + protected string $notification, + /** @var string The type of notification. */ + protected string $type = 'info', + /** @var bool Whether the notification can be canceled. */ + protected bool $cancelable = false + ) { + $this->nosave = true; + + parent::__construct($name, '', '', ''); + } + + #[\Override] + public function get_setting(): bool { + return true; + } + + #[\Override] + public function get_defaultsetting(): bool { + return true; + } + + #[\Override] + public function write_setting($data): string { + // Do not write any setting. + return ''; + } + + #[\Override] + public function output_html($data, $query = ''): string { + global $OUTPUT; + + return $OUTPUT->notification($this->notification, $this->type, $this->cancelable); + } +} diff --git a/public/admin/settings/ai.php b/public/admin/settings/ai.php index ecb00c8c093..5a27bab52de 100644 --- a/public/admin/settings/ai.php +++ b/public/admin/settings/ai.php @@ -32,19 +32,29 @@ if ($hassiteconfig) { $providers->add(new admin_setting_heading('availableproviders', 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( + if (!empty(core_plugin_manager::instance()->get_plugins_of_type("aiprovider"))) { + // 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', new lang_string('manageaiproviders', 'core_ai'), - )); + )); + } else { + $providers->add(new \core_admin\admin\admin_setting_notification( + name:'noproviderplugins', + notification: get_string('noproviderplugins', 'core_ai'), + type: 'danger' + )); + } + $ADMIN->add('ai', $providers); // Add settings page for AI placement settings. diff --git a/public/ai/configure.php b/public/ai/configure.php index 50b787a6f76..9f9e8141f7e 100644 --- a/public/ai/configure.php +++ b/public/ai/configure.php @@ -72,6 +72,12 @@ $PAGE->set_pagelayout('admin'); $PAGE->set_title($title); $PAGE->set_heading($title); +// Explode if there are no provider plugins installed. +$plugins = core_plugin_manager::instance()->get_plugins_of_type('aiprovider'); +if (empty($plugins)) { + throw new moodle_exception('noproviderplugins', 'core_ai'); +} + // Provider instance form processing. $mform = new \core_ai\form\ai_provider_form(customdata: $data); if ($mform->is_cancelled()) { diff --git a/public/lang/en/ai.php b/public/lang/en/ai.php index 07e602c16bf..5cacb0a0c86 100644 --- a/public/lang/en/ai.php +++ b/public/lang/en/ai.php @@ -123,6 +123,7 @@ $string['globalratelimit'] = 'Maximum number of site-wide requests'; $string['globalratelimit_help'] = 'The number of site-wide requests allowed per hour.'; $string['manageaiplacements'] = 'Manage AI placements'; $string['manageaiproviders'] = 'Manage AI providers'; +$string['noproviderplugins'] = 'There are no provider plugins installed. Install a provider plugin to enable provider instance creation.'; $string['noproviders'] = 'This action is unavailable. No AI providers are configured for this action.'; $string['off'] = 'Off'; $string['on'] = 'On';