[ Index ]

PHP Cross Reference of Unnamed Project

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          $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' ));
  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  
  63              $labeltext = $option->text;
  64              if (!empty($option->attributes->disabled)) {
  65                  $labeltext .= ' ' . get_string('full', 'choice');
  66                  $availableoption--;
  67              }
  68  
  69              $html .= html_writer::empty_tag('input', (array)$option->attributes + $disabled);
  70              $html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->id));
  71              $html .= html_writer::end_tag('li');
  72          }
  73          $html .= html_writer::tag('li','', array('class'=>'clearfloat'));
  74          $html .= html_writer::end_tag('ul');
  75          $html .= html_writer::tag('div', '', array('class'=>'clearfloat'));
  76          $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
  77          $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'makechoice'));
  78          $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
  79  
  80          if (empty($options['previewonly'])) {
  81              if (!empty($options['hascapability']) && ($options['hascapability'])) {
  82                  if ($availableoption < 1) {
  83                      $html .= html_writer::tag('label', get_string('choicefull', 'choice'));
  84                  } else {
  85                      $html .= html_writer::empty_tag('input',
  86                              array('type' => 'submit', 'value' => get_string('savemychoice', 'choice'), 'class' => 'button'));
  87                  }
  88  
  89                  if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
  90                      $url = new moodle_url('view.php',
  91                              array('id' => $coursemoduleid, 'action' => 'delchoice', 'sesskey' => sesskey()));
  92                      $html .= html_writer::link($url, get_string('removemychoice', 'choice'));
  93                  }
  94              } else {
  95                  $html .= html_writer::tag('label', get_string('havetologin', 'choice'));
  96              }
  97          }
  98  
  99          $html .= html_writer::end_tag('ul');
 100          $html .= html_writer::end_tag('form');
 101  
 102          return $html;
 103      }
 104  
 105      /**
 106       * Returns HTML to display choices result
 107       * @param object $choices
 108       * @param bool $forcepublish
 109       * @return string
 110       */
 111      public function display_result($choices, $forcepublish = false) {
 112          if (empty($forcepublish)) { //allow the publish setting to be overridden
 113              $forcepublish = $choices->publish;
 114          }
 115  
 116          $displaylayout = $choices->display;
 117  
 118          if ($forcepublish) {  //CHOICE_PUBLISH_NAMES
 119              return $this->display_publish_name_vertical($choices);
 120          } else {
 121              return $this->display_publish_anonymous($choices, $displaylayout);
 122          }
 123      }
 124  
 125      /**
 126       * Returns HTML to display choices result
 127       * @param object $choices
 128       * @param bool $forcepublish
 129       * @return string
 130       */
 131      public function display_publish_name_vertical($choices) {
 132          global $PAGE;
 133          $html ='';
 134          $html .= html_writer::tag('h3',format_string(get_string("responses", "choice")));
 135  
 136          $attributes = array('method'=>'POST');
 137          $attributes['action'] = new moodle_url($PAGE->url);
 138          $attributes['id'] = 'attemptsform';
 139  
 140          if ($choices->viewresponsecapability) {
 141              $html .= html_writer::start_tag('form', $attributes);
 142              $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid));
 143              $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
 144              $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
 145          }
 146  
 147          $table = new html_table();
 148          $table->cellpadding = 0;
 149          $table->cellspacing = 0;
 150          $table->attributes['class'] = 'results names ';
 151          $table->tablealign = 'center';
 152          $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
 153          $table->data = array();
 154  
 155          $count = 0;
 156          ksort($choices->options);
 157  
 158          $columns = array();
 159          $celldefault = new html_table_cell();
 160          $celldefault->attributes['class'] = 'data';
 161  
 162          // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
 163          $accessiblecell = new html_table_cell();
 164          $accessiblecell->scope = 'row';
 165          $accessiblecell->text = get_string('choiceoptions', 'choice');
 166          $columns['options'][] = $accessiblecell;
 167  
 168          $usernumberheader = clone($celldefault);
 169          $usernumberheader->header = true;
 170          $usernumberheader->attributes['class'] = 'header data';
 171          $usernumberheader->text = get_string('numberofuser', 'choice');
 172          $columns['usernumber'][] = $usernumberheader;
 173  
 174  
 175          foreach ($choices->options as $optionid => $options) {
 176              $celloption = clone($celldefault);
 177              $cellusernumber = clone($celldefault);
 178              $cellusernumber->style = 'text-align: center;';
 179  
 180              $celltext = '';
 181              if ($choices->showunanswered && $optionid == 0) {
 182                  $celltext = format_string(get_string('notanswered', 'choice'));
 183              } else if ($optionid > 0) {
 184                  $celltext = format_string($choices->options[$optionid]->text);
 185              }
 186              $numberofuser = 0;
 187              if (!empty($options->user) && count($options->user) > 0) {
 188                  $numberofuser = count($options->user);
 189              }
 190  
 191              $celloption->text = $celltext;
 192              $cellusernumber->text = $numberofuser;
 193  
 194              $columns['options'][] = $celloption;
 195              $columns['usernumber'][] = $cellusernumber;
 196          }
 197  
 198          $table->head = $columns['options'];
 199          $table->data[] = new html_table_row($columns['usernumber']);
 200  
 201          $columns = array();
 202  
 203          // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
 204          $accessiblecell = new html_table_cell();
 205          $accessiblecell->text = get_string('userchoosethisoption', 'choice');
 206          $accessiblecell->header = true;
 207          $accessiblecell->scope = 'row';
 208          $accessiblecell->attributes['class'] = 'header data';
 209          $columns[] = $accessiblecell;
 210  
 211          foreach ($choices->options as $optionid => $options) {
 212              $cell = new html_table_cell();
 213              $cell->attributes['class'] = 'data';
 214  
 215              if ($choices->showunanswered || $optionid > 0) {
 216                  if (!empty($options->user)) {
 217                      $optionusers = '';
 218                      foreach ($options->user as $user) {
 219                          $data = '';
 220                          if (empty($user->imagealt)){
 221                              $user->imagealt = '';
 222                          }
 223  
 224                          $userfullname = fullname($user, $choices->fullnamecapability);
 225                          if ($choices->viewresponsecapability && $choices->deleterepsonsecapability  && $optionid > 0) {
 226                              $attemptaction = html_writer::label($userfullname, 'attempt-user'.$user->id, false, array('class' => 'accesshide'));
 227                              $attemptaction .= html_writer::checkbox('attemptid[]', $user->answerid, '', null,
 228                                      array('id' => 'attempt-user'.$user->id));
 229                              $data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
 230                          }
 231                          $userimage = $this->output->user_picture($user, array('courseid'=>$choices->courseid));
 232                          $data .= html_writer::tag('div', $userimage, array('class'=>'image'));
 233  
 234                          $userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choices->courseid));
 235                          $name = html_writer::tag('a', $userfullname, array('href'=>$userlink, 'class'=>'username'));
 236                          $data .= html_writer::tag('div', $name, array('class'=>'fullname'));
 237                          $data .= html_writer::tag('div','', array('class'=>'clearfloat'));
 238                          $optionusers .= html_writer::tag('div', $data, array('class'=>'user'));
 239                      }
 240                      $cell->text = $optionusers;
 241                  }
 242              }
 243              $columns[] = $cell;
 244              $count++;
 245          }
 246          $row = new html_table_row($columns);
 247          $table->data[] = $row;
 248  
 249          $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
 250  
 251          $actiondata = '';
 252          if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
 253              $selecturl = new moodle_url('#');
 254  
 255              $selectallactions = new component_action('click',"checkall");
 256              $selectall = new action_link($selecturl, get_string('selectall'), $selectallactions);
 257              $actiondata .= $this->output->render($selectall) . ' / ';
 258  
 259              $deselectallactions = new component_action('click',"checknone");
 260              $deselectall = new action_link($selecturl, get_string('deselectall'), $deselectallactions);
 261              $actiondata .= $this->output->render($deselectall);
 262  
 263              $actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'choice') . ' ', array('for'=>'menuaction'));
 264  
 265              $actionurl = new moodle_url($PAGE->url, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
 266              $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choice')), 'attemptsform');
 267  
 268              $actiondata .= $this->output->render($select);
 269          }
 270          $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
 271  
 272          if ($choices->viewresponsecapability) {
 273              $html .= html_writer::end_tag('form');
 274          }
 275  
 276          return $html;
 277      }
 278  
 279  
 280      /**
 281       * Returns HTML to display choices result
 282       * @deprecated since 3.2
 283       * @param object $choices
 284       * @return string
 285       */
 286      public function display_publish_anonymous_horizontal($choices) {
 287          global $CHOICE_COLUMN_HEIGHT;
 288          debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.',
 289                  DEBUG_DEVELOPER);
 290          return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_VERTICAL);
 291      }
 292  
 293      /**
 294       * Returns HTML to display choices result
 295       * @deprecated since 3.2
 296       * @param object $choices
 297       * @return string
 298       */
 299      public function display_publish_anonymous_vertical($choices) {
 300          global $CHOICE_COLUMN_WIDTH;
 301          debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.',
 302                  DEBUG_DEVELOPER);
 303          return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_HORIZONTAL);
 304      }
 305  
 306      /**
 307       * Generate the choice result chart.
 308       *
 309       * Can be displayed either in the vertical or horizontal position.
 310       *
 311       * @param stdClass $choices Choices responses object.
 312       * @param int $displaylayout The constants DISPLAY_HORIZONTAL_LAYOUT or DISPLAY_VERTICAL_LAYOUT.
 313       * @return string the rendered chart.
 314       */
 315      public function display_publish_anonymous($choices, $displaylayout) {
 316          global $OUTPUT;
 317          $count = 0;
 318          $data = [];
 319          $numberofuser = 0;
 320          $percentageamount = 0;
 321          foreach ($choices->options as $optionid => $option) {
 322              if (!empty($option->user)) {
 323                  $numberofuser = count($option->user);
 324              }
 325              if($choices->numberofuser > 0) {
 326                  $percentageamount = ((float)$numberofuser / (float)$choices->numberofuser) * 100.0;
 327              }
 328              $data['labels'][$count] = $option->text;
 329              $data['series'][$count] = $numberofuser;
 330              $data['series_labels'][$count] = $numberofuser . ' (' . format_float($percentageamount, 1) . '%)';
 331              $count++;
 332              $numberofuser = 0;
 333          }
 334  
 335          $chart = new \core\chart_bar();
 336          if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) {
 337              $chart->set_horizontal(true);
 338          }
 339          $series = new \core\chart_series(format_string(get_string("responses", "choice")), $data['series']);
 340          $series->set_labels($data['series_labels']);
 341          $chart->add_series($series);
 342          $chart->set_labels($data['labels']);
 343          $yaxis = $chart->get_yaxis(0, true);
 344          $yaxis->set_stepsize(max(1, round(max($data['series']) / 10)));
 345          return $OUTPUT->render($chart);
 346      }
 347  }
 348  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1