MDL-30998: updated docblocks for backup of advanced grading
[moodle.git] / grade / grading / form / rubric / renderer.php
CommitLineData
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
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Grading method plugin renderer
29 */
2ae7faf1 30class 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'));
29bea634
RW
167 $score = html_writer::label(get_string('criterionempty', 'gradingform_rubric'), '{NAME}criteria{CRITERION-id}levels{LEVEL-id}', false, array('class' => 'accesshide'));
168 $score .= html_writer::empty_tag('input', array('type' => 'text','id' => '{NAME}criteria{CRITERION-id}levels{LEVEL-id}', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][score]', 'size' => '3', 'value' => $level['score']));
ab156741
MG
169 } else {
170 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
3c2b3cb3
MG
171 $leveltemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][definition]', 'value' => $level['definition']));
172 $leveltemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][score]', 'value' => $level['score']));
ab156741
MG
173 }
174 $definition = $level['definition'];
175 $score = $level['score'];
176 }
177 if ($mode == gradingform_rubric_controller::DISPLAY_EVAL) {
5060997b 178 $input = html_writer::empty_tag('input', array('type' => 'radio', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id']) +
ab156741 179 ($level['checked'] ? array('checked' => 'checked') : array()));
3c2b3cb3 180 $leveltemplate .= html_writer::tag('div', $input, array('class' => 'radio'));
ab156741
MG
181 }
182 if ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN && $level['checked']) {
3c2b3cb3 183 $leveltemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id']));
ab156741 184 }
a19d1057 185 $score = html_writer::tag('span', $score, array('id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-score', 'class' => 'scorevalue'));
2ae7faf1
MG
186 $definitionclass = 'definition';
187 if (isset($level['error_definition'])) {
188 $definitionclass .= ' error';
189 }
3c2b3cb3 190 $leveltemplate .= html_writer::tag('div', $definition, array('class' => $definitionclass, 'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-definition'));
39c6f4b6
MG
191 $displayscore = true;
192 if (!$options['showscoreteacher'] && in_array($mode, array(gradingform_rubric_controller::DISPLAY_EVAL, gradingform_rubric_controller::DISPLAY_EVAL_FROZEN, gradingform_rubric_controller::DISPLAY_REVIEW))) {
193 $displayscore = false;
194 }
577c8964 195 if (!$options['showscorestudent'] && in_array($mode, array(gradingform_rubric_controller::DISPLAY_VIEW, gradingform_rubric_controller::DISPLAY_PREVIEW_GRADED))) {
39c6f4b6
MG
196 $displayscore = false;
197 }
198 if ($displayscore) {
2ae7faf1
MG
199 $scoreclass = 'score';
200 if (isset($level['error_score'])) {
201 $scoreclass .= ' error';
202 }
3c2b3cb3 203 $leveltemplate .= html_writer::tag('div', get_string('scorepostfix', 'gradingform_rubric', $score), array('class' => $scoreclass));
39c6f4b6 204 }
ab156741
MG
205 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
206 $value = get_string('leveldelete', 'gradingform_rubric');
4f110c47 207 $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 208 $leveltemplate .= html_writer::tag('div', $button, array('class' => 'delete'));
ab156741 209 }
3c2b3cb3
MG
210 $leveltemplate .= html_writer::end_tag('div'); // .level-wrapper
211 $leveltemplate .= html_writer::end_tag('td'); // .level
ab156741 212
3c2b3cb3
MG
213 $leveltemplate = str_replace('{NAME}', $elementname, $leveltemplate);
214 $leveltemplate = str_replace('{CRITERION-id}', $criterionid, $leveltemplate);
215 $leveltemplate = str_replace('{LEVEL-id}', $level['id'], $leveltemplate);
216 return $leveltemplate;
ab156741
MG
217 }
218
fc5adc3b
MG
219 /**
220 * This function returns html code for displaying rubric template (content before and after
221 * criteria list). Depending on $mode it may be the code to edit rubric, to preview the rubric,
222 * to evaluate somebody or to review the evaluation.
223 *
224 * This function is called from display_rubric() to display the whole rubric.
225 *
226 * When overriding this function it is very important to remember that all elements of html
227 * form (in edit or evaluate mode) must have the name $elementname.
228 *
229 * Also JavaScript relies on the class names of elements and when developer changes them
230 * script might stop working.
231 *
232 * @param int $mode rubric display mode @see gradingform_rubric_controller
233 * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode)
3c2b3cb3 234 * @param string $criteriastr evaluated templates for this rubric's criteria
fc5adc3b
MG
235 * @return string
236 */
3c2b3cb3 237 protected function rubric_template($mode, $options, $elementname, $criteriastr) {
ab156741
MG
238 $classsuffix = ''; // CSS suffix for class of the main div. Depends on the mode
239 switch ($mode) {
240 case gradingform_rubric_controller::DISPLAY_EDIT_FULL:
241 $classsuffix = ' editor editable'; break;
242 case gradingform_rubric_controller::DISPLAY_EDIT_FROZEN:
243 $classsuffix = ' editor frozen'; break;
244 case gradingform_rubric_controller::DISPLAY_PREVIEW:
577c8964 245 case gradingform_rubric_controller::DISPLAY_PREVIEW_GRADED:
ab156741
MG
246 $classsuffix = ' editor preview'; break;
247 case gradingform_rubric_controller::DISPLAY_EVAL:
248 $classsuffix = ' evaluate editable'; break;
249 case gradingform_rubric_controller::DISPLAY_EVAL_FROZEN:
250 $classsuffix = ' evaluate frozen'; break;
251 case gradingform_rubric_controller::DISPLAY_REVIEW:
252 $classsuffix = ' review'; break;
39c6f4b6
MG
253 case gradingform_rubric_controller::DISPLAY_VIEW:
254 $classsuffix = ' view'; break;
ab156741
MG
255 }
256
3c2b3cb3
MG
257 $rubrictemplate = html_writer::start_tag('div', array('id' => 'rubric-{NAME}', 'class' => 'clearfix gradingform_rubric'.$classsuffix));
258 $rubrictemplate .= html_writer::tag('table', $criteriastr, array('class' => 'criteria', 'id' => '{NAME}-criteria'));
ab156741
MG
259 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
260 $value = get_string('addcriterion', 'gradingform_rubric');
39c6f4b6 261 $input = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][addcriterion]', 'id' => '{NAME}-criteria-addcriterion', 'value' => $value, 'title' => $value));
3c2b3cb3 262 $rubrictemplate .= html_writer::tag('div', $input, array('class' => 'addcriterion'));
ab156741 263 }
3c2b3cb3
MG
264 $rubrictemplate .= $this->rubric_edit_options($mode, $options);
265 $rubrictemplate .= html_writer::end_tag('div');
ab156741 266
3c2b3cb3 267 return str_replace('{NAME}', $elementname, $rubrictemplate);
ab156741
MG
268 }
269
3c2b3cb3
MG
270 /**
271 * Generates html template to view/edit the rubric options. Expression {NAME} is used in
272 * template for the form element name
273 *
274 * @param int $mode
275 * @param array $options
276 * @return string
277 */
39c6f4b6
MG
278 protected function rubric_edit_options($mode, $options) {
279 if ($mode != gradingform_rubric_controller::DISPLAY_EDIT_FULL
280 && $mode != gradingform_rubric_controller::DISPLAY_EDIT_FROZEN
281 && $mode != gradingform_rubric_controller::DISPLAY_PREVIEW) {
577c8964 282 // Options are displayed only for people who can manage
39c6f4b6
MG
283 return;
284 }
285 $html = html_writer::start_tag('div', array('class' => 'options'));
286 $html .= html_writer::tag('div', get_string('rubricoptions', 'gradingform_rubric'), array('class' => 'optionsheading'));
287 $attrs = array('type' => 'hidden', 'name' => '{NAME}[options][optionsset]', 'value' => 1);
288 foreach ($options as $option => $value) {
289 $html .= html_writer::start_tag('div', array('class' => 'option '.$option));
290 $attrs = array('name' => '{NAME}[options]['.$option.']', 'id' => '{NAME}-options-'.$option);
291 switch ($option) {
292 case 'sortlevelsasc':
293 // Display option as dropdown
29bea634 294 $html .= html_writer::label(get_string($option, 'gradingform_rubric'), $attrs['id'], false, array('class' => 'label'));
39c6f4b6
MG
295 $value = (int)(!!$value); // make sure $value is either 0 or 1
296 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
297 $selectoptions = array(0 => get_string($option.'0', 'gradingform_rubric'), 1 => get_string($option.'1', 'gradingform_rubric'));
3c2b3cb3
MG
298 $valuestr = html_writer::select($selectoptions, $attrs['name'], $value, false, array('id' => $attrs['id']));
299 $html .= html_writer::tag('span', $valuestr, array('class' => 'value'));
39c6f4b6
MG
300 // TODO add here button 'Sort levels'
301 } else {
302 $html .= html_writer::tag('span', get_string($option.$value, 'gradingform_rubric'), array('class' => 'value'));
303 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
304 $html .= html_writer::empty_tag('input', $attrs + array('type' => 'hidden', 'value' => $value));
305 }
306 }
307 break;
308 default:
0136124e
MG
309 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN && $value) {
310 $html .= html_writer::empty_tag('input', $attrs + array('type' => 'hidden', 'value' => $value));
311 }
39c6f4b6
MG
312 // Display option as checkbox
313 $attrs['type'] = 'checkbox';
314 $attrs['value'] = 1;
315 if ($value) {
316 $attrs['checked'] = 'checked';
317 }
0136124e 318 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_PREVIEW) {
39c6f4b6
MG
319 $attrs['disabled'] = 'disabled';
320 unset($attrs['name']);
321 }
322 $html .= html_writer::empty_tag('input', $attrs);
323 $html .= html_writer::tag('label', get_string($option, 'gradingform_rubric'), array('for' => $attrs['id']));
324 break;
325 }
326 $html .= html_writer::end_tag('div'); // .option
327 }
328 $html .= html_writer::end_tag('div'); // .options
329 return $html;
330 }
331
ab156741 332 /**
fc5adc3b
MG
333 * This function returns html code for displaying rubric. Depending on $mode it may be the code
334 * to edit rubric, to preview the rubric, to evaluate somebody or to review the evaluation.
335 *
336 * It is very unlikely that this function needs to be overriden by theme. It does not produce
337 * any html code, it just prepares data about rubric design and evaluation, adds the CSS
338 * class to elements and calls the functions level_template, criterion_template and
339 * rubric_template
ab156741 340 *
fc5adc3b
MG
341 * @param array $criteria data about the rubric design
342 * @param int $mode rubric display mode @see gradingform_rubric_controller
343 * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode)
344 * @param array $values evaluation result
ab156741
MG
345 * @return string
346 */
39c6f4b6 347 public function display_rubric($criteria, $options, $mode, $elementname = null, $values = null) {
3c2b3cb3 348 $criteriastr = '';
ab156741
MG
349 $cnt = 0;
350 foreach ($criteria as $id => $criterion) {
351 $criterion['class'] = $this->get_css_class_suffix($cnt++, sizeof($criteria) -1);
39c6f4b6 352 $criterion['id'] = $id;
3c2b3cb3 353 $levelsstr = '';
ab156741 354 $levelcnt = 0;
5060997b
MG
355 if (isset($values['criteria'][$id])) {
356 $criterionvalue = $values['criteria'][$id];
357 } else {
358 $criterionvalue = null;
359 }
ab156741 360 foreach ($criterion['levels'] as $levelid => $level) {
39c6f4b6 361 $level['id'] = $levelid;
ab156741 362 $level['class'] = $this->get_css_class_suffix($levelcnt++, sizeof($criterion['levels']) -1);
5060997b 363 $level['checked'] = (isset($criterionvalue['levelid']) && ((int)$criterionvalue['levelid'] === $levelid));
39c6f4b6 364 if ($level['checked'] && ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_REVIEW || $mode == gradingform_rubric_controller::DISPLAY_VIEW)) {
ab156741 365 $level['class'] .= ' checked';
fc5adc3b 366 //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 367 }
188e840b
MG
368 if (isset($criterionvalue['savedlevelid']) && ((int)$criterionvalue['savedlevelid'] === $levelid)) {
369 $level['class'] .= ' currentchecked';
370 }
39c6f4b6 371 $level['tdwidth'] = 100/count($criterion['levels']);
3c2b3cb3 372 $levelsstr .= $this->level_template($mode, $options, $elementname, $id, $level);
ab156741 373 }
3c2b3cb3 374 $criteriastr .= $this->criterion_template($mode, $options, $elementname, $criterion, $levelsstr, $criterionvalue);
ab156741 375 }
3c2b3cb3 376 return $this->rubric_template($mode, $options, $elementname, $criteriastr);
ab156741
MG
377 }
378
379 /**
fc5adc3b 380 * Help function to return CSS class names for element (first/last/even/odd) with leading space
ab156741 381 *
3c2b3cb3
MG
382 * @param int $idx index of this element in the row/column
383 * @param int $maxidx maximum index of the element in the row/column
ab156741
MG
384 * @return string
385 */
3c2b3cb3 386 protected function get_css_class_suffix($idx, $maxidx) {
ab156741 387 $class = '';
3c2b3cb3 388 if ($idx == 0) {
ab156741
MG
389 $class .= ' first';
390 }
3c2b3cb3 391 if ($idx == $maxidx) {
ab156741
MG
392 $class .= ' last';
393 }
3c2b3cb3 394 if ($idx%2) {
ab156741
MG
395 $class .= ' odd';
396 } else {
397 $class .= ' even';
398 }
399 return $class;
400 }
36937f02
MG
401
402 /**
403 * Displays for the student the list of instances or default content if no instances found
404 *
405 * @param array $instances array of objects of type gradingform_rubric_instance
406 * @param string $defaultcontent default string that would be displayed without advanced grading
0136124e 407 * @param boolean $cangrade whether current user has capability to grade in this context
36937f02
MG
408 * @return string
409 */
0136124e 410 public function display_instances($instances, $defaultcontent, $cangrade) {
3c2b3cb3 411 $return = '';
36937f02 412 if (sizeof($instances)) {
3c2b3cb3 413 $return .= html_writer::start_tag('div', array('class' => 'advancedgrade'));
36937f02
MG
414 $idx = 0;
415 foreach ($instances as $instance) {
3c2b3cb3 416 $return .= $this->display_instance($instance, $idx++, $cangrade);
36937f02 417 }
3c2b3cb3 418 $return .= html_writer::end_tag('div');
36937f02 419 }
3c2b3cb3 420 return $return. $defaultcontent;
36937f02
MG
421 }
422
423 /**
424 * Displays one grading instance
425 *
426 * @param gradingform_rubric_instance $instance
427 * @param int idx unique number of instance on page
0136124e 428 * @param boolean $cangrade whether current user has capability to grade in this context
36937f02 429 */
0136124e 430 public function display_instance(gradingform_rubric_instance $instance, $idx, $cangrade) {
36937f02 431 $criteria = $instance->get_controller()->get_definition()->rubric_criteria;
39c6f4b6 432 $options = $instance->get_controller()->get_options();
36937f02 433 $values = $instance->get_rubric_filling();
0136124e
MG
434 if ($cangrade) {
435 $mode = gradingform_rubric_controller::DISPLAY_REVIEW;
577c8964 436 $showdescription = $options['showdescriptionteacher'];
0136124e
MG
437 } else {
438 $mode = gradingform_rubric_controller::DISPLAY_VIEW;
577c8964 439 $showdescription = $options['showdescriptionstudent'];
0136124e 440 }
577c8964
MG
441 $output = '';
442 if ($showdescription) {
443 $output .= $this->box($instance->get_controller()->get_formatted_description(), 'gradingform_rubric-description');
444 }
445 $output .= $this->display_rubric($criteria, $options, $mode, 'rubric'.$idx, $values);
446 return $output;
0136124e
MG
447 }
448
449 public function display_regrade_confirmation($elementname, $changelevel, $value) {
450 $html = html_writer::start_tag('div', array('class' => 'gradingform_rubric-regrade'));
451 if ($changelevel<=2) {
29bea634 452 $html .= html_writer::label(get_string('regrademessage1', 'gradingform_rubric'), 'menu' . $elementname . 'regrade');
0136124e
MG
453 $selectoptions = array(
454 0 => get_string('regradeoption0', 'gradingform_rubric'),
455 1 => get_string('regradeoption1', 'gradingform_rubric')
456 );
457 $html .= html_writer::select($selectoptions, $elementname.'[regrade]', $value, false);
458 } else {
459 $html .= get_string('regrademessage5', 'gradingform_rubric');
460 $html .= html_writer::empty_tag('input', array('name' => $elementname.'[regrade]', 'value' => 1, 'type' => 'hidden'));
461 }
462 $html .= html_writer::end_tag('div');
463 return $html;
36937f02 464 }
a19d1057
MG
465
466 /**
467 * Generates and returns HTML code to display information box about how rubric score is converted to the grade
468 *
469 * @param array $scores
470 * @return string
471 */
472 public function display_rubric_mapping_explained($scores) {
473 $html = '';
474 if (!$scores) {
475 return $html;
476 }
477 $html .= $this->box(
478 html_writer::tag('h4', get_string('rubricmapping', 'gradingform_rubric')).
479 html_writer::tag('div', get_string('rubricmappingexplained', 'gradingform_rubric', (object)$scores))
480 , 'generalbox rubricmappingexplained');
481 return $html;
482 }
c586d2bf 483}