MDL-51090: mod_quiz grading validation of an essay question
[moodle.git] / question / behaviour / rendererbase.php
CommitLineData
d1b7e03d 1<?php
d1b7e03d
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
d1b7e03d
TH
17/**
18 * Defines the renderer base class for question behaviours.
19 *
017bc1d9 20 * @package moodlecore
d1b7e03d 21 * @subpackage questionbehaviours
017bc1d9
TH
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
d1b7e03d
TH
30/**
31 * Renderer base class for question behaviours.
32 *
33 * The methods in this class are mostly called from {@link core_question_renderer}
34 * which coordinates the overall output of questions.
35 *
017bc1d9
TH
36 * @copyright 2009 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d 38 */
2b7da645 39abstract class qbehaviour_renderer extends plugin_renderer_base {
d1b7e03d
TH
40 /**
41 * Generate some HTML (which may be blank) that appears in the question
42 * formulation area, afer the question type generated output.
43 *
44 * For example.
45 * immediatefeedback and interactive mode use this to show the Submit button,
46 * and CBM use this to display the certainty choices.
47 *
48 * @param question_attempt $qa a question attempt.
49 * @param question_display_options $options controls what should and should not be displayed.
50 * @return string HTML fragment.
51 */
52 public function controls(question_attempt $qa, question_display_options $options) {
53 return '';
54 }
55
56 /**
57 * Generate some HTML (which may be blank) that appears in the outcome area,
58 * after the question-type generated output.
59 *
60 * For example, the CBM models use this to display an explanation of the score
61 * adjustment that was made based on the certainty selected.
62 *
63 * @param question_attempt $qa a question attempt.
64 * @param question_display_options $options controls what should and should not be displayed.
65 * @return string HTML fragment.
66 */
67 public function feedback(question_attempt $qa, question_display_options $options) {
68 return '';
69 }
70
52b08f5f
NM
71
72
d1b7e03d 73 public function manual_comment_fields(question_attempt $qa, question_display_options $options) {
2d682972
TH
74 $inputname = $qa->get_behaviour_field_name('comment');
75 $id = $inputname . '_id';
1eb470fc 76 list($commenttext, $commentformat) = $qa->get_current_manual_comment();
d1b7e03d 77
2d682972
TH
78 $editor = editors_get_preferred_editor($commentformat);
79 $strformats = format_text_menu();
80 $formats = $editor->get_supported_formats();
81 foreach ($formats as $fid) {
82 $formats[$fid] = $strformats[$fid];
83 }
84
85 $commenttext = format_text($commenttext, $commentformat, array('para' => false));
86
87 $editor->use_editor($id, array('context' => $options->context));
88
89 $commenteditor = html_writer::tag('div', html_writer::tag('textarea', s($commenttext),
90 array('id' => $id, 'name' => $inputname, 'rows' => 10, 'cols' => 60)));
91
c3cdf1e4 92 $editorformat = '';
83690170 93 if (count($formats) == 1) {
2d682972 94 reset($formats);
c3cdf1e4 95 $editorformat .= html_writer::empty_tag('input', array('type' => 'hidden',
2d682972 96 'name' => $inputname . 'format', 'value' => key($formats)));
2d682972 97 } else {
c3cdf1e4
FM
98 $editorformat = html_writer::start_tag('div', array('class' => 'fitem'));
99 $editorformat .= html_writer::start_tag('div', array('class' => 'fitemtitle'));
100 $editorformat .= html_writer::tag('label', get_string('format'), array('for'=>'menu'.$inputname.'format'));
101 $editorformat .= html_writer::end_tag('div');
102 $editorformat .= html_writer::start_tag('div', array('class' => 'felement fhtmleditor'));
103 $editorformat .= html_writer::select($formats, $inputname.'format', $commentformat, '');
104 $editorformat .= html_writer::end_tag('div');
105 $editorformat .= html_writer::end_tag('div');
2d682972 106 }
d1b7e03d 107
d1b7e03d 108 $comment = html_writer::tag('div', html_writer::tag('div',
1631ceea 109 html_writer::tag('label', get_string('comment', 'question'),
2d682972
TH
110 array('for' => $id)), array('class' => 'fitemtitle')) .
111 html_writer::tag('div', $commenteditor, array('class' => 'felement fhtmleditor')),
d1b7e03d 112 array('class' => 'fitem'));
c3cdf1e4 113 $comment .= $editorformat;
d1b7e03d
TH
114
115 $mark = '';
116 if ($qa->get_max_mark()) {
117 $currentmark = $qa->get_current_manual_mark();
118 $maxmark = $qa->get_max_mark();
119
120 $fieldsize = strlen($qa->format_max_mark($options->markdp)) - 1;
121 $markfield = $qa->get_behaviour_field_name('mark');
122
123 $attributes = array(
124 'type' => 'text',
125 'size' => $fieldsize,
126 'name' => $markfield,
c3cdf1e4 127 'id'=> $markfield
d1b7e03d 128 );
52b08f5f 129 if (!is_null($currentmark) && $qa->manual_mark_format_is_ok() ) {
1631ceea
TH
130 $attributes['value'] = $qa->format_fraction_as_mark(
131 $currentmark / $maxmark, $options->markdp);
d1b7e03d 132 }
52b08f5f
NM
133 // We want that the wrong entry is shown.
134 else if (!is_null($currentmark)){
135 $attributes['value'] = $qa->get_submitted_var(
136 $qa->get_behaviour_field_name('mark'), PARAM_RAW_TRIMMED);
137 }
0ff4bd08 138 $a = new stdClass();
d1b7e03d
TH
139 $a->max = $qa->format_max_mark($options->markdp);
140 $a->mark = html_writer::empty_tag('input', $attributes);
141
142 $markrange = html_writer::empty_tag('input', array(
143 'type' => 'hidden',
144 'name' => $qa->get_behaviour_field_name('maxmark'),
145 'value' => $maxmark,
146 )) . html_writer::empty_tag('input', array(
147 'type' => 'hidden',
148 'name' => $qa->get_control_field_name('minfraction'),
149 'value' => $qa->get_min_fraction(),
4e3d8293
TH
150 )) . html_writer::empty_tag('input', array(
151 'type' => 'hidden',
152 'name' => $qa->get_control_field_name('maxfraction'),
153 'value' => $qa->get_max_fraction(),
d1b7e03d
TH
154 ));
155
52b08f5f 156 $error = $qa->get_validation_message();
d1b7e03d 157 $errorclass = '';
52b08f5f
NM
158 if ($error !== ''){
159 $erroclass = 'error';
d1b7e03d
TH
160 }
161
162 $mark = html_writer::tag('div', html_writer::tag('div',
1631ceea
TH
163 html_writer::tag('label', get_string('mark', 'question'),
164 array('for' => $markfield)),
d1b7e03d
TH
165 array('class' => 'fitemtitle')) .
166 html_writer::tag('div', $error . get_string('xoutofmax', 'question', $a) .
167 $markrange, array('class' => 'felement ftext' . $errorclass)
168 ), array('class' => 'fitem'));
d1b7e03d
TH
169 }
170
171 return html_writer::tag('fieldset', html_writer::tag('div', $comment . $mark,
172 array('class' => 'fcontainer clearfix')), array('class' => 'hidden'));
173 }
174
52b08f5f 175
d1b7e03d
TH
176 public function manual_comment_view(question_attempt $qa, question_display_options $options) {
177 $output = '';
178 if ($qa->has_manual_comment()) {
179 $output .= get_string('commentx', 'question', $qa->get_behaviour()->format_comment());
180 }
181 if ($options->manualcommentlink) {
7ee80cab
TH
182 $url = new moodle_url($options->manualcommentlink, array('slot' => $qa->get_slot()));
183 $link = $this->output->action_link($url, get_string('commentormark', 'question'),
1631ceea
TH
184 new popup_action('click', $url, 'commentquestion',
185 array('width' => 600, 'height' => 800)));
d1b7e03d
TH
186 $output .= html_writer::tag('div', $link, array('class' => 'commentlink'));
187 }
188 return $output;
189 }
190
191 /**
192 * Display the manual comment, and a link to edit it, if appropriate.
193 *
194 * @param question_attempt $qa a question attempt.
195 * @param question_display_options $options controls what should and should not be displayed.
196 * @return string HTML fragment.
197 */
198 public function manual_comment(question_attempt $qa, question_display_options $options) {
199 if ($options->manualcomment == question_display_options::EDITABLE) {
200 return $this->manual_comment_fields($qa, $options);
201
202 } else if ($options->manualcomment == question_display_options::VISIBLE) {
203 return $this->manual_comment_view($qa, $options);
204
205 } else {
206 return '';
207 }
208 }
209
210 /**
211 * Several behaviours need a submit button, so put the common code here.
212 * The button is disabled if the question is displayed read-only.
213 * @param question_display_options $options controls what should and should not be displayed.
214 * @return string HTML fragment.
215 */
216 protected function submit_button(question_attempt $qa, question_display_options $options) {
217 $attributes = array(
218 'type' => 'submit',
219 'id' => $qa->get_behaviour_field_name('submit'),
220 'name' => $qa->get_behaviour_field_name('submit'),
221 'value' => get_string('check', 'question'),
222 'class' => 'submit btn',
223 );
224 if ($options->readonly) {
225 $attributes['disabled'] = 'disabled';
226 }
227 $output = html_writer::empty_tag('input', $attributes);
228 if (!$options->readonly) {
cce1b655
TH
229 $this->page->requires->js_init_call('M.core_question_engine.init_submit_button',
230 array($attributes['id'], $qa->get_slot()));
d1b7e03d
TH
231 }
232 return $output;
233 }
234
235 /**
236 * Return any HTML that needs to be included in the page's <head> when
237 * questions using this model are used.
238 * @param $qa the question attempt that will be displayed on the page.
239 * @return string HTML fragment.
240 */
241 public function head_code(question_attempt $qa) {
242 return '';
243 }
02d3e4d5
TH
244
245 /**
246 * Generate the display of the marks for this question.
247 * @param question_attempt $qa the question attempt to display.
248 * @param core_question_renderer $qoutput the renderer for standard parts of questions.
249 * @param question_display_options $options controls what should and should not be displayed.
250 * @return HTML fragment.
251 */
252 public function mark_summary(question_attempt $qa, core_question_renderer $qoutput,
253 question_display_options $options) {
254 return $qoutput->standard_mark_summary($qa, $this, $options);
255 }
256
257 /**
258 * Generate the display of the available marks for this question.
259 * @param question_attempt $qa the question attempt to display.
260 * @param core_question_renderer $qoutput the renderer for standard parts of questions.
261 * @param question_display_options $options controls what should and should not be displayed.
262 * @return HTML fragment.
263 */
264 public function marked_out_of_max(question_attempt $qa, core_question_renderer $qoutput,
265 question_display_options $options) {
266 return $qoutput->standard_marked_out_of_max($qa, $options);
267 }
268
269 /**
270 * Generate the display of the marks for this question out of the available marks.
271 * @param question_attempt $qa the question attempt to display.
272 * @param core_question_renderer $qoutput the renderer for standard parts of questions.
273 * @param question_display_options $options controls what should and should not be displayed.
274 * @return HTML fragment.
275 */
276 public function mark_out_of_max(question_attempt $qa, core_question_renderer $qoutput,
277 question_display_options $options) {
278 return $qoutput->standard_mark_out_of_max($qa, $options);
279 }
d1b7e03d 280}