Merge branch 'MDL-33575_m23' of git://github.com/rwijaya/moodle into MOODLE_23_STABLE
[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) {
315d4971
RW
38 $layoutclass = 'horizontal';
39 if ($vertical) {
40 $layoutclass = 'vertical';
41 }
31d085e2 42 $target = new moodle_url('/mod/choice/view.php');
6fa51b70 43 $attributes = array('method'=>'POST', 'action'=>$target, 'class'=> $layoutclass);
034ef761 44
315d4971
RW
45 $html = html_writer::start_tag('form', $attributes);
46 $html .= html_writer::start_tag('ul', array('class'=>'choices' ));
034ef761 47
315d4971 48 $availableoption = count($options['options']);
546a3f81 49 $choicecount = 0;
034ef761 50 foreach ($options['options'] as $option) {
546a3f81 51 $choicecount++;
315d4971
RW
52 $html .= html_writer::start_tag('li', array('class'=>'option'));
53 $option->attributes->name = 'answer';
54 $option->attributes->type = 'radio';
546a3f81 55 $option->attributes->id = 'choice_'.$choicecount;
034ef761 56
315d4971 57 $labeltext = $option->text;
034ef761 58 if (!empty($option->attributes->disabled)) {
315d4971
RW
59 $labeltext .= ' ' . get_string('full', 'choice');
60 $availableoption--;
034ef761 61 }
315d4971
RW
62
63 $html .= html_writer::empty_tag('input', (array)$option->attributes);
546a3f81 64 $html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->id));
315d4971
RW
65 $html .= html_writer::end_tag('li');
66 }
67 $html .= html_writer::tag('li','', array('class'=>'clearfloat'));
68 $html .= html_writer::end_tag('ul');
69 $html .= html_writer::tag('div', '', array('class'=>'clearfloat'));
70 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
71 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
72
034ef761 73 if (!empty($options['hascapability']) && ($options['hascapability'])) {
315d4971
RW
74 if ($availableoption < 1) {
75 $html .= html_writer::tag('label', get_string('choicefull', 'choice'));
76 } else {
77 $html .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('savemychoice','choice'), 'class'=>'button'));
78 }
79
80 if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
81 $url = new moodle_url('view.php', array('id'=>$coursemoduleid, 'action'=>'delchoice', 'sesskey'=>sesskey()));
82 $html .= html_writer::link($url, get_string('removemychoice','choice'));
83 }
84 } else {
85 $html .= html_writer::tag('label', get_string('havetologin', 'choice'));
86 }
034ef761 87
315d4971 88 $html .= html_writer::end_tag('ul');
034ef761
PS
89 $html .= html_writer::end_tag('form');
90
315d4971
RW
91 return $html;
92 }
93
94 /**
95 * Returns HTML to display choices result
96 * @param object $choices
97 * @param bool $forcepublish
98 * @return string
99 */
100 public function display_result($choices, $forcepublish = false) {
034ef761 101 if (empty($forcepublish)) { //allow the publish setting to be overridden
315d4971
RW
102 $forcepublish = $choices->publish;
103 }
104
105 $displaylayout = $choices->display;
106
034ef761 107 if ($forcepublish) { //CHOICE_PUBLISH_NAMES
315d4971
RW
108 return $this->display_publish_name_vertical($choices);
109 } else { //CHOICE_PUBLISH_ANONYMOUS';
110 if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) {
111 return $this->display_publish_anonymous_horizontal($choices);
112 }
113 return $this->display_publish_anonymous_vertical($choices);
034ef761 114 }
315d4971
RW
115 }
116
117 /**
118 * Returns HTML to display choices result
119 * @param object $choices
120 * @param bool $forcepublish
121 * @return string
122 */
123 public function display_publish_name_vertical($choices) {
6e4453f5 124 global $PAGE;
315d4971
RW
125 $html ='';
126 $html .= html_writer::tag('h2',format_string(get_string("responses", "choice")), array('class'=>'main'));
034ef761 127
315d4971 128 $attributes = array('method'=>'POST');
6e4453f5 129 $attributes['action'] = new moodle_url($PAGE->url);
315d4971 130 $attributes['id'] = 'attemptsform';
034ef761 131
315d4971
RW
132 if ($choices->viewresponsecapability) {
133 $html .= html_writer::start_tag('form', $attributes);
134 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid));
135 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
034ef761 136 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
315d4971
RW
137 }
138
139 $table = new html_table();
140 $table->cellpadding = 0;
141 $table->cellspacing = 0;
142 $table->attributes['class'] = 'results names ';
143 $table->tablealign = 'center';
a2af7fd4 144 $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
315d4971
RW
145 $table->data = array();
146
147 $count = 0;
148 ksort($choices->options);
149
150 $columns = array();
34e0b4a7
RW
151 $celldefault = new html_table_cell();
152 $celldefault->attributes['class'] = 'data';
153
154 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
155 $accessiblecell = new html_table_cell();
156 $accessiblecell->scope = 'row';
157 $accessiblecell->text = get_string('choiceoptions', 'choice');
158 $columns['options'][] = $accessiblecell;
159
160 $usernumberheader = clone($celldefault);
161 $usernumberheader->header = true;
162 $usernumberheader->attributes['class'] = 'header data';
163 $usernumberheader->text = get_string('numberofuser', 'choice');
164 $columns['usernumber'][] = $usernumberheader;
165
166
315d4971 167 foreach ($choices->options as $optionid => $options) {
34e0b4a7
RW
168 $celloption = clone($celldefault);
169 $cellusernumber = clone($celldefault);
170 $cellusernumber->style = 'text-align: center;';
171
172 $celltext = '';
315d4971 173 if ($choices->showunanswered && $optionid == 0) {
34e0b4a7 174 $celltext = format_string(get_string('notanswered', 'choice'));
315d4971 175 } else if ($optionid > 0) {
34e0b4a7 176 $celltext = format_string($choices->options[$optionid]->text);
315d4971
RW
177 }
178 $numberofuser = 0;
179 if (!empty($options->user) && count($options->user) > 0) {
034ef761
PS
180 $numberofuser = count($options->user);
181 }
182
34e0b4a7
RW
183 $celloption->text = $celltext;
184 $cellusernumber->text = $numberofuser;
185
186 $columns['options'][] = $celloption;
187 $columns['usernumber'][] = $cellusernumber;
315d4971 188 }
034ef761 189
34e0b4a7
RW
190 $table->head = $columns['options'];
191 $table->data[] = new html_table_row($columns['usernumber']);
315d4971 192
315d4971 193 $columns = array();
34e0b4a7
RW
194
195 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
196 $accessiblecell = new html_table_cell();
197 $accessiblecell->text = get_string('userchoosethisoption', 'choice');
198 $accessiblecell->header = true;
199 $accessiblecell->scope = 'row';
200 $accessiblecell->attributes['class'] = 'header data';
201 $columns[] = $accessiblecell;
202
315d4971 203 foreach ($choices->options as $optionid => $options) {
34e0b4a7
RW
204 $cell = new html_table_cell();
205 $cell->attributes['class'] = 'data';
206
315d4971
RW
207 if ($choices->showunanswered || $optionid > 0) {
208 if (!empty($options->user)) {
a2af7fd4 209 $optionusers = '';
034ef761 210 foreach ($options->user as $user) {
315d4971
RW
211 $data = '';
212 if (empty($user->imagealt)){
213 $user->imagealt = '';
214 }
034ef761 215
b6e65b53 216 $userfullname = fullname($user, $choices->fullnamecapability);
034ef761 217 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability && $optionid > 0) {
b6e65b53
RW
218 $attemptaction = html_writer::label($userfullname, 'attempt-user'.$user->id, false, array('class' => 'accesshide'));
219 $attemptaction .= html_writer::checkbox('attemptid[]', $user->id,'', null, array('id' => 'attempt-user'.$user->id));
315d4971
RW
220 $data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
221 }
31d085e2 222 $userimage = $this->output->user_picture($user, array('courseid'=>$choices->courseid));
315d4971
RW
223 $data .= html_writer::tag('div', $userimage, array('class'=>'image'));
224
31d085e2 225 $userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choices->courseid));
b6e65b53 226 $name = html_writer::tag('a', $userfullname, array('href'=>$userlink, 'class'=>'username'));
315d4971
RW
227 $data .= html_writer::tag('div', $name, array('class'=>'fullname'));
228 $data .= html_writer::tag('div','', array('class'=>'clearfloat'));
a2af7fd4 229 $optionusers .= html_writer::tag('div', $data, array('class'=>'user'));
034ef761 230 }
a2af7fd4 231 $cell->text = $optionusers;
034ef761 232 }
315d4971 233 }
34e0b4a7 234 $columns[] = $cell;
315d4971
RW
235 $count++;
236 }
34e0b4a7
RW
237 $row = new html_table_row($columns);
238 $table->data[] = $row;
034ef761 239
aad62468 240 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
034ef761 241
315d4971 242 $actiondata = '';
034ef761 243 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
315d4971
RW
244 $selecturl = new moodle_url('#');
245
c9b8a789 246 $selectallactions = new component_action('click',"checkall");
b447192e 247 $selectall = new action_link($selecturl, get_string('selectall'), $selectallactions);
31d085e2 248 $actiondata .= $this->output->render($selectall) . ' / ';
315d4971 249
c9b8a789 250 $deselectallactions = new component_action('click',"checknone");
b447192e 251 $deselectall = new action_link($selecturl, get_string('deselectall'), $deselectallactions);
31d085e2 252 $actiondata .= $this->output->render($deselectall);
315d4971 253
b447192e 254 $actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'choice') . ' ', array('for'=>'menuaction'));
034ef761 255
6e4453f5 256 $actionurl = new moodle_url($PAGE->url, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
f51d388c 257 $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choice')), 'attemptsform');
034ef761 258
31d085e2 259 $actiondata .= $this->output->render($select);
315d4971
RW
260 }
261 $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
034ef761 262
315d4971
RW
263 if ($choices->viewresponsecapability) {
264 $html .= html_writer::end_tag('form');
265 }
034ef761 266
aad62468 267 return $html;
315d4971
RW
268 }
269
270
271 /**
272 * Returns HTML to display choices result
273 * @param object $choices
274 * @return string
275 */
276 public function display_publish_anonymous_vertical($choices) {
31d085e2 277 global $CHOICE_COLUMN_HEIGHT;
315d4971 278
034ef761 279 $html = '';
315d4971
RW
280 $table = new html_table();
281 $table->cellpadding = 5;
034ef761
PS
282 $table->cellspacing = 0;
283 $table->attributes['class'] = 'results anonymous ';
a2af7fd4 284 $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
034ef761 285 $table->data = array();
34e0b4a7 286
315d4971
RW
287 $count = 0;
288 ksort($choices->options);
289 $columns = array();
290 $rows = array();
291
34e0b4a7
RW
292 $headercelldefault = new html_table_cell();
293 $headercelldefault->scope = 'row';
294 $headercelldefault->header = true;
295 $headercelldefault->attributes = array('class'=>'header data');
296
297 // column header
298 $tableheader = clone($headercelldefault);
299 $tableheader->text = html_writer::tag('div', get_string('choiceoptions', 'choice'), array('class' => 'accesshide'));
300 $rows['header'][] = $tableheader;
301
302 // graph row header
303 $graphheader = clone($headercelldefault);
304 $graphheader->text = html_writer::tag('div', get_string('responsesresultgraphheader', 'choice'), array('class' => 'accesshide'));
305 $rows['graph'][] = $graphheader;
306
307 // user number row header
308 $usernumberheader = clone($headercelldefault);
309 $usernumberheader->text = get_string('numberofuser', 'choice');
310 $rows['usernumber'][] = $usernumberheader;
311
312 // user percentage row header
313 $userpercentageheader = clone($headercelldefault);
314 $userpercentageheader->text = get_string('numberofuserinpercentage', 'choice');
315 $rows['userpercentage'][] = $userpercentageheader;
316
317 $contentcelldefault = new html_table_cell();
318 $contentcelldefault->attributes = array('class'=>'data');
319
320 foreach ($choices->options as $optionid => $option) {
321 // calculate display length
322 $height = $percentageamount = $numberofuser = 0;
323 $usernumber = $userpercentage = '';
324
325 if (!empty($option->user)) {
326 $numberofuser = count($option->user);
315d4971 327 }
34e0b4a7 328
315d4971
RW
329 if($choices->numberofuser > 0) {
330 $height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser));
331 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
332 }
333
34e0b4a7 334 $displaygraph = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output->pix_url('column', 'choice')));
034ef761 335
34e0b4a7
RW
336 // header
337 $headercell = clone($contentcelldefault);
338 $headercell->text = $option->text;
339 $rows['header'][] = $headercell;
315d4971 340
34e0b4a7
RW
341 // Graph
342 $graphcell = clone($contentcelldefault);
343 $graphcell->attributes = array('class'=>'graph vertical data');
344 $graphcell->text = $displaygraph;
345 $rows['graph'][] = $graphcell;
034ef761 346
34e0b4a7
RW
347 $usernumber .= html_writer::tag('div', ' '.$numberofuser.'', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice')));
348 $userpercentage .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
034ef761 349
34e0b4a7
RW
350 // number of user
351 $usernumbercell = clone($contentcelldefault);
352 $usernumbercell->text = $usernumber;
353 $rows['usernumber'][] = $usernumbercell;
034ef761 354
34e0b4a7
RW
355 // percentage of user
356 $numbercell = clone($contentcelldefault);
357 $numbercell->text = $userpercentage;
358 $rows['userpercentage'][] = $numbercell;
315d4971 359 }
315d4971 360
34e0b4a7
RW
361 $table->head = $rows['header'];
362 $trgraph = new html_table_row($rows['graph']);
363 $trusernumber = new html_table_row($rows['usernumber']);
364 $truserpercentage = new html_table_row($rows['userpercentage']);
365 $table->data = array($trgraph, $trusernumber, $truserpercentage);
034ef761 366
315d4971
RW
367 $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
368 $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
034ef761
PS
369 $html .= html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph', 'class'=>'skip-block'));
370 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
aad62468 371
315d4971
RW
372 return $html;
373 }
374
375 /**
376 * Returns HTML to display choices result
377 * @param object $choices
378 * @return string
379 */
380 public function display_publish_anonymous_horizontal($choices) {
31d085e2 381 global $CHOICE_COLUMN_WIDTH;
034ef761 382
315d4971
RW
383 $table = new html_table();
384 $table->cellpadding = 5;
385 $table->cellspacing = 0;
386 $table->attributes['class'] = 'results anonymous ';
a2af7fd4 387 $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
315d4971
RW
388 $table->data = array();
389
34e0b4a7
RW
390 $columnheaderdefault = new html_table_cell();
391 $columnheaderdefault->scope = 'col';
392
393 $tableheadertext = clone($columnheaderdefault);
394 $tableheadertext->text = get_string('choiceoptions', 'choice');
395
396 $tableheadernumber = clone($columnheaderdefault);
397 $tableheadernumber->text = get_string('numberofuser', 'choice');
398
399 $tableheaderpercentage = clone($columnheaderdefault);
400 $tableheaderpercentage->text = get_string('numberofuserinpercentage', 'choice');
401
402 $tableheadergraph = clone($columnheaderdefault);
403 $tableheadergraph->text = get_string('responsesresultgraphheader', 'choice');
404
405 $table->head = array($tableheadertext, $tableheadernumber, $tableheaderpercentage, $tableheadergraph);
406
315d4971
RW
407 $count = 0;
408 ksort($choices->options);
409
34e0b4a7
RW
410 $columndefault = new html_table_cell();
411 $columndefault->attributes['class'] = 'data';
412
413 $colheaderdefault = new html_table_cell();
414 $colheaderdefault->scope = 'row';
415 $colheaderdefault->header = true;
416 $colheaderdefault->attributes['class'] = 'header data';
417
315d4971 418 $rows = array();
034ef761 419 foreach ($choices->options as $optionid => $options) {
34e0b4a7
RW
420 $colheader = clone($colheaderdefault);
421 $colheader->text = $options->text;
422
423 $graphcell = clone($columndefault);
424 $datacellnumber = clone($columndefault);
425 $datacellpercentage = clone($columndefault);
426
427 $numberofuser = $width = $percentageamount = 0;
428
315d4971
RW
429 if (!empty($options->user)) {
430 $numberofuser = count($options->user);
431 }
432
315d4971
RW
433 if($choices->numberofuser > 0) {
434 $width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser));
435 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
436 }
315d4971 437
34e0b4a7
RW
438 $attributes = array();
439 $attributes['style'] = 'height:50px; width:'.$width.'px';
440 $attributes['alt'] = '';
441 $attributes['src'] = $this->output->pix_url('row', 'choice');
442 $displaydiagram = html_writer::tag('img','', $attributes);
034ef761 443
34e0b4a7 444 $graphcell->text = $displaydiagram;
315d4971
RW
445 $graphcell->attributes = array('class'=>'graph horizontal');
446
315d4971
RW
447 if($choices->numberofuser > 0) {
448 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
449 }
315d4971 450
34e0b4a7
RW
451 $datacellnumber->text = $numberofuser;
452 $datacellpercentage->text = format_float($percentageamount,1). '%';
453
315d4971
RW
454
455 $row = new html_table_row();
34e0b4a7 456 $row->cells = array($colheader, $datacellnumber, $datacellpercentage, $graphcell);
315d4971
RW
457 $rows[] = $row;
458 }
034ef761 459
315d4971 460 $table->data = $rows;
034ef761 461
315d4971
RW
462 $html = '';
463 $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
034ef761 464 $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
315d4971 465 $html .= html_writer::table($table);
034ef761 466
315d4971
RW
467 return $html;
468 }
469}
470