MDL-30876 filter_mediaplugin Wrapping the style sheets access with try & catch
[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                         if ($choices->viewresponsecapability && $choices->deleterepsonsecapability  && $optionid > 0) {
217                             $attemptaction = html_writer::checkbox('attemptid[]', $user->id,'');
218                             $data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
219                         }
220                         $userimage = $this->output->user_picture($user, array('courseid'=>$choices->courseid));
221                         $data .= html_writer::tag('div', $userimage, array('class'=>'image'));
223                         $userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choices->courseid));
224                         $name = html_writer::tag('a', fullname($user, $choices->fullnamecapability), array('href'=>$userlink, 'class'=>'username'));
225                         $data .= html_writer::tag('div', $name, array('class'=>'fullname'));
226                         $data .= html_writer::tag('div','', array('class'=>'clearfloat'));
227                         $optionusers .= html_writer::tag('div', $data, array('class'=>'user'));
228                     }
229                     $cell->text = $optionusers;
230                 }
231             }
232             $columns[] = $cell;
233             $count++;
234         }
235         $row = new html_table_row($columns);
236         $table->data[] = $row;
238         $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
240         $actiondata = '';
241         if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
242             $selecturl = new moodle_url('#');
244             $selectallactions = new component_action('click',"checkall");
245             $selectall = new action_link($selecturl, get_string('selectall'), $selectallactions);
246             $actiondata .= $this->output->render($selectall) . ' / ';
248             $deselectallactions = new component_action('click',"checknone");
249             $deselectall = new action_link($selecturl, get_string('deselectall'), $deselectallactions);
250             $actiondata .= $this->output->render($deselectall);
252             $actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'choice') . ' ', array('for'=>'menuaction'));
254             $actionurl = new moodle_url($PAGE->url, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
255             $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choice')), 'attemptsform');
257             $actiondata .= $this->output->render($select);
258         }
259         $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
261         if ($choices->viewresponsecapability) {
262             $html .= html_writer::end_tag('form');
263         }
265         return $html;
266     }
269     /**
270      * Returns HTML to display choices result
271      * @param object $choices
272      * @return string
273      */
274     public function display_publish_anonymous_vertical($choices) {
275         global $CHOICE_COLUMN_HEIGHT;
277         $html = '';
278         $table = new html_table();
279         $table->cellpadding = 5;
280         $table->cellspacing = 0;
281         $table->attributes['class'] = 'results anonymous ';
282         $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
283         $table->data = array();
285         $count = 0;
286         ksort($choices->options);
287         $columns = array();
288         $rows = array();
290         $headercelldefault = new html_table_cell();
291         $headercelldefault->scope = 'row';
292         $headercelldefault->header = true;
293         $headercelldefault->attributes = array('class'=>'header data');
295         // column header
296         $tableheader = clone($headercelldefault);
297         $tableheader->text = html_writer::tag('div', get_string('choiceoptions', 'choice'), array('class' => 'accesshide'));
298         $rows['header'][] = $tableheader;
300         // graph row header
301         $graphheader = clone($headercelldefault);
302         $graphheader->text = html_writer::tag('div', get_string('responsesresultgraphheader', 'choice'), array('class' => 'accesshide'));
303         $rows['graph'][] = $graphheader;
305         // user number row header
306         $usernumberheader = clone($headercelldefault);
307         $usernumberheader->text = get_string('numberofuser', 'choice');
308         $rows['usernumber'][] = $usernumberheader;
310         // user percentage row header
311         $userpercentageheader = clone($headercelldefault);
312         $userpercentageheader->text = get_string('numberofuserinpercentage', 'choice');
313         $rows['userpercentage'][] = $userpercentageheader;
315         $contentcelldefault = new html_table_cell();
316         $contentcelldefault->attributes = array('class'=>'data');
318         foreach ($choices->options as $optionid => $option) {
319             // calculate display length
320             $height = $percentageamount = $numberofuser = 0;
321             $usernumber = $userpercentage = '';
323             if (!empty($option->user)) {
324                $numberofuser = count($option->user);
325             }
327             if($choices->numberofuser > 0) {
328                $height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser));
329                $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
330             }
332             $displaygraph = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output->pix_url('column', 'choice')));
334             // header
335             $headercell = clone($contentcelldefault);
336             $headercell->text = $option->text;
337             $rows['header'][] = $headercell;
339             // Graph
340             $graphcell = clone($contentcelldefault);
341             $graphcell->attributes = array('class'=>'graph vertical data');
342             $graphcell->text = $displaygraph;
343             $rows['graph'][] = $graphcell;
345             $usernumber .= html_writer::tag('div', ' '.$numberofuser.'', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice')));
346             $userpercentage .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
348             // number of user
349             $usernumbercell = clone($contentcelldefault);
350             $usernumbercell->text = $usernumber;
351             $rows['usernumber'][] = $usernumbercell;
353             // percentage of user
354             $numbercell = clone($contentcelldefault);
355             $numbercell->text = $userpercentage;
356             $rows['userpercentage'][] = $numbercell;
357         }
359         $table->head = $rows['header'];
360         $trgraph = new html_table_row($rows['graph']);
361         $trusernumber = new html_table_row($rows['usernumber']);
362         $truserpercentage = new html_table_row($rows['userpercentage']);
363         $table->data = array($trgraph, $trusernumber, $truserpercentage);
365         $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
366         $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
367         $html .= html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph', 'class'=>'skip-block'));
368         $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
370         return $html;
371     }
373     /**
374      * Returns HTML to display choices result
375      * @param object $choices
376      * @return string
377      */
378     public function display_publish_anonymous_horizontal($choices) {
379         global $CHOICE_COLUMN_WIDTH;
381         $table = new html_table();
382         $table->cellpadding = 5;
383         $table->cellspacing = 0;
384         $table->attributes['class'] = 'results anonymous ';
385         $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
386         $table->data = array();
388         $columnheaderdefault = new html_table_cell();
389         $columnheaderdefault->scope = 'col';
391         $tableheadertext = clone($columnheaderdefault);
392         $tableheadertext->text = get_string('choiceoptions', 'choice');
394         $tableheadernumber = clone($columnheaderdefault);
395         $tableheadernumber->text = get_string('numberofuser', 'choice');
397         $tableheaderpercentage = clone($columnheaderdefault);
398         $tableheaderpercentage->text = get_string('numberofuserinpercentage', 'choice');
400         $tableheadergraph = clone($columnheaderdefault);
401         $tableheadergraph->text = get_string('responsesresultgraphheader', 'choice');
403         $table->head = array($tableheadertext, $tableheadernumber, $tableheaderpercentage, $tableheadergraph);
405         $count = 0;
406         ksort($choices->options);
408         $columndefault = new html_table_cell();
409         $columndefault->attributes['class'] = 'data';
411         $colheaderdefault = new html_table_cell();
412         $colheaderdefault->scope = 'row';
413         $colheaderdefault->header = true;
414         $colheaderdefault->attributes['class'] = 'header data';
416         $rows = array();
417         foreach ($choices->options as $optionid => $options) {
418             $colheader = clone($colheaderdefault);
419             $colheader->text = $options->text;
421             $graphcell = clone($columndefault);
422             $datacellnumber = clone($columndefault);
423             $datacellpercentage = clone($columndefault);
425             $numberofuser = $width = $percentageamount = 0;
427             if (!empty($options->user)) {
428                $numberofuser = count($options->user);
429             }
431             if($choices->numberofuser > 0) {
432                $width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser));
433                $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
434             }
436             $attributes = array();
437             $attributes['style'] = 'height:50px; width:'.$width.'px';
438             $attributes['alt'] = '';
439             $attributes['src'] = $this->output->pix_url('row', 'choice');
440             $displaydiagram = html_writer::tag('img','', $attributes);
442             $graphcell->text = $displaydiagram;
443             $graphcell->attributes = array('class'=>'graph horizontal');
445             if($choices->numberofuser > 0) {
446                $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
447             }
449             $datacellnumber->text = $numberofuser;
450             $datacellpercentage->text = format_float($percentageamount,1). '%';
453             $row = new html_table_row();
454             $row->cells = array($colheader, $datacellnumber, $datacellpercentage, $graphcell);
455             $rows[] = $row;
456         }
458         $table->data = $rows;
460         $html = '';
461         $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
462         $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
463         $html .= html_writer::table($table);
465         return $html;
466     }