Commit | Line | Data |
---|---|---|
c586d2bf MG |
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 | * @package gradingform | |
20 | * @subpackage rubric | |
21 | * @copyright 2011 Marina Glancy | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
c586d2bf MG |
25 | defined('MOODLE_INTERNAL') || die(); |
26 | ||
27 | /** | |
28 | * Grading method plugin renderer | |
29 | */ | |
2ae7faf1 | 30 | class gradingform_rubric_renderer extends plugin_renderer_base { |
c586d2bf | 31 | |
ab156741 | 32 | /** |
fc5adc3b MG |
33 | * This function returns html code for displaying criterion. Depending on $mode it may be the |
34 | * code to edit rubric, to preview the rubric, to evaluate somebody or to review the evaluation. | |
35 | * | |
36 | * This function may be called from display_rubric() to display the whole rubric, or it can be | |
37 | * called by itself to return a template used by JavaScript to add new empty criteria to the | |
38 | * rubric being designed. | |
39 | * In this case it will use macros like {NAME}, {LEVELS}, {CRITERION-id}, etc. | |
40 | * | |
41 | * When overriding this function it is very important to remember that all elements of html | |
42 | * form (in edit or evaluate mode) must have the name $elementname. | |
ab156741 | 43 | * |
fc5adc3b MG |
44 | * Also JavaScript relies on the class names of elements and when developer changes them |
45 | * script might stop working. | |
46 | * | |
47 | * @param int $mode rubric display mode @see gradingform_rubric_controller | |
48 | * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode) | |
49 | * @param array|null $criterion criterion data | |
3c2b3cb3 | 50 | * @param string $levelsstr evaluated templates for this criterion levels |
fc5adc3b | 51 | * @param array|null $value (only in view mode) teacher's feedback on this criterion |
ab156741 MG |
52 | * @return string |
53 | */ | |
3c2b3cb3 | 54 | public function criterion_template($mode, $options, $elementname = '{NAME}', $criterion = null, $levelsstr = '{LEVELS}', $value = null) { |
5060997b | 55 | // TODO description format, remark format |
ab156741 MG |
56 | if ($criterion === null || !is_array($criterion) || !array_key_exists('id', $criterion)) { |
57 | $criterion = array('id' => '{CRITERION-id}', 'description' => '{CRITERION-description}', 'sortorder' => '{CRITERION-sortorder}', 'class' => '{CRITERION-class}'); | |
58 | } else { | |
59 | foreach (array('sortorder', 'description', 'class') as $key) { | |
60 | // set missing array elements to empty strings to avoid warnings | |
61 | if (!array_key_exists($key, $criterion)) { | |
62 | $criterion[$key] = ''; | |
63 | } | |
64 | } | |
65 | } | |
3c2b3cb3 | 66 | $criteriontemplate = html_writer::start_tag('tr', array('class' => 'criterion'. $criterion['class'], 'id' => '{NAME}-criteria-{CRITERION-id}')); |
ab156741 | 67 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) { |
3c2b3cb3 | 68 | $criteriontemplate .= html_writer::start_tag('td', array('class' => 'controls')); |
ab156741 MG |
69 | foreach (array('moveup', 'delete', 'movedown') as $key) { |
70 | $value = get_string('criterion'.$key, 'gradingform_rubric'); | |
39c6f4b6 | 71 | $button = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][{CRITERION-id}]['.$key.']', |
4f110c47 | 72 | 'id' => '{NAME}-criteria-{CRITERION-id}-'.$key, 'value' => $value, 'title' => $value, 'tabindex' => -1)); |
3c2b3cb3 | 73 | $criteriontemplate .= html_writer::tag('div', $button, array('class' => $key)); |
ab156741 | 74 | } |
3c2b3cb3 MG |
75 | $criteriontemplate .= html_writer::end_tag('td'); // .controls |
76 | $criteriontemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][sortorder]', 'value' => $criterion['sortorder'])); | |
39c6f4b6 | 77 | $description = html_writer::tag('textarea', htmlspecialchars($criterion['description']), array('name' => '{NAME}[criteria][{CRITERION-id}][description]', 'cols' => '10', 'rows' => '5')); |
ab156741 MG |
78 | } else { |
79 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) { | |
3c2b3cb3 MG |
80 | $criteriontemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][sortorder]', 'value' => $criterion['sortorder'])); |
81 | $criteriontemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][description]', 'value' => $criterion['description'])); | |
ab156741 MG |
82 | } |
83 | $description = $criterion['description']; | |
84 | } | |
2ae7faf1 MG |
85 | $descriptionclass = 'description'; |
86 | if (isset($criterion['error_description'])) { | |
87 | $descriptionclass .= ' error'; | |
88 | } | |
3c2b3cb3 MG |
89 | $criteriontemplate .= html_writer::tag('td', $description, array('class' => $descriptionclass, 'id' => '{NAME}-criteria-{CRITERION-id}-description')); |
90 | $levelsstrtable = html_writer::tag('table', html_writer::tag('tr', $levelsstr, array('id' => '{NAME}-criteria-{CRITERION-id}-levels'))); | |
2ae7faf1 MG |
91 | $levelsclass = 'levels'; |
92 | if (isset($criterion['error_levels'])) { | |
93 | $levelsclass .= ' error'; | |
94 | } | |
3c2b3cb3 | 95 | $criteriontemplate .= html_writer::tag('td', $levelsstrtable, array('class' => $levelsclass)); |
ab156741 MG |
96 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) { |
97 | $value = get_string('criterionaddlevel', 'gradingform_rubric'); | |
39c6f4b6 MG |
98 | $button = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][addlevel]', |
99 | 'id' => '{NAME}-criteria-{CRITERION-id}-levels-addlevel', 'value' => $value, 'title' => $value)); | |
3c2b3cb3 | 100 | $criteriontemplate .= html_writer::tag('td', $button, array('class' => 'addlevel')); |
ab156741 | 101 | } |
39c6f4b6 MG |
102 | $displayremark = ($options['enableremarks'] && ($mode != gradingform_rubric_controller::DISPLAY_VIEW || $options['showremarksstudent'])); |
103 | if ($displayremark) { | |
5060997b | 104 | $currentremark = ''; |
39c6f4b6 MG |
105 | if (isset($value['remark'])) { |
106 | $currentremark = $value['remark']; | |
107 | } | |
108 | if ($mode == gradingform_rubric_controller::DISPLAY_EVAL) { | |
109 | $input = html_writer::tag('textarea', htmlspecialchars($currentremark), array('name' => '{NAME}[criteria][{CRITERION-id}][remark]', 'cols' => '10', 'rows' => '5')); | |
3c2b3cb3 | 110 | $criteriontemplate .= html_writer::tag('td', $input, array('class' => 'remark')); |
39c6f4b6 | 111 | } else if ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN) { |
3c2b3cb3 | 112 | $criteriontemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][remark]', 'value' => $currentremark)); |
39c6f4b6 | 113 | }else if ($mode == gradingform_rubric_controller::DISPLAY_REVIEW || $mode == gradingform_rubric_controller::DISPLAY_VIEW) { |
3c2b3cb3 | 114 | $criteriontemplate .= html_writer::tag('td', $currentremark, array('class' => 'remark')); // TODO maybe some prefix here like 'Teacher remark:' |
39c6f4b6 | 115 | } |
5060997b | 116 | } |
3c2b3cb3 | 117 | $criteriontemplate .= html_writer::end_tag('tr'); // .criterion |
ab156741 | 118 | |
3c2b3cb3 MG |
119 | $criteriontemplate = str_replace('{NAME}', $elementname, $criteriontemplate); |
120 | $criteriontemplate = str_replace('{CRITERION-id}', $criterion['id'], $criteriontemplate); | |
121 | return $criteriontemplate; | |
ab156741 MG |
122 | } |
123 | ||
fc5adc3b MG |
124 | /** |
125 | * This function returns html code for displaying one level of one criterion. Depending on $mode | |
126 | * it may be the code to edit rubric, to preview the rubric, to evaluate somebody or to review the evaluation. | |
127 | * | |
128 | * This function may be called from display_rubric() to display the whole rubric, or it can be | |
129 | * called by itself to return a template used by JavaScript to add new empty level to the | |
130 | * criterion during the design of rubric. | |
131 | * In this case it will use macros like {NAME}, {CRITERION-id}, {LEVEL-id}, etc. | |
132 | * | |
133 | * When overriding this function it is very important to remember that all elements of html | |
134 | * form (in edit or evaluate mode) must have the name $elementname. | |
135 | * | |
136 | * Also JavaScript relies on the class names of elements and when developer changes them | |
137 | * script might stop working. | |
138 | * | |
139 | * @param int $mode rubric display mode @see gradingform_rubric_controller | |
140 | * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode) | |
141 | * @param string|int $criterionid either id of the nesting criterion or a macro for template | |
142 | * @param array|null $level level data, also in view mode it might also have property $level['checked'] whether this level is checked | |
143 | * @return string | |
144 | */ | |
39c6f4b6 | 145 | public function level_template($mode, $options, $elementname = '{NAME}', $criterionid = '{CRITERION-id}', $level = null) { |
ab156741 | 146 | // TODO definition format |
5060997b | 147 | if (!isset($level['id'])) { |
ab156741 MG |
148 | $level = array('id' => '{LEVEL-id}', 'definition' => '{LEVEL-definition}', 'score' => '{LEVEL-score}', 'class' => '{LEVEL-class}', 'checked' => false); |
149 | } else { | |
150 | foreach (array('score', 'definition', 'class', 'checked') as $key) { | |
151 | // set missing array elements to empty strings to avoid warnings | |
152 | if (!array_key_exists($key, $level)) { | |
153 | $level[$key] = ''; | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
158 | // Template for one level within one criterion | |
39c6f4b6 MG |
159 | $tdattributes = array('id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}', 'class' => 'level'. $level['class']); |
160 | if (isset($level['tdwidth'])) { | |
161 | $tdattributes['width'] = round($level['tdwidth']).'%'; | |
162 | } | |
3c2b3cb3 MG |
163 | $leveltemplate = html_writer::start_tag('td', $tdattributes); |
164 | $leveltemplate .= html_writer::start_tag('div', array('class' => 'level-wrapper')); | |
ab156741 | 165 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) { |
39c6f4b6 | 166 | $definition = html_writer::tag('textarea', htmlspecialchars($level['definition']), array('name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][definition]', 'cols' => '10', 'rows' => '4')); |
a19d1057 | 167 | $score = html_writer::empty_tag('input', array('type' => 'text', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][score]', 'size' => '3', 'value' => $level['score'])); |
ab156741 MG |
168 | } else { |
169 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) { | |
3c2b3cb3 MG |
170 | $leveltemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][definition]', 'value' => $level['definition'])); |
171 | $leveltemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][score]', 'value' => $level['score'])); | |
ab156741 MG |
172 | } |
173 | $definition = $level['definition']; | |
174 | $score = $level['score']; | |
175 | } | |
176 | if ($mode == gradingform_rubric_controller::DISPLAY_EVAL) { | |
5060997b | 177 | $input = html_writer::empty_tag('input', array('type' => 'radio', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id']) + |
ab156741 | 178 | ($level['checked'] ? array('checked' => 'checked') : array())); |
3c2b3cb3 | 179 | $leveltemplate .= html_writer::tag('div', $input, array('class' => 'radio')); |
ab156741 MG |
180 | } |
181 | if ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN && $level['checked']) { | |
3c2b3cb3 | 182 | $leveltemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id'])); |
ab156741 | 183 | } |
a19d1057 | 184 | $score = html_writer::tag('span', $score, array('id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-score', 'class' => 'scorevalue')); |
2ae7faf1 MG |
185 | $definitionclass = 'definition'; |
186 | if (isset($level['error_definition'])) { | |
187 | $definitionclass .= ' error'; | |
188 | } | |
3c2b3cb3 | 189 | $leveltemplate .= html_writer::tag('div', $definition, array('class' => $definitionclass, 'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-definition')); |
39c6f4b6 MG |
190 | $displayscore = true; |
191 | if (!$options['showscoreteacher'] && in_array($mode, array(gradingform_rubric_controller::DISPLAY_EVAL, gradingform_rubric_controller::DISPLAY_EVAL_FROZEN, gradingform_rubric_controller::DISPLAY_REVIEW))) { | |
192 | $displayscore = false; | |
193 | } | |
194 | if (!$options['showscorestudent'] && $mode == gradingform_rubric_controller::DISPLAY_VIEW) { | |
195 | $displayscore = false; | |
196 | } | |
197 | if ($displayscore) { | |
2ae7faf1 MG |
198 | $scoreclass = 'score'; |
199 | if (isset($level['error_score'])) { | |
200 | $scoreclass .= ' error'; | |
201 | } | |
3c2b3cb3 | 202 | $leveltemplate .= html_writer::tag('div', get_string('scorepostfix', 'gradingform_rubric', $score), array('class' => $scoreclass)); |
39c6f4b6 | 203 | } |
ab156741 MG |
204 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) { |
205 | $value = get_string('leveldelete', 'gradingform_rubric'); | |
4f110c47 | 206 | $button = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][delete]', 'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-delete', 'value' => $value, 'title' => $value, 'tabindex' => -1)); |
3c2b3cb3 | 207 | $leveltemplate .= html_writer::tag('div', $button, array('class' => 'delete')); |
ab156741 | 208 | } |
3c2b3cb3 MG |
209 | $leveltemplate .= html_writer::end_tag('div'); // .level-wrapper |
210 | $leveltemplate .= html_writer::end_tag('td'); // .level | |
ab156741 | 211 | |
3c2b3cb3 MG |
212 | $leveltemplate = str_replace('{NAME}', $elementname, $leveltemplate); |
213 | $leveltemplate = str_replace('{CRITERION-id}', $criterionid, $leveltemplate); | |
214 | $leveltemplate = str_replace('{LEVEL-id}', $level['id'], $leveltemplate); | |
215 | return $leveltemplate; | |
ab156741 MG |
216 | } |
217 | ||
fc5adc3b MG |
218 | /** |
219 | * This function returns html code for displaying rubric template (content before and after | |
220 | * criteria list). Depending on $mode it may be the code to edit rubric, to preview the rubric, | |
221 | * to evaluate somebody or to review the evaluation. | |
222 | * | |
223 | * This function is called from display_rubric() to display the whole rubric. | |
224 | * | |
225 | * When overriding this function it is very important to remember that all elements of html | |
226 | * form (in edit or evaluate mode) must have the name $elementname. | |
227 | * | |
228 | * Also JavaScript relies on the class names of elements and when developer changes them | |
229 | * script might stop working. | |
230 | * | |
231 | * @param int $mode rubric display mode @see gradingform_rubric_controller | |
232 | * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode) | |
3c2b3cb3 | 233 | * @param string $criteriastr evaluated templates for this rubric's criteria |
fc5adc3b MG |
234 | * @return string |
235 | */ | |
3c2b3cb3 | 236 | protected function rubric_template($mode, $options, $elementname, $criteriastr) { |
ab156741 MG |
237 | $classsuffix = ''; // CSS suffix for class of the main div. Depends on the mode |
238 | switch ($mode) { | |
239 | case gradingform_rubric_controller::DISPLAY_EDIT_FULL: | |
240 | $classsuffix = ' editor editable'; break; | |
241 | case gradingform_rubric_controller::DISPLAY_EDIT_FROZEN: | |
242 | $classsuffix = ' editor frozen'; break; | |
243 | case gradingform_rubric_controller::DISPLAY_PREVIEW: | |
244 | $classsuffix = ' editor preview'; break; | |
245 | case gradingform_rubric_controller::DISPLAY_EVAL: | |
246 | $classsuffix = ' evaluate editable'; break; | |
247 | case gradingform_rubric_controller::DISPLAY_EVAL_FROZEN: | |
248 | $classsuffix = ' evaluate frozen'; break; | |
249 | case gradingform_rubric_controller::DISPLAY_REVIEW: | |
250 | $classsuffix = ' review'; break; | |
39c6f4b6 MG |
251 | case gradingform_rubric_controller::DISPLAY_VIEW: |
252 | $classsuffix = ' view'; break; | |
ab156741 MG |
253 | } |
254 | ||
3c2b3cb3 MG |
255 | $rubrictemplate = html_writer::start_tag('div', array('id' => 'rubric-{NAME}', 'class' => 'clearfix gradingform_rubric'.$classsuffix)); |
256 | $rubrictemplate .= html_writer::tag('table', $criteriastr, array('class' => 'criteria', 'id' => '{NAME}-criteria')); | |
ab156741 MG |
257 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) { |
258 | $value = get_string('addcriterion', 'gradingform_rubric'); | |
39c6f4b6 | 259 | $input = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][addcriterion]', 'id' => '{NAME}-criteria-addcriterion', 'value' => $value, 'title' => $value)); |
3c2b3cb3 | 260 | $rubrictemplate .= html_writer::tag('div', $input, array('class' => 'addcriterion')); |
ab156741 | 261 | } |
3c2b3cb3 MG |
262 | $rubrictemplate .= $this->rubric_edit_options($mode, $options); |
263 | $rubrictemplate .= html_writer::end_tag('div'); | |
ab156741 | 264 | |
3c2b3cb3 | 265 | return str_replace('{NAME}', $elementname, $rubrictemplate); |
ab156741 MG |
266 | } |
267 | ||
3c2b3cb3 MG |
268 | /** |
269 | * Generates html template to view/edit the rubric options. Expression {NAME} is used in | |
270 | * template for the form element name | |
271 | * | |
272 | * @param int $mode | |
273 | * @param array $options | |
274 | * @return string | |
275 | */ | |
39c6f4b6 MG |
276 | protected function rubric_edit_options($mode, $options) { |
277 | if ($mode != gradingform_rubric_controller::DISPLAY_EDIT_FULL | |
278 | && $mode != gradingform_rubric_controller::DISPLAY_EDIT_FROZEN | |
279 | && $mode != gradingform_rubric_controller::DISPLAY_PREVIEW) { | |
280 | // Options are displayed only in edit mode | |
281 | return; | |
282 | } | |
283 | $html = html_writer::start_tag('div', array('class' => 'options')); | |
284 | $html .= html_writer::tag('div', get_string('rubricoptions', 'gradingform_rubric'), array('class' => 'optionsheading')); | |
285 | $attrs = array('type' => 'hidden', 'name' => '{NAME}[options][optionsset]', 'value' => 1); | |
286 | foreach ($options as $option => $value) { | |
287 | $html .= html_writer::start_tag('div', array('class' => 'option '.$option)); | |
288 | $attrs = array('name' => '{NAME}[options]['.$option.']', 'id' => '{NAME}-options-'.$option); | |
289 | switch ($option) { | |
290 | case 'sortlevelsasc': | |
291 | // Display option as dropdown | |
292 | $html .= html_writer::tag('span', get_string($option, 'gradingform_rubric'), array('class' => 'label')); | |
293 | $value = (int)(!!$value); // make sure $value is either 0 or 1 | |
294 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) { | |
295 | $selectoptions = array(0 => get_string($option.'0', 'gradingform_rubric'), 1 => get_string($option.'1', 'gradingform_rubric')); | |
3c2b3cb3 MG |
296 | $valuestr = html_writer::select($selectoptions, $attrs['name'], $value, false, array('id' => $attrs['id'])); |
297 | $html .= html_writer::tag('span', $valuestr, array('class' => 'value')); | |
39c6f4b6 MG |
298 | // TODO add here button 'Sort levels' |
299 | } else { | |
300 | $html .= html_writer::tag('span', get_string($option.$value, 'gradingform_rubric'), array('class' => 'value')); | |
301 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) { | |
302 | $html .= html_writer::empty_tag('input', $attrs + array('type' => 'hidden', 'value' => $value)); | |
303 | } | |
304 | } | |
305 | break; | |
306 | default: | |
0136124e MG |
307 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN && $value) { |
308 | $html .= html_writer::empty_tag('input', $attrs + array('type' => 'hidden', 'value' => $value)); | |
309 | } | |
39c6f4b6 MG |
310 | // Display option as checkbox |
311 | $attrs['type'] = 'checkbox'; | |
312 | $attrs['value'] = 1; | |
313 | if ($value) { | |
314 | $attrs['checked'] = 'checked'; | |
315 | } | |
0136124e | 316 | if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_PREVIEW) { |
39c6f4b6 MG |
317 | $attrs['disabled'] = 'disabled'; |
318 | unset($attrs['name']); | |
319 | } | |
320 | $html .= html_writer::empty_tag('input', $attrs); | |
321 | $html .= html_writer::tag('label', get_string($option, 'gradingform_rubric'), array('for' => $attrs['id'])); | |
322 | break; | |
323 | } | |
324 | $html .= html_writer::end_tag('div'); // .option | |
325 | } | |
326 | $html .= html_writer::end_tag('div'); // .options | |
327 | return $html; | |
328 | } | |
329 | ||
ab156741 | 330 | /** |
fc5adc3b MG |
331 | * This function returns html code for displaying rubric. Depending on $mode it may be the code |
332 | * to edit rubric, to preview the rubric, to evaluate somebody or to review the evaluation. | |
333 | * | |
334 | * It is very unlikely that this function needs to be overriden by theme. It does not produce | |
335 | * any html code, it just prepares data about rubric design and evaluation, adds the CSS | |
336 | * class to elements and calls the functions level_template, criterion_template and | |
337 | * rubric_template | |
ab156741 | 338 | * |
fc5adc3b MG |
339 | * @param array $criteria data about the rubric design |
340 | * @param int $mode rubric display mode @see gradingform_rubric_controller | |
341 | * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode) | |
342 | * @param array $values evaluation result | |
ab156741 MG |
343 | * @return string |
344 | */ | |
39c6f4b6 | 345 | public function display_rubric($criteria, $options, $mode, $elementname = null, $values = null) { |
3c2b3cb3 | 346 | $criteriastr = ''; |
ab156741 MG |
347 | $cnt = 0; |
348 | foreach ($criteria as $id => $criterion) { | |
349 | $criterion['class'] = $this->get_css_class_suffix($cnt++, sizeof($criteria) -1); | |
39c6f4b6 | 350 | $criterion['id'] = $id; |
3c2b3cb3 | 351 | $levelsstr = ''; |
ab156741 | 352 | $levelcnt = 0; |
5060997b MG |
353 | if (isset($values['criteria'][$id])) { |
354 | $criterionvalue = $values['criteria'][$id]; | |
355 | } else { | |
356 | $criterionvalue = null; | |
357 | } | |
ab156741 | 358 | foreach ($criterion['levels'] as $levelid => $level) { |
39c6f4b6 | 359 | $level['id'] = $levelid; |
ab156741 | 360 | $level['class'] = $this->get_css_class_suffix($levelcnt++, sizeof($criterion['levels']) -1); |
5060997b | 361 | $level['checked'] = (isset($criterionvalue['levelid']) && ((int)$criterionvalue['levelid'] === $levelid)); |
39c6f4b6 | 362 | if ($level['checked'] && ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_REVIEW || $mode == gradingform_rubric_controller::DISPLAY_VIEW)) { |
ab156741 | 363 | $level['class'] .= ' checked'; |
fc5adc3b | 364 | //in mode DISPLAY_EVAL the class 'checked' will be added by JS if it is enabled. If JS is not enabled, the 'checked' class will only confuse |
ab156741 | 365 | } |
188e840b MG |
366 | if (isset($criterionvalue['savedlevelid']) && ((int)$criterionvalue['savedlevelid'] === $levelid)) { |
367 | $level['class'] .= ' currentchecked'; | |
368 | } | |
39c6f4b6 | 369 | $level['tdwidth'] = 100/count($criterion['levels']); |
3c2b3cb3 | 370 | $levelsstr .= $this->level_template($mode, $options, $elementname, $id, $level); |
ab156741 | 371 | } |
3c2b3cb3 | 372 | $criteriastr .= $this->criterion_template($mode, $options, $elementname, $criterion, $levelsstr, $criterionvalue); |
ab156741 | 373 | } |
3c2b3cb3 | 374 | return $this->rubric_template($mode, $options, $elementname, $criteriastr); |
ab156741 MG |
375 | } |
376 | ||
377 | /** | |
fc5adc3b | 378 | * Help function to return CSS class names for element (first/last/even/odd) with leading space |
ab156741 | 379 | * |
3c2b3cb3 MG |
380 | * @param int $idx index of this element in the row/column |
381 | * @param int $maxidx maximum index of the element in the row/column | |
ab156741 MG |
382 | * @return string |
383 | */ | |
3c2b3cb3 | 384 | protected function get_css_class_suffix($idx, $maxidx) { |
ab156741 | 385 | $class = ''; |
3c2b3cb3 | 386 | if ($idx == 0) { |
ab156741 MG |
387 | $class .= ' first'; |
388 | } | |
3c2b3cb3 | 389 | if ($idx == $maxidx) { |
ab156741 MG |
390 | $class .= ' last'; |
391 | } | |
3c2b3cb3 | 392 | if ($idx%2) { |
ab156741 MG |
393 | $class .= ' odd'; |
394 | } else { | |
395 | $class .= ' even'; | |
396 | } | |
397 | return $class; | |
398 | } | |
36937f02 MG |
399 | |
400 | /** | |
401 | * Displays for the student the list of instances or default content if no instances found | |
402 | * | |
403 | * @param array $instances array of objects of type gradingform_rubric_instance | |
404 | * @param string $defaultcontent default string that would be displayed without advanced grading | |
0136124e | 405 | * @param boolean $cangrade whether current user has capability to grade in this context |
36937f02 MG |
406 | * @return string |
407 | */ | |
0136124e | 408 | public function display_instances($instances, $defaultcontent, $cangrade) { |
3c2b3cb3 | 409 | $return = ''; |
36937f02 | 410 | if (sizeof($instances)) { |
3c2b3cb3 | 411 | $return .= html_writer::start_tag('div', array('class' => 'advancedgrade')); |
36937f02 MG |
412 | $idx = 0; |
413 | foreach ($instances as $instance) { | |
3c2b3cb3 | 414 | $return .= $this->display_instance($instance, $idx++, $cangrade); |
36937f02 | 415 | } |
3c2b3cb3 | 416 | $return .= html_writer::end_tag('div'); |
36937f02 | 417 | } |
3c2b3cb3 | 418 | return $return. $defaultcontent; |
36937f02 MG |
419 | } |
420 | ||
421 | /** | |
422 | * Displays one grading instance | |
423 | * | |
424 | * @param gradingform_rubric_instance $instance | |
425 | * @param int idx unique number of instance on page | |
0136124e | 426 | * @param boolean $cangrade whether current user has capability to grade in this context |
36937f02 | 427 | */ |
0136124e | 428 | public function display_instance(gradingform_rubric_instance $instance, $idx, $cangrade) { |
36937f02 | 429 | $criteria = $instance->get_controller()->get_definition()->rubric_criteria; |
39c6f4b6 | 430 | $options = $instance->get_controller()->get_options(); |
36937f02 | 431 | $values = $instance->get_rubric_filling(); |
0136124e MG |
432 | if ($cangrade) { |
433 | $mode = gradingform_rubric_controller::DISPLAY_REVIEW; | |
434 | } else { | |
435 | $mode = gradingform_rubric_controller::DISPLAY_VIEW; | |
436 | } | |
437 | return $this->display_rubric($criteria, $options, $mode, 'rubric'.$idx, $values); | |
438 | } | |
439 | ||
440 | public function display_regrade_confirmation($elementname, $changelevel, $value) { | |
441 | $html = html_writer::start_tag('div', array('class' => 'gradingform_rubric-regrade')); | |
442 | if ($changelevel<=2) { | |
443 | $html .= get_string('regrademessage1', 'gradingform_rubric'); | |
444 | $selectoptions = array( | |
445 | 0 => get_string('regradeoption0', 'gradingform_rubric'), | |
446 | 1 => get_string('regradeoption1', 'gradingform_rubric') | |
447 | ); | |
448 | $html .= html_writer::select($selectoptions, $elementname.'[regrade]', $value, false); | |
449 | } else { | |
450 | $html .= get_string('regrademessage5', 'gradingform_rubric'); | |
451 | $html .= html_writer::empty_tag('input', array('name' => $elementname.'[regrade]', 'value' => 1, 'type' => 'hidden')); | |
452 | } | |
453 | $html .= html_writer::end_tag('div'); | |
454 | return $html; | |
36937f02 | 455 | } |
a19d1057 MG |
456 | |
457 | /** | |
458 | * Generates and returns HTML code to display information box about how rubric score is converted to the grade | |
459 | * | |
460 | * @param array $scores | |
461 | * @return string | |
462 | */ | |
463 | public function display_rubric_mapping_explained($scores) { | |
464 | $html = ''; | |
465 | if (!$scores) { | |
466 | return $html; | |
467 | } | |
468 | $html .= $this->box( | |
469 | html_writer::tag('h4', get_string('rubricmapping', 'gradingform_rubric')). | |
470 | html_writer::tag('div', get_string('rubricmappingexplained', 'gradingform_rubric', (object)$scores)) | |
471 | , 'generalbox rubricmappingexplained'); | |
472 | return $html; | |
473 | } | |
c586d2bf | 474 | } |