MDL-43646 "disapprove" changed to "undo approval"
[moodle.git] / question / type / essay / renderer.php
CommitLineData
6d03299d 1<?php
6d03299d
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
6d03299d
TH
17/**
18 * Essay question renderer class.
19 *
7764183a
TH
20 * @package qtype
21 * @subpackage essay
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6d03299d
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
6d03299d
TH
30/**
31 * Generates the output for essay questions.
32 *
7764183a
TH
33 * @copyright 2009 The Open University
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6d03299d
TH
35 */
36class qtype_essay_renderer extends qtype_renderer {
37 public function formulation_and_controls(question_attempt $qa,
38 question_display_options $options) {
39
40 $question = $qa->get_question();
b36d2d06 41 $responseoutput = $question->get_format_renderer($this->page);
6d03299d
TH
42
43 // Answer field.
48d9c17d 44 $step = $qa->get_last_step_with_qt_var('answer');
60527d0c
JMV
45
46 if (!$step->has_qt_var('answer') && empty($options->readonly)) {
47 // Question has never been answered, fill it with response template.
48 $step = new question_attempt_step(array('answer'=>$question->responsetemplate));
49 }
50
6d03299d 51 if (empty($options->readonly)) {
48d9c17d
TH
52 $answer = $responseoutput->response_area_input('answer', $qa,
53 $step, $question->responsefieldlines, $options->context);
6d03299d
TH
54
55 } else {
48d9c17d
TH
56 $answer = $responseoutput->response_area_read_only('answer', $qa,
57 $step, $question->responsefieldlines, $options->context);
b36d2d06
TH
58 }
59
60 $files = '';
d42dbe87
TH
61 if ($question->attachments) {
62 if (empty($options->readonly)) {
63 $files = $this->files_input($qa, $question->attachments, $options);
b36d2d06 64
d42dbe87
TH
65 } else {
66 $files = $this->files_read_only($qa, $options);
67 }
6d03299d
TH
68 }
69
70 $result = '';
71 $result .= html_writer::tag('div', $question->format_questiontext($qa),
72 array('class' => 'qtext'));
73
b36d2d06 74 $result .= html_writer::start_tag('div', array('class' => 'ablock'));
6d03299d 75 $result .= html_writer::tag('div', $answer, array('class' => 'answer'));
caeeff07 76 $result .= html_writer::tag('div', $files, array('class' => 'attachments'));
6d03299d
TH
77 $result .= html_writer::end_tag('div');
78
79 return $result;
80 }
b36d2d06
TH
81
82 /**
83 * Displays any attached files when the question is in read-only mode.
d42dbe87
TH
84 * @param question_attempt $qa the question attempt to display.
85 * @param question_display_options $options controls what should and should
86 * not be displayed. Used to get the context.
b36d2d06 87 */
caeeff07 88 public function files_read_only(question_attempt $qa, question_display_options $options) {
8026d4aa 89 $files = $qa->get_last_qt_files('attachments', $options->context->id);
caeeff07
TH
90 $output = array();
91
92 foreach ($files as $file) {
caeeff07 93 $output[] = html_writer::tag('p', html_writer::link($qa->get_response_file_url($file),
559276b1 94 $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file),
caeeff07
TH
95 'moodle', array('class' => 'icon')) . ' ' . s($file->get_filename())));
96 }
97 return implode($output);
b36d2d06
TH
98 }
99
100 /**
101 * Displays the input control for when the student should upload a single file.
d42dbe87
TH
102 * @param question_attempt $qa the question attempt to display.
103 * @param int $numallowed the maximum number of attachments allowed. -1 = unlimited.
104 * @param question_display_options $options controls what should and should
105 * not be displayed. Used to get the context.
b36d2d06 106 */
121fd4c1
TH
107 public function files_input(question_attempt $qa, $numallowed,
108 question_display_options $options) {
8026d4aa
TH
109 global $CFG;
110 require_once($CFG->dirroot . '/lib/form/filemanager.php');
111
112 $pickeroptions = new stdClass();
113 $pickeroptions->mainfile = null;
d42dbe87 114 $pickeroptions->maxfiles = $numallowed;
8026d4aa
TH
115 $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
116 'attachments', $options->context->id);
117 $pickeroptions->context = $options->context;
c469b396 118 $pickeroptions->return_types = FILE_INTERNAL;
8026d4aa 119
48d9c17d
TH
120 $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
121 'attachments', $options->context->id);
122
6adb5f13
MG
123 $fm = new form_filemanager($pickeroptions);
124 $filesrenderer = $this->page->get_renderer('core', 'files');
125 return $filesrenderer->render($fm). html_writer::empty_tag(
217f9a61
TH
126 'input', array('type' => 'hidden', 'name' => $qa->get_qt_field_name('attachments'),
127 'value' => $pickeroptions->itemid));
b36d2d06
TH
128 }
129
783af252
TH
130 public function manual_comment(question_attempt $qa, question_display_options $options) {
131 if ($options->manualcomment != question_display_options::EDITABLE) {
132 return '';
133 }
134
135 $question = $qa->get_question();
136 return html_writer::nonempty_tag('div', $question->format_text(
137 $question->graderinfo, $question->graderinfo, $qa, 'qtype_essay',
138 'graderinfo', $question->id), array('class' => 'graderinfo'));
139 }
b36d2d06
TH
140}
141
142
143/**
144 * A base class to abstract out the differences between different type of
145 * response format.
146 *
147 * @copyright 2011 The Open University
148 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
149 */
150abstract class qtype_essay_format_renderer_base extends plugin_renderer_base {
151 /**
152 * Render the students respone when the question is in read-only mode.
48d9c17d
TH
153 * @param string $name the variable name this input edits.
154 * @param question_attempt $qa the question attempt being display.
155 * @param question_attempt_step $step the current step.
b36d2d06 156 * @param int $lines approximate size of input box to display.
48d9c17d
TH
157 * @param object $context the context teh output belongs to.
158 * @return string html to display the response.
b36d2d06 159 */
48d9c17d
TH
160 public abstract function response_area_read_only($name, question_attempt $qa,
161 question_attempt_step $step, $lines, $context);
b36d2d06
TH
162
163 /**
164 * Render the students respone when the question is in read-only mode.
48d9c17d
TH
165 * @param string $name the variable name this input edits.
166 * @param question_attempt $qa the question attempt being display.
167 * @param question_attempt_step $step the current step.
b36d2d06 168 * @param int $lines approximate size of input box to display.
48d9c17d
TH
169 * @param object $context the context teh output belongs to.
170 * @return string html to display the response for editing.
171 */
172 public abstract function response_area_input($name, question_attempt $qa,
173 question_attempt_step $step, $lines, $context);
174
175 /**
176 * @return string specific class name to add to the input element.
b36d2d06 177 */
48d9c17d 178 protected abstract function class_name();
b36d2d06
TH
179}
180
181
182/**
183 * An essay format renderer for essays where the student should use the HTML
184 * editor without the file picker.
185 *
186 * @copyright 2011 The Open University
187 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
188 */
189class qtype_essay_format_editor_renderer extends plugin_renderer_base {
48d9c17d
TH
190 protected function class_name() {
191 return 'qtype_essay_editor';
b36d2d06
TH
192 }
193
48d9c17d
TH
194 public function response_area_read_only($name, $qa, $step, $lines, $context) {
195 return html_writer::tag('div', $this->prepare_response($name, $qa, $step, $context),
9199e7a5 196 array('class' => $this->class_name() . ' qtype_essay_response readonly'));
48d9c17d
TH
197 }
198
199 public function response_area_input($name, $qa, $step, $lines, $context) {
2d682972
TH
200 global $CFG;
201 require_once($CFG->dirroot . '/repository/lib.php');
a54ecbbb 202
48d9c17d
TH
203 $inputname = $qa->get_qt_field_name($name);
204 $responseformat = $step->get_qt_var($name . 'format');
a54ecbbb
TH
205 $id = $inputname . '_id';
206
207 $editor = editors_get_preferred_editor($responseformat);
208 $strformats = format_text_menu();
209 $formats = $editor->get_supported_formats();
210 foreach ($formats as $fid) {
211 $formats[$fid] = $strformats[$fid];
212 }
213
562684e3 214 list($draftitemid, $response) = $this->prepare_response_for_editing(
48d9c17d
TH
215 $name, $step, $context);
216
a54ecbbb 217 $editor->use_editor($id, $this->get_editor_options($context),
48d9c17d 218 $this->get_filepicker_options($context, $draftitemid));
a54ecbbb
TH
219
220 $output = '';
48d9c17d
TH
221 $output .= html_writer::start_tag('div', array('class' =>
222 $this->class_name() . ' qtype_essay_response'));
a54ecbbb 223
562684e3 224 $output .= html_writer::tag('div', html_writer::tag('textarea', s($response),
dbf76faf 225 array('id' => $id, 'name' => $inputname, 'rows' => $lines, 'cols' => 60)));
a54ecbbb
TH
226
227 $output .= html_writer::start_tag('div');
83690170 228 if (count($formats) == 1) {
a54ecbbb
TH
229 reset($formats);
230 $output .= html_writer::empty_tag('input', array('type' => 'hidden',
231 'name' => $inputname . 'format', 'value' => key($formats)));
232
233 } else {
c3cdf1e4
FM
234 $output .= html_writer::label(get_string('format'), 'menu' . $inputname . 'format', false);
235 $output .= ' ';
a54ecbbb
TH
236 $output .= html_writer::select($formats, $inputname . 'format', $responseformat, '');
237 }
238 $output .= html_writer::end_tag('div');
239
48d9c17d 240 $output .= $this->filepicker_html($inputname, $draftitemid);
a54ecbbb
TH
241
242 $output .= html_writer::end_tag('div');
243 return $output;
244 }
245
246 /**
48d9c17d
TH
247 * Prepare the response for read-only display.
248 * @param string $name the variable name this input edits.
249 * @param question_attempt $qa the question attempt being display.
250 * @param question_attempt_step $step the current step.
251 * @param object $context the context the attempt belongs to.
252 * @return string the response prepared for display.
a54ecbbb 253 */
48d9c17d
TH
254 protected function prepare_response($name, question_attempt $qa,
255 question_attempt_step $step, $context) {
256 if (!$step->has_qt_var($name)) {
257 return '';
258 }
259
260 $formatoptions = new stdClass();
261 $formatoptions->para = false;
262 return format_text($step->get_qt_var($name), $step->get_qt_var($name . 'format'),
263 $formatoptions);
a54ecbbb
TH
264 }
265
266 /**
48d9c17d
TH
267 * Prepare the response for editing.
268 * @param string $name the variable name this input edits.
269 * @param question_attempt_step $step the current step.
270 * @param object $context the context the attempt belongs to.
271 * @return string the response prepared for display.
a54ecbbb 272 */
48d9c17d
TH
273 protected function prepare_response_for_editing($name,
274 question_attempt_step $step, $context) {
275 return array(0, $step->get_qt_var($name));
a54ecbbb
TH
276 }
277
278 /**
48d9c17d 279 * @param object $context the context the attempt belongs to.
a54ecbbb
TH
280 * @return array options for the editor.
281 */
282 protected function get_editor_options($context) {
283 return array('context' => $context);
284 }
285
286 /**
48d9c17d
TH
287 * @param object $context the context the attempt belongs to.
288 * @param int $draftitemid draft item id.
289 * @return array filepicker options for the editor.
290 */
291 protected function get_filepicker_options($context, $draftitemid) {
c469b396 292 return array('return_types' => FILE_INTERNAL | FILE_EXTERNAL);
48d9c17d
TH
293 }
294
295 /**
296 * @param string $inputname input field name.
297 * @param int $draftitemid draft file area itemid.
298 * @return string HTML for the filepicker, if used.
a54ecbbb 299 */
48d9c17d 300 protected function filepicker_html($inputname, $draftitemid) {
a54ecbbb 301 return '';
b36d2d06
TH
302 }
303}
304
305
306/**
307 * An essay format renderer for essays where the student should use the HTML
308 * editor with the file picker.
309 *
310 * @copyright 2011 The Open University
311 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
312 */
a54ecbbb 313class qtype_essay_format_editorfilepicker_renderer extends qtype_essay_format_editor_renderer {
48d9c17d
TH
314 protected function class_name() {
315 return 'qtype_essay_editorfilepicker';
316 }
317
318 protected function prepare_response($name, question_attempt $qa,
319 question_attempt_step $step, $context) {
320 if (!$step->has_qt_var($name)) {
321 return '';
322 }
a54ecbbb 323
48d9c17d
TH
324 $formatoptions = new stdClass();
325 $formatoptions->para = false;
326 $text = $qa->rewrite_response_pluginfile_urls($step->get_qt_var($name),
327 $context->id, 'answer', $step);
328 return format_text($text, $step->get_qt_var($name . 'format'), $formatoptions);
a54ecbbb
TH
329 }
330
48d9c17d
TH
331 protected function prepare_response_for_editing($name,
332 question_attempt_step $step, $context) {
333 return $step->prepare_response_files_draft_itemid_with_text(
334 $name, $context->id, $step->get_qt_var($name));
b36d2d06
TH
335 }
336
48d9c17d
TH
337 protected function get_editor_options($context) {
338 return array(
339 'subdirs' => 0,
340 'maxbytes' => 0,
341 'maxfiles' => -1,
342 'context' => $context,
343 'noclean' => 0,
344 'trusttext'=>0
345 );
346 }
347
348 /**
349 * Get the options required to configure the filepicker for one of the editor
350 * toolbar buttons.
351 * @param mixed $acceptedtypes array of types of '*'.
352 * @param int $draftitemid the draft area item id.
353 * @param object $context the context.
354 * @return object the required options.
355 */
356 protected function specific_filepicker_options($acceptedtypes, $draftitemid, $context) {
357 $filepickeroptions = new stdClass();
358 $filepickeroptions->accepted_types = $acceptedtypes;
359 $filepickeroptions->return_types = FILE_INTERNAL | FILE_EXTERNAL;
360 $filepickeroptions->context = $context;
361 $filepickeroptions->env = 'filepicker';
362
363 $options = initialise_filepicker($filepickeroptions);
364 $options->context = $context;
365 $options->client_id = uniqid();
366 $options->env = 'editor';
367 $options->itemid = $draftitemid;
368
369 return $options;
370 }
371
372 protected function get_filepicker_options($context, $draftitemid) {
373 global $CFG;
374
375 return array(
376 'image' => $this->specific_filepicker_options(array('image'),
377 $draftitemid, $context),
378 'media' => $this->specific_filepicker_options(array('video', 'audio'),
379 $draftitemid, $context),
380 'link' => $this->specific_filepicker_options('*',
381 $draftitemid, $context),
382 );
383 }
384
385 protected function filepicker_html($inputname, $draftitemid) {
386 $nonjspickerurl = new moodle_url('/repository/draftfiles_manager.php', array(
387 'action' => 'browse',
388 'env' => 'editor',
389 'itemid' => $draftitemid,
390 'subdirs' => false,
391 'maxfiles' => -1,
392 'sesskey' => sesskey(),
393 ));
394
395 return html_writer::empty_tag('input', array('type' => 'hidden',
396 'name' => $inputname . ':itemid', 'value' => $draftitemid)) .
397 html_writer::tag('noscript', html_writer::tag('div',
398 html_writer::tag('object', '', array('type' => 'text/html',
399 'data' => $nonjspickerurl, 'height' => 160, 'width' => 600,
400 'style' => 'border: 1px solid #000;'))));
b36d2d06
TH
401 }
402}
403
404
405/**
406 * An essay format renderer for essays where the student should use a plain
407 * input box, but with a normal, proportional font.
408 *
409 * @copyright 2011 The Open University
410 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
411 */
412class qtype_essay_format_plain_renderer extends plugin_renderer_base {
413 /**
414 * @return string the HTML for the textarea.
415 */
416 protected function textarea($response, $lines, $attributes) {
417 $attributes['class'] = $this->class_name() . ' qtype_essay_response';
418 $attributes['rows'] = $lines;
dbf76faf 419 $attributes['cols'] = 60;
b36d2d06
TH
420 return html_writer::tag('textarea', s($response), $attributes);
421 }
422
423 protected function class_name() {
424 return 'qtype_essay_plain';
425 }
426
48d9c17d
TH
427 public function response_area_read_only($name, $qa, $step, $lines, $context) {
428 return $this->textarea($step->get_qt_var($name), $lines, array('readonly' => 'readonly'));
b36d2d06
TH
429 }
430
48d9c17d
TH
431 public function response_area_input($name, $qa, $step, $lines, $context) {
432 $inputname = $qa->get_qt_field_name($name);
433 return $this->textarea($step->get_qt_var($name), $lines, array('name' => $inputname)) .
a54ecbbb
TH
434 html_writer::empty_tag('input', array('type' => 'hidden',
435 'name' => $inputname . 'format', 'value' => FORMAT_PLAIN));
b36d2d06
TH
436 }
437}
438
439
440/**
441 * An essay format renderer for essays where the student should use a plain
442 * input box with a monospaced font. You might use this, for example, for a
443 * question where the students should type computer code.
444 *
445 * @copyright 2011 The Open University
446 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
447 */
448class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
449 protected function class_name() {
450 return 'qtype_essay_monospaced';
451 }
6d03299d 452}