Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400]
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 141 $attributes = array('method'=>'POST'); 142 $attributes['action'] = new moodle_url($this->page->url); 143 $attributes['id'] = 'attemptsform'; 144 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')); 150 } 151 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(); 159 160 $count = 0; 161 ksort($choices->options); 162 163 $columns = array(); 164 $celldefault = new html_table_cell(); 165 $celldefault->attributes['class'] = 'data'; 166 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; 172 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; 178 179 $optionsnames = []; 180 foreach ($choices->options as $optionid => $options) { 181 $celloption = clone($celldefault); 182 $cellusernumber = clone($celldefault); 183 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 ((count($choices->options[$optionid]->user)) == ($choices->options[$optionid]->maxanswer)) { 190 $headertitle .= ' ' . get_string('full', 'choice'); 191 } 192 } 193 } 194 $celltext = $headertitle; 195 196 // Render select/deselect all checkbox for this option. 197 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) { 198 199 // Build the select/deselect all for this option. 200 $selectallid = 'select-response-option-' . $optionid; 201 $togglegroup = 'responses response-option-' . $optionid; 202 $selectalltext = get_string('selectalloption', 'choice', $headertitle); 203 $deselectalltext = get_string('deselectalloption', 'choice', $headertitle); 204 $mastercheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [ 205 'id' => $selectallid, 206 'name' => $selectallid, 207 'value' => 1, 208 'selectall' => $selectalltext, 209 'deselectall' => $deselectalltext, 210 'label' => $selectalltext, 211 'labelclasses' => 'accesshide', 212 ]); 213 214 $celltext .= html_writer::div($this->output->render($mastercheckbox)); 215 } 216 $numberofuser = 0; 217 if (!empty($options->user) && count($options->user) > 0) { 218 $numberofuser = count($options->user); 219 } 220 if (($choices->limitanswers) && ($choices->showavailable)) { 221 $numberofuser .= html_writer::empty_tag('br'); 222 $numberofuser .= get_string("limita", "choice", $options->maxanswer); 223 } 224 $celloption->text = html_writer::div($celltext, 'text-center'); 225 $optionsnames[$optionid] = $celltext; 226 $cellusernumber->text = html_writer::div($numberofuser, 'text-center'); 227 228 $columns['options'][] = $celloption; 229 $columns['usernumber'][] = $cellusernumber; 230 } 231 232 $table->head = $columns['options']; 233 $table->data[] = new html_table_row($columns['usernumber']); 234 235 $columns = array(); 236 237 // This extra cell is needed in order to support accessibility for screenreader. MDL-30816 238 $accessiblecell = new html_table_cell(); 239 $accessiblecell->text = get_string('userchoosethisoption', 'choice'); 240 $accessiblecell->header = true; 241 $accessiblecell->scope = 'row'; 242 $accessiblecell->attributes['class'] = 'header data'; 243 $columns[] = $accessiblecell; 244 245 foreach ($choices->options as $optionid => $options) { 246 $cell = new html_table_cell(); 247 $cell->attributes['class'] = 'data'; 248 249 if ($choices->showunanswered || $optionid > 0) { 250 if (!empty($options->user)) { 251 $optionusers = ''; 252 foreach ($options->user as $user) { 253 $data = ''; 254 if (empty($user->imagealt)) { 255 $user->imagealt = ''; 256 } 257 258 $userfullname = fullname($user, $choices->fullnamecapability); 259 $checkbox = ''; 260 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) { 261 $checkboxid = 'attempt-user' . $user->id . '-option' . $optionid; 262 if ($optionid > 0) { 263 $checkboxname = 'attemptid[]'; 264 $checkboxvalue = $user->answerid; 265 } else { 266 $checkboxname = 'userid[]'; 267 $checkboxvalue = $user->id; 268 } 269 270 $togglegroup = 'responses response-option-' . $optionid; 271 $slavecheckbox = new \core\output\checkbox_toggleall($togglegroup, false, [ 272 'id' => $checkboxid, 273 'name' => $checkboxname, 274 'classes' => 'mr-1', 275 'value' => $checkboxvalue, 276 'label' => $userfullname . ' ' . $options->text, 277 'labelclasses' => 'accesshide', 278 ]); 279 $checkbox = $this->output->render($slavecheckbox); 280 } 281 $userimage = $this->output->user_picture($user, array('courseid' => $choices->courseid, 'link' => false)); 282 $profileurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $choices->courseid)); 283 $profilelink = html_writer::link($profileurl, $userimage . $userfullname); 284 $data .= html_writer::div($checkbox . $profilelink, 'mb-1'); 285 286 $optionusers .= $data; 287 } 288 $cell->text = $optionusers; 289 } 290 } 291 $columns[] = $cell; 292 $count++; 293 } 294 $row = new html_table_row($columns); 295 $table->data[] = $row; 296 297 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response')); 298 299 $actiondata = ''; 300 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) { 301 // Build the select/deselect all for all of options. 302 $selectallid = 'select-all-responses'; 303 $togglegroup = 'responses'; 304 $selectallcheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [ 305 'id' => $selectallid, 306 'name' => $selectallid, 307 'value' => 1, 308 'label' => get_string('selectall'), 309 'classes' => 'btn-secondary mr-1' 310 ], true); 311 $actiondata .= $this->output->render($selectallcheckbox); 312 313 $actionurl = new moodle_url($this->page->url, 314 ['sesskey' => sesskey(), 'action' => 'delete_confirmation()']); 315 $actionoptions = array('delete' => get_string('delete')); 316 foreach ($choices->options as $optionid => $option) { 317 if ($optionid > 0) { 318 $actionoptions['choose_'.$optionid] = get_string('chooseoption', 'choice', $option->text); 319 } 320 } 321 $selectattributes = [ 322 'data-action' => 'toggle', 323 'data-togglegroup' => 'responses', 324 'data-toggle' => 'action', 325 ]; 326 $selectnothing = ['' => get_string('chooseaction', 'choice')]; 327 $select = new single_select($actionurl, 'action', $actionoptions, null, $selectnothing, 'attemptsform'); 328 $select->set_label(get_string('withselected', 'choice')); 329 $select->disabled = true; 330 $select->attributes = $selectattributes; 331 332 $actiondata .= $this->output->render($select); 333 } 334 $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction')); 335 336 if ($choices->viewresponsecapability) { 337 $html .= html_writer::end_tag('form'); 338 } 339 340 return $html; 341 } 342 343 344 /** 345 * Returns HTML to display choices result 346 * @deprecated since 3.2 347 * @param object $choices 348 * @return string 349 */ 350 public function display_publish_anonymous_horizontal($choices) { 351 debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.', 352 DEBUG_DEVELOPER); 353 return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_VERTICAL); 354 } 355 356 /** 357 * Returns HTML to display choices result 358 * @deprecated since 3.2 359 * @param object $choices 360 * @return string 361 */ 362 public function display_publish_anonymous_vertical($choices) { 363 debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.', 364 DEBUG_DEVELOPER); 365 return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_HORIZONTAL); 366 } 367 368 /** 369 * Generate the choice result chart. 370 * 371 * Can be displayed either in the vertical or horizontal position. 372 * 373 * @param stdClass $choices Choices responses object. 374 * @param int $displaylayout The constants CHOICE_DISPLAY_HORIZONTAL or CHOICE_DISPLAY_VERTICAL. 375 * @return string the rendered chart. 376 */ 377 public function display_publish_anonymous($choices, $displaylayout) { 378 $count = 0; 379 $data = []; 380 $numberofuser = 0; 381 $percentageamount = 0; 382 foreach ($choices->options as $optionid => $option) { 383 if (!empty($option->user)) { 384 $numberofuser = count($option->user); 385 } 386 if($choices->numberofuser > 0) { 387 $percentageamount = ((float)$numberofuser / (float)$choices->numberofuser) * 100.0; 388 } 389 $data['labels'][$count] = $option->text; 390 $data['series'][$count] = $numberofuser; 391 $data['series_labels'][$count] = $numberofuser . ' (' . format_float($percentageamount, 1) . '%)'; 392 $count++; 393 $numberofuser = 0; 394 } 395 396 $chart = new \core\chart_bar(); 397 if ($displaylayout == CHOICE_DISPLAY_VERTICAL) { 398 $chart->set_horizontal(true); // Horizontal bars when choices are vertical. 399 } 400 $series = new \core\chart_series(format_string(get_string("responses", "choice")), $data['series']); 401 $series->set_labels($data['series_labels']); 402 $chart->add_series($series); 403 $chart->set_labels($data['labels']); 404 $yaxis = $chart->get_yaxis(0, true); 405 $yaxis->set_stepsize(max(1, round(max($data['series']) / 10))); 406 return $this->output->render($chart); 407 } 408 } 409
title
Description
Body
title
Description
Body
title
Description
Body
title
Body