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