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