weekly release 2.3+
[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';
144 $table->data = array();
145
146 $count = 0;
147 ksort($choices->options);
148
149 $columns = array();
150 foreach ($choices->options as $optionid => $options) {
034ef761 151 $coldata = '';
315d4971
RW
152 if ($choices->showunanswered && $optionid == 0) {
153 $coldata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
154 } else if ($optionid > 0) {
155 $coldata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
156 }
157 $numberofuser = 0;
158 if (!empty($options->user) && count($options->user) > 0) {
034ef761
PS
159 $numberofuser = count($options->user);
160 }
161
162 $coldata .= html_writer::tag('div', ' ('.$numberofuser. ')', array('class'=>'numberofuser', 'title' => get_string('numberofuser', 'choice')));
315d4971
RW
163 $columns[] = $coldata;
164 }
034ef761 165
315d4971
RW
166 $table->head = $columns;
167
168 $coldata = '';
169 $columns = array();
170 foreach ($choices->options as $optionid => $options) {
171 $coldata = '';
172 if ($choices->showunanswered || $optionid > 0) {
173 if (!empty($options->user)) {
034ef761 174 foreach ($options->user as $user) {
315d4971
RW
175 $data = '';
176 if (empty($user->imagealt)){
177 $user->imagealt = '';
178 }
034ef761
PS
179
180 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability && $optionid > 0) {
315d4971
RW
181 $attemptaction = html_writer::checkbox('attemptid[]', $user->id,'');
182 $data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
183 }
31d085e2 184 $userimage = $this->output->user_picture($user, array('courseid'=>$choices->courseid));
315d4971
RW
185 $data .= html_writer::tag('div', $userimage, array('class'=>'image'));
186
31d085e2 187 $userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choices->courseid));
188 $name = html_writer::tag('a', fullname($user, $choices->fullnamecapability), array('href'=>$userlink, 'class'=>'username'));
315d4971
RW
189 $data .= html_writer::tag('div', $name, array('class'=>'fullname'));
190 $data .= html_writer::tag('div','', array('class'=>'clearfloat'));
034ef761
PS
191 $coldata .= html_writer::tag('div', $data, array('class'=>'user'));
192 }
193 }
315d4971 194 }
034ef761 195
315d4971
RW
196 $columns[] = $coldata;
197 $count++;
198 }
034ef761
PS
199
200 $table->data[] = $columns;
315d4971
RW
201 foreach ($columns as $d) {
202 $table->colclasses[] = 'data';
203 }
aad62468 204 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
034ef761 205
315d4971 206 $actiondata = '';
034ef761 207 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
315d4971
RW
208 $selecturl = new moodle_url('#');
209
c9b8a789 210 $selectallactions = new component_action('click',"checkall");
b447192e 211 $selectall = new action_link($selecturl, get_string('selectall'), $selectallactions);
31d085e2 212 $actiondata .= $this->output->render($selectall) . ' / ';
315d4971 213
c9b8a789 214 $deselectallactions = new component_action('click',"checknone");
b447192e 215 $deselectall = new action_link($selecturl, get_string('deselectall'), $deselectallactions);
31d085e2 216 $actiondata .= $this->output->render($deselectall);
315d4971 217
b447192e 218 $actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'choice') . ' ', array('for'=>'menuaction'));
034ef761 219
6e4453f5 220 $actionurl = new moodle_url($PAGE->url, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
f51d388c 221 $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choice')), 'attemptsform');
034ef761 222
31d085e2 223 $actiondata .= $this->output->render($select);
315d4971
RW
224 }
225 $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
034ef761 226
315d4971
RW
227 if ($choices->viewresponsecapability) {
228 $html .= html_writer::end_tag('form');
229 }
034ef761 230
aad62468 231 return $html;
315d4971
RW
232 }
233
234
235 /**
236 * Returns HTML to display choices result
237 * @param object $choices
238 * @return string
239 */
240 public function display_publish_anonymous_vertical($choices) {
31d085e2 241 global $CHOICE_COLUMN_HEIGHT;
315d4971 242
034ef761 243 $html = '';
315d4971
RW
244 $table = new html_table();
245 $table->cellpadding = 5;
034ef761
PS
246 $table->cellspacing = 0;
247 $table->attributes['class'] = 'results anonymous ';
248 $table->data = array();
315d4971
RW
249 $count = 0;
250 ksort($choices->options);
251 $columns = array();
252 $rows = array();
253
254 foreach ($choices->options as $optionid => $options) {
255 $numberofuser = 0;
256 if (!empty($options->user)) {
257 $numberofuser = count($options->user);
258 }
259 $height = 0;
260 $percentageamount = 0;
261 if($choices->numberofuser > 0) {
262 $height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser));
263 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
264 }
265
31d085e2 266 $displaydiagram = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output->pix_url('column', 'choice')));
315d4971
RW
267
268 $cell = new html_table_cell();
269 $cell->text = $displaydiagram;
270 $cell->attributes = array('class'=>'graph vertical data');
271 $columns[] = $cell;
272 }
273 $rowgraph = new html_table_row();
274 $rowgraph->cells = $columns;
275 $rows[] = $rowgraph;
034ef761 276
315d4971
RW
277 $columns = array();
278 $printskiplink = true;
279 foreach ($choices->options as $optionid => $options) {
280 $columndata = '';
281 $numberofuser = 0;
282 if (!empty($options->user)) {
283 $numberofuser = count($options->user);
034ef761 284 }
315d4971
RW
285
286 if ($printskiplink) {
287 $columndata .= html_writer::tag('div', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'));
288 $printskiplink = false;
289 }
034ef761 290
315d4971
RW
291 if ($choices->showunanswered && $optionid == 0) {
292 $columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
293 } else if ($optionid > 0) {
294 $columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
295 }
296 $columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice')));
034ef761 297
315d4971
RW
298 if($choices->numberofuser > 0) {
299 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
300 }
301 $columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
034ef761 302
315d4971
RW
303 $cell = new html_table_cell();
304 $cell->text = $columndata;
305 $cell->attributes = array('class'=>'data header');
034ef761 306 $columns[] = $cell;
315d4971
RW
307 }
308 $rowdata = new html_table_row();
309 $rowdata->cells = $columns;
310 $rows[] = $rowdata;
311
312 $table->data = $rows;
034ef761 313
315d4971
RW
314 $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
315 $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
034ef761
PS
316 $html .= html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph', 'class'=>'skip-block'));
317 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
aad62468 318
315d4971
RW
319 return $html;
320 }
321
322 /**
323 * Returns HTML to display choices result
324 * @param object $choices
325 * @return string
326 */
327 public function display_publish_anonymous_horizontal($choices) {
31d085e2 328 global $CHOICE_COLUMN_WIDTH;
034ef761 329
315d4971
RW
330 $table = new html_table();
331 $table->cellpadding = 5;
332 $table->cellspacing = 0;
333 $table->attributes['class'] = 'results anonymous ';
334 $table->data = array();
335
336 $count = 0;
337 ksort($choices->options);
338
339 $rows = array();
034ef761 340 foreach ($choices->options as $optionid => $options) {
315d4971
RW
341 $numberofuser = 0;
342 $graphcell = new html_table_cell();
343 if (!empty($options->user)) {
344 $numberofuser = count($options->user);
345 }
346
347 $width = 0;
348 $percentageamount = 0;
349 $columndata = '';
350 if($choices->numberofuser > 0) {
351 $width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser));
352 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
353 }
31d085e2 354 $displaydiagram = html_writer::tag('img','', array('style'=>'height:50px; width:'.$width.'px', 'alt'=>'', 'src'=>$this->output->pix_url('row', 'choice')));
315d4971
RW
355
356 $skiplink = html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph'. $optionid, 'class'=>'skip-block'));
357 $skiphandler = html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'.$optionid));
034ef761 358
315d4971
RW
359 $graphcell->text = $skiplink . $displaydiagram . $skiphandler;
360 $graphcell->attributes = array('class'=>'graph horizontal');
361
362 $datacell = new html_table_cell();
363 if ($choices->showunanswered && $optionid == 0) {
364 $columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
365 } else if ($optionid > 0) {
366 $columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
367 }
368 $columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('title'=> get_string('numberofuser', 'choice'), 'class'=>'numberofuser'));
369
370 if($choices->numberofuser > 0) {
371 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
372 }
034ef761 373 $columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
315d4971
RW
374
375 $datacell->text = $columndata;
376 $datacell->attributes = array('class'=>'header');
377
378 $row = new html_table_row();
379 $row->cells = array($datacell, $graphcell);
380 $rows[] = $row;
381 }
034ef761 382
315d4971 383 $table->data = $rows;
034ef761 384
315d4971
RW
385 $html = '';
386 $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
034ef761 387 $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
315d4971 388 $html .= html_writer::table($table);
034ef761 389
315d4971
RW
390 return $html;
391 }
392}
393