Skip to content

Commit

Permalink
edit form
Browse files Browse the repository at this point in the history
  • Loading branch information
mhughes2k committed Mar 21, 2024
1 parent 60221ec commit 5f4e6b9
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 27 deletions.
4 changes: 3 additions & 1 deletion ai/classes/aiprovider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// We're mocking a core Moodle "AI" Subsystem a la Oauth 2
namespace core_ai;

use \core\persistent;
use core\persistent;
use core_course_category;

class AIProvider extends persistent {
// Ultimately this would extend a persistent.

const CONTEXT_ALL_MY_COURSES = -1;

protected static function define_properties()
Expand Down Expand Up @@ -55,6 +56,7 @@ protected static function define_properties()
];
}


/**
* Work out the context path from the site to this AI Provider's context
* @return void
Expand Down
41 changes: 41 additions & 0 deletions ai/classes/form/aiprovider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace core_ai\form;

class aiprovider extends \core\form\persistent{
/** @var string $persistentclass */
protected static $persistentclass = 'core_ai\\aiprovider';

public function __construct($action = null, $customdata = null, $method = 'post', $target = '', $attributes = null,
$editable = true, array $ajaxformdata = null){
if (array_key_exists('type', $customdata)) {
$this->type = $customdata['type'];
}

parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
}
public function definition() {
global $PAGE, $OUTPUT;

$mform = $this->_form;
$provider = $this->get_persistent();
$mform->addElement('html','intro', 'hello');

// Name.
$mform->addElement('text', 'name', get_string('providername', 'ai'));
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('name', 'providername', 'ai');

// Client Secret.
$mform->addElement('text', 'apikey', get_string('apikey', 'ai'));
$mform->addRule('apikey', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('apikey', 'apikey', 'ai');

$mform->addElement('header', 'features', get_string('features', 'ai'));
$mform->addElement('advcheckbox', 'allowchat', get_string('allowchat', 'ai'));
$mform->addHelpButton('allowchat', 'allowchat', 'ai');

$this->add_action_buttons(true, get_string('savechanges', 'ai'));
}
}
62 changes: 42 additions & 20 deletions ai/classes/output/index_page.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace core_ai\output;
use core_ai\api;

class index_page implements renderable, templatable {
class index_page implements \renderable, \templatable {

private $providers = null;
function __construct($providers) {
Expand All @@ -9,7 +11,7 @@ function __construct($providers) {

public function providers_table($providers) {
global $CFG;
$table = new html_table();
$table = new \html_table();
$table->head = [
'name',
'context',
Expand Down Expand Up @@ -40,7 +42,7 @@ public function providers_table($providers) {
if (
!isset($contextcache[$contextid])
) {
$contextcache[$contextid] = context::instance_by_id($contextid);
$contextcache[$contextid] = \context::instance_by_id($contextid);
}
$contextinstance = $contextcache[$contextid];
$context = $contextinstance->get_context_name();
Expand All @@ -54,39 +56,36 @@ public function providers_table($providers) {
$status = $provider->get('enabled');

// Set up cells.
$namecell = new html_table_cell($name);
$namecell = new \html_table_cell($name);
$namecell->header = true;

$contextcell = new html_table_cell($context);
$contextcell->header = true;
$contextcell = new \html_table_cell($context);

$completioncell = new html_table_cell(
$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(
$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 = new \html_table_cell($status);
$statuscell->header = true;

$links = "";
// Action links.
$editurl = new moodle_url($CFG->wwwroot . '/ai/index.php',
$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([
$links .= \html_writer::link($editurl, 'Edit');
$editcell = new \html_table_cell($links);
$row = new \html_table_row([
$namecell,
$contextcell,
$completioncell,
Expand All @@ -97,12 +96,35 @@ public function providers_table($providers) {
$data[] = $row;
}
$table->data = $data;
return html_writer::table($table);
return \html_writer::table($table);
}
public function export_for_template(renderer_base $output) : stdClass{
$data = (object)[
'providers' => array_values($this->providers_table($this->providers))

private $providertypes = ['OpenAI API'];
protected function template_buttons() {
global $CFG;
$buttons = [];
foreach($this->providertypes as $type) {
$addurl = new \moodle_url($CFG->wwwroot . '/ai/index.php',
[
'action' => api::ACTION_EDIT_PROVIDER,
'type' => $type,
'pid' => null
]
);
$buttontext = get_string('newprovider', 'ai', $type);
$buttons[] = [$addurl, $buttontext];
}
return $buttons;
}
public function export_for_template(\renderer_base $output) : object{
$buttondefs = $this->template_buttons();
$buttons = array_map(function($button) use ($output) {
return $output->single_button($button[0], $button[1]);
}, $buttondefs);
$data = [
'providers_table' => $this->providers_table($this->providers),
'templates' => $buttons
];
return $data;
return (object)$data;
}
}
3 changes: 2 additions & 1 deletion ai/classes/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
//use \html_table_row;
//use html_writer;
//use moodle_url;
use core_ai\output\index_page;
class core_ai_renderer extends \plugin_renderer_base {

public function render_index_page($indexpage) {
public function render_index_page(index_page $indexpage) {
$data = $indexpage->export_for_template($this);
return parent::render_from_template('core_ai/index_page', $data);
}
Expand Down
11 changes: 8 additions & 3 deletions ai/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
} else {
// Create new
}
$mform = new \core\ai\form\provider(null, ['persistent' => $provider]);
$mform = new \core_ai\form\aiprovider(null, [
'persistent' => $provider,
'type' => required_param('type', PARAM_RAW)
]);
}


Expand All @@ -60,9 +63,11 @@
// Handle remove.
} else {
// Display list of providers.
$providers = api::get_providers();
$indexpage = new \core_ai\output\index_page(
api::get_providers()
);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'ai'));
echo $renderer->providers_table($providers);
echo $renderer->render_index_page($indexpage);
echo $OUTPUT->footer();
}
7 changes: 5 additions & 2 deletions ai/templates/index_page.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<div>
{{{issuers_table}}}
{{{providers_table}}}
</div>
<div>
{{#str}}createnewprovider, ai{{/str}}
{{#str}}addprovider, ai{{/str}}
{{#templates}}
{{{.}}}
{{/templates}}
</div>

0 comments on commit 5f4e6b9

Please sign in to comment.