See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 mod_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); 27 28 class 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, $multiple = 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); 44 $disabled = empty($options['previewonly']) ? array() : array('disabled' => 'disabled'); 45 46 $html = html_writer::start_tag('form', $attributes); 47 $html .= html_writer::start_tag('ul', array('class' => 'choices list-unstyled unstyled')); 48 49 $availableoption = count($options['options']); 50 $choicecount = 0; 51 foreach ($options['options'] as $option) { 52 $choicecount++; 53 $html .= html_writer::start_tag('li', array('class'=>'option')); 54 if ($multiple) { 55 $option->attributes->name = 'answer[]'; 56 $option->attributes->type = 'checkbox'; 57 } else { 58 $option->attributes->name = 'answer'; 59 $option->attributes->type = 'radio'; 60 } 61 $option->attributes->id = 'choice_'.$choicecount; 62 $option->attributes->class = 'mx-1'; 63 64 $labeltext = $option->text; 65 if (!empty($option->attributes->disabled)) { 66 $labeltext .= ' ' . get_string('full', 'choice'); 67 $availableoption--; 68 } 69 70 $html .= html_writer::empty_tag('input', (array)$option->attributes + $disabled); 71 $html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->id)); 72 $html .= html_writer::end_tag('li'); 73 } 74 $html .= html_writer::tag('li','', array('class'=>'clearfloat')); 75 $html .= html_writer::end_tag('ul'); 76 $html .= html_writer::tag('div', '', array('class'=>'clearfloat')); 77 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey())); 78 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'makechoice')); 79 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid)); 80 81 if (empty($options['previewonly'])) { 82 if (!empty($options['hascapability']) && ($options['hascapability'])) { 83 if ($availableoption < 1) { 84 $html .= html_writer::tag('label', get_string('choicefull', 'choice')); 85 } else { 86 $html .= html_writer::empty_tag('input', array( 87 'type' => 'submit', 88 'value' => get_string('savemychoice', 'choice'), 89 'class' => 'btn btn-primary' 90 )); 91 } 92 93 if (!empty($options['allowupdate']) && ($options['allowupdate'])) { 94 $url = new moodle_url('view.php', 95 array('id' => $coursemoduleid, 'action' => 'delchoice', 'sesskey' => sesskey())); 96 $html .= html_writer::link($url, get_string('removemychoice', 'choice'), array('class' => 'ml-1')); 97 } 98 } else { 99 $html .= html_writer::tag('label', get_string('havetologin', 'choice')); 100 } 101 } 102 103 $html .= html_writer::end_tag('ul'); 104 $html .= html_writer::end_tag('form'); 105 106 return $html; 107 } 108 109 /** 110 * Returns HTML to display choices result 111 * @param object $choices 112 * @param bool $forcepublish 113 * @return string 114 */ 115 public function display_result($choices, $forcepublish = false) { 116 if (empty($forcepublish)) { //allow the publish setting to be overridden 117 $forcepublish = $choices->publish; 118 } 119 120 $displaylayout = $choices->display; 121 122 if ($forcepublish) { //CHOICE_PUBLISH_NAMES 123 return $this->display_publish_name_vertical($choices); 124 } else { 125 return $this->display_publish_anonymous($choices, $displaylayout); 126 } 127 } 128 129 /** 130 * Returns HTML to display choices result 131 * @param object $choices 132 * @return string 133 */ 134 public function display_publish_name_vertical($choices) { 135 $html =''; 136 $html .= html_writer::tag('h3',format_string(get_string("responses", "choice"))); 137 138 $attributes = array('method'=>'POST'); 139 $attributes['action'] = new moodle_url($this->page->url); 140 $attributes['id'] = 'attemptsform'; 141 142 if ($choices->viewresponsecapability) { 143 $html .= html_writer::start_tag('form', $attributes); 144 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid)); 145 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey())); 146 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview')); 147 } 148 149 $table = new html_table(); 150 $table->cellpadding = 0; 151 $table->cellspacing = 0; 152 $table->attributes['class'] = 'results names table table-bordered'; 153 $table->tablealign = 'center'; 154 $table->summary = get_string('responsesto', 'choice', format_string($choices->name)); 155 $table->data = array(); 156 157 $count = 0; 158 ksort($choices->options); 159 160 $columns = array(); 161 $celldefault = new html_table_cell(); 162 $celldefault->attributes['class'] = 'data'; 163 164 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816 165 $accessiblecell = new html_table_cell(); 166 $accessiblecell->scope = 'row'; 167 $accessiblecell->text = get_string('choiceoptions', 'choice'); 168 $columns['options'][] = $accessiblecell; 169 170 $usernumberheader = clone($celldefault); 171 $usernumberheader->header = true; 172 $usernumberheader->attributes['class'] = 'header data'; 173 $usernumberheader->text = get_string('numberofuser', 'choice'); 174 $columns['usernumber'][] = $usernumberheader; 175 176 $optionsnames = []; 177 foreach ($choices->options as $optionid => $options) { 178 $celloption = clone($celldefault); 179 $cellusernumber = clone($celldefault); 180 181 if ($choices->showunanswered && $optionid == 0) { 182 $headertitle = get_string('notanswered', 'choice'); 183 } else if ($optionid > 0) { 184 $headertitle = format_string($choices->options[$optionid]->text); 185 } 186 $celltext = $headertitle; 187 188 // Render select/deselect all checkbox for this option. 189 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) { 190 191 // Build the select/deselect all for this option. 192 $selectallid = 'select-response-option-' . $optionid; 193 $togglegroup = 'responses response-option-' . $optionid; 194 $selectalltext = get_string('selectalloption', 'choice', $headertitle); 195 $deselectalltext = get_string('deselectalloption', 'choice', $headertitle); 196 $mastercheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [ 197 'id' => $selectallid, 198 'name' => $selectallid, 199 'value' => 1, 200 'selectall' => $selectalltext, 201 'deselectall' => $deselectalltext, 202 'label' => $selectalltext, 203 'labelclasses' => 'accesshide', 204 ]); 205 206 $celltext .= html_writer::div($this->output->render($mastercheckbox)); 207 } 208 $numberofuser = 0; 209 if (!empty($options->user) && count($options->user) > 0) { 210 $numberofuser = count($options->user); 211 } 212 213 $celloption->text = html_writer::div($celltext, 'text-center'); 214 $optionsnames[$optionid] = $celltext; 215 $cellusernumber->text = html_writer::div($numberofuser, 'text-center'); 216 217 $columns['options'][] = $celloption; 218 $columns['usernumber'][] = $cellusernumber; 219 } 220 221 $table->head = $columns['options']; 222 $table->data[] = new html_table_row($columns['usernumber']); 223 224 $columns = array(); 225 226 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816 227 $accessiblecell = new html_table_cell(); 228 $accessiblecell->text = get_string('userchoosethisoption', 'choice'); 229 $accessiblecell->header = true; 230 $accessiblecell->scope = 'row'; 231 $accessiblecell->attributes['class'] = 'header data'; 232 $columns[] = $accessiblecell; 233 234 foreach ($choices->options as $optionid => $options) { 235 $cell = new html_table_cell(); 236 $cell->attributes['class'] = 'data'; 237 238 if ($choices->showunanswered || $optionid > 0) { 239 if (!empty($options->user)) { 240 $optionusers = ''; 241 foreach ($options->user as $user) { 242 $data = ''; 243 if (empty($user->imagealt)) { 244 $user->imagealt = ''; 245 } 246 247 $userfullname = fullname($user, $choices->fullnamecapability); 248 $checkbox = ''; 249 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) { 250 $checkboxid = 'attempt-user' . $user->id . '-option' . $optionid; 251 if ($optionid > 0) { 252 $checkboxname = 'attemptid[]'; 253 $checkboxvalue = $user->answerid; 254 } else { 255 $checkboxname = 'userid[]'; 256 $checkboxvalue = $user->id; 257 } 258 259 $togglegroup = 'responses response-option-' . $optionid; 260 $slavecheckbox = new \core\output\checkbox_toggleall($togglegroup, false, [ 261 'id' => $checkboxid, 262 'name' => $checkboxname, 263 'classes' => 'mr-1', 264 'value' => $checkboxvalue, 265 'label' => $userfullname . ' ' . $options->text, 266 'labelclasses' => 'accesshide', 267 ]); 268 $checkbox = $this->output->render($slavecheckbox); 269 } 270 $userimage = $this->output->user_picture($user, array('courseid' => $choices->courseid, 'link' => false)); 271 $profileurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $choices->courseid)); 272 $profilelink = html_writer::link($profileurl, $userimage . $userfullname); 273 $data .= html_writer::div($checkbox . $profilelink, 'mb-1'); 274 275 $optionusers .= $data; 276 } 277 $cell->text = $optionusers; 278 } 279 } 280 $columns[] = $cell; 281 $count++; 282 } 283 $row = new html_table_row($columns); 284 $table->data[] = $row; 285 286 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response')); 287 288 $actiondata = ''; 289 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) { 290 // Build the select/deselect all for all of options. 291 $selectallid = 'select-all-responses'; 292 $togglegroup = 'responses'; 293 $selectallcheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [ 294 'id' => $selectallid, 295 'name' => $selectallid, 296 'value' => 1, 297 'label' => get_string('selectall'), 298 'classes' => 'btn-secondary mr-1' 299 ], true); 300 $actiondata .= $this->output->render($selectallcheckbox); 301 302 $actionurl = new moodle_url($this->page->url, 303 ['sesskey' => sesskey(), 'action' => 'delete_confirmation()']); 304 $actionoptions = array('delete' => get_string('delete')); 305 foreach ($choices->options as $optionid => $option) { 306 if ($optionid > 0) { 307 $actionoptions['choose_'.$optionid] = get_string('chooseoption', 'choice', $option->text); 308 } 309 } 310 $selectattributes = [ 311 'data-action' => 'toggle', 312 'data-togglegroup' => 'responses', 313 'data-toggle' => 'action', 314 ]; 315 $selectnothing = ['' => get_string('chooseaction', 'choice')]; 316 $select = new single_select($actionurl, 'action', $actionoptions, null, $selectnothing, 'attemptsform'); 317 $select->set_label(get_string('withselected', 'choice')); 318 $select->disabled = true; 319 $select->attributes = $selectattributes; 320 321 $actiondata .= $this->output->render($select); 322 } 323 $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction')); 324 325 if ($choices->viewresponsecapability) { 326 $html .= html_writer::end_tag('form'); 327 } 328 329 return $html; 330 } 331 332 333 /** 334 * Returns HTML to display choices result 335 * @deprecated since 3.2 336 * @param object $choices 337 * @return string 338 */ 339 public function display_publish_anonymous_horizontal($choices) { 340 debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.', 341 DEBUG_DEVELOPER); 342 return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_VERTICAL); 343 } 344 345 /** 346 * Returns HTML to display choices result 347 * @deprecated since 3.2 348 * @param object $choices 349 * @return string 350 */ 351 public function display_publish_anonymous_vertical($choices) { 352 debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.', 353 DEBUG_DEVELOPER); 354 return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_HORIZONTAL); 355 } 356 357 /** 358 * Generate the choice result chart. 359 * 360 * Can be displayed either in the vertical or horizontal position. 361 * 362 * @param stdClass $choices Choices responses object. 363 * @param int $displaylayout The constants DISPLAY_HORIZONTAL_LAYOUT or DISPLAY_VERTICAL_LAYOUT. 364 * @return string the rendered chart. 365 */ 366 public function display_publish_anonymous($choices, $displaylayout) { 367 $count = 0; 368 $data = []; 369 $numberofuser = 0; 370 $percentageamount = 0; 371 foreach ($choices->options as $optionid => $option) { 372 if (!empty($option->user)) { 373 $numberofuser = count($option->user); 374 } 375 if($choices->numberofuser > 0) { 376 $percentageamount = ((float)$numberofuser / (float)$choices->numberofuser) * 100.0; 377 } 378 $data['labels'][$count] = $option->text; 379 $data['series'][$count] = $numberofuser; 380 $data['series_labels'][$count] = $numberofuser . ' (' . format_float($percentageamount, 1) . '%)'; 381 $count++; 382 $numberofuser = 0; 383 } 384 385 $chart = new \core\chart_bar(); 386 if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) { 387 $chart->set_horizontal(true); 388 } 389 $series = new \core\chart_series(format_string(get_string("responses", "choice")), $data['series']); 390 $series->set_labels($data['series_labels']); 391 $chart->add_series($series); 392 $chart->set_labels($data['labels']); 393 $yaxis = $chart->get_yaxis(0, true); 394 $yaxis->set_stepsize(max(1, round(max($data['series']) / 10))); 395 return $this->output->render($chart); 396 } 397 } 398
title
Description
Body
title
Description
Body
title
Description
Body
title
Body