MDL-20636 Start supporting different response formats in essay. #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)) {
b36d2d06
TH
48 $answer =$responseoutput->response_area_input($inputname,
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) {
59 $files = $this->filepicker_input();
60 } else if ($question->attachments != 0) {
61 $files = $this->filemanager_input();
62 }
63
64 } else if ($question->attachments != 0) {
65 $files = $this->files_read_only();
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
TH
73 $result .= html_writer::tag('div', $answer, array('class' => 'answer'));
74 $result .= html_writer::end_tag('div');
75
76 return $result;
77 }
b36d2d06
TH
78
79 /**
80 * Displays any attached files when the question is in read-only mode.
81 */
82 public function files_read_only() {
83 return '';
84 }
85
86 /**
87 * Displays the input control for when the student should upload a single file.
88 */
89 public function filepicker_input() {
90 return '';
91 }
92
93 /**
94 * Displays the input control for when the student should upload a number of files.
95 */
96 public function filemanager_input() {
97 return '';
98 }
99}
100
101
102/**
103 * A base class to abstract out the differences between different type of
104 * response format.
105 *
106 * @copyright 2011 The Open University
107 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
108 */
109abstract class qtype_essay_format_renderer_base extends plugin_renderer_base {
110 /**
111 * Render the students respone when the question is in read-only mode.
112 * @param string $inputname the field name to use for this input.
113 * @param string $response the student's current response.
114 * @param int $lines approximate size of input box to display.
115 */
116 public abstract function response_area_read_only($inputname, $response, $lines);
117
118 /**
119 * Render the students respone when the question is in read-only mode.
120 * @param string $inputname the field name to use for this input.
121 * @param string $response the student's current response.
122 * @param int $lines approximate size of input box to display.
123 */
124 public abstract function response_area_input($inputname, $response, $lines);
125}
126
127
128/**
129 * An essay format renderer for essays where the student should use the HTML
130 * editor without the file picker.
131 *
132 * @copyright 2011 The Open University
133 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
134 */
135class qtype_essay_format_editor_renderer extends plugin_renderer_base {
136 public function response_area_read_only($inputname, $response, $lines) {
137 $formatoptions = new stdClass();
138 $formatoptions->para = false;
139 return html_writer::tag('div', format_text($response, FORMAT_HTML, $formatoptions),
140 array('class' => 'qtype_essay_editor qtype_essay_response'));
141 }
142
143 public function response_area_input($inputname, $response, $lines) {
144 return print_textarea(can_use_html_editor(), 18, 80, 630, 400, $inputname, $response, 0, true);
145 }
146}
147
148
149/**
150 * An essay format renderer for essays where the student should use the HTML
151 * editor with the file picker.
152 *
153 * @copyright 2011 The Open University
154 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
155 */
156class qtype_essay_format_editorfilepicker_renderer extends plugin_renderer_base {
157 public function response_area_read_only($inputname, $response, $lines) {
158 return 'Not yet implemented.';
159 }
160
161 public function response_area_input($inputname, $response, $lines) {
162 return 'Not yet implemented.';
163 }
164}
165
166
167/**
168 * An essay format renderer for essays where the student should use a plain
169 * input box, but with a normal, proportional font.
170 *
171 * @copyright 2011 The Open University
172 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
173 */
174class qtype_essay_format_plain_renderer extends plugin_renderer_base {
175 /**
176 * @return string the HTML for the textarea.
177 */
178 protected function textarea($response, $lines, $attributes) {
179 $attributes['class'] = $this->class_name() . ' qtype_essay_response';
180 $attributes['rows'] = $lines;
181 return html_writer::tag('textarea', s($response), $attributes);
182 }
183
184 protected function class_name() {
185 return 'qtype_essay_plain';
186 }
187
188 public function response_area_read_only($inputname, $response, $lines) {
189 return $this->textarea($response, $lines, array('readonly' => 'readonly'));
190 }
191
192 public function response_area_input($inputname, $response, $lines) {
193 return $this->textarea($response, $lines, array('name' => $inputname));
194 }
195}
196
197
198/**
199 * An essay format renderer for essays where the student should use a plain
200 * input box with a monospaced font. You might use this, for example, for a
201 * question where the students should type computer code.
202 *
203 * @copyright 2011 The Open University
204 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
205 */
206class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
207 protected function class_name() {
208 return 'qtype_essay_monospaced';
209 }
6d03299d 210}