Merge branch 'install_master' of https://git.in.moodle.com/amosbot/moodle-install
[moodle.git] / mod / choice / renderer.php
blob7cff337632b0ebc4e59fdb748dd1056c093786e7
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.
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
21 * @package mod_choice
22 * @copyright 2010 Rossiani Wijaya
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
25 class mod_choice_renderer extends plugin_renderer_base {
27 /**
28 * Returns HTML to display choices of option
29 * @param object $options
30 * @param int $coursemoduleid
31 * @param bool $vertical
32 * @return string
34 public function display_options($options, $coursemoduleid, $vertical = false, $multiple = false) {
35 $layoutclass = 'horizontal';
36 if ($vertical) {
37 $layoutclass = 'vertical';
39 $target = new moodle_url('/mod/choice/view.php');
40 $attributes = array('method'=>'POST', 'action'=>$target, 'class'=> $layoutclass);
41 $disabled = empty($options['previewonly']) ? array() : array('disabled' => 'disabled');
43 $html = html_writer::start_tag('form', $attributes);
44 $html .= html_writer::start_tag('ul', array('class' => 'choices list-unstyled unstyled'));
46 $availableoption = count($options['options']);
47 $choicecount = 0;
48 foreach ($options['options'] as $option) {
49 $choicecount++;
50 $html .= html_writer::start_tag('li', array('class' => 'option mr-3'));
51 if ($multiple) {
52 $option->attributes->name = 'answer[]';
53 $option->attributes->type = 'checkbox';
54 } else {
55 $option->attributes->name = 'answer';
56 $option->attributes->type = 'radio';
58 $option->attributes->id = 'choice_'.$choicecount;
59 $option->attributes->class = 'mx-1';
61 $labeltext = $option->text;
62 if (!empty($option->attributes->disabled)) {
63 $labeltext .= ' ' . get_string('full', 'choice');
64 $availableoption--;
67 if (!empty($options['limitanswers']) && !empty($options['showavailable'])) {
68 $labeltext .= html_writer::empty_tag('br');
69 $labeltext .= get_string("responsesa", "choice", $option->countanswers);
70 $labeltext .= html_writer::empty_tag('br');
71 $labeltext .= get_string("limita", "choice", $option->maxanswers);
74 $html .= html_writer::empty_tag('input', (array)$option->attributes + $disabled);
75 $html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->id));
76 $html .= html_writer::end_tag('li');
78 $html .= html_writer::tag('li','', array('class'=>'clearfloat'));
79 $html .= html_writer::end_tag('ul');
80 $html .= html_writer::tag('div', '', array('class'=>'clearfloat'));
81 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
82 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'makechoice'));
83 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
85 if (empty($options['previewonly'])) {
86 if (!empty($options['hascapability']) && ($options['hascapability'])) {
87 if ($availableoption < 1) {
88 $html .= html_writer::tag('label', get_string('choicefull', 'choice'));
89 } else {
90 $html .= html_writer::empty_tag('input', array(
91 'type' => 'submit',
92 'value' => get_string('savemychoice', 'choice'),
93 'class' => 'btn btn-primary'
94 ));
97 if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
98 $url = new moodle_url('view.php',
99 array('id' => $coursemoduleid, 'action' => 'delchoice', 'sesskey' => sesskey()));
100 $html .= html_writer::link($url, get_string('removemychoice', 'choice'), array('class' => 'ml-1'));
102 } else {
103 $html .= html_writer::tag('label', get_string('havetologin', 'choice'));
107 $html .= html_writer::end_tag('ul');
108 $html .= html_writer::end_tag('form');
110 return $html;
114 * Returns HTML to display choices result
115 * @param object $choices
116 * @param bool $forcepublish
117 * @return string
119 public function display_result($choices, $forcepublish = false) {
120 if (empty($forcepublish)) { //allow the publish setting to be overridden
121 $forcepublish = $choices->publish;
124 $displaylayout = $choices->display;
126 if ($forcepublish) { //CHOICE_PUBLISH_NAMES
127 return $this->display_publish_name_vertical($choices);
128 } else {
129 return $this->display_publish_anonymous($choices, $displaylayout);
134 * Returns HTML to display choices result
135 * @param object $choices
136 * @return string
138 public function display_publish_name_vertical($choices) {
139 $html ='';
141 $attributes = array('method'=>'POST');
142 $attributes['action'] = new moodle_url($this->page->url);
143 $attributes['id'] = 'attemptsform';
145 if ($choices->viewresponsecapability) {
146 $html .= html_writer::start_tag('form', $attributes);
147 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid));
148 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
149 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
152 $table = new html_table();
153 $table->cellpadding = 0;
154 $table->cellspacing = 0;
155 $table->attributes['class'] = 'results names table table-bordered';
156 $table->tablealign = 'center';
157 $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
158 $table->data = array();
160 $count = 0;
161 ksort($choices->options);
163 $columns = array();
164 $celldefault = new html_table_cell();
165 $celldefault->attributes['class'] = 'data';
167 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
168 $accessiblecell = new html_table_cell();
169 $accessiblecell->scope = 'row';
170 $accessiblecell->text = get_string('choiceoptions', 'choice');
171 $columns['options'][] = $accessiblecell;
173 $usernumberheader = clone($celldefault);
174 $usernumberheader->header = true;
175 $usernumberheader->attributes['class'] = 'header data';
176 $usernumberheader->text = get_string('numberofuser', 'choice');
177 $columns['usernumber'][] = $usernumberheader;
179 $optionsnames = [];
180 foreach ($choices->options as $optionid => $options) {
181 $celloption = clone($celldefault);
182 $cellusernumber = clone($celldefault);
184 if ($choices->showunanswered && $optionid == 0) {
185 $headertitle = get_string('notanswered', 'choice');
186 } else if ($optionid > 0) {
187 $headertitle = format_string($choices->options[$optionid]->text);
188 if (!empty($choices->options[$optionid]->user) && count($choices->options[$optionid]->user) > 0) {
189 if (
190 $choices->limitanswers &&
191 (count($choices->options[$optionid]->user) == $choices->options[$optionid]->maxanswer)
193 $headertitle .= ' ' . get_string('full', 'choice');
197 $celltext = $headertitle;
199 // Render select/deselect all checkbox for this option.
200 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
202 // Build the select/deselect all for this option.
203 $selectallid = 'select-response-option-' . $optionid;
204 $togglegroup = 'responses response-option-' . $optionid;
205 $selectalltext = get_string('selectalloption', 'choice', $headertitle);
206 $deselectalltext = get_string('deselectalloption', 'choice', $headertitle);
207 $mastercheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [
208 'id' => $selectallid,
209 'name' => $selectallid,
210 'value' => 1,
211 'selectall' => $selectalltext,
212 'deselectall' => $deselectalltext,
213 'label' => $selectalltext,
214 'labelclasses' => 'accesshide',
217 $celltext .= html_writer::div($this->output->render($mastercheckbox));
219 $numberofuser = 0;
220 if (!empty($options->user) && count($options->user) > 0) {
221 $numberofuser = count($options->user);
223 if (($choices->limitanswers) && ($choices->showavailable)) {
224 $numberofuser .= html_writer::empty_tag('br');
225 $numberofuser .= get_string("limita", "choice", $options->maxanswer);
227 $celloption->text = html_writer::div($celltext, 'text-center');
228 $optionsnames[$optionid] = $celltext;
229 $cellusernumber->text = html_writer::div($numberofuser, 'text-center');
231 $columns['options'][] = $celloption;
232 $columns['usernumber'][] = $cellusernumber;
235 $table->head = $columns['options'];
236 $table->data[] = new html_table_row($columns['usernumber']);
238 $columns = array();
240 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
241 $accessiblecell = new html_table_cell();
242 $accessiblecell->text = get_string('userchoosethisoption', 'choice');
243 $accessiblecell->header = true;
244 $accessiblecell->scope = 'row';
245 $accessiblecell->attributes['class'] = 'header data';
246 $columns[] = $accessiblecell;
248 foreach ($choices->options as $optionid => $options) {
249 $cell = new html_table_cell();
250 $cell->attributes['class'] = 'data';
252 if ($choices->showunanswered || $optionid > 0) {
253 if (!empty($options->user)) {
254 $optionusers = '';
255 foreach ($options->user as $user) {
256 $data = '';
257 if (empty($user->imagealt)) {
258 $user->imagealt = '';
261 $userfullname = fullname($user, $choices->fullnamecapability);
262 $checkbox = '';
263 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
264 $checkboxid = 'attempt-user' . $user->id . '-option' . $optionid;
265 if ($optionid > 0) {
266 $checkboxname = 'attemptid[]';
267 $checkboxvalue = $user->answerid;
268 } else {
269 $checkboxname = 'userid[]';
270 $checkboxvalue = $user->id;
273 $togglegroup = 'responses response-option-' . $optionid;
274 $slavecheckbox = new \core\output\checkbox_toggleall($togglegroup, false, [
275 'id' => $checkboxid,
276 'name' => $checkboxname,
277 'classes' => 'mr-1',
278 'value' => $checkboxvalue,
279 'label' => $userfullname . ' ' . $options->text,
280 'labelclasses' => 'accesshide',
282 $checkbox = $this->output->render($slavecheckbox);
284 $userimage = $this->output->user_picture($user, array('courseid' => $choices->courseid, 'link' => false));
285 $profileurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $choices->courseid));
286 $profilelink = html_writer::link($profileurl, $userimage . $userfullname);
287 $data .= html_writer::div($checkbox . $profilelink, 'mb-1');
289 $optionusers .= $data;
291 $cell->text = $optionusers;
294 $columns[] = $cell;
295 $count++;
297 $row = new html_table_row($columns);
298 $table->data[] = $row;
300 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
302 $actiondata = '';
303 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
304 // Build the select/deselect all for all of options.
305 $selectallid = 'select-all-responses';
306 $togglegroup = 'responses';
307 $selectallcheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [
308 'id' => $selectallid,
309 'name' => $selectallid,
310 'value' => 1,
311 'label' => get_string('selectall'),
312 'classes' => 'btn-secondary mr-1'
313 ], true);
314 $actiondata .= $this->output->render($selectallcheckbox);
316 $actionurl = new moodle_url($this->page->url,
317 ['sesskey' => sesskey(), 'action' => 'delete_confirmation()']);
318 $actionoptions = array('delete' => get_string('delete'));
319 foreach ($choices->options as $optionid => $option) {
320 if ($optionid > 0) {
321 $actionoptions['choose_'.$optionid] = get_string('chooseoption', 'choice', $option->text);
324 $selectattributes = [
325 'data-action' => 'toggle',
326 'data-togglegroup' => 'responses',
327 'data-toggle' => 'action',
329 $selectnothing = ['' => get_string('chooseaction', 'choice')];
330 $select = new single_select($actionurl, 'action', $actionoptions, null, $selectnothing, 'attemptsform');
331 $select->set_label(get_string('withselected', 'choice'));
332 $select->disabled = true;
333 $select->attributes = $selectattributes;
335 $actiondata .= $this->output->render($select);
337 $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
339 if ($choices->viewresponsecapability) {
340 $html .= html_writer::end_tag('form');
343 return $html;
348 * Returns HTML to display choices result
349 * @deprecated since 3.2
350 * @param object $choices
351 * @return string
353 public function display_publish_anonymous_horizontal($choices) {
354 debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.',
355 DEBUG_DEVELOPER);
356 return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_VERTICAL);
360 * Returns HTML to display choices result
361 * @deprecated since 3.2
362 * @param object $choices
363 * @return string
365 public function display_publish_anonymous_vertical($choices) {
366 debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.',
367 DEBUG_DEVELOPER);
368 return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_HORIZONTAL);
372 * Generate the choice result chart.
374 * Can be displayed either in the vertical or horizontal position.
376 * @param stdClass $choices Choices responses object.
377 * @param int $displaylayout The constants CHOICE_DISPLAY_HORIZONTAL or CHOICE_DISPLAY_VERTICAL.
378 * @return string the rendered chart.
380 public function display_publish_anonymous($choices, $displaylayout) {
381 $count = 0;
382 $data = [];
383 $numberofuser = 0;
384 $percentageamount = 0;
385 foreach ($choices->options as $optionid => $option) {
386 if (!empty($option->user)) {
387 $numberofuser = count($option->user);
389 if($choices->numberofuser > 0) {
390 $percentageamount = ((float)$numberofuser / (float)$choices->numberofuser) * 100.0;
392 $data['labels'][$count] = $option->text;
393 $data['series'][$count] = $numberofuser;
394 $data['series_labels'][$count] = $numberofuser . ' (' . format_float($percentageamount, 1) . '%)';
395 $count++;
396 $numberofuser = 0;
399 $chart = new \core\chart_bar();
400 if ($displaylayout == CHOICE_DISPLAY_VERTICAL) {
401 $chart->set_horizontal(true); // Horizontal bars when choices are vertical.
403 $series = new \core\chart_series(format_string(get_string("responses", "choice")), $data['series']);
404 $series->set_labels($data['series_labels']);
405 $chart->add_series($series);
406 $chart->set_labels($data['labels']);
407 $yaxis = $chart->get_yaxis(0, true);
408 $yaxis->set_stepsize(max(1, round(max($data['series']) / 10)));
409 return $this->output->render($chart);