MDL-41090 questions: Move editor and filepicker options into engine
[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
71 public function manual_comment_fields(question_attempt $qa, question_display_options $options) {
2d682972
TH
72 $inputname = $qa->get_behaviour_field_name('comment');
73 $id = $inputname . '_id';
1eb470fc 74 list($commenttext, $commentformat) = $qa->get_current_manual_comment();
d1b7e03d 75
2d682972
TH
76 $editor = editors_get_preferred_editor($commentformat);
77 $strformats = format_text_menu();
78 $formats = $editor->get_supported_formats();
79 foreach ($formats as $fid) {
80 $formats[$fid] = $strformats[$fid];
81 }
82
988592c5 83 $editor->set_text($commenttext);
2d682972
TH
84 $editor->use_editor($id, array('context' => $options->context));
85
86 $commenteditor = html_writer::tag('div', html_writer::tag('textarea', s($commenttext),
87 array('id' => $id, 'name' => $inputname, 'rows' => 10, 'cols' => 60)));
88
c3cdf1e4 89 $editorformat = '';
83690170 90 if (count($formats) == 1) {
2d682972 91 reset($formats);
c3cdf1e4 92 $editorformat .= html_writer::empty_tag('input', array('type' => 'hidden',
2d682972 93 'name' => $inputname . 'format', 'value' => key($formats)));
2d682972 94 } else {
c3cdf1e4
FM
95 $editorformat = html_writer::start_tag('div', array('class' => 'fitem'));
96 $editorformat .= html_writer::start_tag('div', array('class' => 'fitemtitle'));
97 $editorformat .= html_writer::tag('label', get_string('format'), array('for'=>'menu'.$inputname.'format'));
98 $editorformat .= html_writer::end_tag('div');
99 $editorformat .= html_writer::start_tag('div', array('class' => 'felement fhtmleditor'));
100 $editorformat .= html_writer::select($formats, $inputname.'format', $commentformat, '');
101 $editorformat .= html_writer::end_tag('div');
102 $editorformat .= html_writer::end_tag('div');
2d682972 103 }
d1b7e03d 104
d1b7e03d 105 $comment = html_writer::tag('div', html_writer::tag('div',
1631ceea 106 html_writer::tag('label', get_string('comment', 'question'),
2d682972
TH
107 array('for' => $id)), array('class' => 'fitemtitle')) .
108 html_writer::tag('div', $commenteditor, array('class' => 'felement fhtmleditor')),
d1b7e03d 109 array('class' => 'fitem'));
c3cdf1e4 110 $comment .= $editorformat;
d1b7e03d
TH
111
112 $mark = '';
113 if ($qa->get_max_mark()) {
114 $currentmark = $qa->get_current_manual_mark();
115 $maxmark = $qa->get_max_mark();
116
117 $fieldsize = strlen($qa->format_max_mark($options->markdp)) - 1;
118 $markfield = $qa->get_behaviour_field_name('mark');
119
120 $attributes = array(
121 'type' => 'text',
122 'size' => $fieldsize,
123 'name' => $markfield,
c3cdf1e4 124 'id'=> $markfield
d1b7e03d
TH
125 );
126 if (!is_null($currentmark)) {
b2694c02 127 $attributes['value'] = $currentmark;
d1b7e03d 128 }
d1b7e03d
TH
129
130 $markrange = html_writer::empty_tag('input', array(
131 'type' => 'hidden',
132 'name' => $qa->get_behaviour_field_name('maxmark'),
133 'value' => $maxmark,
134 )) . html_writer::empty_tag('input', array(
135 'type' => 'hidden',
136 'name' => $qa->get_control_field_name('minfraction'),
137 'value' => $qa->get_min_fraction(),
4e3d8293
TH
138 )) . html_writer::empty_tag('input', array(
139 'type' => 'hidden',
140 'name' => $qa->get_control_field_name('maxfraction'),
141 'value' => $qa->get_max_fraction(),
d1b7e03d
TH
142 ));
143
b2694c02 144 $error = $qa->validate_manual_mark($currentmark);
d1b7e03d 145 $errorclass = '';
b2694c02
TH
146 if ($error !== '') {
147 $erroclass = ' error';
148 $error = html_writer::tag('span', $error,
d1b7e03d
TH
149 array('class' => 'error')) . html_writer::empty_tag('br');
150 }
151
284c795d
TH
152 $a = new stdClass();
153 $a->max = $qa->format_max_mark($options->markdp);
154 $a->mark = html_writer::empty_tag('input', $attributes);
d1b7e03d 155 $mark = html_writer::tag('div', html_writer::tag('div',
1631ceea
TH
156 html_writer::tag('label', get_string('mark', 'question'),
157 array('for' => $markfield)),
d1b7e03d
TH
158 array('class' => 'fitemtitle')) .
159 html_writer::tag('div', $error . get_string('xoutofmax', 'question', $a) .
160 $markrange, array('class' => 'felement ftext' . $errorclass)
161 ), array('class' => 'fitem'));
d1b7e03d
TH
162 }
163
164 return html_writer::tag('fieldset', html_writer::tag('div', $comment . $mark,
165 array('class' => 'fcontainer clearfix')), array('class' => 'hidden'));
166 }
167
168 public function manual_comment_view(question_attempt $qa, question_display_options $options) {
169 $output = '';
170 if ($qa->has_manual_comment()) {
171 $output .= get_string('commentx', 'question', $qa->get_behaviour()->format_comment());
172 }
173 if ($options->manualcommentlink) {
7ee80cab
TH
174 $url = new moodle_url($options->manualcommentlink, array('slot' => $qa->get_slot()));
175 $link = $this->output->action_link($url, get_string('commentormark', 'question'),
1631ceea
TH
176 new popup_action('click', $url, 'commentquestion',
177 array('width' => 600, 'height' => 800)));
d1b7e03d
TH
178 $output .= html_writer::tag('div', $link, array('class' => 'commentlink'));
179 }
180 return $output;
181 }
182
183 /**
184 * Display the manual comment, and a link to edit it, if appropriate.
185 *
186 * @param question_attempt $qa a question attempt.
187 * @param question_display_options $options controls what should and should not be displayed.
188 * @return string HTML fragment.
189 */
190 public function manual_comment(question_attempt $qa, question_display_options $options) {
191 if ($options->manualcomment == question_display_options::EDITABLE) {
192 return $this->manual_comment_fields($qa, $options);
193
194 } else if ($options->manualcomment == question_display_options::VISIBLE) {
195 return $this->manual_comment_view($qa, $options);
196
197 } else {
198 return '';
199 }
200 }
201
202 /**
203 * Several behaviours need a submit button, so put the common code here.
204 * The button is disabled if the question is displayed read-only.
205 * @param question_display_options $options controls what should and should not be displayed.
206 * @return string HTML fragment.
207 */
208 protected function submit_button(question_attempt $qa, question_display_options $options) {
81e47a35
TH
209 if (!$qa->get_state()->is_active()) {
210 return '';
211 }
d1b7e03d
TH
212 $attributes = array(
213 'type' => 'submit',
214 'id' => $qa->get_behaviour_field_name('submit'),
215 'name' => $qa->get_behaviour_field_name('submit'),
216 'value' => get_string('check', 'question'),
217 'class' => 'submit btn',
218 );
219 if ($options->readonly) {
220 $attributes['disabled'] = 'disabled';
221 }
222 $output = html_writer::empty_tag('input', $attributes);
223 if (!$options->readonly) {
cce1b655
TH
224 $this->page->requires->js_init_call('M.core_question_engine.init_submit_button',
225 array($attributes['id'], $qa->get_slot()));
d1b7e03d
TH
226 }
227 return $output;
228 }
229
230 /**
231 * Return any HTML that needs to be included in the page's <head> when
232 * questions using this model are used.
233 * @param $qa the question attempt that will be displayed on the page.
234 * @return string HTML fragment.
235 */
236 public function head_code(question_attempt $qa) {
237 return '';
238 }
02d3e4d5
TH
239
240 /**
241 * Generate the display of the marks for this question.
242 * @param question_attempt $qa the question attempt to display.
243 * @param core_question_renderer $qoutput the renderer for standard parts of questions.
244 * @param question_display_options $options controls what should and should not be displayed.
245 * @return HTML fragment.
246 */
247 public function mark_summary(question_attempt $qa, core_question_renderer $qoutput,
248 question_display_options $options) {
249 return $qoutput->standard_mark_summary($qa, $this, $options);
250 }
251
252 /**
253 * Generate the display of the available marks for this question.
254 * @param question_attempt $qa the question attempt to display.
255 * @param core_question_renderer $qoutput the renderer for standard parts of questions.
256 * @param question_display_options $options controls what should and should not be displayed.
257 * @return HTML fragment.
258 */
259 public function marked_out_of_max(question_attempt $qa, core_question_renderer $qoutput,
260 question_display_options $options) {
261 return $qoutput->standard_marked_out_of_max($qa, $options);
262 }
263
264 /**
265 * Generate the display of the marks for this question out of the available marks.
266 * @param question_attempt $qa the question attempt to display.
267 * @param core_question_renderer $qoutput the renderer for standard parts of questions.
268 * @param question_display_options $options controls what should and should not be displayed.
269 * @return HTML fragment.
270 */
271 public function mark_out_of_max(question_attempt $qa, core_question_renderer $qoutput,
272 question_display_options $options) {
273 return $qoutput->standard_mark_out_of_max($qa, $options);
274 }
d1b7e03d 275}