MDL-20508 - update choice module to fix accessibility issue and eliminate the use...
[moodle.git] / mod / choice / renderer.php
CommitLineData
315d4971
RW
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 * Moodle renderer used to display special elements of the lesson module
20 *
21 * @package Choice
22 * @copyright 2010 Rossiani Wijaya
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
25define ('DISPLAY_HORIZONTAL_LAYOUT', 0);
26define ('DISPLAY_VERTICAL_LAYOUT', 1);
27
28class mod_choice_renderer extends plugin_renderer_base {
29
30 /**
31 * Returns HTML to display choices of option
32 * @param object $options
33 * @param int $coursemoduleid
34 * @param bool $vertical
35 * @return string
36 */
37 public function display_options($options, $coursemoduleid, $vertical = false) {
38 global $CFG;
39
40 $layoutclass = 'horizontal';
41 if ($vertical) {
42 $layoutclass = 'vertical';
43 }
44
45 $attributes = array('method'=>'POST', 'target'=>$CFG->wwwroot.'/mod/choice/view.php', 'class'=> $layoutclass);
46
47 $html = html_writer::start_tag('form', $attributes);
48 $html .= html_writer::start_tag('ul', array('class'=>'choices' ));
49
50 $availableoption = count($options['options']);
51 foreach ($options['options'] as $option) {
52 $html .= html_writer::start_tag('li', array('class'=>'option'));
53 $option->attributes->name = 'answer';
54 $option->attributes->type = 'radio';
55
56 $labeltext = $option->text;
57 if (!empty($option->attributes->disabled)) {
58 $labeltext .= ' ' . get_string('full', 'choice');
59 $availableoption--;
60 }
61
62 $html .= html_writer::empty_tag('input', (array)$option->attributes);
63 $html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->name));
64 $html .= html_writer::end_tag('li');
65 }
66 $html .= html_writer::tag('li','', array('class'=>'clearfloat'));
67 $html .= html_writer::end_tag('ul');
68 $html .= html_writer::tag('div', '', array('class'=>'clearfloat'));
69 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
70 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
71
72 if (!empty($options['hascapability']) && ($options['hascapability'])) {
73 if ($availableoption < 1) {
74 $html .= html_writer::tag('label', get_string('choicefull', 'choice'));
75 } else {
76 $html .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('savemychoice','choice'), 'class'=>'button'));
77 }
78
79 if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
80 $url = new moodle_url('view.php', array('id'=>$coursemoduleid, 'action'=>'delchoice', 'sesskey'=>sesskey()));
81 $html .= html_writer::link($url, get_string('removemychoice','choice'));
82 }
83 } else {
84 $html .= html_writer::tag('label', get_string('havetologin', 'choice'));
85 }
86
87 $html .= html_writer::end_tag('ul');
88 $html .= html_writer::end_tag('form');
89
90 return $html;
91 }
92
93 /**
94 * Returns HTML to display choices result
95 * @param object $choices
96 * @param bool $forcepublish
97 * @return string
98 */
99 public function display_result($choices, $forcepublish = false) {
100 global $CFG, $OUTPUT;
101
102 if (empty($forcepublish)) { //alow the publish setting to be overridden
103 $forcepublish = $choices->publish;
104 }
105
106 $displaylayout = $choices->display;
107
108 if ($forcepublish) { //CHOICE_PUBLISH_NAMES
109 return $this->display_publish_name_vertical($choices);
110 } else { //CHOICE_PUBLISH_ANONYMOUS';
111 if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) {
112 return $this->display_publish_anonymous_horizontal($choices);
113 }
114 return $this->display_publish_anonymous_vertical($choices);
115 }
116 }
117
118 /**
119 * Returns HTML to display choices result
120 * @param object $choices
121 * @param bool $forcepublish
122 * @return string
123 */
124 public function display_publish_name_vertical($choices) {
125 global $CFG, $OUTPUT, $PAGE;
126
127 $html ='';
128 $html .= html_writer::tag('h2',format_string(get_string("responses", "choice")), array('class'=>'main'));
129
130 $attributes = array('method'=>'POST');
131 $attributes['action'] = $CFG->wwwroot.'/mod/choice/view.php';
132 $attributes['id'] = 'attemptsform';
133
134 if ($choices->viewresponsecapability) {
135 $html .= html_writer::start_tag('form', $attributes);
136 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid));
137 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
138 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
139 }
140
141 $table = new html_table();
142 $table->cellpadding = 0;
143 $table->cellspacing = 0;
144 $table->attributes['class'] = 'results names ';
145 $table->tablealign = 'center';
146 $table->data = array();
147
148 $count = 0;
149 ksort($choices->options);
150
151 $columns = array();
152 foreach ($choices->options as $optionid => $options) {
153 $coldata = '';
154 if ($choices->showunanswered && $optionid == 0) {
155 $coldata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
156 } else if ($optionid > 0) {
157 $coldata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
158 }
159 $numberofuser = 0;
160 if (!empty($options->user) && count($options->user) > 0) {
161 $numberofuser = count($options->user);
162 }
163
164 $coldata .= html_writer::tag('div', ' ('.$numberofuser. ')', array('class'=>'numberofuser', 'title' => get_string('numberofuser', 'choice')));
165 $columns[] = $coldata;
166 }
167
168 $table->head = $columns;
169
170 $coldata = '';
171 $columns = array();
172 foreach ($choices->options as $optionid => $options) {
173 $coldata = '';
174 if ($choices->showunanswered || $optionid > 0) {
175 if (!empty($options->user)) {
176 foreach ($options->user as $user) {
177 $data = '';
178 if (empty($user->imagealt)){
179 $user->imagealt = '';
180 }
181
182 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability && $optionid > 0) {
183 $attemptaction = html_writer::checkbox('attemptid[]', $user->id,'');
184 $data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
185 }
186 $userimage = $OUTPUT->user_picture($user, array('courseid'=>$choices->courseid));
187 $data .= html_writer::tag('div', $userimage, array('class'=>'image'));
188
189 $name = html_writer::tag('a', fullname($user, $choices->fullnamecapability), array('href'=>$CFG->wwwroot . '/user/view.php?id='.$user->id.'&course='.$choices->courseid, 'class'=>'username'));
190 $data .= html_writer::tag('div', $name, array('class'=>'fullname'));
191 $data .= html_writer::tag('div','', array('class'=>'clearfloat'));
192 $coldata .= html_writer::tag('div', $data, array('class'=>'user'));
193 }
194 }
195 }
196
197 $columns[] = $coldata;
198 $count++;
199 }
200
201 $table->data[] = $columns;
202 foreach ($columns as $d) {
203 $table->colclasses[] = 'data';
204 }
205
206 $html .= html_writer::table($table);
207
208 $actiondata = '';
209 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
210 $selecturl = new moodle_url('#');
211
212 $selectallactions = new component_action('click',"select_all_in", array('div',null,'tablecontainer'));
213 $selectall = new action_link($selecturl, get_string('selectall', 'quiz'), $selectallactions);
214 $actiondata .= $OUTPUT->render($selectall) . ' / ';
215
216 $deselectallactions = new component_action('click',"deselect_all_in", array('div',null,'tablecontainer'));
217 $deselectall = new action_link($selecturl, get_string('selectnone', 'quiz'), $deselectallactions);
218 $actiondata .= $OUTPUT->render($deselectall);
219
220 $actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'quiz') . ' ', array('for'=>'menuaction'));
221
222 $actionurl = new moodle_url($CFG->wwwroot.'/mod/choice/view.php', array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
223 $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('moveselectedusersto', 'choice')), 'attemptsform');
224
225 $actiondata .= $OUTPUT->render($select);
226 }
227 $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
228
229 if ($choices->viewresponsecapability) {
230 $html .= html_writer::end_tag('form');
231 }
232
233 return $html;
234 }
235
236
237 /**
238 * Returns HTML to display choices result
239 * @param object $choices
240 * @return string
241 */
242 public function display_publish_anonymous_vertical($choices) {
243 global $CHOICE_COLUMN_HEIGHT, $OUTPUT;
244
245 $html = '';
246 $table = new html_table();
247 $table->cellpadding = 5;
248 $table->cellspacing = 0;
249 $table->attributes['class'] = 'results anonymous ';
250 $table->data = array();
251 $count = 0;
252 ksort($choices->options);
253 $columns = array();
254 $rows = array();
255
256 foreach ($choices->options as $optionid => $options) {
257 $numberofuser = 0;
258 if (!empty($options->user)) {
259 $numberofuser = count($options->user);
260 }
261 $height = 0;
262 $percentageamount = 0;
263 if($choices->numberofuser > 0) {
264 $height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser));
265 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
266 }
267
268 $displaydiagram = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$OUTPUT->pix_url('column', 'choice')));
269
270 $cell = new html_table_cell();
271 $cell->text = $displaydiagram;
272 $cell->attributes = array('class'=>'graph vertical data');
273 $columns[] = $cell;
274 }
275 $rowgraph = new html_table_row();
276 $rowgraph->cells = $columns;
277 $rows[] = $rowgraph;
278
279 $columns = array();
280 $printskiplink = true;
281 foreach ($choices->options as $optionid => $options) {
282 $columndata = '';
283 $numberofuser = 0;
284 if (!empty($options->user)) {
285 $numberofuser = count($options->user);
286 }
287
288 if ($printskiplink) {
289 $columndata .= html_writer::tag('div', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'));
290 $printskiplink = false;
291 }
292
293 if ($choices->showunanswered && $optionid == 0) {
294 $columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
295 } else if ($optionid > 0) {
296 $columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
297 }
298 $columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice')));
299
300 if($choices->numberofuser > 0) {
301 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
302 }
303 $columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
304
305 $cell = new html_table_cell();
306 $cell->text = $columndata;
307 $cell->attributes = array('class'=>'data header');
308 $columns[] = $cell;
309 }
310 $rowdata = new html_table_row();
311 $rowdata->cells = $columns;
312 $rows[] = $rowdata;
313
314 $table->data = $rows;
315
316 $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
317 $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
318 $html .= html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph', 'class'=>'skip-block'));
319 $html .= html_writer::table($table);
320
321 return $html;
322 }
323
324 /**
325 * Returns HTML to display choices result
326 * @param object $choices
327 * @return string
328 */
329 public function display_publish_anonymous_horizontal($choices) {
330 global $CHOICE_COLUMN_WIDTH, $OUTPUT;
331
332 $table = new html_table();
333 $table->cellpadding = 5;
334 $table->cellspacing = 0;
335 $table->attributes['class'] = 'results anonymous ';
336 $table->data = array();
337
338 $count = 0;
339 ksort($choices->options);
340
341 $rows = array();
342 foreach ($choices->options as $optionid => $options) {
343 $numberofuser = 0;
344 $graphcell = new html_table_cell();
345 if (!empty($options->user)) {
346 $numberofuser = count($options->user);
347 }
348
349 $width = 0;
350 $percentageamount = 0;
351 $columndata = '';
352 if($choices->numberofuser > 0) {
353 $width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser));
354 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
355 }
356 $displaydiagram = html_writer::tag('img','', array('style'=>'height:50px; width:'.$width.'px', 'alt'=>'', 'src'=>$OUTPUT->pix_url('row', 'choice')));
357
358 $skiplink = html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph'. $optionid, 'class'=>'skip-block'));
359 $skiphandler = html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'.$optionid));
360
361 $graphcell->text = $skiplink . $displaydiagram . $skiphandler;
362 $graphcell->attributes = array('class'=>'graph horizontal');
363
364 $datacell = new html_table_cell();
365 if ($choices->showunanswered && $optionid == 0) {
366 $columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
367 } else if ($optionid > 0) {
368 $columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
369 }
370 $columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('title'=> get_string('numberofuser', 'choice'), 'class'=>'numberofuser'));
371
372 if($choices->numberofuser > 0) {
373 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
374 }
375 $columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
376
377 $datacell->text = $columndata;
378 $datacell->attributes = array('class'=>'header');
379
380 $row = new html_table_row();
381 $row->cells = array($datacell, $graphcell);
382 $rows[] = $row;
383 }
384
385 $table->data = $rows;
386
387 $html = '';
388 $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
389 $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
390 $html .= html_writer::table($table);
391
392 return $html;
393 }
394}
395