Skip to content

Commit 8065fad

Browse files
committed
Add delete buttons
1 parent a3ecbc5 commit 8065fad

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

backup/moodle2/restore_qtype_aitext_plugin.class.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ public static function define_decode_contents() {
102102
*/
103103
protected function after_execute_question() {
104104
global $DB;
105-
106-
$aitextswithoutoptions = $DB->get_records_sql("
105+
$qwithoutoptions = $DB->get_records_sql("
107106
SELECT q.*
108107
FROM {question} q
109108
JOIN {backup_ids_temp} bi ON bi.newitemid = q.id
@@ -114,7 +113,7 @@ protected function after_execute_question() {
114113
AND bi.itemname = ?
115114
", ['aitext', $this->get_restoreid(), 'question_created']);
116115

117-
foreach ($aitextswithoutoptions as $q) {
116+
foreach ($qwithoutoptions as $q) {
118117
$defaultoptions = new stdClass();
119118
$defaultoptions->questionid = $q->id;
120119
$defaultoptions->aiprompt = '';

db/upgrade.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

25-
2625
/**
2726
* Upgrade code for the aitext question type.
2827
*
@@ -100,6 +99,5 @@ function xmldb_qtype_aitext_upgrade($oldversion) {
10099
upgrade_plugin_savepoint(true, 2024071895, 'qtype', 'aitext');
101100
}
102101

103-
104102
return true;
105103
}

edit_aitext_form.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ protected function definition_inner($mform) {
8686
, ['rows' => 10], $this->editoroptions);
8787
$mform->addElement('html', '</div>');
8888

89+
// Add repeated sample answer options along with the field for returned responses.
8990
$mform->addElement('header', 'prompttester', get_string('prompttester', 'qtype_aitext'));
9091

9192
$answeroptions = ['maxlen' => 50, 'rows' => 6, 'size' => 30];
@@ -95,21 +96,23 @@ protected function definition_inner($mform) {
9596
$mform->createElement('textarea', 'sampleanswers', get_string('sampleanswer', 'qtype_aitext'), $answeroptions),
9697
$mform->createElement('textarea', 'sampleanswereval', get_string('sampleanswereval', 'qtype_aitext'), $evaloptions),
9798
$mform->createelement('button', 'sampleanswerbtn', get_string('sampleanswerevaluate', 'qtype_aitext')),
99+
$mform->createElement('submit', 'delete', get_string('deletesample', 'qtype_aitext'), 0),
98100
$mform->createElement('html', '<hr/>'),
99101
];
100102

101103
$repeateloptions = [];
102104
$mform->setType('sampleanswereval', PARAM_CLEANHTML);
103105
$mform->setType('optionid', PARAM_INT);
104106
$samplecount = $this->get_sample_count();
107+
$mform->registerNoSubmitButton('delete');
105108
$this->repeat_elements(
106109
$repeatarray,
107110
$samplecount,
108111
$repeateloptions,
109112
'option_repeats',
110113
'option_add_fields',
111114
1,
112-
null,
115+
get_string('addsample', 'qtype_aitext'),
113116
true,
114117
'delete',
115118
);
@@ -160,15 +163,23 @@ protected function definition_inner($mform) {
160163

161164
// Load any JS that we need to make things happen, specifically the prompt tester.
162165
$PAGE->requires->js_call_amd('qtype_aitext/responserun', 'init', [$this->context->id]);
163-
164166
}
167+
168+
/**
169+
* retrieved from function get_sampleanswers in class quesitontype
170+
*
171+
* @return int count of sample answers
172+
*/
165173
protected function get_sample_count() {
166174
if (isset($this->question->id)) {
167-
if(isset($this->question->options->sampleanswers)) {
168-
return count($this->question->options->sampleanswers);
175+
if (isset($this->question->options->sampleanswers)) {
176+
if (count($this->question->options->sampleanswers) > 0) {
177+
return count($this->question->options->sampleanswers);
178+
}
169179
}
170180
}
171-
return 0;
181+
// Always have one empty sample answer set of fields.
182+
return 1;
172183
}
173184

174185
/**
@@ -195,7 +206,7 @@ protected function data_preprocessing($question) {
195206
$question->spellcheck = $question->options->spellcheck;
196207
// Make the count start from 0 like the repeat array elements.
197208
$question->sampleanswers = [];
198-
foreach($question->options->sampleanswers as $sampleanswer) {
209+
foreach ($question->options->sampleanswers as $sampleanswer) {
199210
$question->sampleanswers[] = $sampleanswer->response;
200211
}
201212

lang/en/qtype_aitext.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
$string['acceptedfiletypes'] = 'Accepted file types';
27+
$string['addsample'] = 'Add a sample response';
2728
$string['aiprompt'] = 'AI Prompt';
2829
$string['aiprompt_help'] = 'A prompt for the Ai Grader. This is the guideline that AI uses to give feedback on the student response.';
2930
$string['aipromptmissing'] = 'The ai prompt is missing. Please enter a prompt on the basis of which the feedback is generated.';
@@ -39,6 +40,7 @@
3940
$string['defaultmarksscheme_setting'] = 'This will be the default marks scheme for new questions. Questions authors should alter this to suit the question.';
4041
$string['defaultprompt'] = 'AI Prompt';
4142
$string['defaultprompt_setting'] = 'This will be the default AI prompt for new questions. It tells the AI grader how to analyse the student response. It is the guideline that AI uses to give feedback on the student response. Question authors should alter this to suit the question.';
43+
$string['deletesample'] = 'Delete sample';
4244
$string['disclaimer'] = 'Disclaimer';
4345
$string['disclaimer_setting'] = 'Text appended to each response indicating feedback is from a Large Language Model and not a human';
4446
$string['err_invalidbackend'] = 'Err invalidbackend;';
@@ -105,17 +107,17 @@
105107
$string['sampleanswer'] = 'Sample Answer';
106108
$string['sampleanswer_help'] = 'The sample answer can be used to test how the AI grader will respond to a given response.';
107109
$string['sampleanswerempty'] = 'Make sure that you have an AI prompt and sample answer before testing.';
108-
$string['sampleanswerevaluate'] = 'Evaluate Sample Answer';
109110
$string['sampleanswereval'] = 'Sample Answer Evaluation';
111+
$string['sampleanswerevaluate'] = 'Evaluate Sample Answer';
110112
$string['showprompt'] = 'Show prompt';
111113
$string['spellcheck_editor_desc'] = 'This is the text in which the spelling mistakes have been corrected by AI. You can edit this suggested correction.';
112114
$string['spellcheck_prompt'] = 'Reproduce the text 1:1. Give no feedback!. But correct all spelling mistakes in the following text: ';
113115
$string['spellcheck_student_anser_desc'] = 'This is the original student\'s answer';
114116
$string['spellcheckedit'] = 'Edit spellcheck';
115117
$string['spellcheckeditor'] = 'Edit the ai based spellcheck';
118+
$string['testresponses'] = 'Test responses';
116119
$string['thedefaultmarksscheme'] = 'Deduct a point from the total score for each grammar or spelling mistake.';
117120
$string['thedefaultprompt'] = 'Explain if there is anything wrong with the grammar and spelling in the text.';
118-
$string['testresponses'] = 'Test responses';
119121
$string['toolaimanager'] = 'Tool AI Manager';
120122
$string['translatepostfix'] = 'Translate postfix';
121123
$string['translatepostfix_text'] = 'The end of the prompt has &quot;translate the feedback to the language .current_language()&quot; appended';

questiontype.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ protected function initialise_question_instance(question_definition $question, $
173173
$question->aiprompt = $questiondata->options->aiprompt;
174174
$question->markscheme = $questiondata->options->markscheme;
175175
parent::get_question_options($question);
176+
$question->options->sampleanswers = $this->get_sampleanswers($question);
176177

177178
/* Legacy quesitons may not have a model set, so assign the first in the settings */
178179
if (empty($question->model)) {
@@ -183,11 +184,18 @@ protected function initialise_question_instance(question_definition $question, $
183184
}
184185
}
185186

187+
/**
188+
* Get the structure from the database
189+
*
190+
* @param qtype_aitext $question
191+
* @return array
192+
*/
186193
public function get_sampleanswers($question) {
187194
global $DB;
188195
$sampleanswers = $DB->get_records('qtype_aitext_sampleanswers', ['question' => $question->id]);
189196
return $sampleanswers;
190197
}
198+
191199
/**
192200
* Delete a question from the database
193201
*

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'qtype_aitext';
28-
$plugin->version = 2024071895;
28+
$plugin->version = 2024111900;
2929
$plugin->requires = 2020110900;
3030
$plugin->release = '0.02';
3131
$plugin->maturity = MATURITY_BETA;

0 commit comments

Comments
 (0)