MDL-20636 Can now display the file picker in essay questions with attachments #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 {
b36d2d06
TH
52 $answer =$responseoutput->response_area_read_only($inputname,
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
110 return form_filemanager_render($pickeroptions);
b36d2d06
TH
111 }
112
113 /**
114 * Displays the input control for when the student should upload a number of files.
115 */
caeeff07 116 public function filemanager_input(question_attempt $qa, question_display_options $options) {
b36d2d06
TH
117 return '';
118 }
119}
120
121
122/**
123 * A base class to abstract out the differences between different type of
124 * response format.
125 *
126 * @copyright 2011 The Open University
127 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
128 */
129abstract class qtype_essay_format_renderer_base extends plugin_renderer_base {
130 /**
131 * Render the students respone when the question is in read-only mode.
132 * @param string $inputname the field name to use for this input.
133 * @param string $response the student's current response.
134 * @param int $lines approximate size of input box to display.
135 */
136 public abstract function response_area_read_only($inputname, $response, $lines);
137
138 /**
139 * Render the students respone when the question is in read-only mode.
140 * @param string $inputname the field name to use for this input.
141 * @param string $response the student's current response.
142 * @param int $lines approximate size of input box to display.
143 */
144 public abstract function response_area_input($inputname, $response, $lines);
145}
146
147
148/**
149 * An essay format renderer for essays where the student should use the HTML
150 * editor without the file picker.
151 *
152 * @copyright 2011 The Open University
153 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
154 */
155class qtype_essay_format_editor_renderer extends plugin_renderer_base {
156 public function response_area_read_only($inputname, $response, $lines) {
157 $formatoptions = new stdClass();
158 $formatoptions->para = false;
159 return html_writer::tag('div', format_text($response, FORMAT_HTML, $formatoptions),
160 array('class' => 'qtype_essay_editor qtype_essay_response'));
161 }
162
163 public function response_area_input($inputname, $response, $lines) {
164 return print_textarea(can_use_html_editor(), 18, 80, 630, 400, $inputname, $response, 0, true);
165 }
166}
167
168
169/**
170 * An essay format renderer for essays where the student should use the HTML
171 * editor with the file picker.
172 *
173 * @copyright 2011 The Open University
174 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
175 */
176class qtype_essay_format_editorfilepicker_renderer extends plugin_renderer_base {
177 public function response_area_read_only($inputname, $response, $lines) {
178 return 'Not yet implemented.';
179 }
180
181 public function response_area_input($inputname, $response, $lines) {
182 return 'Not yet implemented.';
183 }
184}
185
186
187/**
188 * An essay format renderer for essays where the student should use a plain
189 * input box, but with a normal, proportional font.
190 *
191 * @copyright 2011 The Open University
192 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
193 */
194class qtype_essay_format_plain_renderer extends plugin_renderer_base {
195 /**
196 * @return string the HTML for the textarea.
197 */
198 protected function textarea($response, $lines, $attributes) {
199 $attributes['class'] = $this->class_name() . ' qtype_essay_response';
200 $attributes['rows'] = $lines;
201 return html_writer::tag('textarea', s($response), $attributes);
202 }
203
204 protected function class_name() {
205 return 'qtype_essay_plain';
206 }
207
208 public function response_area_read_only($inputname, $response, $lines) {
209 return $this->textarea($response, $lines, array('readonly' => 'readonly'));
210 }
211
212 public function response_area_input($inputname, $response, $lines) {
213 return $this->textarea($response, $lines, array('name' => $inputname));
214 }
215}
216
217
218/**
219 * An essay format renderer for essays where the student should use a plain
220 * input box with a monospaced font. You might use this, for example, for a
221 * question where the students should type computer code.
222 *
223 * @copyright 2011 The Open University
224 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
225 */
226class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
227 protected function class_name() {
228 return 'qtype_essay_monospaced';
229 }
6d03299d 230}