-
Notifications
You must be signed in to change notification settings - Fork 3
/
edit_aitext_form.php
executable file
·211 lines (188 loc) · 9.78 KB
/
edit_aitext_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines the editing form for the aitext question type.
*
* @package qtype_aitext
* @subpackage aitext
* @copyright 2023 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* aitext question type editing form.
*
* @author 2023 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_aitext_edit_form extends question_edit_form {
/**
* Add aitext specific form fields for editing
*
* @param object $mform
* @return void
*/
protected function definition_inner($mform) {
global $PAGE;
/** @var qtype_aitext $qtype */
$qtype = question_bank::get_qtype('aitext');
$mform->removeelement('generalfeedback');
$mform->removeelement('questiontext');
$mform->addElement('editor', 'questiontext', get_string('questiontext', 'mod_quiz'),
['maxlen' => 50, 'rows' => 8, 'size' => 30], $this->editoroptions);
$mform->addElement('textarea', 'aiprompt', get_string('aiprompt', 'qtype_aitext'),
['maxlen' => 50, 'rows' => 5, 'size' => 30]);
$mform->setType('aiprompt', PARAM_RAW);
$mform->setDefault('aiprompt', get_config('qtype_aitext', 'defaultprompt'));
$mform->addHelpButton('aiprompt', 'aiprompt', 'qtype_aitext');
$mform->addRule('aiprompt', get_string('aipromptmissing', 'qtype_aitext'), 'required');
$mform->addElement('textarea', 'markscheme', get_string('markscheme', 'qtype_aitext'),
['maxlen' => 50, 'rows' => 6, 'size' => 30]);
$mform->setType('markscheme', PARAM_RAW);
$mform->setDefault('markscheme', get_config('qtype_aitext', 'defaultmarksscheme'));
$mform->addHelpButton('markscheme', 'markscheme', 'qtype_aitext');
$models = explode(",", get_config('tool_aiconnect', 'model'));
if (count($models) > 1 ) {
$models = array_combine($models, $models);
$mform->addElement('select', 'model', get_string('model', 'qtype_aitext'), $models);
} else {
$mform->addElement('hidden', 'model', $models[0]);
}
$mform->setType('model', PARAM_RAW);
// The question_edit_form that this class extends expects a general feedback field.
$mform->addElement('html', '<div class="hidden">');
$mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'question')
, ['rows' => 10], $this->editoroptions);
$mform->addElement('html', '</div>');
$mform->addElement('header', 'prompttester', get_string('prompttester', 'qtype_aitext'));
$mform->addElement('textarea', 'sampleanswer', get_string('sampleanswer', 'qtype_aitext'),
['maxlen' => 50, 'rows' => 6, 'size' => 30]);
$mform->setType('sampleanswer', PARAM_RAW);
$mform->setDefault('sampleanswer', '');
$mform->addHelpButton('sampleanswer', 'sampleanswer', 'qtype_aitext');
$mform->addElement('static', 'sampleanswereval', '', '<a class="qtype_aitext_sampleanswerbtn btn btn-secondary"
id="id_sampleanswerbtn">'
. get_string('sampleanswerevaluate', 'qtype_aitext') . '</a>' .
'<div class="qtype_aitext_sampleanswereval" id="id_sampleanswereval"></div>');
$mform->addElement('header', 'responseoptions', get_string('responseoptions', 'qtype_aitext'));
$mform->setExpanded('responseoptions');
$mform->addElement('select', 'responseformat',
get_string('responseformat', 'qtype_aitext'), $qtype->response_formats());
$mform->setDefault('responseformat', get_config('qtype_aitext', 'responseformat'));
$mform->addElement('select', 'responsefieldlines',
get_string('responsefieldlines', 'qtype_aitext'), $qtype->response_sizes());
$mform->setDefault('responsefieldlines', $this->get_default_value('responsefieldlines', 10));
$mform->hideIf('responsefieldlines', 'responseformat', 'eq', 'noinline');
// Create a text box that can be enabled/disabled for max/min word limits options.
$wordlimitoptions = ['size' => '6', 'maxlength' => '6'];
$mingrp[] = $mform->createElement('text', 'minwordlimit', '', $wordlimitoptions);
$mform->setType('minwordlimit', PARAM_INT);
$mingrp[] = $mform->createElement('checkbox', 'minwordenabled', '', get_string('enable'));
$mform->setDefault('minwordenabled', 0);
$mform->addGroup($mingrp, 'mingroup', get_string('minwordlimit', 'qtype_aitext'), ' ', false);
$mform->addHelpButton('mingroup', 'minwordlimit', 'qtype_aitext');
$mform->disabledIf('minwordlimit', 'minwordenabled', 'notchecked');
$mform->hideIf('mingroup', 'responseformat', 'eq', 'noinline');
$maxgrp[] = $mform->createElement('text', 'maxwordlimit', '', $wordlimitoptions);
$mform->setType('maxwordlimit', PARAM_INT);
$maxgrp[] = $mform->createElement('checkbox', 'maxwordenabled', '', get_string('enable'));
$mform->setDefault('maxwordenabled', 0);
$mform->addGroup($maxgrp, 'maxgroup', get_string('maxwordlimit', 'qtype_aitext'), ' ', false);
$mform->addHelpButton('maxgroup', 'maxwordlimit', 'qtype_aitext');
$mform->disabledIf('maxwordlimit', 'maxwordenabled', 'notchecked');
$mform->hideIf('maxgroup', 'responseformat', 'eq', 'noinline');
$mform->addElement('header', 'responsetemplateheader', get_string('responsetemplateheader', 'qtype_aitext'));
$mform->addElement('editor', 'responsetemplate', get_string('responsetemplate', 'qtype_aitext'),
['rows' => 10], array_merge($this->editoroptions, ['maxfiles' => 0]));
$mform->addHelpButton('responsetemplate', 'responsetemplate', 'qtype_aitext');
$mform->addElement('header', 'graderinfoheader', get_string('graderinfoheader', 'qtype_aitext'));
$mform->setExpanded('graderinfoheader');
$mform->addElement('editor', 'graderinfo', get_string('graderinfo', 'qtype_aitext'),
['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', []);
}
/**
* Perform any preprocessing needed on the data passed in
* before it is used to initialise the form.
* @param object $question the data being passed to the form.
* @return object $question the modified data.
*/
protected function data_preprocessing($question) {
$question = parent::data_preprocessing($question);
if (empty($question->options)) {
return $question;
}
$question->responseformat = $question->options->responseformat;
$question->responsefieldlines = $question->options->responsefieldlines;
$question->minwordenabled = $question->options->minwordlimit ? 1 : 0;
$question->minwordlimit = $question->options->minwordlimit;
$question->maxwordenabled = $question->options->maxwordlimit ? 1 : 0;
$question->maxwordlimit = $question->options->maxwordlimit;
$question->aiprompt = $question->options->aiprompt;
$question->responsetemplate = [
'text' => $question->options->responsetemplate,
'format' => $question->options->responsetemplateformat,
];
return $question;
}
/**
* Check the question text is valid, specifically that
* any word limits make sense.
*
* @param array $fromform
* @param array $data
* @return array
*/
public function validation($fromform, $data) {
$errors = parent::validation($fromform, $data);
if (isset($fromform['minwordenabled'])) {
if (!is_numeric($fromform['minwordlimit'])) {
$errors['mingroup'] = get_string('err_numeric', 'form');
}
if ($fromform['minwordlimit'] < 0) {
$errors['mingroup'] = get_string('err_minwordlimitnegative', 'qtype_aitext');
}
if (!$fromform['minwordlimit']) {
$errors['mingroup'] = get_string('err_minwordlimit', 'qtype_aitext');
}
}
if (isset($fromform['maxwordenabled'])) {
if (!is_numeric($fromform['maxwordlimit'])) {
$errors['maxgroup'] = get_string('err_numeric', 'form');
}
if ($fromform['maxwordlimit'] < 0) {
$errors['maxgroup'] = get_string('err_maxwordlimitnegative', 'qtype_aitext');
}
if (!$fromform['maxwordlimit']) {
$errors['maxgroup'] = get_string('err_maxwordlimit', 'qtype_aitext');
}
}
if (isset($fromform['maxwordenabled']) && isset($fromform['minwordenabled'])) {
if ($fromform['maxwordlimit'] < $fromform['minwordlimit'] &&
$fromform['maxwordlimit'] > 0 && $fromform['minwordlimit'] > 0) {
$errors['maxgroup'] = get_string('err_maxminmismatch', 'qtype_aitext');
}
}
return $errors;
}
/**
* Name of this question type
* @return string
*/
public function qtype() {
return 'aitext';
}
}