3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Moodle renderer used to display special elements of the lesson module
22 * @copyright 2010 Rossiani Wijaya
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define ('DISPLAY_HORIZONTAL_LAYOUT', 0);
26 define ('DISPLAY_VERTICAL_LAYOUT', 1);
28 class mod_choice_renderer
extends plugin_renderer_base
{
31 * Returns HTML to display choices of option
32 * @param object $options
33 * @param int $coursemoduleid
34 * @param bool $vertical
37 public function display_options($options, $coursemoduleid, $vertical = false, $multiple = false) {
38 $layoutclass = 'horizontal';
40 $layoutclass = 'vertical';
42 $target = new moodle_url('/mod/choice/view.php');
43 $attributes = array('method'=>'POST', 'action'=>$target, 'class'=> $layoutclass);
44 $disabled = empty($options['previewonly']) ?
array() : array('disabled' => 'disabled');
46 $html = html_writer
::start_tag('form', $attributes);
47 $html .= html_writer
::start_tag('ul', array('class' => 'choices list-unstyled unstyled'));
49 $availableoption = count($options['options']);
51 foreach ($options['options'] as $option) {
53 $html .= html_writer
::start_tag('li', array('class' => 'option mr-3'));
55 $option->attributes
->name
= 'answer[]';
56 $option->attributes
->type
= 'checkbox';
58 $option->attributes
->name
= 'answer';
59 $option->attributes
->type
= 'radio';
61 $option->attributes
->id
= 'choice_'.$choicecount;
62 $option->attributes
->class = 'mx-1';
64 $labeltext = $option->text
;
65 if (!empty($option->attributes
->disabled
)) {
66 $labeltext .= ' ' . get_string('full', 'choice');
70 if (!empty($options['limitanswers']) && !empty($options['showavailable'])) {
71 $labeltext .= html_writer
::empty_tag('br');
72 $labeltext .= get_string("responsesa", "choice", $option->countanswers
);
73 $labeltext .= html_writer
::empty_tag('br');
74 $labeltext .= get_string("limita", "choice", $option->maxanswers
);
77 $html .= html_writer
::empty_tag('input', (array)$option->attributes +
$disabled);
78 $html .= html_writer
::tag('label', $labeltext, array('for'=>$option->attributes
->id
));
79 $html .= html_writer
::end_tag('li');
81 $html .= html_writer
::tag('li','', array('class'=>'clearfloat'));
82 $html .= html_writer
::end_tag('ul');
83 $html .= html_writer
::tag('div', '', array('class'=>'clearfloat'));
84 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
85 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'makechoice'));
86 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
88 if (empty($options['previewonly'])) {
89 if (!empty($options['hascapability']) && ($options['hascapability'])) {
90 if ($availableoption < 1) {
91 $html .= html_writer
::tag('label', get_string('choicefull', 'choice'));
93 $html .= html_writer
::empty_tag('input', array(
95 'value' => get_string('savemychoice', 'choice'),
96 'class' => 'btn btn-primary'
100 if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
101 $url = new moodle_url('view.php',
102 array('id' => $coursemoduleid, 'action' => 'delchoice', 'sesskey' => sesskey()));
103 $html .= html_writer
::link($url, get_string('removemychoice', 'choice'), array('class' => 'ml-1'));
106 $html .= html_writer
::tag('label', get_string('havetologin', 'choice'));
110 $html .= html_writer
::end_tag('ul');
111 $html .= html_writer
::end_tag('form');
117 * Returns HTML to display choices result
118 * @param object $choices
119 * @param bool $forcepublish
122 public function display_result($choices, $forcepublish = false) {
123 if (empty($forcepublish)) { //allow the publish setting to be overridden
124 $forcepublish = $choices->publish
;
127 $displaylayout = $choices->display
;
129 if ($forcepublish) { //CHOICE_PUBLISH_NAMES
130 return $this->display_publish_name_vertical($choices);
132 return $this->display_publish_anonymous($choices, $displaylayout);
137 * Returns HTML to display choices result
138 * @param object $choices
141 public function display_publish_name_vertical($choices) {
143 $html .= html_writer
::tag('h3',format_string(get_string("responses", "choice")));
145 $attributes = array('method'=>'POST');
146 $attributes['action'] = new moodle_url($this->page
->url
);
147 $attributes['id'] = 'attemptsform';
149 if ($choices->viewresponsecapability
) {
150 $html .= html_writer
::start_tag('form', $attributes);
151 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid
));
152 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
153 $html .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
156 $table = new html_table();
157 $table->cellpadding
= 0;
158 $table->cellspacing
= 0;
159 $table->attributes
['class'] = 'results names table table-bordered';
160 $table->tablealign
= 'center';
161 $table->summary
= get_string('responsesto', 'choice', format_string($choices->name
));
162 $table->data
= array();
165 ksort($choices->options
);
168 $celldefault = new html_table_cell();
169 $celldefault->attributes
['class'] = 'data';
171 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
172 $accessiblecell = new html_table_cell();
173 $accessiblecell->scope
= 'row';
174 $accessiblecell->text
= get_string('choiceoptions', 'choice');
175 $columns['options'][] = $accessiblecell;
177 $usernumberheader = clone($celldefault);
178 $usernumberheader->header
= true;
179 $usernumberheader->attributes
['class'] = 'header data';
180 $usernumberheader->text
= get_string('numberofuser', 'choice');
181 $columns['usernumber'][] = $usernumberheader;
184 foreach ($choices->options
as $optionid => $options) {
185 $celloption = clone($celldefault);
186 $cellusernumber = clone($celldefault);
188 if ($choices->showunanswered
&& $optionid == 0) {
189 $headertitle = get_string('notanswered', 'choice');
190 } else if ($optionid > 0) {
191 $headertitle = format_string($choices->options
[$optionid]->text
);
192 if (!empty($choices->options
[$optionid]->user
) && count($choices->options
[$optionid]->user
) > 0) {
193 if ((count($choices->options
[$optionid]->user
)) == ($choices->options
[$optionid]->maxanswer
)) {
194 $headertitle .= ' ' . get_string('full', 'choice');
198 $celltext = $headertitle;
200 // Render select/deselect all checkbox for this option.
201 if ($choices->viewresponsecapability
&& $choices->deleterepsonsecapability
) {
203 // Build the select/deselect all for this option.
204 $selectallid = 'select-response-option-' . $optionid;
205 $togglegroup = 'responses response-option-' . $optionid;
206 $selectalltext = get_string('selectalloption', 'choice', $headertitle);
207 $deselectalltext = get_string('deselectalloption', 'choice', $headertitle);
208 $mastercheckbox = new \core\output\
checkbox_toggleall($togglegroup, true, [
209 'id' => $selectallid,
210 'name' => $selectallid,
212 'selectall' => $selectalltext,
213 'deselectall' => $deselectalltext,
214 'label' => $selectalltext,
215 'labelclasses' => 'accesshide',
218 $celltext .= html_writer
::div($this->output
->render($mastercheckbox));
221 if (!empty($options->user
) && count($options->user
) > 0) {
222 $numberofuser = count($options->user
);
224 if (($choices->limitanswers
) && ($choices->showavailable
)) {
225 $numberofuser .= html_writer
::empty_tag('br');
226 $numberofuser .= get_string("limita", "choice", $options->maxanswer
);
228 $celloption->text
= html_writer
::div($celltext, 'text-center');
229 $optionsnames[$optionid] = $celltext;
230 $cellusernumber->text
= html_writer
::div($numberofuser, 'text-center');
232 $columns['options'][] = $celloption;
233 $columns['usernumber'][] = $cellusernumber;
236 $table->head
= $columns['options'];
237 $table->data
[] = new html_table_row($columns['usernumber']);
241 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
242 $accessiblecell = new html_table_cell();
243 $accessiblecell->text
= get_string('userchoosethisoption', 'choice');
244 $accessiblecell->header
= true;
245 $accessiblecell->scope
= 'row';
246 $accessiblecell->attributes
['class'] = 'header data';
247 $columns[] = $accessiblecell;
249 foreach ($choices->options
as $optionid => $options) {
250 $cell = new html_table_cell();
251 $cell->attributes
['class'] = 'data';
253 if ($choices->showunanswered ||
$optionid > 0) {
254 if (!empty($options->user
)) {
256 foreach ($options->user
as $user) {
258 if (empty($user->imagealt
)) {
259 $user->imagealt
= '';
262 $userfullname = fullname($user, $choices->fullnamecapability
);
264 if ($choices->viewresponsecapability
&& $choices->deleterepsonsecapability
) {
265 $checkboxid = 'attempt-user' . $user->id
. '-option' . $optionid;
267 $checkboxname = 'attemptid[]';
268 $checkboxvalue = $user->answerid
;
270 $checkboxname = 'userid[]';
271 $checkboxvalue = $user->id
;
274 $togglegroup = 'responses response-option-' . $optionid;
275 $slavecheckbox = new \core\output\
checkbox_toggleall($togglegroup, false, [
277 'name' => $checkboxname,
279 'value' => $checkboxvalue,
280 'label' => $userfullname . ' ' . $options->text
,
281 'labelclasses' => 'accesshide',
283 $checkbox = $this->output
->render($slavecheckbox);
285 $userimage = $this->output
->user_picture($user, array('courseid' => $choices->courseid
, 'link' => false));
286 $profileurl = new moodle_url('/user/view.php', array('id' => $user->id
, 'course' => $choices->courseid
));
287 $profilelink = html_writer
::link($profileurl, $userimage . $userfullname);
288 $data .= html_writer
::div($checkbox . $profilelink, 'mb-1');
290 $optionusers .= $data;
292 $cell->text
= $optionusers;
298 $row = new html_table_row($columns);
299 $table->data
[] = $row;
301 $html .= html_writer
::tag('div', html_writer
::table($table), array('class'=>'response'));
304 if ($choices->viewresponsecapability
&& $choices->deleterepsonsecapability
) {
305 // Build the select/deselect all for all of options.
306 $selectallid = 'select-all-responses';
307 $togglegroup = 'responses';
308 $selectallcheckbox = new \core\output\
checkbox_toggleall($togglegroup, true, [
309 'id' => $selectallid,
310 'name' => $selectallid,
312 'label' => get_string('selectall'),
313 'classes' => 'btn-secondary mr-1'
315 $actiondata .= $this->output
->render($selectallcheckbox);
317 $actionurl = new moodle_url($this->page
->url
,
318 ['sesskey' => sesskey(), 'action' => 'delete_confirmation()']);
319 $actionoptions = array('delete' => get_string('delete'));
320 foreach ($choices->options
as $optionid => $option) {
322 $actionoptions['choose_'.$optionid] = get_string('chooseoption', 'choice', $option->text
);
325 $selectattributes = [
326 'data-action' => 'toggle',
327 'data-togglegroup' => 'responses',
328 'data-toggle' => 'action',
330 $selectnothing = ['' => get_string('chooseaction', 'choice')];
331 $select = new single_select($actionurl, 'action', $actionoptions, null, $selectnothing, 'attemptsform');
332 $select->set_label(get_string('withselected', 'choice'));
333 $select->disabled
= true;
334 $select->attributes
= $selectattributes;
336 $actiondata .= $this->output
->render($select);
338 $html .= html_writer
::tag('div', $actiondata, array('class'=>'responseaction'));
340 if ($choices->viewresponsecapability
) {
341 $html .= html_writer
::end_tag('form');
349 * Returns HTML to display choices result
350 * @deprecated since 3.2
351 * @param object $choices
354 public function display_publish_anonymous_horizontal($choices) {
355 debugging(__FUNCTION__
.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.',
357 return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_VERTICAL
);
361 * Returns HTML to display choices result
362 * @deprecated since 3.2
363 * @param object $choices
366 public function display_publish_anonymous_vertical($choices) {
367 debugging(__FUNCTION__
.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.',
369 return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_HORIZONTAL
);
373 * Generate the choice result chart.
375 * Can be displayed either in the vertical or horizontal position.
377 * @param stdClass $choices Choices responses object.
378 * @param int $displaylayout The constants DISPLAY_HORIZONTAL_LAYOUT or DISPLAY_VERTICAL_LAYOUT.
379 * @return string the rendered chart.
381 public function display_publish_anonymous($choices, $displaylayout) {
385 $percentageamount = 0;
386 foreach ($choices->options
as $optionid => $option) {
387 if (!empty($option->user
)) {
388 $numberofuser = count($option->user
);
390 if($choices->numberofuser
> 0) {
391 $percentageamount = ((float)$numberofuser / (float)$choices->numberofuser
) * 100.0;
393 $data['labels'][$count] = $option->text
;
394 $data['series'][$count] = $numberofuser;
395 $data['series_labels'][$count] = $numberofuser . ' (' . format_float($percentageamount, 1) . '%)';
400 $chart = new \core\
chart_bar();
401 if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT
) {
402 $chart->set_horizontal(true);
404 $series = new \core\
chart_series(format_string(get_string("responses", "choice")), $data['series']);
405 $series->set_labels($data['series_labels']);
406 $chart->add_series($series);
407 $chart->set_labels($data['labels']);
408 $yaxis = $chart->get_yaxis(0, true);
409 $yaxis->set_stepsize(max(1, round(max($data['series']) / 10)));
410 return $this->output
->render($chart);