MDL-20636 Handle deleting response files when the usage is deleted. #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 = '';
57 if (empty($options->readonly)) {
58 if ($question->attachments == 1) {
caeeff07 59 $files = $this->filepicker_input($qa, $options);
b36d2d06 60 } else if ($question->attachments != 0) {
caeeff07 61 $files = $this->filemanager_input($qa, $options);
b36d2d06
TH
62 }
63
64 } else if ($question->attachments != 0) {
caeeff07 65 $files = $this->files_read_only($qa, $options);
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.
82 */
caeeff07 83 public function files_read_only(question_attempt $qa, question_display_options $options) {
8026d4aa 84 $files = $qa->get_last_qt_files('attachments', $options->context->id);
caeeff07
TH
85 $output = array();
86
87 foreach ($files as $file) {
88 $mimetype = $file->get_mimetype();
89 $output[] = html_writer::tag('p', html_writer::link($qa->get_response_file_url($file),
90 $this->output->pix_icon(file_mimetype_icon($mimetype), $mimetype,
91 'moodle', array('class' => 'icon')) . ' ' . s($file->get_filename())));
92 }
93 return implode($output);
b36d2d06
TH
94 }
95
96 /**
97 * Displays the input control for when the student should upload a single file.
98 */
caeeff07 99 public function filepicker_input(question_attempt $qa, question_display_options $options) {
8026d4aa
TH
100 global $CFG;
101 require_once($CFG->dirroot . '/lib/form/filemanager.php');
102
103 $pickeroptions = new stdClass();
104 $pickeroptions->mainfile = null;
105 $pickeroptions->maxfiles = 1;
106 $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
107 'attachments', $options->context->id);
108 $pickeroptions->context = $options->context;
109
217f9a61
TH
110 return form_filemanager_render($pickeroptions) . html_writer::empty_tag(
111 'input', array('type' => 'hidden', 'name' => $qa->get_qt_field_name('attachments'),
112 'value' => $pickeroptions->itemid));
b36d2d06
TH
113 }
114
115 /**
116 * Displays the input control for when the student should upload a number of files.
117 */
caeeff07 118 public function filemanager_input(question_attempt $qa, question_display_options $options) {
b36d2d06
TH
119 return '';
120 }
783af252
TH
121
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 */
149 public abstract function response_area_read_only($inputname, $response, $lines);
150
151 /**
152 * Render the students respone when the question is in read-only mode.
153 * @param string $inputname the field name to use for this input.
154 * @param string $response the student's current response.
155 * @param int $lines approximate size of input box to display.
156 */
157 public abstract function response_area_input($inputname, $response, $lines);
158}
159
160
161/**
162 * An essay format renderer for essays where the student should use the HTML
163 * editor without the file picker.
164 *
165 * @copyright 2011 The Open University
166 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
167 */
168class qtype_essay_format_editor_renderer extends plugin_renderer_base {
169 public function response_area_read_only($inputname, $response, $lines) {
170 $formatoptions = new stdClass();
171 $formatoptions->para = false;
172 return html_writer::tag('div', format_text($response, FORMAT_HTML, $formatoptions),
173 array('class' => 'qtype_essay_editor qtype_essay_response'));
174 }
175
176 public function response_area_input($inputname, $response, $lines) {
177 return print_textarea(can_use_html_editor(), 18, 80, 630, 400, $inputname, $response, 0, true);
178 }
179}
180
181
182/**
183 * An essay format renderer for essays where the student should use the HTML
184 * editor with 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_editorfilepicker_renderer extends plugin_renderer_base {
190 public function response_area_read_only($inputname, $response, $lines) {
191 return 'Not yet implemented.';
192 }
193
194 public function response_area_input($inputname, $response, $lines) {
195 return 'Not yet implemented.';
196 }
197}
198
199
200/**
201 * An essay format renderer for essays where the student should use a plain
202 * input box, but with a normal, proportional font.
203 *
204 * @copyright 2011 The Open University
205 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
206 */
207class qtype_essay_format_plain_renderer extends plugin_renderer_base {
208 /**
209 * @return string the HTML for the textarea.
210 */
211 protected function textarea($response, $lines, $attributes) {
212 $attributes['class'] = $this->class_name() . ' qtype_essay_response';
213 $attributes['rows'] = $lines;
214 return html_writer::tag('textarea', s($response), $attributes);
215 }
216
217 protected function class_name() {
218 return 'qtype_essay_plain';
219 }
220
221 public function response_area_read_only($inputname, $response, $lines) {
222 return $this->textarea($response, $lines, array('readonly' => 'readonly'));
223 }
224
225 public function response_area_input($inputname, $response, $lines) {
226 return $this->textarea($response, $lines, array('name' => $inputname));
227 }
228}
229
230
231/**
232 * An essay format renderer for essays where the student should use a plain
233 * input box with a monospaced font. You might use this, for example, for a
234 * question where the students should type computer code.
235 *
236 * @copyright 2011 The Open University
237 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
238 */
239class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
240 protected function class_name() {
241 return 'qtype_essay_monospaced';
242 }
6d03299d 243}