MDL-30241: fixing Javascript to work in IE as well
[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
50 * @param string $levels_str evaluated templates for this criterion levels
51 * @param array|null $value (only in view mode) teacher's feedback on this criterion
ab156741
MG
52 * @return string
53 */
39c6f4b6 54 public function criterion_template($mode, $options, $elementname = '{NAME}', $criterion = null, $levels_str = '{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 }
39c6f4b6 66 $criterion_template = 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) {
8df55bbe 68 $criterion_template .= 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));
ab156741
MG
73 $criterion_template .= html_writer::tag('div', $button, array('class' => $key));
74 }
8df55bbe 75 $criterion_template .= html_writer::end_tag('td'); // .controls
39c6f4b6
MG
76 $criterion_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][sortorder]', 'value' => $criterion['sortorder']));
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) {
39c6f4b6
MG
80 $criterion_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][sortorder]', 'value' => $criterion['sortorder']));
81 $criterion_template .= 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 }
89 $criterion_template .= html_writer::tag('td', $description, array('class' => $descriptionclass, 'id' => '{NAME}-criteria-{CRITERION-id}-description'));
39c6f4b6 90 $levels_str_table = html_writer::tag('table', html_writer::tag('tr', $levels_str, array('id' => '{NAME}-criteria-{CRITERION-id}-levels')));
2ae7faf1
MG
91 $levelsclass = 'levels';
92 if (isset($criterion['error_levels'])) {
93 $levelsclass .= ' error';
94 }
95 $criterion_template .= html_writer::tag('td', $levels_str_table, 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));
8df55bbe 100 $criterion_template .= 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'));
110 $criterion_template .= html_writer::tag('td', $input, array('class' => 'remark'));
111 } else if ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN) {
112 $criterion_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][remark]', 'value' => $currentremark));
113 }else if ($mode == gradingform_rubric_controller::DISPLAY_REVIEW || $mode == gradingform_rubric_controller::DISPLAY_VIEW) {
114 $criterion_template .= html_writer::tag('td', $currentremark, array('class' => 'remark')); // TODO maybe some prefix here like 'Teacher remark:'
115 }
5060997b 116 }
8df55bbe 117 $criterion_template .= html_writer::end_tag('tr'); // .criterion
ab156741
MG
118
119 $criterion_template = str_replace('{NAME}', $elementname, $criterion_template);
120 $criterion_template = str_replace('{CRITERION-id}', $criterion['id'], $criterion_template);
121 return $criterion_template;
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 }
163 $level_template = html_writer::start_tag('td', $tdattributes);
164 $level_template .= html_writer::start_tag('div', array('class' => 'level-wrapper'));
ab156741 165 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
39c6f4b6
MG
166 $definition = html_writer::tag('textarea', htmlspecialchars($level['definition']), array('name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][definition]', 'cols' => '10', 'rows' => '4'));
167 $score = html_writer::empty_tag('input', array('type' => 'text', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][score]', 'size' => '4', 'value' => $level['score']));
ab156741
MG
168 } else {
169 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
39c6f4b6
MG
170 $level_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][definition]', 'value' => $level['definition']));
171 $level_template .= 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
MG
178 ($level['checked'] ? array('checked' => 'checked') : array()));
179 $level_template .= html_writer::tag('div', $input, array('class' => 'radio'));
180 }
181 if ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN && $level['checked']) {
5060997b 182 $level_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id']));
ab156741 183 }
39c6f4b6 184 $score = html_writer::tag('span', $score, array('id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-score'));
2ae7faf1
MG
185 $definitionclass = 'definition';
186 if (isset($level['error_definition'])) {
187 $definitionclass .= ' error';
188 }
189 $level_template .= 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 }
de2eb195 202 $level_template .= 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));
ab156741
MG
207 $level_template .= html_writer::tag('div', $button, array('class' => 'delete'));
208 }
39c6f4b6 209 $level_template .= html_writer::end_tag('div'); // .level-wrapper
8df55bbe 210 $level_template .= html_writer::end_tag('td'); // .level
ab156741
MG
211
212 $level_template = str_replace('{NAME}', $elementname, $level_template);
213 $level_template = str_replace('{CRITERION-id}', $criterionid, $level_template);
214 $level_template = str_replace('{LEVEL-id}', $level['id'], $level_template);
215 return $level_template;
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)
233 * @param string $criteria_str evaluated templates for this rubric's criteria
234 * @return string
235 */
39c6f4b6 236 protected function rubric_template($mode, $options, $elementname, $criteria_str) {
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
fc5adc3b 255 $rubric_template = html_writer::start_tag('div', array('id' => 'rubric-{NAME}', 'class' => 'clearfix gradingform_rubric'.$classsuffix));
8df55bbe 256 $rubric_template .= html_writer::tag('table', $criteria_str, 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));
ab156741
MG
260 $rubric_template .= html_writer::tag('div', $input, array('class' => 'addcriterion'));
261 }
39c6f4b6 262 $rubric_template .= $this->rubric_edit_options($mode, $options);
ab156741
MG
263 $rubric_template .= html_writer::end_tag('div');
264
265 return str_replace('{NAME}', $elementname, $rubric_template);
266 }
267
39c6f4b6
MG
268 protected function rubric_edit_options($mode, $options) {
269 if ($mode != gradingform_rubric_controller::DISPLAY_EDIT_FULL
270 && $mode != gradingform_rubric_controller::DISPLAY_EDIT_FROZEN
271 && $mode != gradingform_rubric_controller::DISPLAY_PREVIEW) {
272 // Options are displayed only in edit mode
273 return;
274 }
275 $html = html_writer::start_tag('div', array('class' => 'options'));
276 $html .= html_writer::tag('div', get_string('rubricoptions', 'gradingform_rubric'), array('class' => 'optionsheading'));
277 $attrs = array('type' => 'hidden', 'name' => '{NAME}[options][optionsset]', 'value' => 1);
278 foreach ($options as $option => $value) {
279 $html .= html_writer::start_tag('div', array('class' => 'option '.$option));
280 $attrs = array('name' => '{NAME}[options]['.$option.']', 'id' => '{NAME}-options-'.$option);
281 switch ($option) {
282 case 'sortlevelsasc':
283 // Display option as dropdown
284 $html .= html_writer::tag('span', get_string($option, 'gradingform_rubric'), array('class' => 'label'));
285 $value = (int)(!!$value); // make sure $value is either 0 or 1
286 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
287 $selectoptions = array(0 => get_string($option.'0', 'gradingform_rubric'), 1 => get_string($option.'1', 'gradingform_rubric'));
288 $value_str = html_writer::select($selectoptions, $attrs['name'], $value, false, array('id' => $attrs['id']));
289 $html .= html_writer::tag('span', $value_str, array('class' => 'value'));
290 // TODO add here button 'Sort levels'
291 } else {
292 $html .= html_writer::tag('span', get_string($option.$value, 'gradingform_rubric'), array('class' => 'value'));
293 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
294 $html .= html_writer::empty_tag('input', $attrs + array('type' => 'hidden', 'value' => $value));
295 }
296 }
297 break;
298 default:
0136124e
MG
299 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN && $value) {
300 $html .= html_writer::empty_tag('input', $attrs + array('type' => 'hidden', 'value' => $value));
301 }
39c6f4b6
MG
302 // Display option as checkbox
303 $attrs['type'] = 'checkbox';
304 $attrs['value'] = 1;
305 if ($value) {
306 $attrs['checked'] = 'checked';
307 }
0136124e 308 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_PREVIEW) {
39c6f4b6
MG
309 $attrs['disabled'] = 'disabled';
310 unset($attrs['name']);
311 }
312 $html .= html_writer::empty_tag('input', $attrs);
313 $html .= html_writer::tag('label', get_string($option, 'gradingform_rubric'), array('for' => $attrs['id']));
314 break;
315 }
316 $html .= html_writer::end_tag('div'); // .option
317 }
318 $html .= html_writer::end_tag('div'); // .options
319 return $html;
320 }
321
ab156741 322 /**
fc5adc3b
MG
323 * This function returns html code for displaying rubric. Depending on $mode it may be the code
324 * to edit rubric, to preview the rubric, to evaluate somebody or to review the evaluation.
325 *
326 * It is very unlikely that this function needs to be overriden by theme. It does not produce
327 * any html code, it just prepares data about rubric design and evaluation, adds the CSS
328 * class to elements and calls the functions level_template, criterion_template and
329 * rubric_template
ab156741 330 *
fc5adc3b
MG
331 * @param array $criteria data about the rubric design
332 * @param int $mode rubric display mode @see gradingform_rubric_controller
333 * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode)
334 * @param array $values evaluation result
ab156741
MG
335 * @return string
336 */
39c6f4b6 337 public function display_rubric($criteria, $options, $mode, $elementname = null, $values = null) {
ab156741
MG
338 $criteria_str = '';
339 $cnt = 0;
340 foreach ($criteria as $id => $criterion) {
341 $criterion['class'] = $this->get_css_class_suffix($cnt++, sizeof($criteria) -1);
39c6f4b6 342 $criterion['id'] = $id;
ab156741
MG
343 $levels_str = '';
344 $levelcnt = 0;
5060997b
MG
345 if (isset($values['criteria'][$id])) {
346 $criterionvalue = $values['criteria'][$id];
347 } else {
348 $criterionvalue = null;
349 }
ab156741 350 foreach ($criterion['levels'] as $levelid => $level) {
39c6f4b6 351 $level['id'] = $levelid;
ab156741 352 $level['class'] = $this->get_css_class_suffix($levelcnt++, sizeof($criterion['levels']) -1);
5060997b 353 $level['checked'] = (isset($criterionvalue['levelid']) && ((int)$criterionvalue['levelid'] === $levelid));
39c6f4b6 354 if ($level['checked'] && ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_REVIEW || $mode == gradingform_rubric_controller::DISPLAY_VIEW)) {
ab156741 355 $level['class'] .= ' checked';
fc5adc3b 356 //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 357 }
188e840b
MG
358 if (isset($criterionvalue['savedlevelid']) && ((int)$criterionvalue['savedlevelid'] === $levelid)) {
359 $level['class'] .= ' currentchecked';
360 }
39c6f4b6
MG
361 $level['tdwidth'] = 100/count($criterion['levels']);
362 $levels_str .= $this->level_template($mode, $options, $elementname, $id, $level);
ab156741 363 }
39c6f4b6 364 $criteria_str .= $this->criterion_template($mode, $options, $elementname, $criterion, $levels_str, $criterionvalue);
ab156741 365 }
39c6f4b6 366 return $this->rubric_template($mode, $options, $elementname, $criteria_str);
ab156741
MG
367 }
368
369 /**
fc5adc3b 370 * Help function to return CSS class names for element (first/last/even/odd) with leading space
ab156741 371 *
fc5adc3b
MG
372 * @param int $cnt
373 * @param int $maxcnt
ab156741
MG
374 * @return string
375 */
fc5adc3b 376 protected function get_css_class_suffix($cnt, $maxcnt) {
ab156741
MG
377 $class = '';
378 if ($cnt == 0) {
379 $class .= ' first';
380 }
381 if ($cnt == $maxcnt) {
382 $class .= ' last';
383 }
384 if ($cnt%2) {
385 $class .= ' odd';
386 } else {
387 $class .= ' even';
388 }
389 return $class;
390 }
36937f02
MG
391
392 /**
393 * Displays for the student the list of instances or default content if no instances found
394 *
395 * @param array $instances array of objects of type gradingform_rubric_instance
396 * @param string $defaultcontent default string that would be displayed without advanced grading
0136124e 397 * @param boolean $cangrade whether current user has capability to grade in this context
36937f02
MG
398 * @return string
399 */
0136124e
MG
400 public function display_instances($instances, $defaultcontent, $cangrade) {
401 $rv = '';
36937f02 402 if (sizeof($instances)) {
0136124e 403 $rv .= html_writer::start_tag('div', array('class' => 'advancedgrade'));
36937f02
MG
404 $idx = 0;
405 foreach ($instances as $instance) {
0136124e 406 $rv .= $this->display_instance($instance, $idx++, $cangrade);
36937f02
MG
407 }
408 $rv .= html_writer::end_tag('div');
409 }
410 return $rv. $defaultcontent;
411 }
412
413 /**
414 * Displays one grading instance
415 *
416 * @param gradingform_rubric_instance $instance
417 * @param int idx unique number of instance on page
0136124e 418 * @param boolean $cangrade whether current user has capability to grade in this context
36937f02 419 */
0136124e 420 public function display_instance(gradingform_rubric_instance $instance, $idx, $cangrade) {
36937f02 421 $criteria = $instance->get_controller()->get_definition()->rubric_criteria;
39c6f4b6 422 $options = $instance->get_controller()->get_options();
36937f02 423 $values = $instance->get_rubric_filling();
0136124e
MG
424 if ($cangrade) {
425 $mode = gradingform_rubric_controller::DISPLAY_REVIEW;
426 } else {
427 $mode = gradingform_rubric_controller::DISPLAY_VIEW;
428 }
429 return $this->display_rubric($criteria, $options, $mode, 'rubric'.$idx, $values);
430 }
431
432 public function display_regrade_confirmation($elementname, $changelevel, $value) {
433 $html = html_writer::start_tag('div', array('class' => 'gradingform_rubric-regrade'));
434 if ($changelevel<=2) {
435 $html .= get_string('regrademessage1', 'gradingform_rubric');
436 $selectoptions = array(
437 0 => get_string('regradeoption0', 'gradingform_rubric'),
438 1 => get_string('regradeoption1', 'gradingform_rubric')
439 );
440 $html .= html_writer::select($selectoptions, $elementname.'[regrade]', $value, false);
441 } else {
442 $html .= get_string('regrademessage5', 'gradingform_rubric');
443 $html .= html_writer::empty_tag('input', array('name' => $elementname.'[regrade]', 'value' => 1, 'type' => 'hidden'));
444 }
445 $html .= html_writer::end_tag('div');
446 return $html;
36937f02 447 }
c586d2bf 448}