Skip to content

Commit

Permalink
recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mhughes2k committed Apr 3, 2024
1 parent c8af633 commit 26213ee
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
8 changes: 0 additions & 8 deletions ai/classes/aiprovider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) [
Expand All @@ -235,35 +234,28 @@ 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) {
if ($key == "contextid") {
$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
);
Expand Down
22 changes: 15 additions & 7 deletions ai/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
]);
}

Expand Down Expand Up @@ -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();
}
1 change: 1 addition & 0 deletions lang/en/ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
6 changes: 3 additions & 3 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use core\moodlenet\utilities;
use core_contentbank\contentbank;
use core\ai\api;
use core_ai\api;

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -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
);
Expand Down

0 comments on commit 26213ee

Please sign in to comment.