MDL-26887 Refactor file_save_draft_area_files to separate out
[moodle.git] / question / type / essay / renderer.php
CommitLineData
6d03299d
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
6d03299d
TH
18/**
19 * Essay question renderer class.
20 *
7764183a
TH
21 * @package qtype
22 * @subpackage essay
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6d03299d
TH
25 */
26
27
a17b297d
TH
28defined('MOODLE_INTERNAL') || die();
29
30
6d03299d
TH
31/**
32 * Generates the output for essay questions.
33 *
7764183a
TH
34 * @copyright 2009 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6d03299d
TH
36 */
37class qtype_essay_renderer extends qtype_renderer {
38 public function formulation_and_controls(question_attempt $qa,
39 question_display_options $options) {
40
41 $question = $qa->get_question();
b36d2d06 42 $responseoutput = $question->get_format_renderer($this->page);
6d03299d
TH
43
44 // Answer field.
45 $inputname = $qa->get_qt_field_name('answer');
46 $response = $qa->get_last_qt_var('answer', '');
a54ecbbb 47 $responseformat = $qa->get_last_qt_var('answerformat', FORMAT_HTML);
6d03299d 48 if (empty($options->readonly)) {
caeeff07 49 $answer = $responseoutput->response_area_input($inputname,
a54ecbbb
TH
50 $response, $responseformat, $question->responsefieldlines,
51 $options->context);
6d03299d
TH
52
53 } else {
217f9a61 54 $answer = $responseoutput->response_area_read_only($inputname,
a54ecbbb 55 $response, $responseformat, $question->responsefieldlines);
b36d2d06
TH
56 }
57
58 $files = '';
d42dbe87
TH
59 if ($question->attachments) {
60 if (empty($options->readonly)) {
61 $files = $this->files_input($qa, $question->attachments, $options);
b36d2d06 62
d42dbe87
TH
63 } else {
64 $files = $this->files_read_only($qa, $options);
65 }
6d03299d
TH
66 }
67
68 $result = '';
69 $result .= html_writer::tag('div', $question->format_questiontext($qa),
70 array('class' => 'qtext'));
71
b36d2d06 72 $result .= html_writer::start_tag('div', array('class' => 'ablock'));
6d03299d 73 $result .= html_writer::tag('div', $answer, array('class' => 'answer'));
caeeff07 74 $result .= html_writer::tag('div', $files, array('class' => 'attachments'));
6d03299d
TH
75 $result .= html_writer::end_tag('div');
76
77 return $result;
78 }
b36d2d06
TH
79
80 /**
81 * Displays any attached files when the question is in read-only mode.
d42dbe87
TH
82 * @param question_attempt $qa the question attempt to display.
83 * @param question_display_options $options controls what should and should
84 * not be displayed. Used to get the context.
b36d2d06 85 */
caeeff07 86 public function files_read_only(question_attempt $qa, question_display_options $options) {
8026d4aa 87 $files = $qa->get_last_qt_files('attachments', $options->context->id);
caeeff07
TH
88 $output = array();
89
90 foreach ($files as $file) {
91 $mimetype = $file->get_mimetype();
92 $output[] = html_writer::tag('p', html_writer::link($qa->get_response_file_url($file),
93 $this->output->pix_icon(file_mimetype_icon($mimetype), $mimetype,
94 'moodle', array('class' => 'icon')) . ' ' . s($file->get_filename())));
95 }
96 return implode($output);
b36d2d06
TH
97 }
98
99 /**
100 * Displays the input control for when the student should upload a single file.
d42dbe87
TH
101 * @param question_attempt $qa the question attempt to display.
102 * @param int $numallowed the maximum number of attachments allowed. -1 = unlimited.
103 * @param question_display_options $options controls what should and should
104 * not be displayed. Used to get the context.
b36d2d06 105 */
d42dbe87 106 public function files_input(question_attempt $qa, $numallowed, question_display_options $options) {
8026d4aa
TH
107 global $CFG;
108 require_once($CFG->dirroot . '/lib/form/filemanager.php');
109
110 $pickeroptions = new stdClass();
111 $pickeroptions->mainfile = null;
d42dbe87 112 $pickeroptions->maxfiles = $numallowed;
8026d4aa
TH
113 $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
114 'attachments', $options->context->id);
115 $pickeroptions->context = $options->context;
116
217f9a61
TH
117 return form_filemanager_render($pickeroptions) . html_writer::empty_tag(
118 'input', array('type' => 'hidden', 'name' => $qa->get_qt_field_name('attachments'),
119 'value' => $pickeroptions->itemid));
b36d2d06
TH
120 }
121
783af252
TH
122 public function manual_comment(question_attempt $qa, question_display_options $options) {
123 if ($options->manualcomment != question_display_options::EDITABLE) {
124 return '';
125 }
126
127 $question = $qa->get_question();
128 return html_writer::nonempty_tag('div', $question->format_text(
129 $question->graderinfo, $question->graderinfo, $qa, 'qtype_essay',
130 'graderinfo', $question->id), array('class' => 'graderinfo'));
131 }
b36d2d06
TH
132}
133
134
135/**
136 * A base class to abstract out the differences between different type of
137 * response format.
138 *
139 * @copyright 2011 The Open University
140 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
141 */
142abstract class qtype_essay_format_renderer_base extends plugin_renderer_base {
143 /**
144 * Render the students respone when the question is in read-only mode.
145 * @param string $inputname the field name to use for this input.
146 * @param string $response the student's current response.
147 * @param int $lines approximate size of input box to display.
148 */
a54ecbbb
TH
149 public abstract function response_area_read_only($inputname, $response,
150 $responseformat, $lines);
b36d2d06
TH
151
152 /**
153 * Render the students respone when the question is in read-only mode.
154 * @param string $inputname the field name to use for this input.
155 * @param string $response the student's current response.
156 * @param int $lines approximate size of input box to display.
157 */
a54ecbbb
TH
158 public abstract function response_area_input($inputname, $response,
159 $responseformat, $lines, $contex);
b36d2d06
TH
160}
161
162
163/**
164 * An essay format renderer for essays where the student should use the HTML
165 * editor without the file picker.
166 *
167 * @copyright 2011 The Open University
168 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
169 */
170class qtype_essay_format_editor_renderer extends plugin_renderer_base {
a54ecbbb 171 public function response_area_read_only($inputname, $response, $responseformat, $lines) {
b36d2d06
TH
172 $formatoptions = new stdClass();
173 $formatoptions->para = false;
a54ecbbb
TH
174 $response = $this->rewrite_pluginfile_urls($response);
175 return html_writer::tag('div', format_text($response, $responseformat, $formatoptions),
b36d2d06
TH
176 array('class' => 'qtype_essay_editor qtype_essay_response'));
177 }
178
a54ecbbb
TH
179 public function response_area_input($inputname, $response, $responseformat, $lines, $context) {
180 global $CFG, $PAGE;
181 require_once($CFG->dirroot.'/repository/lib.php');
182
183 $id = $inputname . '_id';
184
185 $editor = editors_get_preferred_editor($responseformat);
186 $strformats = format_text_menu();
187 $formats = $editor->get_supported_formats();
188 foreach ($formats as $fid) {
189 $formats[$fid] = $strformats[$fid];
190 }
191
192 $editor->use_editor($id, $this->get_editor_options($context),
193 $this->get_filepicker_options());
194
195 $output = '';
196 $output .= html_writer::start_tag('div');
197
198 $output .= html_writer::tag('div', html_writer::tag('textarea', s($response),
199 array('id' => $id, 'name' => $inputname, 'rows' => $lines)));
200
201 $output .= html_writer::start_tag('div');
202 if (count($formats == 1)) {
203 reset($formats);
204 $output .= html_writer::empty_tag('input', array('type' => 'hidden',
205 'name' => $inputname . 'format', 'value' => key($formats)));
206
207 } else {
208 $output .= html_writer::select($formats, $inputname . 'format', $responseformat, '');
209 }
210 $output .= html_writer::end_tag('div');
211
212 $output .= $this->nonjs_filepicker();
213
214 $output .= html_writer::end_tag('div');
215 return $output;
216 }
217
218 /**
219 * @param string $response the student's response.
220 * @return string the response with file URLs processed.
221 */
222 protected function rewrite_pluginfile_urls($response) {
223 return $response;
224 }
225
226 /**
227 * @return array filepicker options for the editor.
228 */
229 protected function get_filepicker_options() {
230 return array();
231 }
232
233 /**
234 * @return array options for the editor.
235 */
236 protected function get_editor_options($context) {
237 return array('context' => $context);
238 }
239
240 /**
241 * Extra output for the filepicker, if used.
242 */
243 protected function nonjs_filepicker() {
244 return '';
b36d2d06
TH
245 }
246}
247
248
249/**
250 * An essay format renderer for essays where the student should use the HTML
251 * editor with the file picker.
252 *
253 * @copyright 2011 The Open University
254 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
255 */
a54ecbbb
TH
256class qtype_essay_format_editorfilepicker_renderer extends qtype_essay_format_editor_renderer {
257
258 protected function rewrite_pluginfile_urls($response) {
259 return $response;
260 }
261
262 protected function get_filepicker_options() {
263 return array();
b36d2d06
TH
264 }
265
a54ecbbb
TH
266 protected function nonjs_filepicker() {
267 return '';
b36d2d06
TH
268 }
269}
270
271
272/**
273 * An essay format renderer for essays where the student should use a plain
274 * input box, but with a normal, proportional font.
275 *
276 * @copyright 2011 The Open University
277 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
278 */
279class qtype_essay_format_plain_renderer extends plugin_renderer_base {
280 /**
281 * @return string the HTML for the textarea.
282 */
283 protected function textarea($response, $lines, $attributes) {
284 $attributes['class'] = $this->class_name() . ' qtype_essay_response';
285 $attributes['rows'] = $lines;
286 return html_writer::tag('textarea', s($response), $attributes);
287 }
288
289 protected function class_name() {
290 return 'qtype_essay_plain';
291 }
292
a54ecbbb 293 public function response_area_read_only($inputname, $response, $responseformat, $lines) {
b36d2d06
TH
294 return $this->textarea($response, $lines, array('readonly' => 'readonly'));
295 }
296
a54ecbbb
TH
297 public function response_area_input($inputname, $response, $responseformat, $lines, $contex) {
298 return $this->textarea($response, $lines, array('name' => $inputname)) .
299 html_writer::empty_tag('input', array('type' => 'hidden',
300 'name' => $inputname . 'format', 'value' => FORMAT_PLAIN));
b36d2d06
TH
301 }
302}
303
304
305/**
306 * An essay format renderer for essays where the student should use a plain
307 * input box with a monospaced font. You might use this, for example, for a
308 * question where the students should type computer code.
309 *
310 * @copyright 2011 The Open University
311 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
312 */
313class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
314 protected function class_name() {
315 return 'qtype_essay_monospaced';
316 }
6d03299d 317}