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