3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * @package gradingform
21 * @copyright 2011 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * Grading method plugin renderer
30 class gradingform_rubric_renderer
extends plugin_renderer_base
{
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.
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.
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.
44 * Also JavaScript relies on the class names of elements and when developer changes them
45 * script might stop working.
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 $levelsstr evaluated templates for this criterion levels
51 * @param array|null $value (only in view mode) teacher's feedback on this criterion
54 public function criterion_template($mode, $options, $elementname = '{NAME}', $criterion = null, $levelsstr = '{LEVELS}', $value = null) {
55 // TODO description format, remark format
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}');
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] = '';
66 $criteriontemplate = html_writer
::start_tag('tr', array('class' => 'criterion'. $criterion['class'], 'id' => '{NAME}-criteria-{CRITERION-id}'));
67 if ($mode == gradingform_rubric_controller
::DISPLAY_EDIT_FULL
) {
68 $criteriontemplate .= html_writer
::start_tag('td', array('class' => 'controls'));
69 foreach (array('moveup', 'delete', 'movedown') as $key) {
70 $value = get_string('criterion'.$key, 'gradingform_rubric');
71 $button = html_writer
::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][{CRITERION-id}]['.$key.']',
72 'id' => '{NAME}-criteria-{CRITERION-id}-'.$key, 'value' => $value, 'title' => $value, 'tabindex' => -1));
73 $criteriontemplate .= html_writer
::tag('div', $button, array('class' => $key));
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']));
77 $description = html_writer
::tag('textarea', htmlspecialchars($criterion['description']), array('name' => '{NAME}[criteria][{CRITERION-id}][description]', 'cols' => '10', 'rows' => '5'));
79 if ($mode == gradingform_rubric_controller
::DISPLAY_EDIT_FROZEN
) {
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']));
83 $description = $criterion['description'];
85 $descriptionclass = 'description';
86 if (isset($criterion['error_description'])) {
87 $descriptionclass .= ' error';
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')));
91 $levelsclass = 'levels';
92 if (isset($criterion['error_levels'])) {
93 $levelsclass .= ' error';
95 $criteriontemplate .= html_writer
::tag('td', $levelsstrtable, array('class' => $levelsclass));
96 if ($mode == gradingform_rubric_controller
::DISPLAY_EDIT_FULL
) {
97 $value = get_string('criterionaddlevel', 'gradingform_rubric');
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));
100 $criteriontemplate .= html_writer
::tag('td', $button, array('class' => 'addlevel'));
102 $displayremark = ($options['enableremarks'] && ($mode != gradingform_rubric_controller
::DISPLAY_VIEW ||
$options['showremarksstudent']));
103 if ($displayremark) {
105 if (isset($value['remark'])) {
106 $currentremark = $value['remark'];
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 $criteriontemplate .= html_writer
::tag('td', $input, array('class' => 'remark'));
111 } else if ($mode == gradingform_rubric_controller
::DISPLAY_EVAL_FROZEN
) {
112 $criteriontemplate .= 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 $criteriontemplate .= html_writer
::tag('td', $currentremark, array('class' => 'remark')); // TODO maybe some prefix here like 'Teacher remark:'
117 $criteriontemplate .= html_writer
::end_tag('tr'); // .criterion
119 $criteriontemplate = str_replace('{NAME}', $elementname, $criteriontemplate);
120 $criteriontemplate = str_replace('{CRITERION-id}', $criterion['id'], $criteriontemplate);
121 return $criteriontemplate;
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.
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.
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.
136 * Also JavaScript relies on the class names of elements and when developer changes them
137 * script might stop working.
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
145 public function level_template($mode, $options, $elementname = '{NAME}', $criterionid = '{CRITERION-id}', $level = null) {
146 // TODO definition format
147 if (!isset($level['id'])) {
148 $level = array('id' => '{LEVEL-id}', 'definition' => '{LEVEL-definition}', 'score' => '{LEVEL-score}', 'class' => '{LEVEL-class}', 'checked' => false);
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)) {
158 // Template for one level within one criterion
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']).'%';
163 $leveltemplate = html_writer
::start_tag('td', $tdattributes);
164 $leveltemplate .= html_writer
::start_tag('div', array('class' => 'level-wrapper'));
165 if ($mode == gradingform_rubric_controller
::DISPLAY_EDIT_FULL
) {
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' => '3', 'value' => $level['score']));
169 if ($mode == gradingform_rubric_controller
::DISPLAY_EDIT_FROZEN
) {
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']));
173 $definition = $level['definition'];
174 $score = $level['score'];
176 if ($mode == gradingform_rubric_controller
::DISPLAY_EVAL
) {
177 $input = html_writer
::empty_tag('input', array('type' => 'radio', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id']) +
178 ($level['checked'] ?
array('checked' => 'checked') : array()));
179 $leveltemplate .= html_writer
::tag('div', $input, array('class' => 'radio'));
181 if ($mode == gradingform_rubric_controller
::DISPLAY_EVAL_FROZEN
&& $level['checked']) {
182 $leveltemplate .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id']));
184 $score = html_writer
::tag('span', $score, array('id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-score', 'class' => 'scorevalue'));
185 $definitionclass = 'definition';
186 if (isset($level['error_definition'])) {
187 $definitionclass .= ' error';
189 $leveltemplate .= html_writer
::tag('div', $definition, array('class' => $definitionclass, 'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-definition'));
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;
194 if (!$options['showscorestudent'] && $mode == gradingform_rubric_controller
::DISPLAY_VIEW
) {
195 $displayscore = false;
198 $scoreclass = 'score';
199 if (isset($level['error_score'])) {
200 $scoreclass .= ' error';
202 $leveltemplate .= html_writer
::tag('div', get_string('scorepostfix', 'gradingform_rubric', $score), array('class' => $scoreclass));
204 if ($mode == gradingform_rubric_controller
::DISPLAY_EDIT_FULL
) {
205 $value = get_string('leveldelete', 'gradingform_rubric');
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));
207 $leveltemplate .= html_writer
::tag('div', $button, array('class' => 'delete'));
209 $leveltemplate .= html_writer
::end_tag('div'); // .level-wrapper
210 $leveltemplate .= html_writer
::end_tag('td'); // .level
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;
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.
223 * This function is called from display_rubric() to display the whole rubric.
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.
228 * Also JavaScript relies on the class names of elements and when developer changes them
229 * script might stop working.
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 $criteriastr evaluated templates for this rubric's criteria
236 protected function rubric_template($mode, $options, $elementname, $criteriastr) {
237 $classsuffix = ''; // CSS suffix for class of the main div. Depends on the 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;
251 case gradingform_rubric_controller
::DISPLAY_VIEW
:
252 $classsuffix = ' view'; break;
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'));
257 if ($mode == gradingform_rubric_controller
::DISPLAY_EDIT_FULL
) {
258 $value = get_string('addcriterion', 'gradingform_rubric');
259 $input = html_writer
::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][addcriterion]', 'id' => '{NAME}-criteria-addcriterion', 'value' => $value, 'title' => $value));
260 $rubrictemplate .= html_writer
::tag('div', $input, array('class' => 'addcriterion'));
262 $rubrictemplate .= $this->rubric_edit_options($mode, $options);
263 $rubrictemplate .= html_writer
::end_tag('div');
265 return str_replace('{NAME}', $elementname, $rubrictemplate);
269 * Generates html template to view/edit the rubric options. Expression {NAME} is used in
270 * template for the form element name
273 * @param array $options
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
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);
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'));
296 $valuestr = html_writer
::select($selectoptions, $attrs['name'], $value, false, array('id' => $attrs['id']));
297 $html .= html_writer
::tag('span', $valuestr, array('class' => 'value'));
298 // TODO add here button 'Sort levels'
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));
307 if ($mode == gradingform_rubric_controller
::DISPLAY_EDIT_FROZEN
&& $value) {
308 $html .= html_writer
::empty_tag('input', $attrs +
array('type' => 'hidden', 'value' => $value));
310 // Display option as checkbox
311 $attrs['type'] = 'checkbox';
314 $attrs['checked'] = 'checked';
316 if ($mode == gradingform_rubric_controller
::DISPLAY_EDIT_FROZEN ||
$mode == gradingform_rubric_controller
::DISPLAY_PREVIEW
) {
317 $attrs['disabled'] = 'disabled';
318 unset($attrs['name']);
320 $html .= html_writer
::empty_tag('input', $attrs);
321 $html .= html_writer
::tag('label', get_string($option, 'gradingform_rubric'), array('for' => $attrs['id']));
324 $html .= html_writer
::end_tag('div'); // .option
326 $html .= html_writer
::end_tag('div'); // .options
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.
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
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
345 public function display_rubric($criteria, $options, $mode, $elementname = null, $values = null) {
348 foreach ($criteria as $id => $criterion) {
349 $criterion['class'] = $this->get_css_class_suffix($cnt++
, sizeof($criteria) -1);
350 $criterion['id'] = $id;
353 if (isset($values['criteria'][$id])) {
354 $criterionvalue = $values['criteria'][$id];
356 $criterionvalue = null;
358 foreach ($criterion['levels'] as $levelid => $level) {
359 $level['id'] = $levelid;
360 $level['class'] = $this->get_css_class_suffix($levelcnt++
, sizeof($criterion['levels']) -1);
361 $level['checked'] = (isset($criterionvalue['levelid']) && ((int)$criterionvalue['levelid'] === $levelid));
362 if ($level['checked'] && ($mode == gradingform_rubric_controller
::DISPLAY_EVAL_FROZEN ||
$mode == gradingform_rubric_controller
::DISPLAY_REVIEW ||
$mode == gradingform_rubric_controller
::DISPLAY_VIEW
)) {
363 $level['class'] .= ' checked';
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
366 if (isset($criterionvalue['savedlevelid']) && ((int)$criterionvalue['savedlevelid'] === $levelid)) {
367 $level['class'] .= ' currentchecked';
369 $level['tdwidth'] = 100/count($criterion['levels']);
370 $levelsstr .= $this->level_template($mode, $options, $elementname, $id, $level);
372 $criteriastr .= $this->criterion_template($mode, $options, $elementname, $criterion, $levelsstr, $criterionvalue);
374 return $this->rubric_template($mode, $options, $elementname, $criteriastr);
378 * Help function to return CSS class names for element (first/last/even/odd) with leading space
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
384 protected function get_css_class_suffix($idx, $maxidx) {
389 if ($idx == $maxidx) {
401 * Displays for the student the list of instances or default content if no instances found
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
405 * @param boolean $cangrade whether current user has capability to grade in this context
408 public function display_instances($instances, $defaultcontent, $cangrade) {
410 if (sizeof($instances)) {
411 $return .= html_writer
::start_tag('div', array('class' => 'advancedgrade'));
413 foreach ($instances as $instance) {
414 $return .= $this->display_instance($instance, $idx++
, $cangrade);
416 $return .= html_writer
::end_tag('div');
418 return $return. $defaultcontent;
422 * Displays one grading instance
424 * @param gradingform_rubric_instance $instance
425 * @param int idx unique number of instance on page
426 * @param boolean $cangrade whether current user has capability to grade in this context
428 public function display_instance(gradingform_rubric_instance
$instance, $idx, $cangrade) {
429 $criteria = $instance->get_controller()->get_definition()->rubric_criteria
;
430 $options = $instance->get_controller()->get_options();
431 $values = $instance->get_rubric_filling();
433 $mode = gradingform_rubric_controller
::DISPLAY_REVIEW
;
435 $mode = gradingform_rubric_controller
::DISPLAY_VIEW
;
437 return $this->display_rubric($criteria, $options, $mode, 'rubric'.$idx, $values);
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')
448 $html .= html_writer
::select($selectoptions, $elementname.'[regrade]', $value, false);
450 $html .= get_string('regrademessage5', 'gradingform_rubric');
451 $html .= html_writer
::empty_tag('input', array('name' => $elementname.'[regrade]', 'value' => 1, 'type' => 'hidden'));
453 $html .= html_writer
::end_tag('div');
458 * Generates and returns HTML code to display information box about how rubric score is converted to the grade
460 * @param array $scores
463 public function display_rubric_mapping_explained($scores) {
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');