MDL-20636 Essay questions allowing multiple attachments now work. #216
[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', '');
47 if (empty($options->readonly)) {
caeeff07 48 $answer = $responseoutput->response_area_input($inputname,
b36d2d06 49 $response, $question->responsefieldlines);
6d03299d
TH
50
51 } else {
217f9a61 52 $answer = $responseoutput->response_area_read_only($inputname,
b36d2d06
TH
53 $response, $question->responsefieldlines);
54 }
55
56 $files = '';
d42dbe87
TH
57 if ($question->attachments) {
58 if (empty($options->readonly)) {
59 $files = $this->files_input($qa, $question->attachments, $options);
b36d2d06 60
d42dbe87
TH
61 } else {
62 $files = $this->files_read_only($qa, $options);
63 }
6d03299d
TH
64 }
65
66 $result = '';
67 $result .= html_writer::tag('div', $question->format_questiontext($qa),
68 array('class' => 'qtext'));
69
b36d2d06 70 $result .= html_writer::start_tag('div', array('class' => 'ablock'));
6d03299d 71 $result .= html_writer::tag('div', $answer, array('class' => 'answer'));
caeeff07 72 $result .= html_writer::tag('div', $files, array('class' => 'attachments'));
6d03299d
TH
73 $result .= html_writer::end_tag('div');
74
75 return $result;
76 }
b36d2d06
TH
77
78 /**
79 * Displays any attached files when the question is in read-only mode.
d42dbe87
TH
80 * @param question_attempt $qa the question attempt to display.
81 * @param question_display_options $options controls what should and should
82 * not be displayed. Used to get the context.
b36d2d06 83 */
caeeff07 84 public function files_read_only(question_attempt $qa, question_display_options $options) {
8026d4aa 85 $files = $qa->get_last_qt_files('attachments', $options->context->id);
caeeff07
TH
86 $output = array();
87
88 foreach ($files as $file) {
89 $mimetype = $file->get_mimetype();
90 $output[] = html_writer::tag('p', html_writer::link($qa->get_response_file_url($file),
91 $this->output->pix_icon(file_mimetype_icon($mimetype), $mimetype,
92 'moodle', array('class' => 'icon')) . ' ' . s($file->get_filename())));
93 }
94 return implode($output);
b36d2d06
TH
95 }
96
97 /**
98 * Displays the input control for when the student should upload a single file.
d42dbe87
TH
99 * @param question_attempt $qa the question attempt to display.
100 * @param int $numallowed the maximum number of attachments allowed. -1 = unlimited.
101 * @param question_display_options $options controls what should and should
102 * not be displayed. Used to get the context.
b36d2d06 103 */
d42dbe87 104 public function files_input(question_attempt $qa, $numallowed, question_display_options $options) {
8026d4aa
TH
105 global $CFG;
106 require_once($CFG->dirroot . '/lib/form/filemanager.php');
107
108 $pickeroptions = new stdClass();
109 $pickeroptions->mainfile = null;
d42dbe87 110 $pickeroptions->maxfiles = $numallowed;
8026d4aa
TH
111 $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
112 'attachments', $options->context->id);
113 $pickeroptions->context = $options->context;
114
217f9a61
TH
115 return form_filemanager_render($pickeroptions) . html_writer::empty_tag(
116 'input', array('type' => 'hidden', 'name' => $qa->get_qt_field_name('attachments'),
117 'value' => $pickeroptions->itemid));
b36d2d06
TH
118 }
119
783af252
TH
120 public function manual_comment(question_attempt $qa, question_display_options $options) {
121 if ($options->manualcomment != question_display_options::EDITABLE) {
122 return '';
123 }
124
125 $question = $qa->get_question();
126 return html_writer::nonempty_tag('div', $question->format_text(
127 $question->graderinfo, $question->graderinfo, $qa, 'qtype_essay',
128 'graderinfo', $question->id), array('class' => 'graderinfo'));
129 }
b36d2d06
TH
130}
131
132
133/**
134 * A base class to abstract out the differences between different type of
135 * response format.
136 *
137 * @copyright 2011 The Open University
138 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
139 */
140abstract class qtype_essay_format_renderer_base extends plugin_renderer_base {
141 /**
142 * Render the students respone when the question is in read-only mode.
143 * @param string $inputname the field name to use for this input.
144 * @param string $response the student's current response.
145 * @param int $lines approximate size of input box to display.
146 */
147 public abstract function response_area_read_only($inputname, $response, $lines);
148
149 /**
150 * Render the students respone when the question is in read-only mode.
151 * @param string $inputname the field name to use for this input.
152 * @param string $response the student's current response.
153 * @param int $lines approximate size of input box to display.
154 */
155 public abstract function response_area_input($inputname, $response, $lines);
156}
157
158
159/**
160 * An essay format renderer for essays where the student should use the HTML
161 * editor without the file picker.
162 *
163 * @copyright 2011 The Open University
164 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
165 */
166class qtype_essay_format_editor_renderer extends plugin_renderer_base {
167 public function response_area_read_only($inputname, $response, $lines) {
168 $formatoptions = new stdClass();
169 $formatoptions->para = false;
170 return html_writer::tag('div', format_text($response, FORMAT_HTML, $formatoptions),
171 array('class' => 'qtype_essay_editor qtype_essay_response'));
172 }
173
174 public function response_area_input($inputname, $response, $lines) {
175 return print_textarea(can_use_html_editor(), 18, 80, 630, 400, $inputname, $response, 0, true);
176 }
177}
178
179
180/**
181 * An essay format renderer for essays where the student should use the HTML
182 * editor with the file picker.
183 *
184 * @copyright 2011 The Open University
185 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
186 */
187class qtype_essay_format_editorfilepicker_renderer extends plugin_renderer_base {
188 public function response_area_read_only($inputname, $response, $lines) {
189 return 'Not yet implemented.';
190 }
191
192 public function response_area_input($inputname, $response, $lines) {
193 return 'Not yet implemented.';
194 }
195}
196
197
198/**
199 * An essay format renderer for essays where the student should use a plain
200 * input box, but with a normal, proportional font.
201 *
202 * @copyright 2011 The Open University
203 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
204 */
205class qtype_essay_format_plain_renderer extends plugin_renderer_base {
206 /**
207 * @return string the HTML for the textarea.
208 */
209 protected function textarea($response, $lines, $attributes) {
210 $attributes['class'] = $this->class_name() . ' qtype_essay_response';
211 $attributes['rows'] = $lines;
212 return html_writer::tag('textarea', s($response), $attributes);
213 }
214
215 protected function class_name() {
216 return 'qtype_essay_plain';
217 }
218
219 public function response_area_read_only($inputname, $response, $lines) {
220 return $this->textarea($response, $lines, array('readonly' => 'readonly'));
221 }
222
223 public function response_area_input($inputname, $response, $lines) {
224 return $this->textarea($response, $lines, array('name' => $inputname));
225 }
226}
227
228
229/**
230 * An essay format renderer for essays where the student should use a plain
231 * input box with a monospaced font. You might use this, for example, for a
232 * question where the students should type computer code.
233 *
234 * @copyright 2011 The Open University
235 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
236 */
237class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
238 protected function class_name() {
239 return 'qtype_essay_monospaced';
240 }
6d03299d 241}