MDL-20636 Add @package and GPL boiler-plate to files in /question.
[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
7ee80cab 74 list($commenttext, $commentformat) = $qa->get_manual_comment();
7a719748 75 $comment = print_textarea(can_use_html_editor(), 10, 80, null, null, $commentfield, $commenttext, 0, true);
d1b7e03d
TH
76 $comment = html_writer::tag('div', html_writer::tag('div',
77 html_writer::tag('label', get_string('comment', 'question'), array('for' => $commentfield)),
78 array('class' => 'fitemtitle')) .
79 html_writer::tag('div', $comment, array('class' => 'felement fhtmleditor')),
80 array('class' => 'fitem'));
81
82 $mark = '';
83 if ($qa->get_max_mark()) {
84 $currentmark = $qa->get_current_manual_mark();
85 $maxmark = $qa->get_max_mark();
86
87 $fieldsize = strlen($qa->format_max_mark($options->markdp)) - 1;
88 $markfield = $qa->get_behaviour_field_name('mark');
89
90 $attributes = array(
91 'type' => 'text',
92 'size' => $fieldsize,
93 'name' => $markfield,
94 );
95 if (!is_null($currentmark)) {
96 $attributes['value'] = $qa->format_fraction_as_mark($currentmark / $maxmark, $options->markdp);
97 }
0ff4bd08 98 $a = new stdClass();
d1b7e03d
TH
99 $a->max = $qa->format_max_mark($options->markdp);
100 $a->mark = html_writer::empty_tag('input', $attributes);
101
102 $markrange = html_writer::empty_tag('input', array(
103 'type' => 'hidden',
104 'name' => $qa->get_behaviour_field_name('maxmark'),
105 'value' => $maxmark,
106 )) . html_writer::empty_tag('input', array(
107 'type' => 'hidden',
108 'name' => $qa->get_control_field_name('minfraction'),
109 'value' => $qa->get_min_fraction(),
110 ));
111
112 $errorclass = '';
113 $error = '';
114 if ($currentmark > $maxmark || $currentmark < $maxmark * $qa->get_min_fraction()) {
115 $errorclass = ' error';
116 $error = html_writer::tag('span', get_string('manualgradeoutofrange', 'question'),
117 array('class' => 'error')) . html_writer::empty_tag('br');
118 }
119
120 $mark = html_writer::tag('div', html_writer::tag('div',
121 html_writer::tag('label', get_string('mark', 'question'), array('for' => $markfield)),
122 array('class' => 'fitemtitle')) .
123 html_writer::tag('div', $error . get_string('xoutofmax', 'question', $a) .
124 $markrange, array('class' => 'felement ftext' . $errorclass)
125 ), array('class' => 'fitem'));
d1b7e03d
TH
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) {
7ee80cab
TH
138 $url = new moodle_url($options->manualcommentlink, array('slot' => $qa->get_slot()));
139 $link = $this->output->action_link($url, get_string('commentormark', 'question'),
140 new popup_action('click', $url, 'commentquestion', array('width' => 600, 'height' => 800)));
d1b7e03d
TH
141 $output .= html_writer::tag('div', $link, array('class' => 'commentlink'));
142 }
143 return $output;
144 }
145
146 /**
147 * Display the manual comment, and a link to edit it, if appropriate.
148 *
149 * @param question_attempt $qa a question attempt.
150 * @param question_display_options $options controls what should and should not be displayed.
151 * @return string HTML fragment.
152 */
153 public function manual_comment(question_attempt $qa, question_display_options $options) {
154 if ($options->manualcomment == question_display_options::EDITABLE) {
155 return $this->manual_comment_fields($qa, $options);
156
157 } else if ($options->manualcomment == question_display_options::VISIBLE) {
158 return $this->manual_comment_view($qa, $options);
159
160 } else {
161 return '';
162 }
163 }
164
165 /**
166 * Several behaviours need a submit button, so put the common code here.
167 * The button is disabled if the question is displayed read-only.
168 * @param question_display_options $options controls what should and should not be displayed.
169 * @return string HTML fragment.
170 */
171 protected function submit_button(question_attempt $qa, question_display_options $options) {
172 $attributes = array(
173 'type' => 'submit',
174 'id' => $qa->get_behaviour_field_name('submit'),
175 'name' => $qa->get_behaviour_field_name('submit'),
176 'value' => get_string('check', 'question'),
177 'class' => 'submit btn',
178 );
179 if ($options->readonly) {
180 $attributes['disabled'] = 'disabled';
181 }
182 $output = html_writer::empty_tag('input', $attributes);
183 if (!$options->readonly) {
cce1b655
TH
184 $this->page->requires->js_init_call('M.core_question_engine.init_submit_button',
185 array($attributes['id'], $qa->get_slot()));
d1b7e03d
TH
186 }
187 return $output;
188 }
189
190 /**
191 * Return any HTML that needs to be included in the page's <head> when
192 * questions using this model are used.
193 * @param $qa the question attempt that will be displayed on the page.
194 * @return string HTML fragment.
195 */
196 public function head_code(question_attempt $qa) {
197 return '';
198 }
199}