[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/choice/ -> renderer.php (source)

   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  
  45          $html = html_writer::start_tag('form', $attributes);
  46          $html .= html_writer::start_tag('ul', array('class'=>'choices' ));
  47  
  48          $availableoption = count($options['options']);
  49          $choicecount = 0;
  50          foreach ($options['options'] as $option) {
  51              $choicecount++;
  52              $html .= html_writer::start_tag('li', array('class'=>'option'));
  53              if ($multiple) {
  54                  $option->attributes->name = 'answer[]';
  55                  $option->attributes->type = 'checkbox';
  56              } else {
  57                  $option->attributes->name = 'answer';
  58                  $option->attributes->type = 'radio';
  59              }
  60              $option->attributes->id = 'choice_'.$choicecount;
  61  
  62              $labeltext = $option->text;
  63              if (!empty($option->attributes->disabled)) {
  64                  $labeltext .= ' ' . get_string('full', 'choice');
  65                  $availableoption--;
  66              }
  67  
  68              $html .= html_writer::empty_tag('input', (array)$option->attributes);
  69              $html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->id));
  70              $html .= html_writer::end_tag('li');
  71          }
  72          $html .= html_writer::tag('li','', array('class'=>'clearfloat'));
  73          $html .= html_writer::end_tag('ul');
  74          $html .= html_writer::tag('div', '', array('class'=>'clearfloat'));
  75          $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
  76          $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
  77  
  78          if (!empty($options['hascapability']) && ($options['hascapability'])) {
  79              if ($availableoption < 1) {
  80                 $html .= html_writer::tag('label', get_string('choicefull', 'choice'));
  81              } else {
  82                  $html .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('savemychoice','choice'), 'class'=>'button'));
  83              }
  84  
  85              if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
  86                  $url = new moodle_url('view.php', array('id'=>$coursemoduleid, 'action'=>'delchoice', 'sesskey'=>sesskey()));
  87                  $html .= html_writer::link($url, get_string('removemychoice','choice'));
  88              }
  89          } else {
  90              $html .= html_writer::tag('label', get_string('havetologin', 'choice'));
  91          }
  92  
  93          $html .= html_writer::end_tag('ul');
  94          $html .= html_writer::end_tag('form');
  95  
  96          return $html;
  97      }
  98  
  99      /**
 100       * Returns HTML to display choices result
 101       * @param object $choices
 102       * @param bool $forcepublish
 103       * @return string
 104       */
 105      public function display_result($choices, $forcepublish = false) {
 106          if (empty($forcepublish)) { //allow the publish setting to be overridden
 107              $forcepublish = $choices->publish;
 108          }
 109  
 110          $displaylayout = $choices->display;
 111  
 112          if ($forcepublish) {  //CHOICE_PUBLISH_NAMES
 113              return $this->display_publish_name_vertical($choices);
 114          } else { //CHOICE_PUBLISH_ANONYMOUS';
 115              if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) {
 116                  return $this->display_publish_anonymous_horizontal($choices);
 117              }
 118              return $this->display_publish_anonymous_vertical($choices);
 119          }
 120      }
 121  
 122      /**
 123       * Returns HTML to display choices result
 124       * @param object $choices
 125       * @param bool $forcepublish
 126       * @return string
 127       */
 128      public function display_publish_name_vertical($choices) {
 129          global $PAGE;
 130          $html ='';
 131          $html .= html_writer::tag('h3',format_string(get_string("responses", "choice")));
 132  
 133          $attributes = array('method'=>'POST');
 134          $attributes['action'] = new moodle_url($PAGE->url);
 135          $attributes['id'] = 'attemptsform';
 136  
 137          if ($choices->viewresponsecapability) {
 138              $html .= html_writer::start_tag('form', $attributes);
 139              $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid));
 140              $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
 141              $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
 142          }
 143  
 144          $table = new html_table();
 145          $table->cellpadding = 0;
 146          $table->cellspacing = 0;
 147          $table->attributes['class'] = 'results names ';
 148          $table->tablealign = 'center';
 149          $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
 150          $table->data = array();
 151  
 152          $count = 0;
 153          ksort($choices->options);
 154  
 155          $columns = array();
 156          $celldefault = new html_table_cell();
 157          $celldefault->attributes['class'] = 'data';
 158  
 159          // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
 160          $accessiblecell = new html_table_cell();
 161          $accessiblecell->scope = 'row';
 162          $accessiblecell->text = get_string('choiceoptions', 'choice');
 163          $columns['options'][] = $accessiblecell;
 164  
 165          $usernumberheader = clone($celldefault);
 166          $usernumberheader->header = true;
 167          $usernumberheader->attributes['class'] = 'header data';
 168          $usernumberheader->text = get_string('numberofuser', 'choice');
 169          $columns['usernumber'][] = $usernumberheader;
 170  
 171  
 172          foreach ($choices->options as $optionid => $options) {
 173              $celloption = clone($celldefault);
 174              $cellusernumber = clone($celldefault);
 175              $cellusernumber->style = 'text-align: center;';
 176  
 177              $celltext = '';
 178              if ($choices->showunanswered && $optionid == 0) {
 179                  $celltext = format_string(get_string('notanswered', 'choice'));
 180              } else if ($optionid > 0) {
 181                  $celltext = format_string($choices->options[$optionid]->text);
 182              }
 183              $numberofuser = 0;
 184              if (!empty($options->user) && count($options->user) > 0) {
 185                  $numberofuser = count($options->user);
 186              }
 187  
 188              $celloption->text = $celltext;
 189              $cellusernumber->text = $numberofuser;
 190  
 191              $columns['options'][] = $celloption;
 192              $columns['usernumber'][] = $cellusernumber;
 193          }
 194  
 195          $table->head = $columns['options'];
 196          $table->data[] = new html_table_row($columns['usernumber']);
 197  
 198          $columns = array();
 199  
 200          // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
 201          $accessiblecell = new html_table_cell();
 202          $accessiblecell->text = get_string('userchoosethisoption', 'choice');
 203          $accessiblecell->header = true;
 204          $accessiblecell->scope = 'row';
 205          $accessiblecell->attributes['class'] = 'header data';
 206          $columns[] = $accessiblecell;
 207  
 208          foreach ($choices->options as $optionid => $options) {
 209              $cell = new html_table_cell();
 210              $cell->attributes['class'] = 'data';
 211  
 212              if ($choices->showunanswered || $optionid > 0) {
 213                  if (!empty($options->user)) {
 214                      $optionusers = '';
 215                      foreach ($options->user as $user) {
 216                          $data = '';
 217                          if (empty($user->imagealt)){
 218                              $user->imagealt = '';
 219                          }
 220  
 221                          $userfullname = fullname($user, $choices->fullnamecapability);
 222                          if ($choices->viewresponsecapability && $choices->deleterepsonsecapability  && $optionid > 0) {
 223                              $attemptaction = html_writer::label($userfullname, 'attempt-user'.$user->id, false, array('class' => 'accesshide'));
 224                              $attemptaction .= html_writer::checkbox('attemptid[]', $user->answerid, '', null,
 225                                      array('id' => 'attempt-user'.$user->id));
 226                              $data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
 227                          }
 228                          $userimage = $this->output->user_picture($user, array('courseid'=>$choices->courseid));
 229                          $data .= html_writer::tag('div', $userimage, array('class'=>'image'));
 230  
 231                          $userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choices->courseid));
 232                          $name = html_writer::tag('a', $userfullname, array('href'=>$userlink, 'class'=>'username'));
 233                          $data .= html_writer::tag('div', $name, array('class'=>'fullname'));
 234                          $data .= html_writer::tag('div','', array('class'=>'clearfloat'));
 235                          $optionusers .= html_writer::tag('div', $data, array('class'=>'user'));
 236                      }
 237                      $cell->text = $optionusers;
 238                  }
 239              }
 240              $columns[] = $cell;
 241              $count++;
 242          }
 243          $row = new html_table_row($columns);
 244          $table->data[] = $row;
 245  
 246          $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
 247  
 248          $actiondata = '';
 249          if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
 250              $selecturl = new moodle_url('#');
 251  
 252              $selectallactions = new component_action('click',"checkall");
 253              $selectall = new action_link($selecturl, get_string('selectall'), $selectallactions);
 254              $actiondata .= $this->output->render($selectall) . ' / ';
 255  
 256              $deselectallactions = new component_action('click',"checknone");
 257              $deselectall = new action_link($selecturl, get_string('deselectall'), $deselectallactions);
 258              $actiondata .= $this->output->render($deselectall);
 259  
 260              $actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'choice') . ' ', array('for'=>'menuaction'));
 261  
 262              $actionurl = new moodle_url($PAGE->url, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
 263              $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choice')), 'attemptsform');
 264  
 265              $actiondata .= $this->output->render($select);
 266          }
 267          $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
 268  
 269          if ($choices->viewresponsecapability) {
 270              $html .= html_writer::end_tag('form');
 271          }
 272  
 273          return $html;
 274      }
 275  
 276  
 277      /**
 278       * Returns HTML to display choices result
 279       * @param object $choices
 280       * @return string
 281       */
 282      public function display_publish_anonymous_vertical($choices) {
 283          global $CHOICE_COLUMN_HEIGHT;
 284  
 285          $html = '';
 286          $table = new html_table();
 287          $table->cellpadding = 5;
 288          $table->cellspacing = 0;
 289          $table->attributes['class'] = 'results anonymous ';
 290          $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
 291          $table->data = array();
 292  
 293          $count = 0;
 294          ksort($choices->options);
 295          $columns = array();
 296          $rows = array();
 297  
 298          $headercelldefault = new html_table_cell();
 299          $headercelldefault->scope = 'row';
 300          $headercelldefault->header = true;
 301          $headercelldefault->attributes = array('class'=>'header data');
 302  
 303          // column header
 304          $tableheader = clone($headercelldefault);
 305          $tableheader->text = html_writer::tag('div', get_string('choiceoptions', 'choice'), array('class' => 'accesshide'));
 306          $rows['header'][] = $tableheader;
 307  
 308          // graph row header
 309          $graphheader = clone($headercelldefault);
 310          $graphheader->text = html_writer::tag('div', get_string('responsesresultgraphheader', 'choice'), array('class' => 'accesshide'));
 311          $rows['graph'][] = $graphheader;
 312  
 313          // user number row header
 314          $usernumberheader = clone($headercelldefault);
 315          $usernumberheader->text = get_string('numberofuser', 'choice');
 316          $rows['usernumber'][] = $usernumberheader;
 317  
 318          // user percentage row header
 319          $userpercentageheader = clone($headercelldefault);
 320          $userpercentageheader->text = get_string('numberofuserinpercentage', 'choice');
 321          $rows['userpercentage'][] = $userpercentageheader;
 322  
 323          $contentcelldefault = new html_table_cell();
 324          $contentcelldefault->attributes = array('class'=>'data');
 325  
 326          foreach ($choices->options as $optionid => $option) {
 327              // calculate display length
 328              $height = $percentageamount = $numberofuser = 0;
 329              $usernumber = $userpercentage = '';
 330  
 331              if (!empty($option->user)) {
 332                 $numberofuser = count($option->user);
 333              }
 334  
 335              if($choices->numberofuser > 0) {
 336                 $height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser));
 337                 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
 338              }
 339  
 340              $displaygraph = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output->pix_url('column', 'choice')));
 341  
 342              // header
 343              $headercell = clone($contentcelldefault);
 344              $headercell->text = $option->text;
 345              $rows['header'][] = $headercell;
 346  
 347              // Graph
 348              $graphcell = clone($contentcelldefault);
 349              $graphcell->attributes = array('class'=>'graph vertical data');
 350              $graphcell->text = $displaygraph;
 351              $rows['graph'][] = $graphcell;
 352  
 353              $usernumber .= html_writer::tag('div', ' '.$numberofuser.'', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice')));
 354              $userpercentage .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
 355  
 356              // number of user
 357              $usernumbercell = clone($contentcelldefault);
 358              $usernumbercell->text = $usernumber;
 359              $rows['usernumber'][] = $usernumbercell;
 360  
 361              // percentage of user
 362              $numbercell = clone($contentcelldefault);
 363              $numbercell->text = $userpercentage;
 364              $rows['userpercentage'][] = $numbercell;
 365          }
 366  
 367          $table->head = $rows['header'];
 368          $trgraph = new html_table_row($rows['graph']);
 369          $trusernumber = new html_table_row($rows['usernumber']);
 370          $truserpercentage = new html_table_row($rows['userpercentage']);
 371          $table->data = array($trgraph, $trusernumber, $truserpercentage);
 372  
 373          $header = html_writer::tag('h3',format_string(get_string("responses", "choice")));
 374          $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
 375          $html .= html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph', 'class'=>'skip-block'));
 376          $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
 377  
 378          return $html;
 379      }
 380  
 381      /**
 382       * Returns HTML to display choices result
 383       * @param object $choices
 384       * @return string
 385       */
 386      public function display_publish_anonymous_horizontal($choices) {
 387          global $CHOICE_COLUMN_WIDTH;
 388  
 389          $table = new html_table();
 390          $table->cellpadding = 5;
 391          $table->cellspacing = 0;
 392          $table->attributes['class'] = 'results anonymous ';
 393          $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
 394          $table->data = array();
 395  
 396          $columnheaderdefault = new html_table_cell();
 397          $columnheaderdefault->scope = 'col';
 398  
 399          $tableheadertext = clone($columnheaderdefault);
 400          $tableheadertext->text = get_string('choiceoptions', 'choice');
 401  
 402          $tableheadernumber = clone($columnheaderdefault);
 403          $tableheadernumber->text = get_string('numberofuser', 'choice');
 404  
 405          $tableheaderpercentage = clone($columnheaderdefault);
 406          $tableheaderpercentage->text = get_string('numberofuserinpercentage', 'choice');
 407  
 408          $tableheadergraph = clone($columnheaderdefault);
 409          $tableheadergraph->text = get_string('responsesresultgraphheader', 'choice');
 410  
 411          $table->head = array($tableheadertext, $tableheadernumber, $tableheaderpercentage, $tableheadergraph);
 412  
 413          $count = 0;
 414          ksort($choices->options);
 415  
 416          $columndefault = new html_table_cell();
 417          $columndefault->attributes['class'] = 'data';
 418  
 419          $colheaderdefault = new html_table_cell();
 420          $colheaderdefault->scope = 'row';
 421          $colheaderdefault->header = true;
 422          $colheaderdefault->attributes['class'] = 'header data';
 423  
 424          $rows = array();
 425          foreach ($choices->options as $optionid => $options) {
 426              $colheader = clone($colheaderdefault);
 427              $colheader->text = $options->text;
 428  
 429              $graphcell = clone($columndefault);
 430              $datacellnumber = clone($columndefault);
 431              $datacellpercentage = clone($columndefault);
 432  
 433              $numberofuser = $width = $percentageamount = 0;
 434  
 435              if (!empty($options->user)) {
 436                 $numberofuser = count($options->user);
 437              }
 438  
 439              if($choices->numberofuser > 0) {
 440                 $width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser));
 441                 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
 442              }
 443  
 444              $attributes = array();
 445              $attributes['style'] = 'height:50px; width:'.$width.'px';
 446              $attributes['alt'] = '';
 447              $attributes['src'] = $this->output->pix_url('row', 'choice');
 448              $displaydiagram = html_writer::tag('img','', $attributes);
 449  
 450              $graphcell->text = $displaydiagram;
 451              $graphcell->attributes = array('class'=>'graph horizontal');
 452  
 453              if($choices->numberofuser > 0) {
 454                 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
 455              }
 456  
 457              $datacellnumber->text = $numberofuser;
 458              $datacellpercentage->text = format_float($percentageamount,1). '%';
 459  
 460  
 461              $row = new html_table_row();
 462              $row->cells = array($colheader, $datacellnumber, $datacellpercentage, $graphcell);
 463              $rows[] = $row;
 464          }
 465  
 466          $table->data = $rows;
 467  
 468          $html = '';
 469          $header = html_writer::tag('h3',format_string(get_string("responses", "choice")));
 470          $html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
 471          $html .= html_writer::table($table);
 472  
 473          return $html;
 474      }
 475  }
 476  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1