Merge branch 'MDL-33575_m23' of git://github.com/rwijaya/moodle into MOODLE_23_STABLE
[moodle.git] / mod / choice / renderer.php
1 <?php
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/>.
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  **/
25 define ('DISPLAY_HORIZONTAL_LAYOUT', 0);
26 define ('DISPLAY_VERTICAL_LAYOUT', 1);
28 class mod_choice_renderer extends plugin_renderer_base {
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         $layoutclass = 'horizontal';
39         if ($vertical) {
40             $layoutclass = 'vertical';
41         }
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']);
49         $choicecount = 0;
50         foreach ($options['options'] as $option) {
51             $choicecount++;
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');
60                 $availableoption--;
61             }
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');
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));
73         if (!empty($options['hascapability']) && ($options['hascapability'])) {
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             }
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         }
88         $html .= html_writer::end_tag('ul');
89         $html .= html_writer::end_tag('form');
91         return $html;
92     }
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) {
101         if (empty($forcepublish)) { //allow the publish setting to be overridden
102             $forcepublish = $choices->publish;
103         }
105         $displaylayout = $choices->display;
107         if ($forcepublish) {  //CHOICE_PUBLISH_NAMES
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);
114         }
115     }
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) {
124         global $PAGE;
125         $html ='';
126         $html .= html_writer::tag('h2',format_string(get_string("responses", "choice")), array('class'=>'main'));
128         $attributes = array('method'=>'POST');
129         $attributes['action'] = new moodle_url($PAGE->url);
130         $attributes['id'] = 'attemptsform';
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()));
136             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
137         }
139         $table = new html_table();
140         $table->cellpadding = 0;
141         $table->cellspacing = 0;
142         $table->attributes['class'] = 'results names ';
143         $table->tablealign = 'center';
144         $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
145         $table->data = array();
147         $count = 0;
148         ksort($choices->options);
150         $columns = array();
151         $celldefault = new html_table_cell();
152         $celldefault->attributes['class'] = 'data';
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;
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;
167         foreach ($choices->options as $optionid => $options) {
168             $celloption = clone($celldefault);
169             $cellusernumber = clone($celldefault);
170             $cellusernumber->style = 'text-align: center;';
172             $celltext = '';
173             if ($choices->showunanswered && $optionid == 0) {
174                 $celltext = format_string(get_string('notanswered', 'choice'));
175             } else if ($optionid > 0) {
176                 $celltext = format_string($choices->options[$optionid]->text);
177             }
178             $numberofuser = 0;
179             if (!empty($options->user) && count($options->user) > 0) {
180                 $numberofuser = count($options->user);
181             }
183             $celloption->text = $celltext;
184             $cellusernumber->text = $numberofuser;
186             $columns['options'][] = $celloption;
187             $columns['usernumber'][] = $cellusernumber;
188         }
190         $table->head = $columns['options'];
191         $table->data[] = new html_table_row($columns['usernumber']);
193         $columns = array();
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;
203         foreach ($choices->options as $optionid => $options) {
204             $cell = new html_table_cell();
205             $cell->attributes['class'] = 'data';
207             if ($choices->showunanswered || $optionid > 0) {
208                 if (!empty($options->user)) {
209                     $optionusers = '';
210                     foreach ($options->user as $user) {
211                         $data = '';
212                         if (empty($user->imagealt)){
213                             $user->imagealt = '';
214                         }
216                         $userfullname = fullname($user, $choices->fullnamecapability);
217                         if ($choices->viewresponsecapability && $choices->deleterepsonsecapability  && $optionid > 0) {
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));
220                             $data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
221                         }
222                         $userimage = $this->output->user_picture($user, array('courseid'=>$choices->courseid));
223                         $data .= html_writer::tag('div', $userimage, array('class'=>'image'));
225                         $userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choices->courseid));
226                         $name = html_writer::tag('a', $userfullname, array('href'=>$userlink, 'class'=>'username'));
227                         $data .= html_writer::tag('div', $name, array('class'=>'fullname'));
228                         $data .= html_writer::tag('div','', array('class'=>'clearfloat'));
229                         $optionusers .= html_writer::tag('div', $data, array('class'=>'user'));
230                     }
231                     $cell->text = $optionusers;
232                 }
233             }
234             $columns[] = $cell;
235             $count++;
236         }
237         $row = new html_table_row($columns);
238         $table->data[] = $row;
240         $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
242         $actiondata = '';
243         if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
244             $selecturl = new moodle_url('#');
246             $selectallactions = new component_action('click',"checkall");
247             $selectall = new action_link($selecturl, get_string('selectall'), $selectallactions);
248             $actiondata .= $this->output->render($selectall) . ' / ';
250             $deselectallactions = new component_action('click',"checknone");
251             $deselectall = new action_link($selecturl, get_string('deselectall'), $deselectallactions);
252             $actiondata .= $this->output->render($deselectall);
254             $actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'choice') . ' ', array('for'=>'menuaction'));
256             $actionurl = new moodle_url($PAGE->url, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
257             $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choice')), 'attemptsform');
259             $actiondata .= $this->output->render($select);
260         }
261         $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
263         if ($choices->viewresponsecapability) {
264             $html .= html_writer::end_tag('form');
265         }
267         return $html;
268     }
271     /**
272      * Returns HTML to display choices result
273      * @param object $choices
274      * @return string
275      */
276     public function display_publish_anonymous_vertical($choices) {
277         global $CHOICE_COLUMN_HEIGHT;
279         $html = '';
280         $table = new html_table();
281         $table->cellpadding = 5;
282         $table->cellspacing = 0;
283         $table->attributes['class'] = 'results anonymous ';
284         $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
285         $table->data = array();
287         $count = 0;
288         ksort($choices->options);
289         $columns = array();
290         $rows = array();
292         $headercelldefault = new html_table_cell();
293         $headercelldefault->scope = 'row';
294         $headercelldefault->header = true;
295         $headercelldefault->attributes = array('class'=>'header data');
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;
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;
307         // user number row header
308         $usernumberheader = clone($headercelldefault);
309         $usernumberheader->text = get_string('numberofuser', 'choice');
310         $rows['usernumber'][] = $usernumberheader;
312         // user percentage row header
313         $userpercentageheader = clone($headercelldefault);
314         $userpercentageheader->text = get_string('numberofuserinpercentage', 'choice');
315         $rows['userpercentage'][] = $userpercentageheader;
317         $contentcelldefault = new html_table_cell();
318         $contentcelldefault->attributes = array('class'=>'data');
320         foreach ($choices->options as $optionid => $option) {
321             // calculate display length
322             $height = $percentageamount = $numberofuser = 0;
323             $usernumber = $userpercentage = '';
325             if (!empty($option->user)) {
326                $numberofuser = count($option->user);
327             }
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             }
334             $displaygraph = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output->pix_url('column', 'choice')));
336             // header
337             $headercell = clone($contentcelldefault);
338             $headercell->text = $option->text;
339             $rows['header'][] = $headercell;
341             // Graph
342             $graphcell = clone($contentcelldefault);
343             $graphcell->attributes = array('class'=>'graph vertical data');
344             $graphcell->text = $displaygraph;
345             $rows['graph'][] = $graphcell;
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'));
350             // number of user
351             $usernumbercell = clone($contentcelldefault);
352             $usernumbercell->text = $usernumber;
353             $rows['usernumber'][] = $usernumbercell;
355             // percentage of user
356             $numbercell = clone($contentcelldefault);
357             $numbercell->text = $userpercentage;
358             $rows['userpercentage'][] = $numbercell;
359         }
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);
367         $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
368         $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
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'));
372         return $html;
373     }
375     /**
376      * Returns HTML to display choices result
377      * @param object $choices
378      * @return string
379      */
380     public function display_publish_anonymous_horizontal($choices) {
381         global $CHOICE_COLUMN_WIDTH;
383         $table = new html_table();
384         $table->cellpadding = 5;
385         $table->cellspacing = 0;
386         $table->attributes['class'] = 'results anonymous ';
387         $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
388         $table->data = array();
390         $columnheaderdefault = new html_table_cell();
391         $columnheaderdefault->scope = 'col';
393         $tableheadertext = clone($columnheaderdefault);
394         $tableheadertext->text = get_string('choiceoptions', 'choice');
396         $tableheadernumber = clone($columnheaderdefault);
397         $tableheadernumber->text = get_string('numberofuser', 'choice');
399         $tableheaderpercentage = clone($columnheaderdefault);
400         $tableheaderpercentage->text = get_string('numberofuserinpercentage', 'choice');
402         $tableheadergraph = clone($columnheaderdefault);
403         $tableheadergraph->text = get_string('responsesresultgraphheader', 'choice');
405         $table->head = array($tableheadertext, $tableheadernumber, $tableheaderpercentage, $tableheadergraph);
407         $count = 0;
408         ksort($choices->options);
410         $columndefault = new html_table_cell();
411         $columndefault->attributes['class'] = 'data';
413         $colheaderdefault = new html_table_cell();
414         $colheaderdefault->scope = 'row';
415         $colheaderdefault->header = true;
416         $colheaderdefault->attributes['class'] = 'header data';
418         $rows = array();
419         foreach ($choices->options as $optionid => $options) {
420             $colheader = clone($colheaderdefault);
421             $colheader->text = $options->text;
423             $graphcell = clone($columndefault);
424             $datacellnumber = clone($columndefault);
425             $datacellpercentage = clone($columndefault);
427             $numberofuser = $width = $percentageamount = 0;
429             if (!empty($options->user)) {
430                $numberofuser = count($options->user);
431             }
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             }
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);
444             $graphcell->text = $displaydiagram;
445             $graphcell->attributes = array('class'=>'graph horizontal');
447             if($choices->numberofuser > 0) {
448                $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
449             }
451             $datacellnumber->text = $numberofuser;
452             $datacellpercentage->text = format_float($percentageamount,1). '%';
455             $row = new html_table_row();
456             $row->cells = array($colheader, $datacellnumber, $datacellpercentage, $graphcell);
457             $rows[] = $row;
458         }
460         $table->data = $rows;
462         $html = '';
463         $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
464         $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
465         $html .= html_writer::table($table);
467         return $html;
468     }