MDL-20636 Fix a bunch of minor errors in the truefalse preview.
[moodle.git] / question / behaviour / rendererbase.php
CommitLineData
d1b7e03d
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
18
19/**
20 * Defines the renderer base class for question behaviours.
21 *
22 * @package moodlecore
23 * @subpackage questionbehaviours
24 * @copyright 2009 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28
29/**
30 * Renderer base class for question behaviours.
31 *
32 * The methods in this class are mostly called from {@link core_question_renderer}
33 * which coordinates the overall output of questions.
34 *
35 * @copyright 2009 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
2b7da645 38abstract class qbehaviour_renderer extends plugin_renderer_base {
d1b7e03d
TH
39 /**
40 * Generate some HTML (which may be blank) that appears in the question
41 * formulation area, afer the question type generated output.
42 *
43 * For example.
44 * immediatefeedback and interactive mode use this to show the Submit button,
45 * and CBM use this to display the certainty choices.
46 *
47 * @param question_attempt $qa a question attempt.
48 * @param question_display_options $options controls what should and should not be displayed.
49 * @return string HTML fragment.
50 */
51 public function controls(question_attempt $qa, question_display_options $options) {
52 return '';
53 }
54
55 /**
56 * Generate some HTML (which may be blank) that appears in the outcome area,
57 * after the question-type generated output.
58 *
59 * For example, the CBM models use this to display an explanation of the score
60 * adjustment that was made based on the certainty selected.
61 *
62 * @param question_attempt $qa a question attempt.
63 * @param question_display_options $options controls what should and should not be displayed.
64 * @return string HTML fragment.
65 */
66 public function feedback(question_attempt $qa, question_display_options $options) {
67 return '';
68 }
69
70 public function manual_comment_fields(question_attempt $qa, question_display_options $options) {
71
72 $commentfield = $qa->get_behaviour_field_name('comment');
73
74 $comment = print_textarea(can_use_html_editor(), 10, 80, null, null, $commentfield, $qa->get_manual_comment(), 0, true);
75 $comment = html_writer::tag('div', html_writer::tag('div',
76 html_writer::tag('label', get_string('comment', 'question'), array('for' => $commentfield)),
77 array('class' => 'fitemtitle')) .
78 html_writer::tag('div', $comment, array('class' => 'felement fhtmleditor')),
79 array('class' => 'fitem'));
80
81 $mark = '';
82 if ($qa->get_max_mark()) {
83 $currentmark = $qa->get_current_manual_mark();
84 $maxmark = $qa->get_max_mark();
85
86 $fieldsize = strlen($qa->format_max_mark($options->markdp)) - 1;
87 $markfield = $qa->get_behaviour_field_name('mark');
88
89 $attributes = array(
90 'type' => 'text',
91 'size' => $fieldsize,
92 'name' => $markfield,
93 );
94 if (!is_null($currentmark)) {
95 $attributes['value'] = $qa->format_fraction_as_mark($currentmark / $maxmark, $options->markdp);
96 }
97 $a = new stdClass;
98 $a->max = $qa->format_max_mark($options->markdp);
99 $a->mark = html_writer::empty_tag('input', $attributes);
100
101 $markrange = html_writer::empty_tag('input', array(
102 'type' => 'hidden',
103 'name' => $qa->get_behaviour_field_name('maxmark'),
104 'value' => $maxmark,
105 )) . html_writer::empty_tag('input', array(
106 'type' => 'hidden',
107 'name' => $qa->get_control_field_name('minfraction'),
108 'value' => $qa->get_min_fraction(),
109 ));
110
111 $errorclass = '';
112 $error = '';
113 if ($currentmark > $maxmark || $currentmark < $maxmark * $qa->get_min_fraction()) {
114 $errorclass = ' error';
115 $error = html_writer::tag('span', get_string('manualgradeoutofrange', 'question'),
116 array('class' => 'error')) . html_writer::empty_tag('br');
117 }
118
119 $mark = html_writer::tag('div', html_writer::tag('div',
120 html_writer::tag('label', get_string('mark', 'question'), array('for' => $markfield)),
121 array('class' => 'fitemtitle')) .
122 html_writer::tag('div', $error . get_string('xoutofmax', 'question', $a) .
123 $markrange, array('class' => 'felement ftext' . $errorclass)
124 ), array('class' => 'fitem'));
125
126 }
127
128 return html_writer::tag('fieldset', html_writer::tag('div', $comment . $mark,
129 array('class' => 'fcontainer clearfix')), array('class' => 'hidden'));
130 }
131
132 public function manual_comment_view(question_attempt $qa, question_display_options $options) {
133 $output = '';
134 if ($qa->has_manual_comment()) {
135 $output .= get_string('commentx', 'question', $qa->get_behaviour()->format_comment());
136 }
137 if ($options->manualcommentlink) {
138 $strcomment = get_string('commentormark', 'question');
139 $link = link_to_popup_window($options->manualcommentlink .
140 '&amp;slot=' . $qa->get_slot(),
141 'commentquestion', $strcomment, 600, 800, $strcomment, 'none', true);
142 $output .= html_writer::tag('div', $link, array('class' => 'commentlink'));
143 }
144 return $output;
145 }
146
147 /**
148 * Display the manual comment, and a link to edit it, if appropriate.
149 *
150 * @param question_attempt $qa a question attempt.
151 * @param question_display_options $options controls what should and should not be displayed.
152 * @return string HTML fragment.
153 */
154 public function manual_comment(question_attempt $qa, question_display_options $options) {
155 if ($options->manualcomment == question_display_options::EDITABLE) {
156 return $this->manual_comment_fields($qa, $options);
157
158 } else if ($options->manualcomment == question_display_options::VISIBLE) {
159 return $this->manual_comment_view($qa, $options);
160
161 } else {
162 return '';
163 }
164 }
165
166 /**
167 * Several behaviours need a submit button, so put the common code here.
168 * The button is disabled if the question is displayed read-only.
169 * @param question_display_options $options controls what should and should not be displayed.
170 * @return string HTML fragment.
171 */
172 protected function submit_button(question_attempt $qa, question_display_options $options) {
173 $attributes = array(
174 'type' => 'submit',
175 'id' => $qa->get_behaviour_field_name('submit'),
176 'name' => $qa->get_behaviour_field_name('submit'),
177 'value' => get_string('check', 'question'),
178 'class' => 'submit btn',
179 );
180 if ($options->readonly) {
181 $attributes['disabled'] = 'disabled';
182 }
183 $output = html_writer::empty_tag('input', $attributes);
184 if (!$options->readonly) {
cce1b655
TH
185 $this->page->requires->js_init_call('M.core_question_engine.init_submit_button',
186 array($attributes['id'], $qa->get_slot()));
d1b7e03d
TH
187 }
188 return $output;
189 }
190
191 /**
192 * Return any HTML that needs to be included in the page's <head> when
193 * questions using this model are used.
194 * @param $qa the question attempt that will be displayed on the page.
195 * @return string HTML fragment.
196 */
197 public function head_code(question_attempt $qa) {
198 return '';
199 }
200}