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 * Moodle renderer used to display special elements of the lesson module
22 * @copyright 2010 Rossiani Wijaya
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define ('DISPLAY_HORIZONTAL_LAYOUT', 0);
26 define ('DISPLAY_VERTICAL_LAYOUT', 1);
28 class mod_choice_renderer
extends plugin_renderer_base
{
31 * Returns HTML to display choices of option
32 * @param object $options
33 * @param int $coursemoduleid
34 * @param bool $vertical
37 public function display_options($options, $coursemoduleid, $vertical = false) {
38 $layoutclass = 'horizontal';
40 $layoutclass = 'vertical';
42 $target = new moodle_url('/mod/choice/view.php');
43 $attributes = array('method'=>'POST', 'action'=>$target, 'class'=> $layoutclass);
45 $html = html_writer
::start_tag('form', $attributes);
46 $html .= html_writer
::start_tag('ul', array('class'=>'choices' ));
48 $availableoption = count($options['options']);
50 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 $option->attributes
->id
= 'choice_'.$choicecount;
57 $labeltext = $option->text
;
58 if (!empty($option->attributes
->disabled
)) {
59 $labeltext .= ' ' . get_string('full', 'choice');
63 $html .= html_writer
::empty_tag('input', (array)$option->attributes
);
64 $html .= html_writer
::tag('label', $labeltext, array('for'=>$option->attributes
->id
));
65 $html .= html_writer
::end_tag('li');
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'=>'action', 'value'=>'makechoice'));
72 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
74 if (!empty($options['hascapability']) && ($options['hascapability'])) {
75 if ($availableoption < 1) {
76 $html .= html_writer
::tag('label', get_string('choicefull', 'choice'));
78 $html .= html_writer
::empty_tag('input', array('type'=>'submit', 'value'=>get_string('savemychoice','choice'), 'class'=>'button'));
81 if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
82 $url = new moodle_url('view.php', array('id'=>$coursemoduleid, 'action'=>'delchoice', 'sesskey'=>sesskey()));
83 $html .= html_writer
::link($url, get_string('removemychoice','choice'));
86 $html .= html_writer
::tag('label', get_string('havetologin', 'choice'));
89 $html .= html_writer
::end_tag('ul');
90 $html .= html_writer
::end_tag('form');
96 * Returns HTML to display choices result
97 * @param object $choices
98 * @param bool $forcepublish
101 public function display_result($choices, $forcepublish = false) {
102 if (empty($forcepublish)) { //allow the publish setting to be overridden
103 $forcepublish = $choices->publish
;
106 $displaylayout = $choices->display
;
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);
114 return $this->display_publish_anonymous_vertical($choices);
119 * Returns HTML to display choices result
120 * @param object $choices
121 * @param bool $forcepublish
124 public function display_publish_name_vertical($choices) {
127 $html .= html_writer
::tag('h3',format_string(get_string("responses", "choice")));
129 $attributes = array('method'=>'POST');
130 $attributes['action'] = new moodle_url($PAGE->url
);
131 $attributes['id'] = 'attemptsform';
133 if ($choices->viewresponsecapability
) {
134 $html .= html_writer
::start_tag('form', $attributes);
135 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid
));
136 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
137 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
140 $table = new html_table();
141 $table->cellpadding
= 0;
142 $table->cellspacing
= 0;
143 $table->attributes
['class'] = 'results names ';
144 $table->tablealign
= 'center';
145 $table->summary
= get_string('responsesto', 'choice', format_string($choices->name
));
146 $table->data
= array();
149 ksort($choices->options
);
152 $celldefault = new html_table_cell();
153 $celldefault->attributes
['class'] = 'data';
155 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
156 $accessiblecell = new html_table_cell();
157 $accessiblecell->scope
= 'row';
158 $accessiblecell->text
= get_string('choiceoptions', 'choice');
159 $columns['options'][] = $accessiblecell;
161 $usernumberheader = clone($celldefault);
162 $usernumberheader->header
= true;
163 $usernumberheader->attributes
['class'] = 'header data';
164 $usernumberheader->text
= get_string('numberofuser', 'choice');
165 $columns['usernumber'][] = $usernumberheader;
168 foreach ($choices->options
as $optionid => $options) {
169 $celloption = clone($celldefault);
170 $cellusernumber = clone($celldefault);
171 $cellusernumber->style
= 'text-align: center;';
174 if ($choices->showunanswered
&& $optionid == 0) {
175 $celltext = format_string(get_string('notanswered', 'choice'));
176 } else if ($optionid > 0) {
177 $celltext = format_string($choices->options
[$optionid]->text
);
180 if (!empty($options->user
) && count($options->user
) > 0) {
181 $numberofuser = count($options->user
);
184 $celloption->text
= $celltext;
185 $cellusernumber->text
= $numberofuser;
187 $columns['options'][] = $celloption;
188 $columns['usernumber'][] = $cellusernumber;
191 $table->head
= $columns['options'];
192 $table->data
[] = new html_table_row($columns['usernumber']);
196 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
197 $accessiblecell = new html_table_cell();
198 $accessiblecell->text
= get_string('userchoosethisoption', 'choice');
199 $accessiblecell->header
= true;
200 $accessiblecell->scope
= 'row';
201 $accessiblecell->attributes
['class'] = 'header data';
202 $columns[] = $accessiblecell;
204 foreach ($choices->options
as $optionid => $options) {
205 $cell = new html_table_cell();
206 $cell->attributes
['class'] = 'data';
208 if ($choices->showunanswered ||
$optionid > 0) {
209 if (!empty($options->user
)) {
211 foreach ($options->user
as $user) {
213 if (empty($user->imagealt
)){
214 $user->imagealt
= '';
217 $userfullname = fullname($user, $choices->fullnamecapability
);
218 if ($choices->viewresponsecapability
&& $choices->deleterepsonsecapability
&& $optionid > 0) {
219 $attemptaction = html_writer
::label($userfullname, 'attempt-user'.$user->id
, false, array('class' => 'accesshide'));
220 $attemptaction .= html_writer
::checkbox('attemptid[]', $user->id
,'', null, array('id' => 'attempt-user'.$user->id
));
221 $data .= html_writer
::tag('div', $attemptaction, array('class'=>'attemptaction'));
223 $userimage = $this->output
->user_picture($user, array('courseid'=>$choices->courseid
));
224 $data .= html_writer
::tag('div', $userimage, array('class'=>'image'));
226 $userlink = new moodle_url('/user/view.php', array('id'=>$user->id
,'course'=>$choices->courseid
));
227 $name = html_writer
::tag('a', $userfullname, array('href'=>$userlink, 'class'=>'username'));
228 $data .= html_writer
::tag('div', $name, array('class'=>'fullname'));
229 $data .= html_writer
::tag('div','', array('class'=>'clearfloat'));
230 $optionusers .= html_writer
::tag('div', $data, array('class'=>'user'));
232 $cell->text
= $optionusers;
238 $row = new html_table_row($columns);
239 $table->data
[] = $row;
241 $html .= html_writer
::tag('div', html_writer
::table($table), array('class'=>'response'));
244 if ($choices->viewresponsecapability
&& $choices->deleterepsonsecapability
) {
245 $selecturl = new moodle_url('#');
247 $selectallactions = new component_action('click',"checkall");
248 $selectall = new action_link($selecturl, get_string('selectall'), $selectallactions);
249 $actiondata .= $this->output
->render($selectall) . ' / ';
251 $deselectallactions = new component_action('click',"checknone");
252 $deselectall = new action_link($selecturl, get_string('deselectall'), $deselectallactions);
253 $actiondata .= $this->output
->render($deselectall);
255 $actiondata .= html_writer
::tag('label', ' ' . get_string('withselected', 'choice') . ' ', array('for'=>'menuaction'));
257 $actionurl = new moodle_url($PAGE->url
, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
258 $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choice')), 'attemptsform');
260 $actiondata .= $this->output
->render($select);
262 $html .= html_writer
::tag('div', $actiondata, array('class'=>'responseaction'));
264 if ($choices->viewresponsecapability
) {
265 $html .= html_writer
::end_tag('form');
273 * Returns HTML to display choices result
274 * @param object $choices
277 public function display_publish_anonymous_vertical($choices) {
278 global $CHOICE_COLUMN_HEIGHT;
281 $table = new html_table();
282 $table->cellpadding
= 5;
283 $table->cellspacing
= 0;
284 $table->attributes
['class'] = 'results anonymous ';
285 $table->summary
= get_string('responsesto', 'choice', format_string($choices->name
));
286 $table->data
= array();
289 ksort($choices->options
);
293 $headercelldefault = new html_table_cell();
294 $headercelldefault->scope
= 'row';
295 $headercelldefault->header
= true;
296 $headercelldefault->attributes
= array('class'=>'header data');
299 $tableheader = clone($headercelldefault);
300 $tableheader->text
= html_writer
::tag('div', get_string('choiceoptions', 'choice'), array('class' => 'accesshide'));
301 $rows['header'][] = $tableheader;
304 $graphheader = clone($headercelldefault);
305 $graphheader->text
= html_writer
::tag('div', get_string('responsesresultgraphheader', 'choice'), array('class' => 'accesshide'));
306 $rows['graph'][] = $graphheader;
308 // user number row header
309 $usernumberheader = clone($headercelldefault);
310 $usernumberheader->text
= get_string('numberofuser', 'choice');
311 $rows['usernumber'][] = $usernumberheader;
313 // user percentage row header
314 $userpercentageheader = clone($headercelldefault);
315 $userpercentageheader->text
= get_string('numberofuserinpercentage', 'choice');
316 $rows['userpercentage'][] = $userpercentageheader;
318 $contentcelldefault = new html_table_cell();
319 $contentcelldefault->attributes
= array('class'=>'data');
321 foreach ($choices->options
as $optionid => $option) {
322 // calculate display length
323 $height = $percentageamount = $numberofuser = 0;
324 $usernumber = $userpercentage = '';
326 if (!empty($option->user
)) {
327 $numberofuser = count($option->user
);
330 if($choices->numberofuser
> 0) {
331 $height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser
));
332 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser
)*100.0;
335 $displaygraph = html_writer
::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output
->pix_url('column', 'choice')));
338 $headercell = clone($contentcelldefault);
339 $headercell->text
= $option->text
;
340 $rows['header'][] = $headercell;
343 $graphcell = clone($contentcelldefault);
344 $graphcell->attributes
= array('class'=>'graph vertical data');
345 $graphcell->text
= $displaygraph;
346 $rows['graph'][] = $graphcell;
348 $usernumber .= html_writer
::tag('div', ' '.$numberofuser.'', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice')));
349 $userpercentage .= html_writer
::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
352 $usernumbercell = clone($contentcelldefault);
353 $usernumbercell->text
= $usernumber;
354 $rows['usernumber'][] = $usernumbercell;
356 // percentage of user
357 $numbercell = clone($contentcelldefault);
358 $numbercell->text
= $userpercentage;
359 $rows['userpercentage'][] = $numbercell;
362 $table->head
= $rows['header'];
363 $trgraph = new html_table_row($rows['graph']);
364 $trusernumber = new html_table_row($rows['usernumber']);
365 $truserpercentage = new html_table_row($rows['userpercentage']);
366 $table->data
= array($trgraph, $trusernumber, $truserpercentage);
368 $header = html_writer
::tag('h3',format_string(get_string("responses", "choice")));
369 $html .= html_writer
::tag('div', $header, array('class'=>'responseheader'));
370 $html .= html_writer
::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph', 'class'=>'skip-block'));
371 $html .= html_writer
::tag('div', html_writer
::table($table), array('class'=>'response'));
377 * Returns HTML to display choices result
378 * @param object $choices
381 public function display_publish_anonymous_horizontal($choices) {
382 global $CHOICE_COLUMN_WIDTH;
384 $table = new html_table();
385 $table->cellpadding
= 5;
386 $table->cellspacing
= 0;
387 $table->attributes
['class'] = 'results anonymous ';
388 $table->summary
= get_string('responsesto', 'choice', format_string($choices->name
));
389 $table->data
= array();
391 $columnheaderdefault = new html_table_cell();
392 $columnheaderdefault->scope
= 'col';
394 $tableheadertext = clone($columnheaderdefault);
395 $tableheadertext->text
= get_string('choiceoptions', 'choice');
397 $tableheadernumber = clone($columnheaderdefault);
398 $tableheadernumber->text
= get_string('numberofuser', 'choice');
400 $tableheaderpercentage = clone($columnheaderdefault);
401 $tableheaderpercentage->text
= get_string('numberofuserinpercentage', 'choice');
403 $tableheadergraph = clone($columnheaderdefault);
404 $tableheadergraph->text
= get_string('responsesresultgraphheader', 'choice');
406 $table->head
= array($tableheadertext, $tableheadernumber, $tableheaderpercentage, $tableheadergraph);
409 ksort($choices->options
);
411 $columndefault = new html_table_cell();
412 $columndefault->attributes
['class'] = 'data';
414 $colheaderdefault = new html_table_cell();
415 $colheaderdefault->scope
= 'row';
416 $colheaderdefault->header
= true;
417 $colheaderdefault->attributes
['class'] = 'header data';
420 foreach ($choices->options
as $optionid => $options) {
421 $colheader = clone($colheaderdefault);
422 $colheader->text
= $options->text
;
424 $graphcell = clone($columndefault);
425 $datacellnumber = clone($columndefault);
426 $datacellpercentage = clone($columndefault);
428 $numberofuser = $width = $percentageamount = 0;
430 if (!empty($options->user
)) {
431 $numberofuser = count($options->user
);
434 if($choices->numberofuser
> 0) {
435 $width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser
));
436 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser
)*100.0;
439 $attributes = array();
440 $attributes['style'] = 'height:50px; width:'.$width.'px';
441 $attributes['alt'] = '';
442 $attributes['src'] = $this->output
->pix_url('row', 'choice');
443 $displaydiagram = html_writer
::tag('img','', $attributes);
445 $graphcell->text
= $displaydiagram;
446 $graphcell->attributes
= array('class'=>'graph horizontal');
448 if($choices->numberofuser
> 0) {
449 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser
)*100.0;
452 $datacellnumber->text
= $numberofuser;
453 $datacellpercentage->text
= format_float($percentageamount,1). '%';
456 $row = new html_table_row();
457 $row->cells
= array($colheader, $datacellnumber, $datacellpercentage, $graphcell);
461 $table->data
= $rows;
464 $header = html_writer
::tag('h3',format_string(get_string("responses", "choice")));
465 $html .= html_writer
::tag('div', $header, array('class'=>'responseheader'));
466 $html .= html_writer
::table($table);