fixed var name typo
[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');
43 $attributes = array('method'=>'POST', 'target'=>$target, 'class'=> $layoutclass);
315d4971
RW
44
45 $html = html_writer::start_tag('form', $attributes);
46 $html .= html_writer::start_tag('ul', array('class'=>'choices' ));
47
48 $availableoption = count($options['options']);
49 foreach ($options['options'] as $option) {
50 $html .= html_writer::start_tag('li', array('class'=>'option'));
51 $option->attributes->name = 'answer';
52 $option->attributes->type = 'radio';
53
54 $labeltext = $option->text;
55 if (!empty($option->attributes->disabled)) {
56 $labeltext .= ' ' . get_string('full', 'choice');
57 $availableoption--;
58 }
59
60 $html .= html_writer::empty_tag('input', (array)$option->attributes);
61 $html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->name));
62 $html .= html_writer::end_tag('li');
63 }
64 $html .= html_writer::tag('li','', array('class'=>'clearfloat'));
65 $html .= html_writer::end_tag('ul');
66 $html .= html_writer::tag('div', '', array('class'=>'clearfloat'));
67 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
68 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
69
70 if (!empty($options['hascapability']) && ($options['hascapability'])) {
71 if ($availableoption < 1) {
72 $html .= html_writer::tag('label', get_string('choicefull', 'choice'));
73 } else {
74 $html .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('savemychoice','choice'), 'class'=>'button'));
75 }
76
77 if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
78 $url = new moodle_url('view.php', array('id'=>$coursemoduleid, 'action'=>'delchoice', 'sesskey'=>sesskey()));
79 $html .= html_writer::link($url, get_string('removemychoice','choice'));
80 }
81 } else {
82 $html .= html_writer::tag('label', get_string('havetologin', 'choice'));
83 }
84
85 $html .= html_writer::end_tag('ul');
86 $html .= html_writer::end_tag('form');
87
88 return $html;
89 }
90
91 /**
92 * Returns HTML to display choices result
93 * @param object $choices
94 * @param bool $forcepublish
95 * @return string
96 */
97 public function display_result($choices, $forcepublish = false) {
315d4971
RW
98 if (empty($forcepublish)) { //alow the publish setting to be overridden
99 $forcepublish = $choices->publish;
100 }
101
102 $displaylayout = $choices->display;
103
104 if ($forcepublish) { //CHOICE_PUBLISH_NAMES
105 return $this->display_publish_name_vertical($choices);
106 } else { //CHOICE_PUBLISH_ANONYMOUS';
107 if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) {
108 return $this->display_publish_anonymous_horizontal($choices);
109 }
110 return $this->display_publish_anonymous_vertical($choices);
111 }
112 }
113
114 /**
115 * Returns HTML to display choices result
116 * @param object $choices
117 * @param bool $forcepublish
118 * @return string
119 */
120 public function display_publish_name_vertical($choices) {
315d4971
RW
121 $html ='';
122 $html .= html_writer::tag('h2',format_string(get_string("responses", "choice")), array('class'=>'main'));
123
124 $attributes = array('method'=>'POST');
31d085e2 125 $attributes['action'] = new moodle_url('/mod/choice/view.php');
315d4971
RW
126 $attributes['id'] = 'attemptsform';
127
128 if ($choices->viewresponsecapability) {
129 $html .= html_writer::start_tag('form', $attributes);
130 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid));
131 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
132 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
133 }
134
135 $table = new html_table();
136 $table->cellpadding = 0;
137 $table->cellspacing = 0;
138 $table->attributes['class'] = 'results names ';
139 $table->tablealign = 'center';
140 $table->data = array();
141
142 $count = 0;
143 ksort($choices->options);
144
145 $columns = array();
146 foreach ($choices->options as $optionid => $options) {
147 $coldata = '';
148 if ($choices->showunanswered && $optionid == 0) {
149 $coldata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
150 } else if ($optionid > 0) {
151 $coldata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
152 }
153 $numberofuser = 0;
154 if (!empty($options->user) && count($options->user) > 0) {
155 $numberofuser = count($options->user);
156 }
157
158 $coldata .= html_writer::tag('div', ' ('.$numberofuser. ')', array('class'=>'numberofuser', 'title' => get_string('numberofuser', 'choice')));
159 $columns[] = $coldata;
160 }
161
162 $table->head = $columns;
163
164 $coldata = '';
165 $columns = array();
166 foreach ($choices->options as $optionid => $options) {
167 $coldata = '';
168 if ($choices->showunanswered || $optionid > 0) {
169 if (!empty($options->user)) {
170 foreach ($options->user as $user) {
171 $data = '';
172 if (empty($user->imagealt)){
173 $user->imagealt = '';
174 }
175
176 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability && $optionid > 0) {
177 $attemptaction = html_writer::checkbox('attemptid[]', $user->id,'');
178 $data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
179 }
31d085e2 180 $userimage = $this->output->user_picture($user, array('courseid'=>$choices->courseid));
315d4971
RW
181 $data .= html_writer::tag('div', $userimage, array('class'=>'image'));
182
31d085e2 183 $userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choices->courseid));
184 $name = html_writer::tag('a', fullname($user, $choices->fullnamecapability), array('href'=>$userlink, 'class'=>'username'));
315d4971
RW
185 $data .= html_writer::tag('div', $name, array('class'=>'fullname'));
186 $data .= html_writer::tag('div','', array('class'=>'clearfloat'));
187 $coldata .= html_writer::tag('div', $data, array('class'=>'user'));
188 }
189 }
190 }
191
192 $columns[] = $coldata;
193 $count++;
194 }
195
196 $table->data[] = $columns;
197 foreach ($columns as $d) {
198 $table->colclasses[] = 'data';
199 }
aad62468 200 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
315d4971
RW
201
202 $actiondata = '';
203 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
204 $selecturl = new moodle_url('#');
205
206 $selectallactions = new component_action('click',"select_all_in", array('div',null,'tablecontainer'));
207 $selectall = new action_link($selecturl, get_string('selectall', 'quiz'), $selectallactions);
31d085e2 208 $actiondata .= $this->output->render($selectall) . ' / ';
315d4971
RW
209
210 $deselectallactions = new component_action('click',"deselect_all_in", array('div',null,'tablecontainer'));
211 $deselectall = new action_link($selecturl, get_string('selectnone', 'quiz'), $deselectallactions);
31d085e2 212 $actiondata .= $this->output->render($deselectall);
315d4971
RW
213
214 $actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'quiz') . ' ', array('for'=>'menuaction'));
215
31d085e2 216 $actionurl = new moodle_url('/mod/choice/view.php', array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
315d4971
RW
217 $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('moveselectedusersto', 'choice')), 'attemptsform');
218
31d085e2 219 $actiondata .= $this->output->render($select);
315d4971
RW
220 }
221 $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
222
223 if ($choices->viewresponsecapability) {
224 $html .= html_writer::end_tag('form');
225 }
aad62468
RW
226
227 return $html;
315d4971
RW
228 }
229
230
231 /**
232 * Returns HTML to display choices result
233 * @param object $choices
234 * @return string
235 */
236 public function display_publish_anonymous_vertical($choices) {
31d085e2 237 global $CHOICE_COLUMN_HEIGHT;
315d4971
RW
238
239 $html = '';
240 $table = new html_table();
241 $table->cellpadding = 5;
242 $table->cellspacing = 0;
243 $table->attributes['class'] = 'results anonymous ';
244 $table->data = array();
245 $count = 0;
246 ksort($choices->options);
247 $columns = array();
248 $rows = array();
249
250 foreach ($choices->options as $optionid => $options) {
251 $numberofuser = 0;
252 if (!empty($options->user)) {
253 $numberofuser = count($options->user);
254 }
255 $height = 0;
256 $percentageamount = 0;
257 if($choices->numberofuser > 0) {
258 $height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser));
259 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
260 }
261
31d085e2 262 $displaydiagram = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output->pix_url('column', 'choice')));
315d4971
RW
263
264 $cell = new html_table_cell();
265 $cell->text = $displaydiagram;
266 $cell->attributes = array('class'=>'graph vertical data');
267 $columns[] = $cell;
268 }
269 $rowgraph = new html_table_row();
270 $rowgraph->cells = $columns;
271 $rows[] = $rowgraph;
272
273 $columns = array();
274 $printskiplink = true;
275 foreach ($choices->options as $optionid => $options) {
276 $columndata = '';
277 $numberofuser = 0;
278 if (!empty($options->user)) {
279 $numberofuser = count($options->user);
280 }
281
282 if ($printskiplink) {
283 $columndata .= html_writer::tag('div', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'));
284 $printskiplink = false;
285 }
286
287 if ($choices->showunanswered && $optionid == 0) {
288 $columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
289 } else if ($optionid > 0) {
290 $columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
291 }
292 $columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice')));
293
294 if($choices->numberofuser > 0) {
295 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
296 }
297 $columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
298
299 $cell = new html_table_cell();
300 $cell->text = $columndata;
301 $cell->attributes = array('class'=>'data header');
302 $columns[] = $cell;
303 }
304 $rowdata = new html_table_row();
305 $rowdata->cells = $columns;
306 $rows[] = $rowdata;
307
308 $table->data = $rows;
309
310 $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
311 $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
aad62468
RW
312 $html .= html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph', 'class'=>'skip-block'));
313 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
314
315d4971
RW
315 return $html;
316 }
317
318 /**
319 * Returns HTML to display choices result
320 * @param object $choices
321 * @return string
322 */
323 public function display_publish_anonymous_horizontal($choices) {
31d085e2 324 global $CHOICE_COLUMN_WIDTH;
315d4971
RW
325
326 $table = new html_table();
327 $table->cellpadding = 5;
328 $table->cellspacing = 0;
329 $table->attributes['class'] = 'results anonymous ';
330 $table->data = array();
331
332 $count = 0;
333 ksort($choices->options);
334
335 $rows = array();
336 foreach ($choices->options as $optionid => $options) {
337 $numberofuser = 0;
338 $graphcell = new html_table_cell();
339 if (!empty($options->user)) {
340 $numberofuser = count($options->user);
341 }
342
343 $width = 0;
344 $percentageamount = 0;
345 $columndata = '';
346 if($choices->numberofuser > 0) {
347 $width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser));
348 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
349 }
31d085e2 350 $displaydiagram = html_writer::tag('img','', array('style'=>'height:50px; width:'.$width.'px', 'alt'=>'', 'src'=>$this->output->pix_url('row', 'choice')));
315d4971
RW
351
352 $skiplink = html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph'. $optionid, 'class'=>'skip-block'));
353 $skiphandler = html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'.$optionid));
354
355 $graphcell->text = $skiplink . $displaydiagram . $skiphandler;
356 $graphcell->attributes = array('class'=>'graph horizontal');
357
358 $datacell = new html_table_cell();
359 if ($choices->showunanswered && $optionid == 0) {
360 $columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
361 } else if ($optionid > 0) {
362 $columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
363 }
364 $columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('title'=> get_string('numberofuser', 'choice'), 'class'=>'numberofuser'));
365
366 if($choices->numberofuser > 0) {
367 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
368 }
369 $columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
370
371 $datacell->text = $columndata;
372 $datacell->attributes = array('class'=>'header');
373
374 $row = new html_table_row();
375 $row->cells = array($datacell, $graphcell);
376 $rows[] = $row;
377 }
378
379 $table->data = $rows;
380
381 $html = '';
382 $header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
383 $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
384 $html .= html_writer::table($table);
385
386 return $html;
387 }
388}
389