diff --git a/ai/classes/aiprovider.php b/ai/classes/aiprovider.php index a4bdb4151aa78..56dda3dc1fb46 100644 --- a/ai/classes/aiprovider.php +++ b/ai/classes/aiprovider.php @@ -219,7 +219,6 @@ public static function get_records($filters = [], $sort = '', $order = 'ASC', $s 'completionmodel' => 'llama2', 'contextid' => null, // Global AI Provider 'onlyenrolledcourses' => true - // 'apikey'=> $_ENV['OPENAIKEY'] ]); array_push($records, $fake); $fake = new static(0, (object) [ @@ -235,20 +234,16 @@ public static function get_records($filters = [], $sort = '', $order = 'ASC', $s 'completionmodel' => 'llama2', 'contextid' => 111, // Global AI Provider 'onlyenrolledcourses' => true, - // 'apikey'=> $_ENV['OPENAIKEY'] ]); array_push($records, $fake); $targetcontextid = $filters['contextid'] ?? null; $targetcontext = null; if (is_null($targetcontextid)) { -// debugging("No Context Restriction", DEBUG_DEVELOPER); unset($filters['contextid']); // Because we need special handling. } else { -// debugging("Context Restriction: {$targetcontextid}", DEBUG_DEVELOPER); $targetcontext = \context::instance_by_id($targetcontextid); } -// debugging(print_r($filters,1), DEBUG_DEVELOPER); $records = array_filter($records, function($record) use ($filters, $targetcontext) { $result = true; foreach($filters as $key => $value) { @@ -256,14 +251,11 @@ public static function get_records($filters = [], $sort = '', $order = 'ASC', $s $providercontextid = $record->get('contextid'); if ($providercontextid == self::CONTEXT_ALL_MY_COURSES) { // More problematic. -// debugging('Provider needs to be in one of user\'s courses', DEBUG_DEVELOPER); $result = $result & true; } else if ($providercontextid == null) { // System provider so always matches. -// debugging("System AI provider", DEBUG_DEVELOPER); $result = $result & true; } else { -// debugging("Context linked AI provider", DEBUG_DEVELOPER); $providercontext = \context::instance_by_id( $providercontextid ); diff --git a/ai/index.php b/ai/index.php index f7d312e78efd8..f480f05d19d0e 100644 --- a/ai/index.php +++ b/ai/index.php @@ -15,15 +15,22 @@ $PAGE->set_context(context_system::instance()); $PAGE->set_pagelayout("admin"); -$strheading = get_string('aiprovider', 'ai'); -$PAGE->set_heading($strheading); -$PAGE->set_title($strheading); - $renderer = $PAGE->get_renderer('core', 'ai'); $action = optional_param('action', '', PARAM_ALPHAEXT); // We're using pid as "id" is used to specify contextids. $providerid = optional_param('pid', '', PARAM_RAW); +$incontextid = optional_param('contextid', null, PARAM_RAW); + +$context = !is_null($incontextid) ? \context::instance_by_id($incontextid) : null; + +if (empty($context)) { + $strheading = get_string(get_string('pluginname', 'ai')); +} else { + $strheading = get_string('aiprovidersin', 'ai', $context->get_context_name()); +} +$PAGE->set_heading($strheading); +$PAGE->set_title($strheading); $provider = null; $mform = null; @@ -43,7 +50,8 @@ } $mform = new \core_ai\form\openaiapiprovider(null, [ 'persistent' => $provider, - 'type' => required_param('type', PARAM_RAW) + 'type' => required_param('type', PARAM_RAW), + 'contextid' => $incontextid ]); } @@ -78,10 +86,10 @@ } else { // Display list of providers. $indexpage = new \core_ai\output\index_page( - api::get_providers() + api::get_providers($incontextid) ); echo $OUTPUT->header(); - echo $OUTPUT->heading(get_string('pluginname', 'ai')); + echo $renderer->render_index_page($indexpage); echo $OUTPUT->footer(); } diff --git a/lang/en/ai.php b/lang/en/ai.php index a4b76a272e38c..9139ce95919f8 100644 --- a/lang/en/ai.php +++ b/lang/en/ai.php @@ -23,6 +23,7 @@ */ $string['pluginname'] = 'AI Providers'; $string['aiprovider'] = 'AI Provider'; +$string['aiprovidersin'] = 'AI Providers in {$a}'; $string['addprovider'] = 'Add AI Provider'; $string['anyusercourse'] = 'Any course user is enrolled in'; $string['anywhere'] = 'Anywhere in site'; diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 3b03454d5acd7..08bdb3192ab7f 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -25,7 +25,7 @@ use core\moodlenet\utilities; use core_contentbank\contentbank; -use core\ai\api; +use core_ai\api; defined('MOODLE_INTERNAL') || die(); @@ -5651,8 +5651,8 @@ protected function load_category_settings() { new moodle_url( '/ai/index.php', [ - 'id' => $catcontext->id, - 'action' => api::ACTION_MANAGE_PROVIDERS + 'contextid' => $catcontext->id, + 'action' => api::ACTION_MANAGE_PROVIDERS, ]), navigation_node::TYPE_SETTING );