Skip to content

Commit a9a81f8

Browse files
committed
Changed sampleanswer evaluate to use a disabled text area in the form
and tightend up the spinner while the request is being processed.
1 parent 7569fa3 commit a9a81f8

File tree

6 files changed

+129
-7
lines changed

6 files changed

+129
-7
lines changed

amd/build/responserun.min.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/responserun.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/editformhelper.js.org

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// This file is part of Moodle - http://moodle.org/ //
2+
// Moodle is free software: you can redistribute it and/or modify
3+
// it under the terms of the GNU General Public License as published by
4+
// the Free Software Foundation, either version 3 of the License, or
5+
// (at your option) any later version.
6+
//
7+
// Moodle is distributed in the hope that it will be useful,
8+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
// GNU General Public License for more details.
11+
//
12+
// You should have received a copy of the GNU General Public License
13+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
14+
15+
import {get_strings} from 'core/str';
16+
import Ajax from 'core/ajax';
17+
import Log from 'core/log';
18+
import Notify from 'core/notification';
19+
import {exception as displayException} from 'core/notification';
20+
21+
/**
22+
* Question AI Text Edit Form Helper
23+
*
24+
* @module qtype_aitext/editformhelper
25+
* @copyright 2024 Justin Hunt <justin@poodll.com>
26+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
*/
28+
29+
const Selectors = {
30+
fields: {
31+
sampleanswer: '#id_sampleanswer',
32+
sampleanswerbtn: '#id_sampleanswerbtn',
33+
sampleanswereval: '#id_sampleanswereval',
34+
aiprompt: '#id_aiprompt',
35+
markscheme: '#id_markscheme',
36+
defaultmark: '#id_defaultmark',
37+
},
38+
};
39+
40+
/**
41+
* Initialise the format chooser.
42+
*/
43+
export const init = (contextid) => {
44+
return;
45+
46+
// Set up strings
47+
var strings={};
48+
get_strings([
49+
{ "key": "prompttester", "component": 'qtype_aitext'},
50+
{ "key": "sampleanswerempty", "component": 'qtype_aitext'},
51+
52+
]).done(function (s) {
53+
var i = 0;
54+
strings.prompttester = s[i++];
55+
strings.sampleanswerempty = s[i++];
56+
});
57+
58+
document.querySelector(Selectors.fields.sampleanswerbtn).addEventListener('click', e => {
59+
60+
const form = e.target.closest('form');
61+
const sampleanswer = form.querySelector(Selectors.fields.sampleanswer);
62+
const sampleanswereval = form.querySelector(Selectors.fields.sampleanswereval);
63+
const aiprompt = form.querySelector(Selectors.fields.aiprompt);
64+
const marksscheme = form.querySelector(Selectors.fields.markscheme);
65+
const defaultmark = form.querySelector(Selectors.fields.defaultmark);
66+
67+
if (sampleanswer.value === "" || aiprompt.value === "") {
68+
Notify.alert(strings.prompttester, strings.sampleanswerempty);
69+
return;
70+
}
71+
72+
//put spinner in place
73+
sampleanswereval.innerHTML='<i class="icon fa fa-spinner fa-spin fa-2x"></i>';
74+
75+
Ajax.call([{
76+
methodname: 'qtype_aitext_fetch_ai_grade',
77+
args: {
78+
response: sampleanswer.value,
79+
defaultmark: defaultmark.value,
80+
prompt: aiprompt.value,
81+
marksscheme: marksscheme.value,
82+
contextid: contextid
83+
},
84+
async: false
85+
}])[0].then(function(airesponse) {
86+
Log.debug(airesponse);
87+
if (airesponse.feedback) {
88+
sampleanswereval.textContent = airesponse.feedback + ' (GRADE: ' + airesponse.marks + '/' + defaultmark.value + ')';
89+
}
90+
}).fail(error => {
91+
displayException(error);
92+
sampleanswereval.innerHTML = '';
93+
});
94+
});//end of click
95+
};

amd/src/responserun.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const Selectors = {
3232
sampleanswer: '#id_sampleanswer',
3333
sampleanswerbtn: '#id_sampleanswerbtn',
3434
sampleanswereval: '#id_sampleanswereval',
35+
spinner: '#id_spinner',
3536
aiprompt: '#id_aiprompt',
3637
markscheme: '#id_markscheme',
3738
defaultmark: '#id_defaultmark',
@@ -51,7 +52,6 @@ export const init = (contextid) => {
5152
strings.prompttester = s[i++];
5253
strings.sampleanswerempty = s[i++];
5354
});
54-
5555
document.querySelector(Selectors.fields.sampleanswerbtn).addEventListener('click', e => {
5656
const form = e.target.closest('form');
5757
const sampleanswer = form.querySelector(Selectors.fields.sampleanswer);
@@ -60,14 +60,18 @@ export const init = (contextid) => {
6060
const aiprompt = form.querySelector(Selectors.fields.aiprompt);
6161
const marksscheme = form.querySelector(Selectors.fields.markscheme);
6262
const defaultmark = form.querySelector(Selectors.fields.defaultmark);
63+
const spinner = form.querySelector(Selectors.fields.spinner);
6364

6465
if (sampleanswer.value === "" || aiprompt.value === "") {
6566
Notify.alert(strings.prompttester, strings.sampleanswerempty);
6667
return;
6768
}
6869
// Put spinner in place.
69-
sampleanswereval.innerHTML = '<i class="icon fa fa-cog fa-spin"></i>';
70-
sampleanswereval.classList.remove('hide');
70+
spinner.innerHTML = '<span class="loading-icon icon-no-margin">';
71+
spinner.innerHTML += ' <i class="fa fa-spinner fa-spin fa-3x fa-fw"" title="Loading" role="img" aria-label="Loading"></i>';
72+
spinner.innerHTML += '</span>';
73+
74+
spinner.classList.remove('hide');
7175
Ajax.call([{
7276
methodname: 'qtype_aitext_fetch_ai_grade',
7377
args: {
@@ -82,8 +86,7 @@ export const init = (contextid) => {
8286
Log.debug(airesponse);
8387
if (airesponse.feedback) {
8488
sampleanswereval.textContent = airesponse.feedback + ' (GRADE: ' + airesponse.marks + '/' + defaultmark.value + ')';
85-
sampleanswereval.classList.add("qtype_aitext_sampleanswereval");
86-
89+
spinner.classList.add('hide');
8790
}
8891
return true;
8992
}).fail(error => {

edit_aitext_form.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,28 @@ protected function definition_inner($mform) {
8282
$mform->addElement('html', '</div>');
8383

8484
$mform->addElement('header', 'prompttester', get_string('prompttester', 'qtype_aitext'));
85+
8586
$mform->addElement('textarea', 'sampleanswer', get_string('sampleanswer', 'qtype_aitext'),
8687
['maxlen' => 50, 'rows' => 6, 'size' => 30]);
8788
$mform->setType('sampleanswer', PARAM_RAW);
8889
$mform->setDefault('sampleanswer', '');
89-
$mform->addElement('static', 'sampleanswereval', '', '<div class ="hide col-md-9" id="id_sampleanswereval"></div>');
90+
$mform->addElement('static', 'spinner', '', '<div class ="hide col-md-9" id="id_spinner"></div>');
9091

9192
$mform->addElement('button', 'sampleanswerbtn"', get_string('sampleanswerevaluate', 'qtype_aitext'),);
9293
$mform->registerNoSubmitButton('sampleanswerbtn"');
9394

95+
96+
$options = [
97+
'cols' => 50,
98+
'rows' => 5,
99+
'disabled' => 'disabled',
100+
];
101+
102+
$mform->addElement('textarea', 'sampleanswereval', get_string('sampleanswereval', 'qtype_aitext'), $options);
103+
104+
105+
$mform->setDefault('element_name', 'The default value');
106+
94107
$mform->addElement('header', 'responseoptions', get_string('responseoptions', 'qtype_aitext'));
95108
$mform->setExpanded('responseoptions');
96109

lang/en/qtype_aitext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525

2626
$string['acceptedfiletypes'] = 'Accepted file types';
27-
27+
$string['sampleanswereval'] = 'Sample Answer Evaluation';
2828
$string['aiprompt'] = 'AI Prompt';
2929
$string['aiprompt_help'] = 'A prompt for the Ai Grader. This is the guideline that AI uses to give feedback on the student response.';
3030
$string['aipromptmissing'] = 'The ai prompt is missing. Please enter a prompt on the basis of which the feedback is generated.';

0 commit comments

Comments
 (0)