Skip to content

Commit

Permalink
MBS-9809: Provide component and context id
Browse files Browse the repository at this point in the history
  • Loading branch information
PhMemmel committed Jan 7, 2025
1 parent 791cd59 commit e8957d0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion amd/build/editformhelper.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/editformhelper.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions amd/src/editformhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const Selectors = {
/**
* Initialise the format chooser.
*/
export const init = () => {
export const init = (contextid) => {

// Set up strings
var strings={};
Expand Down Expand Up @@ -77,7 +77,8 @@ export const init = () => {
response: sampleanswer.value,
defaultmark: defaultmark.value,
prompt: aiprompt.value,
marksscheme: marksscheme.value
marksscheme: marksscheme.value,
contextid: contextid
},
async: false
}])[0].then(function(airesponse) {
Expand Down
29 changes: 26 additions & 3 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function fetch_ai_grade_parameters(): external_function_parameters
'defaultmark' => new external_value(PARAM_INT, 'The total possible score'),
'prompt' => new external_value(PARAM_TEXT, 'The AI Prompt'),
'marksscheme' => new external_value(PARAM_TEXT, 'The marks scheme'),
'contextid' => new external_value(PARAM_INT, 'The context id'),
]
);

Expand All @@ -55,18 +56,40 @@ public static function fetch_ai_grade_parameters(): external_function_parameters
/**
* Similar to clicking the submit button.
*
* @param array $response
* @param string $response
* @param integer $defaultmark
* @param string $prompt
* @param string $marksscheme
* @param int $contextid the context id
* @return array the response array
*/
public static function fetch_ai_grade($response, $defaultmark, $prompt, $marksscheme): stdClass {
public static function fetch_ai_grade(string $response, int $defaultmark, string $prompt, string $marksscheme, int $contextid): stdClass {
[
'response' => $response,
'defaultmark' => $defaultmark,
'prompt' => $prompt,
'marksscheme' => $marksscheme,
'contextid' => $contextid,
] = self::validate_parameters(self::fetch_ai_grade_parameters(),
[
'response' => $response,
'defaultmark' => $defaultmark,
'prompt' => $prompt,
'marksscheme' => $marksscheme,
'contextid' => $contextid,
]
);
$context = $contextid === 0 ? context_system::instance() : context::instance_by_id($contextid);
self::validate_context($context);

// TODO Eventually move this to a own capability which by default is assigned to a teacher in a course.
require_capability('mod/quiz:grade', $context);

// Build an aitext question instance so we can call the same code that the question type uses when it grades.
$type = 'aitext';
\question_bank::load_question_definition_classes($type);
$aiquestion = new qtype_aitext_question();
$aiquestion->contextid = 0;
$aiquestion->contextid = $contextid;
$aiquestion->qtype = \question_bank::get_qtype('aitext');
// Make sure we have the right data for AI to work with.
if (!empty($response) && !empty($prompt) && $defaultmark > 0) {
Expand Down
2 changes: 1 addition & 1 deletion edit_aitext_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected function definition_inner($mform) {
['rows' => 10], $this->editoroptions);

// Load any JS that we need to make things happen, specifically the prompt tester.
$PAGE->requires->js_call_amd('qtype_aitext/editformhelper', 'init', []);
$PAGE->requires->js_call_amd('qtype_aitext/editformhelper', 'init', [$this->context->id]);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ public function perform_request(string $prompt, string $purpose = 'feedback'): s
$backend = get_config('qtype_aitext', 'backend');
if ($backend == 'local_ai_manager') {
$manager = new local_ai_manager\manager($purpose);
$llmresponse = (object) $manager->perform_request($prompt, ['component' => 'qtype_aitext',
'contextid' => $this->contextid]);
$llmresponse = (object) $manager->perform_request($prompt, 'qtype_aitext', $this->contextid);
if ($llmresponse->get_code() !== 200) {
throw new moodle_exception(
'err_retrievingfeedback',
Expand Down
2 changes: 0 additions & 2 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use qtype_aitext\form\edit_spellcheck;

defined('MOODLE_INTERNAL') || die();
/**
* Generates the output for aitext questions.
Expand Down

0 comments on commit e8957d0

Please sign in to comment.