forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
537 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
<?php | ||
|
||
namespace core\ai; | ||
|
||
namespace core_ai; | ||
class AiException extends \moodle_exception { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
// We're mocking a core Moodle "AI" Subsystem a la Oauth 2 | ||
|
||
namespace core_ai; | ||
use core_ai\aiprovider; | ||
|
||
/** | ||
* AI Help API. | ||
*/ | ||
class api { | ||
|
||
const ACTION_ADD_PROVIDER = "add"; | ||
const ACTION_REMOVE_PROVIDER = "remove"; | ||
const ACTION_EDIT_PROVIDER = "edit"; | ||
const ACTION_MANAGE_PROVIDERS = "manage"; | ||
/** | ||
* Return a list of AIProviders that are available for specified context. | ||
* @param $context | ||
* @return array | ||
*/ | ||
public static function get_all_providers($context = null) { | ||
return array_values(aiprovider::get_records()); | ||
} | ||
public static function get_provider(int $id): AIProvider { | ||
$fakes = aiprovider::get_records(); | ||
return $fakes[0]; // Open AI | ||
// return $fakes[1]; // Ollama | ||
} | ||
|
||
/** | ||
* @param $contextid | ||
* @param $allowchat | ||
* @param $allowembeddings | ||
* @return array | ||
*/ | ||
public static function get_providers($contextid = null, $allowchat = null, $allowembeddings = null) { | ||
$requirements = ['contextid','allowchat', 'allowembeddings']; | ||
// Filtering AI providers that are available to $contextid, walking up the | ||
// tree when we only have the contextid the AIProvider is set *on* is going to take | ||
// more work. | ||
$filters = []; | ||
foreach($requirements as $req) { | ||
$reqparam = ${$req}; | ||
// Null means we don't consider it. | ||
if (!is_null($reqparam)) { | ||
// True means it must be offered | ||
// false means it must *not* be offered by the provider | ||
$filters[$req] = $reqparam; | ||
} | ||
} | ||
//debugging(print_r($filters,1), DEBUG_DEVELOPER); | ||
$providers = aiprovider::get_records($filters); | ||
return array_values($providers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
class index_page implements renderable, templatable { | ||
|
||
private $providers = null; | ||
function __construct($providers) { | ||
$this->providers = $providers; | ||
} | ||
|
||
public function providers_table($providers) { | ||
global $CFG; | ||
$table = new html_table(); | ||
$table->head = [ | ||
'name', | ||
'context', | ||
'completion', | ||
'embeddings', | ||
'status', | ||
'edit' | ||
]; | ||
|
||
$table->attributes['class'] = 'admintable generaltable'; | ||
$data = []; | ||
$contextcache = []; | ||
$index = 0; | ||
foreach ($providers as $provider) { | ||
$first = false; | ||
if ($index == 0) { | ||
$first = true; | ||
} | ||
$last = false; | ||
if ($index == count($providers) - 1) { | ||
$last = true; | ||
} | ||
|
||
$name = $provider->get('name'); | ||
$contextid = $provider->get('contextid'); | ||
$context = ""; | ||
if($contextid >0) { | ||
if ( | ||
!isset($contextcache[$contextid]) | ||
) { | ||
$contextcache[$contextid] = context::instance_by_id($contextid); | ||
} | ||
$contextinstance = $contextcache[$contextid]; | ||
$context = $contextinstance->get_context_name(); | ||
} else if ($contextid < 0){ | ||
$context = "User's own courses"; | ||
} else { | ||
$context = "System"; | ||
} | ||
$completion = $provider->get('allowchat'); | ||
$embeddings = $provider->get('embeddings'); | ||
$status = $provider->get('enabled'); | ||
|
||
// Set up cells. | ||
$namecell = new html_table_cell($name); | ||
$namecell->header = true; | ||
|
||
$contextcell = new html_table_cell($context); | ||
$contextcell->header = true; | ||
|
||
$completioncell = new html_table_cell( | ||
$completion | ||
?"yes"//$this->pix_icon('yes', get_string('enabled','ai')) | ||
:"no"//$this->pix_icon('no', get_string('disabled','ai')) | ||
); | ||
$completioncell->header = true; | ||
|
||
$embeddingscell = new html_table_cell( | ||
$embeddings | ||
?"yes"//$this->pix_icon('yes', get_string('enabled','ai'), 'ai') | ||
:"no"//$this->pix_icon('no', get_string('disabled','ai'), 'ai') | ||
); | ||
$embeddingscell->header = true; | ||
|
||
$statuscell = new html_table_cell($status); | ||
$statuscell->header = true; | ||
|
||
$links = ""; | ||
// Action links. | ||
$editurl = new moodle_url($CFG->wwwroot . '/ai/index.php', | ||
[ | ||
'action' => api::ACTION_EDIT_PROVIDER, | ||
'pid' => $provider->get('id') | ||
]); | ||
$links .= html_writer::link($editurl, 'Edit'); | ||
$editcell = new html_table_cell($links); | ||
$row = new html_table_row([ | ||
$namecell, | ||
$contextcell, | ||
$completioncell, | ||
$embeddingscell, | ||
$statuscell, | ||
$editcell | ||
]); | ||
$data[] = $row; | ||
} | ||
$table->data = $data; | ||
return html_writer::table($table); | ||
} | ||
public function export_for_template(renderer_base $output) : stdClass{ | ||
$data = (object)[ | ||
'providers' => array_values($this->providers_table($this->providers)) | ||
]; | ||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
//namespace core_ai\output; | ||
|
||
use core_ai\api; | ||
|
||
//use \html_table; | ||
//use \html_table_cell; | ||
//use \html_table_row; | ||
//use html_writer; | ||
//use moodle_url; | ||
class core_ai_renderer extends \plugin_renderer_base { | ||
|
||
public function render_index_page($indexpage) { | ||
$data = $indexpage->export_for_template($this); | ||
return parent::render_from_template('core_ai/index_page', $data); | ||
} | ||
|
||
} |
Oops, something went wrong.