MDL-34570 accessibility compliance for question: edit string label
[moodle.git] / question / behaviour / rendererbase.php
CommitLineData
d1b7e03d 1<?php
d1b7e03d
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
d1b7e03d
TH
17/**
18 * Defines the renderer base class for question behaviours.
19 *
017bc1d9 20 * @package moodlecore
d1b7e03d 21 * @subpackage questionbehaviours
017bc1d9
TH
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
d1b7e03d
TH
30/**
31 * Renderer base class for question behaviours.
32 *
33 * The methods in this class are mostly called from {@link core_question_renderer}
34 * which coordinates the overall output of questions.
35 *
017bc1d9
TH
36 * @copyright 2009 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d 38 */
2b7da645 39abstract class qbehaviour_renderer extends plugin_renderer_base {
d1b7e03d
TH
40 /**
41 * Generate some HTML (which may be blank) that appears in the question
42 * formulation area, afer the question type generated output.
43 *
44 * For example.
45 * immediatefeedback and interactive mode use this to show the Submit button,
46 * and CBM use this to display the certainty choices.
47 *
48 * @param question_attempt $qa a question attempt.
49 * @param question_display_options $options controls what should and should not be displayed.
50 * @return string HTML fragment.
51 */
52 public function controls(question_attempt $qa, question_display_options $options) {
53 return '';
54 }
55
56 /**
57 * Generate some HTML (which may be blank) that appears in the outcome area,
58 * after the question-type generated output.
59 *
60 * For example, the CBM models use this to display an explanation of the score
61 * adjustment that was made based on the certainty selected.
62 *
63 * @param question_attempt $qa a question attempt.
64 * @param question_display_options $options controls what should and should not be displayed.
65 * @return string HTML fragment.
66 */
67 public function feedback(question_attempt $qa, question_display_options $options) {
68 return '';
69 }
70
71 public function manual_comment_fields(question_attempt $qa, question_display_options $options) {
2d682972
TH
72 $inputname = $qa->get_behaviour_field_name('comment');
73 $id = $inputname . '_id';
74 list($commenttext, $commentformat) = $qa->get_manual_comment();
d1b7e03d 75
2d682972
TH
76 $editor = editors_get_preferred_editor($commentformat);
77 $strformats = format_text_menu();
78 $formats = $editor->get_supported_formats();
79 foreach ($formats as $fid) {
80 $formats[$fid] = $strformats[$fid];
81 }
82
83 $commenttext = format_text($commenttext, $commentformat, array('para' => false));
84
85 $editor->use_editor($id, array('context' => $options->context));
86
87 $commenteditor = html_writer::tag('div', html_writer::tag('textarea', s($commenttext),
88 array('id' => $id, 'name' => $inputname, 'rows' => 10, 'cols' => 60)));
89
90 $commenteditor .= html_writer::start_tag('div');
83690170 91 if (count($formats) == 1) {
2d682972
TH
92 reset($formats);
93 $commenteditor .= html_writer::empty_tag('input', array('type' => 'hidden',
94 'name' => $inputname . 'format', 'value' => key($formats)));
95
96 } else {
83690170 97 $commenteditor .= html_writer::label(get_string('formattexttype'), 'menu' . $inputname . 'format', false, array('class' => 'accesshide'));
2d682972
TH
98 $commenteditor .= html_writer::select(
99 $formats, $inputname . 'format', $commentformat, '');
100 }
101 $commenteditor .= html_writer::end_tag('div');
d1b7e03d 102
d1b7e03d 103 $comment = html_writer::tag('div', html_writer::tag('div',
1631ceea 104 html_writer::tag('label', get_string('comment', 'question'),
2d682972
TH
105 array('for' => $id)), array('class' => 'fitemtitle')) .
106 html_writer::tag('div', $commenteditor, array('class' => 'felement fhtmleditor')),
d1b7e03d
TH
107 array('class' => 'fitem'));
108
109 $mark = '';
110 if ($qa->get_max_mark()) {
111 $currentmark = $qa->get_current_manual_mark();
112 $maxmark = $qa->get_max_mark();
113
114 $fieldsize = strlen($qa->format_max_mark($options->markdp)) - 1;
115 $markfield = $qa->get_behaviour_field_name('mark');
116
117 $attributes = array(
118 'type' => 'text',
119 'size' => $fieldsize,
120 'name' => $markfield,
121 );
122 if (!is_null($currentmark)) {
1631ceea
TH
123 $attributes['value'] = $qa->format_fraction_as_mark(
124 $currentmark / $maxmark, $options->markdp);
d1b7e03d 125 }
0ff4bd08 126 $a = new stdClass();
d1b7e03d
TH
127 $a->max = $qa->format_max_mark($options->markdp);
128 $a->mark = html_writer::empty_tag('input', $attributes);
129
130 $markrange = html_writer::empty_tag('input', array(
131 'type' => 'hidden',
132 'name' => $qa->get_behaviour_field_name('maxmark'),
133 'value' => $maxmark,
134 )) . html_writer::empty_tag('input', array(
135 'type' => 'hidden',
136 'name' => $qa->get_control_field_name('minfraction'),
137 'value' => $qa->get_min_fraction(),
138 ));
139
140 $errorclass = '';
141 $error = '';
142 if ($currentmark > $maxmark || $currentmark < $maxmark * $qa->get_min_fraction()) {
143 $errorclass = ' error';
144 $error = html_writer::tag('span', get_string('manualgradeoutofrange', 'question'),
145 array('class' => 'error')) . html_writer::empty_tag('br');
146 }
147
148 $mark = html_writer::tag('div', html_writer::tag('div',
1631ceea
TH
149 html_writer::tag('label', get_string('mark', 'question'),
150 array('for' => $markfield)),
d1b7e03d
TH
151 array('class' => 'fitemtitle')) .
152 html_writer::tag('div', $error . get_string('xoutofmax', 'question', $a) .
153 $markrange, array('class' => 'felement ftext' . $errorclass)
154 ), array('class' => 'fitem'));
d1b7e03d
TH
155 }
156
157 return html_writer::tag('fieldset', html_writer::tag('div', $comment . $mark,
158 array('class' => 'fcontainer clearfix')), array('class' => 'hidden'));
159 }
160
161 public function manual_comment_view(question_attempt $qa, question_display_options $options) {
162 $output = '';
163 if ($qa->has_manual_comment()) {
164 $output .= get_string('commentx', 'question', $qa->get_behaviour()->format_comment());
165 }
166 if ($options->manualcommentlink) {
7ee80cab
TH
167 $url = new moodle_url($options->manualcommentlink, array('slot' => $qa->get_slot()));
168 $link = $this->output->action_link($url, get_string('commentormark', 'question'),
1631ceea
TH
169 new popup_action('click', $url, 'commentquestion',
170 array('width' => 600, 'height' => 800)));
d1b7e03d
TH
171 $output .= html_writer::tag('div', $link, array('class' => 'commentlink'));
172 }
173 return $output;
174 }
175
176 /**
177 * Display the manual comment, and a link to edit it, if appropriate.
178 *
179 * @param question_attempt $qa a question attempt.
180 * @param question_display_options $options controls what should and should not be displayed.
181 * @return string HTML fragment.
182 */
183 public function manual_comment(question_attempt $qa, question_display_options $options) {
184 if ($options->manualcomment == question_display_options::EDITABLE) {
185 return $this->manual_comment_fields($qa, $options);
186
187 } else if ($options->manualcomment == question_display_options::VISIBLE) {
188 return $this->manual_comment_view($qa, $options);
189
190 } else {
191 return '';
192 }
193 }
194
195 /**
196 * Several behaviours need a submit button, so put the common code here.
197 * The button is disabled if the question is displayed read-only.
198 * @param question_display_options $options controls what should and should not be displayed.
199 * @return string HTML fragment.
200 */
201 protected function submit_button(question_attempt $qa, question_display_options $options) {
202 $attributes = array(
203 'type' => 'submit',
204 'id' => $qa->get_behaviour_field_name('submit'),
205 'name' => $qa->get_behaviour_field_name('submit'),
206 'value' => get_string('check', 'question'),
207 'class' => 'submit btn',
208 );
209 if ($options->readonly) {
210 $attributes['disabled'] = 'disabled';
211 }
212 $output = html_writer::empty_tag('input', $attributes);
213 if (!$options->readonly) {
cce1b655
TH
214 $this->page->requires->js_init_call('M.core_question_engine.init_submit_button',
215 array($attributes['id'], $qa->get_slot()));
d1b7e03d
TH
216 }
217 return $output;
218 }
219
220 /**
221 * Return any HTML that needs to be included in the page's <head> when
222 * questions using this model are used.
223 * @param $qa the question attempt that will be displayed on the page.
224 * @return string HTML fragment.
225 */
226 public function head_code(question_attempt $qa) {
227 return '';
228 }
229}