Merge remote branch 'origin/master' into rubric
[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
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Grading method plugin renderer
30 */
ab156741 31class gradingform_rubric_renderer {
c586d2bf 32
ab156741
MG
33 /**
34 *
35 * @param int $mode @see gradingform_rubric_controller
36 * @return string
37 */
5060997b
MG
38 public function criterion_template($mode, $elementname = '{NAME}', $criterion = null, $levels_str = '{LEVELS}', $value = null) {
39 // TODO description format, remark format
ab156741
MG
40 if ($criterion === null || !is_array($criterion) || !array_key_exists('id', $criterion)) {
41 $criterion = array('id' => '{CRITERION-id}', 'description' => '{CRITERION-description}', 'sortorder' => '{CRITERION-sortorder}', 'class' => '{CRITERION-class}');
42 } else {
43 foreach (array('sortorder', 'description', 'class') as $key) {
44 // set missing array elements to empty strings to avoid warnings
45 if (!array_key_exists($key, $criterion)) {
46 $criterion[$key] = '';
47 }
48 }
49 }
50 $criterion_template = html_writer::start_tag('div', array('class' => 'clearfix criterion'. $criterion['class'], 'id' => '{NAME}-{CRITERION-id}'));
51 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
52 $criterion_template .= html_writer::start_tag('div', array('class' => 'controls'));
53 foreach (array('moveup', 'delete', 'movedown') as $key) {
54 $value = get_string('criterion'.$key, 'gradingform_rubric');
55 $button = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[{CRITERION-id}]['.$key.']',
56 'id' => '{NAME}-{CRITERION-id}-'.$key, 'value' => $value, 'title' => $value));
57 $criterion_template .= html_writer::tag('div', $button, array('class' => $key));
58 }
59 $criterion_template .= html_writer::end_tag('div'); // .controls
60 $criterion_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[{CRITERION-id}][sortorder]', 'value' => $criterion['sortorder']));
61 $description = html_writer::tag('textarea', htmlspecialchars($criterion['description']), array('name' => '{NAME}[{CRITERION-id}][description]', 'cols' => '10', 'rows' => '5'));
62 } else {
63 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
64 $criterion_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[{CRITERION-id}][sortorder]', 'value' => $criterion['sortorder']));
65 $criterion_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[{CRITERION-id}][description]', 'value' => $criterion['description']));
66 }
67 $description = $criterion['description'];
68 }
69 $criterion_template .= html_writer::tag('div', $description, array('class' => 'description', 'id' => '{NAME}-{CRITERION-id}-description'));
70 $criterion_template .= html_writer::tag('div', $levels_str, array('class' => 'clearfix levels', 'id' => '{NAME}-{CRITERION-id}-levels'));
71 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
72 $value = get_string('criterionaddlevel', 'gradingform_rubric');
73 $button = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[{CRITERION-id}][levels][addlevel]',
74 'id' => '{NAME}-{CRITERION-id}-levels-addlevel', 'value' => $value, 'title' => $value)); //TODO '{NAME}-{CRITERION-id}-levels-addlevel
75 $criterion_template .= html_writer::tag('div', $button, array('class' => 'addlevel'));
76 }
5060997b
MG
77 if (isset($value['remark'])) {
78 $currentremark = $value['remark'];
79 } else {
80 $currentremark = '';
81 }
82 if ($mode == gradingform_rubric_controller::DISPLAY_EVAL) {
83 $input = html_writer::tag('textarea', htmlspecialchars($currentremark), array('name' => '{NAME}[criteria][{CRITERION-id}][remark]', 'cols' => '10', 'rows' => '5'));
84 $criterion_template .= html_writer::tag('div', $input, array('class' => 'remark'));
85 }
86 if ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN) {
87 $criterion_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][remark]', 'value' => $currentremark));
88 }
89 if ($mode == gradingform_rubric_controller::DISPLAY_REVIEW) {
90 $criterion_template .= html_writer::tag('div', $currentremark, array('class' => 'remark')); // TODO maybe some prefix here like 'Teacher remark:'
91 }
ab156741
MG
92 $criterion_template .= html_writer::end_tag('div'); // .criterion
93
94 $criterion_template = str_replace('{NAME}', $elementname, $criterion_template);
95 $criterion_template = str_replace('{CRITERION-id}', $criterion['id'], $criterion_template);
96 return $criterion_template;
97 }
98
99 public function level_template($mode, $elementname = '{NAME}', $criterionid = '{CRITERION-id}', $level = null) {
100 // TODO definition format
5060997b 101 if (!isset($level['id'])) {
ab156741
MG
102 $level = array('id' => '{LEVEL-id}', 'definition' => '{LEVEL-definition}', 'score' => '{LEVEL-score}', 'class' => '{LEVEL-class}', 'checked' => false);
103 } else {
104 foreach (array('score', 'definition', 'class', 'checked') as $key) {
105 // set missing array elements to empty strings to avoid warnings
106 if (!array_key_exists($key, $level)) {
107 $level[$key] = '';
108 }
109 }
110 }
111
112 // Template for one level within one criterion
113 $level_template = html_writer::start_tag('div', array('id' => '{NAME}-{CRITERION-id}-levels-{LEVEL-id}', 'class' => 'clearfix level'. $level['class']));
114 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
115 $definition = html_writer::tag('textarea', htmlspecialchars($level['definition']), array('name' => '{NAME}[{CRITERION-id}][levels][{LEVEL-id}][definition]', 'cols' => '10', 'rows' => '4'));
116 $score = html_writer::empty_tag('input', array('type' => 'text', 'name' => '{NAME}[{CRITERION-id}][levels][{LEVEL-id}][score]', 'size' => '4', 'value' => $level['score']));
117 } else {
118 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
119 $level_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[{CRITERION-id}][levels][{LEVEL-id}][definition]', 'value' => $level['definition']));
120 $level_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[{CRITERION-id}][levels][{LEVEL-id}][score]', 'value' => $level['score']));
121 }
122 $definition = $level['definition'];
123 $score = $level['score'];
124 }
125 if ($mode == gradingform_rubric_controller::DISPLAY_EVAL) {
5060997b 126 $input = html_writer::empty_tag('input', array('type' => 'radio', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id']) +
ab156741
MG
127 ($level['checked'] ? array('checked' => 'checked') : array()));
128 $level_template .= html_writer::tag('div', $input, array('class' => 'radio'));
129 }
130 if ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN && $level['checked']) {
5060997b 131 $level_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id']));
ab156741
MG
132 }
133 $score = html_writer::tag('span', $score, array('id' => '{NAME}-{CRITERION-id}-levels-{LEVEL-id}-score'));
134 $level_template .= html_writer::tag('div', $definition, array('class' => 'definition', 'id' => '{NAME}-{CRITERION-id}-levels-{LEVEL-id}-definition'));
135 $level_template .= html_writer::tag('div', $score. get_string('scorepostfix', 'gradingform_rubric'), array('class' => 'score'));
136 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
137 $value = get_string('leveldelete', 'gradingform_rubric');
138 $button = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[{CRITERION-id}][levels][{LEVEL-id}][delete]', 'id' => '{NAME}-{CRITERION-id}-levels-{LEVEL-id}-delete', 'value' => $value, 'title' => $value));
139 $level_template .= html_writer::tag('div', $button, array('class' => 'delete'));
140 }
141 $level_template .= html_writer::end_tag('div'); // .level
142
143 $level_template = str_replace('{NAME}', $elementname, $level_template);
144 $level_template = str_replace('{CRITERION-id}', $criterionid, $level_template);
145 $level_template = str_replace('{LEVEL-id}', $level['id'], $level_template);
146 return $level_template;
147 }
148
149 protected function rubric_template($mode, $elementname = '{NAME}', $criteria_str = '{CRITERIA}') {
150 $classsuffix = ''; // CSS suffix for class of the main div. Depends on the mode
151 switch ($mode) {
152 case gradingform_rubric_controller::DISPLAY_EDIT_FULL:
153 $classsuffix = ' editor editable'; break;
154 case gradingform_rubric_controller::DISPLAY_EDIT_FROZEN:
155 $classsuffix = ' editor frozen'; break;
156 case gradingform_rubric_controller::DISPLAY_PREVIEW:
157 $classsuffix = ' editor preview'; break;
158 case gradingform_rubric_controller::DISPLAY_EVAL:
159 $classsuffix = ' evaluate editable'; break;
160 case gradingform_rubric_controller::DISPLAY_EVAL_FROZEN:
161 $classsuffix = ' evaluate frozen'; break;
162 case gradingform_rubric_controller::DISPLAY_REVIEW:
163 $classsuffix = ' review'; break;
164 }
165
166 $rubric_template = html_writer::start_tag('div', array('id' => 'rubric-{NAME}', 'class' => 'clearfix form_rubric'.$classsuffix));
167 $rubric_template .= html_writer::tag('div', $criteria_str, array('class' => 'criteria', 'id' => '{NAME}-criteria'));
168 if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
169 $value = get_string('addcriterion', 'gradingform_rubric');
170 $input = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[addcriterion]', 'id' => '{NAME}-addcriterion', 'value' => $value, 'title' => $value));
171 $rubric_template .= html_writer::tag('div', $input, array('class' => 'addcriterion'));
172 }
173 $rubric_template .= html_writer::end_tag('div');
174
175 return str_replace('{NAME}', $elementname, $rubric_template);
176 }
177
178 /**
179 * Returns html code for displaying the rubric in the specified mode
180 *
181 * @param array $criteria
182 * @param int $mode
183 * @param string $elementname
184 * @param array $values
185 * @return string
186 */
187 public function display_rubric($criteria, $mode, $elementname = null, $values = null) {
188 $criteria_str = '';
189 $cnt = 0;
190 foreach ($criteria as $id => $criterion) {
191 $criterion['class'] = $this->get_css_class_suffix($cnt++, sizeof($criteria) -1);
192 $levels_str = '';
193 $levelcnt = 0;
5060997b
MG
194 if (isset($values['criteria'][$id])) {
195 $criterionvalue = $values['criteria'][$id];
196 } else {
197 $criterionvalue = null;
198 }
ab156741
MG
199 foreach ($criterion['levels'] as $levelid => $level) {
200 $level['score'] = (float)$level['score']; // otherwise the display will look like 1.00000
201 $level['class'] = $this->get_css_class_suffix($levelcnt++, sizeof($criterion['levels']) -1);
5060997b 202 $level['checked'] = (isset($criterionvalue['levelid']) && ((int)$criterionvalue['levelid'] === $levelid));
ab156741
MG
203 if ($level['checked'] && ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_REVIEW)) {
204 $level['class'] .= ' checked';
205 //in mode DISPLAY_EVAL the class 'checked' will be added by JS if it is enabled. If it is not enabled, the 'checked' class will only confuse
206 }
207 $levels_str .= $this->level_template($mode, $elementname, $id, $level);
208 }
5060997b 209 $criteria_str .= $this->criterion_template($mode, $elementname, $criterion, $levels_str, $criterionvalue);
ab156741
MG
210 }
211 return $this->rubric_template($mode, $elementname, $criteria_str);
212 }
213
214 /**
215 * Help function to return CSS class names for element (first/last/even/odd)
216 *
217 * @param <type> $cnt
218 * @param <type> $maxcnt
219 * @return string
220 */
221 private function get_css_class_suffix($cnt, $maxcnt) {
222 $class = '';
223 if ($cnt == 0) {
224 $class .= ' first';
225 }
226 if ($cnt == $maxcnt) {
227 $class .= ' last';
228 }
229 if ($cnt%2) {
230 $class .= ' odd';
231 } else {
232 $class .= ' even';
233 }
234 return $class;
235 }
36937f02
MG
236
237 /**
238 * Displays for the student the list of instances or default content if no instances found
239 *
240 * @param array $instances array of objects of type gradingform_rubric_instance
241 * @param string $defaultcontent default string that would be displayed without advanced grading
242 * @return string
243 */
244 public function display_instances($instances, $defaultcontent) {
245 if (sizeof($instances)) {
246 $rv = html_writer::start_tag('div', array('class' => 'advancedgrade'));
247 $idx = 0;
248 foreach ($instances as $instance) {
249 $rv .= $this->display_instance($instance, $idx++);
250 }
251 $rv .= html_writer::end_tag('div');
252 }
253 return $rv. $defaultcontent;
254 }
255
256 /**
257 * Displays one grading instance
258 *
259 * @param gradingform_rubric_instance $instance
260 * @param int idx unique number of instance on page
261 */
262 public function display_instance(gradingform_rubric_instance $instance, $idx) {
263 $criteria = $instance->get_controller()->get_definition()->rubric_criteria;
264 $values = $instance->get_rubric_filling();
265 return $this->display_rubric($criteria, gradingform_rubric_controller::DISPLAY_REVIEW, 'rubric'.$idx, $values);
266 }
c586d2bf 267}