diff --git a/admin/classes/admin/admin_setting_notification.php b/admin/classes/admin/admin_setting_notification.php new file mode 100644 index 00000000000..de4b21bf8df --- /dev/null +++ b/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/admin/settings/ai.php b/admin/settings/ai.php index ecb00c8c093..5a27bab52de 100644 --- a/admin/settings/ai.php +++ b/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/ai/configure.php b/ai/configure.php index 50b787a6f76..9f9e8141f7e 100644 --- a/ai/configure.php +++ b/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/lang/en/ai.php b/lang/en/ai.php index 9e4945af312..fed72f53f4e 100644 --- a/lang/en/ai.php +++ b/lang/en/ai.php @@ -96,6 +96,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['placement'] = 'Placement'; $string['placementactionsettings'] = 'Actions';