[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/feedback/item/multichoice/ -> lib.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  defined('MOODLE_INTERNAL') OR die('not allowed');
  18  require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
  19  
  20  define('FEEDBACK_MULTICHOICE_TYPE_SEP', '>>>>>');
  21  define('FEEDBACK_MULTICHOICE_LINE_SEP', '|');
  22  define('FEEDBACK_MULTICHOICE_ADJUST_SEP', '<<<<<');
  23  define('FEEDBACK_MULTICHOICE_IGNOREEMPTY', 'i');
  24  define('FEEDBACK_MULTICHOICE_HIDENOSELECT', 'h');
  25  
  26  class feedback_item_multichoice extends feedback_item_base {
  27      protected $type = "multichoice";
  28      private $commonparams;
  29      private $item_form;
  30      private $item;
  31  
  32      public function init() {
  33  
  34      }
  35  
  36      public function build_editform($item, $feedback, $cm) {
  37          global $DB, $CFG;
  38          require_once ('multichoice_form.php');
  39  
  40          //get the lastposition number of the feedback_items
  41          $position = $item->position;
  42          $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
  43          if ($position == -1) {
  44              $i_formselect_last = $lastposition + 1;
  45              $i_formselect_value = $lastposition + 1;
  46              $item->position = $lastposition + 1;
  47          } else {
  48              $i_formselect_last = $lastposition;
  49              $i_formselect_value = $item->position;
  50          }
  51          //the elements for position dropdownlist
  52          $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
  53  
  54          $item->presentation = empty($item->presentation) ? '' : $item->presentation;
  55          $info = $this->get_info($item);
  56  
  57          $item->ignoreempty = $this->ignoreempty($item);
  58          $item->hidenoselect = $this->hidenoselect($item);
  59  
  60          //all items for dependitem
  61          $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
  62          $commonparams = array('cmid'=>$cm->id,
  63                               'id'=>isset($item->id) ? $item->id : null,
  64                               'typ'=>$item->typ,
  65                               'items'=>$feedbackitems,
  66                               'feedback'=>$feedback->id);
  67  
  68          //build the form
  69          $customdata = array('item' => $item,
  70                              'common' => $commonparams,
  71                              'positionlist' => $positionlist,
  72                              'position' => $position,
  73                              'info' => $info);
  74  
  75          $this->item_form = new feedback_multichoice_form('edit_item.php', $customdata);
  76      }
  77  
  78      //this function only can used after the call of build_editform()
  79      public function show_editform() {
  80          $this->item_form->display();
  81      }
  82  
  83      public function is_cancelled() {
  84          return $this->item_form->is_cancelled();
  85      }
  86  
  87      public function get_data() {
  88          if ($this->item = $this->item_form->get_data()) {
  89              return true;
  90          }
  91          return false;
  92      }
  93  
  94      public function save_item() {
  95          global $DB;
  96  
  97          if (!$item = $this->item_form->get_data()) {
  98              return false;
  99          }
 100  
 101          if (isset($item->clone_item) AND $item->clone_item) {
 102              $item->id = ''; //to clone this item
 103              $item->position++;
 104          }
 105  
 106          $this->set_ignoreempty($item, $item->ignoreempty);
 107          $this->set_hidenoselect($item, $item->hidenoselect);
 108  
 109          $item->hasvalue = $this->get_hasvalue();
 110          if (!$item->id) {
 111              $item->id = $DB->insert_record('feedback_item', $item);
 112          } else {
 113              $DB->update_record('feedback_item', $item);
 114          }
 115  
 116          return $DB->get_record('feedback_item', array('id'=>$item->id));
 117      }
 118  
 119  
 120      //gets an array with three values(typ, name, XXX)
 121      //XXX is an object with answertext, answercount and quotient
 122      public function get_analysed($item, $groupid = false, $courseid = false) {
 123          $info = $this->get_info($item);
 124  
 125          $analysed_item = array();
 126          $analysed_item[] = $item->typ;
 127          $analysed_item[] = $item->name;
 128  
 129          //get the possible answers
 130          $answers = null;
 131          $answers = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
 132          if (!is_array($answers)) {
 133              return null;
 134          }
 135  
 136          //get the values
 137          $values = feedback_get_group_values($item, $groupid, $courseid, $this->ignoreempty($item));
 138          if (!$values) {
 139              return null;
 140          }
 141  
 142          //get answertext, answercount and quotient for each answer
 143          $analysed_answer = array();
 144          if ($info->subtype == 'c') {
 145              $sizeofanswers = count($answers);
 146              for ($i = 1; $i <= $sizeofanswers; $i++) {
 147                  $ans = new stdClass();
 148                  $ans->answertext = $answers[$i-1];
 149                  $ans->answercount = 0;
 150                  foreach ($values as $value) {
 151                      //ist die Antwort gleich dem index der Antworten + 1?
 152                      $vallist = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value->value);
 153                      foreach ($vallist as $val) {
 154                          if ($val == $i) {
 155                              $ans->answercount++;
 156                          }
 157                      }
 158                  }
 159                  $ans->quotient = $ans->answercount / count($values);
 160                  $analysed_answer[] = $ans;
 161              }
 162          } else {
 163              $sizeofanswers = count($answers);
 164              for ($i = 1; $i <= $sizeofanswers; $i++) {
 165                  $ans = new stdClass();
 166                  $ans->answertext = $answers[$i-1];
 167                  $ans->answercount = 0;
 168                  foreach ($values as $value) {
 169                      //ist die Antwort gleich dem index der Antworten + 1?
 170                      if ($value->value == $i) {
 171                          $ans->answercount++;
 172                      }
 173                  }
 174                  $ans->quotient = $ans->answercount / count($values);
 175                  $analysed_answer[] = $ans;
 176              }
 177          }
 178          $analysed_item[] = $analysed_answer;
 179          return $analysed_item;
 180      }
 181  
 182      public function get_printval($item, $value) {
 183          $info = $this->get_info($item);
 184  
 185          $printval = '';
 186  
 187          if (!isset($value->value)) {
 188              return $printval;
 189          }
 190  
 191          $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
 192  
 193          if ($info->subtype == 'c') {
 194              $vallist = array_values(explode (FEEDBACK_MULTICHOICE_LINE_SEP, $value->value));
 195              $sizeofvallist = count($vallist);
 196              $sizeofpresentation = count($presentation);
 197              for ($i = 0; $i < $sizeofvallist; $i++) {
 198                  for ($k = 0; $k < $sizeofpresentation; $k++) {
 199                      if ($vallist[$i] == ($k + 1)) {//Die Werte beginnen bei 1, das Array aber mit 0
 200                          $printval .= trim($presentation[$k]) . chr(10);
 201                          break;
 202                      }
 203                  }
 204              }
 205          } else {
 206              $index = 1;
 207              foreach ($presentation as $pres) {
 208                  if ($value->value == $index) {
 209                      $printval = $pres;
 210                      break;
 211                  }
 212                  $index++;
 213              }
 214          }
 215          return $printval;
 216      }
 217  
 218      public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
 219          global $OUTPUT;
 220          $sep_dec = get_string('separator_decimal', 'feedback');
 221          if (substr($sep_dec, 0, 2) == '[[') {
 222              $sep_dec = FEEDBACK_DECIMAL;
 223          }
 224  
 225          $sep_thous = get_string('separator_thousand', 'feedback');
 226          if (substr($sep_thous, 0, 2) == '[[') {
 227              $sep_thous = FEEDBACK_THOUSAND;
 228          }
 229  
 230          $analysed_item = $this->get_analysed($item, $groupid, $courseid);
 231          if ($analysed_item) {
 232              $itemname = $analysed_item[1];
 233              echo '<tr><th colspan="2" align="left">';
 234              echo $itemnr.'&nbsp;('.$item->label.') '.$itemname;
 235              echo '</th></tr>';
 236  
 237              $analysed_vals = $analysed_item[2];
 238              $pixnr = 0;
 239              foreach ($analysed_vals as $val) {
 240                  $intvalue = $pixnr % 10;
 241                  $pix = $OUTPUT->pix_url('multichoice/' . $intvalue, 'feedback');
 242                  $pixnr++;
 243                  $pixwidth = intval($val->quotient * FEEDBACK_MAX_PIX_LENGTH);
 244                  $quotient = number_format(($val->quotient * 100), 2, $sep_dec, $sep_thous);
 245                  $str_quotient = '';
 246                  if ($val->quotient > 0) {
 247                      $str_quotient = '&nbsp;('. $quotient . '&nbsp;%)';
 248                  }
 249                  echo '<tr>';
 250                  echo '<td align="left" valign="top">
 251                              -&nbsp;&nbsp;'.trim($val->answertext).':
 252                        </td>
 253                        <td align="left" style="width:'.FEEDBACK_MAX_PIX_LENGTH.';">
 254                          <img class="feedback_bar_image" alt="'.$intvalue.'" src="'.$pix.'" height="5" width="'.$pixwidth.'" />
 255                          &nbsp;'.$val->answercount.$str_quotient.'
 256                        </td>';
 257                  echo '</tr>';
 258              }
 259          }
 260      }
 261  
 262      public function excelprint_item(&$worksheet, $row_offset,
 263                               $xls_formats, $item,
 264                               $groupid, $courseid = false) {
 265  
 266          $analysed_item = $this->get_analysed($item, $groupid, $courseid);
 267  
 268          $data = $analysed_item[2];
 269  
 270          //frage schreiben
 271          $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
 272          $worksheet->write_string($row_offset, 1, $analysed_item[1], $xls_formats->head2);
 273          if (is_array($data)) {
 274              $sizeofdata = count($data);
 275              for ($i = 0; $i < $sizeofdata; $i++) {
 276                  $analysed_data = $data[$i];
 277  
 278                  $worksheet->write_string($row_offset,
 279                                           $i + 2,
 280                                           trim($analysed_data->answertext),
 281                                           $xls_formats->head2);
 282  
 283                  $worksheet->write_number($row_offset + 1,
 284                                           $i + 2,
 285                                           $analysed_data->answercount,
 286                                           $xls_formats->default);
 287  
 288                  $worksheet->write_number($row_offset + 2,
 289                                           $i + 2,
 290                                           $analysed_data->quotient,
 291                                           $xls_formats->procent);
 292              }
 293          }
 294          $row_offset += 3;
 295          return $row_offset;
 296      }
 297  
 298      /**
 299       * print the item at the edit-page of feedback
 300       *
 301       * @global object
 302       * @param object $item
 303       * @return void
 304       */
 305      public function print_item_preview($item) {
 306          global $OUTPUT, $DB;
 307          $info = $this->get_info($item);
 308          $align = right_to_left() ? 'right' : 'left';
 309  
 310          $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
 311          $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
 312              get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
 313  
 314          //test if required and no value is set so we have to mark this item
 315          //we have to differ check and the other subtypes
 316          $requiredmark = ($item->required == 1) ? $strrequiredmark : '';
 317  
 318          //print the question and label
 319          echo '<div class="feedback_item_label_'.$align.'">';
 320          if ($info->subtype == 'd') {
 321              echo '<label for="'. $item->typ . '_' . $item->id .'">';
 322          }
 323          echo '('.$item->label.') ';
 324          echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
 325          if ($item->dependitem) {
 326              if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
 327                  echo ' <span class="feedback_depend">';
 328                  echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
 329                  echo '</span>';
 330              }
 331          }
 332          if ($info->subtype == 'd') {
 333              echo '</label>';
 334          }
 335          echo '</div>';
 336  
 337          //print the presentation
 338          echo '<div class="feedback_item_presentation_'.$align.'">';
 339          $index = 1;
 340          $checked = '';
 341          if ($info->subtype == 'r' || $info->subtype == 'c') {
 342              // if (r)adio buttons or (c)heckboxes
 343              echo '<fieldset>';
 344              echo '<ul>';
 345          }
 346  
 347          if ($info->horizontal) {
 348              $hv = 'h';
 349          } else {
 350              $hv = 'v';
 351          }
 352  
 353          if ($info->subtype == 'r' AND !$this->hidenoselect($item)) {
 354          //print the "not_selected" item on radiobuttons
 355          ?>
 356          <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
 357              <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
 358                  <?php
 359                      echo '<input type="radio" '.
 360                              'name="'.$item->typ.'_'.$item->id.'[]" '.
 361                              'id="'.$item->typ.'_'.$item->id.'_xxx" '.
 362                              'value="" checked="checked" />';
 363                  ?>
 364              </span>
 365              <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
 366                  <label for="<?php echo $item->typ . '_' . $item->id.'_xxx';?>">
 367                      <?php print_string('not_selected', 'feedback');?>&nbsp;
 368                  </label>
 369              </span>
 370          </li>
 371          <?php
 372          }
 373  
 374          switch($info->subtype) {
 375              case 'r':
 376                  $this->print_item_radio($presentation, $item, false, $info, $align);
 377                  break;
 378              case 'c':
 379                  $this->print_item_check($presentation, $item, false, $info, $align);
 380                  break;
 381              case 'd':
 382                  $this->print_item_dropdown($presentation, $item, false, $info, $align);
 383                  break;
 384          }
 385          if ($info->subtype == 'r' || $info->subtype == 'c') {
 386              // if (r)adio buttons or (c)heckboxes
 387              echo '</ul>';
 388              echo '</fieldset>';
 389          }
 390          echo '</div>';
 391      }
 392  
 393      /**
 394       * print the item at the complete-page of feedback
 395       *
 396       * @global object
 397       * @param object $item
 398       * @param string $value
 399       * @param bool $highlightrequire
 400       * @return void
 401       */
 402      public function print_item_complete($item, $value = null, $highlightrequire = false) {
 403          global $OUTPUT;
 404          $info = $this->get_info($item);
 405          $align = right_to_left() ? 'right' : 'left';
 406  
 407          if ($value == null) {
 408              $value = array();
 409          }
 410          $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
 411          $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
 412              get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
 413  
 414          //test if required and no value is set so we have to mark this item
 415          //we have to differ check and the other subtypes
 416          if (is_array($value)) {
 417              $values = $value;
 418          } else {
 419              $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
 420          }
 421          $requiredmark = ($item->required == 1) ? $strrequiredmark : '';
 422  
 423          //print the question and label
 424          $inputname = $item->typ . '_' . $item->id;
 425          echo '<div class="feedback_item_label_'.$align.'">';
 426          if ($info->subtype == 'd') {
 427              echo '<label for="'. $inputname .'">';
 428              echo format_text($item->name.$requiredmark, true, false, false);
 429              if ($highlightrequire AND $item->required AND (count($values) == 0 OR $values[0] == '' OR $values[0] == 0)) {
 430                  echo '<br class="error"><span id="id_error_'.$inputname.'" class="error"> '.get_string('err_required', 'form').
 431                      '</span><br id="id_error_break_'.$inputname.'" class="error" >';
 432              }
 433              echo '</label>';
 434          } else {
 435              echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
 436              if ($highlightrequire AND $item->required AND (count($values) == 0 OR $values[0] == '' OR $values[0] == 0)) {
 437                  echo '<br class="error"><span id="id_error_'.$inputname.'" class="error"> '.get_string('err_required', 'form').
 438                      '</span><br id="id_error_break_'.$inputname.'" class="error" >';
 439              }
 440          }
 441          echo '</div>';
 442  
 443          //print the presentation
 444          echo '<div class="feedback_item_presentation_'.$align.'">';
 445  
 446          if ($info->subtype == 'r' || $info->subtype == 'c') {
 447              // if (r)adio buttons or (c)heckboxes
 448              echo '<fieldset>';
 449              echo '<ul>';
 450          }
 451          if ($info->horizontal) {
 452              $hv = 'h';
 453          } else {
 454              $hv = 'v';
 455          }
 456          //print the "not_selected" item on radiobuttons
 457          if ($info->subtype == 'r' AND !$this->hidenoselect($item)) {
 458          ?>
 459              <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
 460                  <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
 461                      <?php
 462                      $checked = '';
 463                      // if (!$value) {
 464                          // $checked = 'checked="checked"';
 465                      // }
 466                      if (count($values) == 0 OR $values[0] == '' OR $values[0] == 0) {
 467                          $checked = 'checked="checked"';
 468                      }
 469                      echo '<input type="radio" '.
 470                              'name="'.$item->typ.'_'.$item->id.'[]" '.
 471                              'id="'.$item->typ.'_'.$item->id.'_xxx" '.
 472                              'value="" '.$checked.' />';
 473                      ?>
 474                  </span>
 475                  <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
 476                      <label for="<?php echo $item->typ.'_'.$item->id.'_xxx';?>">
 477                          <?php print_string('not_selected', 'feedback');?>&nbsp;
 478                      </label>
 479                  </span>
 480              </li>
 481          <?php
 482          }
 483  
 484          switch($info->subtype) {
 485              case 'r':
 486                  $this->print_item_radio($presentation, $item, $value, $info, $align);
 487                  break;
 488              case 'c':
 489                  $this->print_item_check($presentation, $item, $value, $info, $align);
 490                  break;
 491              case 'd':
 492                  $this->print_item_dropdown($presentation, $item, $value, $info, $align);
 493                  break;
 494          }
 495          if ($info->subtype == 'r' || $info->subtype == 'c') {
 496              // if (r)adio buttons or (c)heckboxes
 497              echo '</ul>';
 498              echo '</fieldset>';
 499          }
 500          echo '</div>';
 501      }
 502  
 503      /**
 504       * print the item at the complete-page of feedback
 505       *
 506       * @global object
 507       * @param object $item
 508       * @param string $value
 509       * @return void
 510       */
 511      public function print_item_show_value($item, $value = null) {
 512          global $OUTPUT;
 513          $info = $this->get_info($item);
 514          $align = right_to_left() ? 'right' : 'left';
 515  
 516          if ($value == null) {
 517              $value = array();
 518          }
 519  
 520          $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
 521  
 522          //test if required and no value is set so we have to mark this item
 523          //we have to differ check and the other subtypes
 524          if ($info->subtype == 'c') {
 525              if (is_array($value)) {
 526                  $values = $value;
 527              } else {
 528                  $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
 529              }
 530          }
 531          $requiredmark = '';
 532          if ($item->required == 1) {
 533              $requiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
 534                  get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
 535          }
 536  
 537          //print the question and label
 538          echo '<div class="feedback_item_label_'.$align.'">';
 539          echo '('.$item->label.') ';
 540          echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
 541          echo '</div>';
 542  
 543          //print the presentation
 544          echo '<div class="feedback_item_presentation_'.$align.'">';
 545          $index = 1;
 546          if ($info->subtype == 'c') {
 547              echo $OUTPUT->box_start('generalbox boxalign'.$align);
 548              foreach ($presentation as $pres) {
 549                  foreach ($values as $val) {
 550                      if ($val == $index) {
 551                          echo '<div class="feedback_item_multianswer">';
 552                          echo format_text($pres, FORMAT_HTML, array('noclean' => true, 'para' => false));
 553                          echo '</div>';
 554                          break;
 555                      }
 556                  }
 557                  $index++;
 558              }
 559              echo $OUTPUT->box_end();
 560          } else {
 561              foreach ($presentation as $pres) {
 562                  if ($value == $index) {
 563                      echo $OUTPUT->box_start('generalbox boxalign'.$align);
 564                      echo format_text($pres, FORMAT_HTML, array('noclean' => true, 'para' => false));
 565                      echo $OUTPUT->box_end();
 566                      break;
 567                  }
 568                  $index++;
 569              }
 570          }
 571          echo '</div>';
 572      }
 573  
 574      public function check_value($value, $item) {
 575          $info = $this->get_info($item);
 576  
 577          if ($item->required != 1) {
 578              return true;
 579          }
 580  
 581          if (!isset($value) OR !is_array($value) OR $value[0] == '' OR $value[0] == 0) {
 582              return false;
 583          }
 584  
 585          return true;
 586      }
 587  
 588      public function create_value($data) {
 589          $vallist = $data;
 590          if (is_array($vallist)) {
 591              $vallist = array_unique($vallist);
 592          }
 593          return trim($this->item_array_to_string($vallist));
 594      }
 595  
 596      //compares the dbvalue with the dependvalue
 597      //dbvalue is the number of one selection
 598      //dependvalue is the presentation of one selection
 599      public function compare_value($item, $dbvalue, $dependvalue) {
 600  
 601          if (is_array($dbvalue)) {
 602              $dbvalues = $dbvalue;
 603          } else {
 604              $dbvalues = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $dbvalue);
 605          }
 606  
 607          $info = $this->get_info($item);
 608          $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
 609          $index = 1;
 610          foreach ($presentation as $pres) {
 611              foreach ($dbvalues as $dbval) {
 612                  if ($dbval == $index AND trim($pres) == $dependvalue) {
 613                      return true;
 614                  }
 615              }
 616              $index++;
 617          }
 618          return false;
 619      }
 620  
 621      public function get_presentation($data) {
 622          $present = str_replace("\n", FEEDBACK_MULTICHOICE_LINE_SEP, trim($data->itemvalues));
 623          if (!isset($data->subtype)) {
 624              $subtype = 'r';
 625          } else {
 626              $subtype = substr($data->subtype, 0, 1);
 627          }
 628          if (isset($data->horizontal) AND $data->horizontal == 1 AND $subtype != 'd') {
 629              $present .= FEEDBACK_MULTICHOICE_ADJUST_SEP.'1';
 630          }
 631          return $subtype.FEEDBACK_MULTICHOICE_TYPE_SEP.$present;
 632      }
 633  
 634      public function get_hasvalue() {
 635          return 1;
 636      }
 637  
 638      public function get_info($item) {
 639          $presentation = empty($item->presentation) ? '' : $item->presentation;
 640  
 641          $info = new stdClass();
 642          //check the subtype of the multichoice
 643          //it can be check(c), radio(r) or dropdown(d)
 644          $info->subtype = '';
 645          $info->presentation = '';
 646          $info->horizontal = false;
 647  
 648          $parts = explode(FEEDBACK_MULTICHOICE_TYPE_SEP, $item->presentation);
 649          @list($info->subtype, $info->presentation) = $parts;
 650          if (!isset($info->subtype)) {
 651              $info->subtype = 'r';
 652          }
 653  
 654          if ($info->subtype != 'd') {
 655              $parts = explode(FEEDBACK_MULTICHOICE_ADJUST_SEP, $info->presentation);
 656              @list($info->presentation, $info->horizontal) = $parts;
 657              if (isset($info->horizontal) AND $info->horizontal == 1) {
 658                  $info->horizontal = true;
 659              } else {
 660                  $info->horizontal = false;
 661              }
 662          }
 663          return $info;
 664      }
 665  
 666      private function item_array_to_string($value) {
 667          if (!is_array($value)) {
 668              return $value;
 669          }
 670          $retval = '';
 671          $arrvals = array_values($value);
 672          $arrvals = clean_param_array($arrvals, PARAM_INT);  //prevent sql-injection
 673          $retval = $arrvals[0];
 674          $sizeofarrvals = count($arrvals);
 675          for ($i = 1; $i < $sizeofarrvals; $i++) {
 676              $retval .= FEEDBACK_MULTICHOICE_LINE_SEP.$arrvals[$i];
 677          }
 678          return $retval;
 679      }
 680  
 681      private function print_item_radio($presentation, $item, $value, $info, $align) {
 682          $index = 1;
 683          $checked = '';
 684  
 685          if (is_array($value)) {
 686              $values = $value;
 687          } else {
 688              $values = array($value);
 689          }
 690  
 691          if ($info->horizontal) {
 692              $hv = 'h';
 693          } else {
 694              $hv = 'v';
 695          }
 696  
 697          foreach ($presentation as $radio) {
 698              foreach ($values as $val) {
 699                  if ($val == $index) {
 700                      $checked = 'checked="checked"';
 701                      break;
 702                  } else {
 703                      $checked = '';
 704                  }
 705              }
 706              $inputname = $item->typ . '_' . $item->id;
 707              $inputid = $inputname.'_'.$index;
 708          ?>
 709              <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
 710                  <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
 711                      <?php
 712                          echo '<input type="radio" '.
 713                                  'name="'.$inputname.'[]" '.
 714                                  'id="'.$inputid.'" '.
 715                                  'value="'.$index.'" '.$checked.' />';
 716                      ?>
 717                  </span>
 718                  <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
 719                      <label for="<?php echo $inputid;?>">
 720                          <?php echo format_text($radio, FORMAT_HTML, array('noclean' => true, 'para' => false));?>&nbsp;
 721                      </label>
 722                  </span>
 723              </li>
 724          <?php
 725              $index++;
 726          }
 727      }
 728  
 729      private function print_item_check($presentation, $item, $value, $info, $align) {
 730  
 731          if (is_array($value)) {
 732              $values = $value;
 733          } else {
 734              $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
 735          }
 736  
 737          if ($info->horizontal) {
 738              $hv = 'h';
 739          } else {
 740              $hv = 'v';
 741          }
 742  
 743          $index = 1;
 744          $checked = '';
 745          foreach ($presentation as $check) {
 746              foreach ($values as $val) {
 747                  if ($val == $index) {
 748                      $checked = 'checked="checked"';
 749                      break;
 750                  } else {
 751                      $checked = '';
 752                  }
 753              }
 754              $inputname = $item->typ. '_' . $item->id;
 755              $inputid = $item->typ. '_' . $item->id.'_'.$index;
 756          ?>
 757              <li class="feedback_item_check_<?php echo $hv.'_'.$align;?>">
 758                  <span class="feedback_item_check_<?php echo $hv.'_'.$align;?>">
 759                      <?php
 760                          echo '<input type="checkbox" '.
 761                                'name="'.$inputname.'[]" '.
 762                                'id="'.$inputid.'" '.
 763                                'value="'.$index.'" '.$checked.' />';
 764                      ?>
 765                  </span>
 766                  <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
 767                      <label for="<?php echo $inputid;?>">
 768                          <?php echo format_text($check, FORMAT_HTML, array('noclean' => true, 'para' => false));?>&nbsp;
 769                      </label>
 770                  </span>
 771              </li>
 772          <?php
 773              $index++;
 774          }
 775      }
 776  
 777      private function print_item_dropdown($presentation, $item, $value, $info, $align) {
 778          if (is_array($value)) {
 779              $values = $value;
 780          } else {
 781              $values = array($value);
 782          }
 783  
 784          if ($info->horizontal) {
 785              $hv = 'h';
 786          } else {
 787              $hv = 'v';
 788          }
 789  
 790          ?>
 791          <div class="feedback_item_select_<?php echo $hv.'_'.$align;?>">
 792              <select  id="<?php echo $item->typ .'_' . $item->id;?>" name="<?php echo $item->typ .'_' . $item->id;?>[]" size="1">
 793                  <option value="0">&nbsp;</option>
 794                  <?php
 795                  $index = 1;
 796                  $selected = '';
 797                  foreach ($presentation as $dropdown) {
 798                      foreach ($values as $val) {
 799                          if ($val == $index) {
 800                              $selected = 'selected="selected"';
 801                              break;
 802                          } else {
 803                              $selected = '';
 804                          }
 805                      }
 806                  ?>
 807                      <option value="<?php echo $index;?>" <?php echo $selected;?>>
 808                          <?php echo format_text($dropdown, FORMAT_HTML, array('noclean' => true, 'para' => false));?>
 809                      </option>
 810                  <?php
 811                      $index++;
 812                  }
 813                  ?>
 814              </select>
 815          </div>
 816          <?php
 817      }
 818  
 819      public function set_ignoreempty($item, $ignoreempty=true) {
 820          $item->options = str_replace(FEEDBACK_MULTICHOICE_IGNOREEMPTY, '', $item->options);
 821          if ($ignoreempty) {
 822              $item->options .= FEEDBACK_MULTICHOICE_IGNOREEMPTY;
 823          }
 824      }
 825  
 826      public function ignoreempty($item) {
 827          if (strstr($item->options, FEEDBACK_MULTICHOICE_IGNOREEMPTY)) {
 828              return true;
 829          }
 830          return false;
 831      }
 832  
 833      public function set_hidenoselect($item, $hidenoselect=true) {
 834          $item->options = str_replace(FEEDBACK_MULTICHOICE_HIDENOSELECT, '', $item->options);
 835          if ($hidenoselect) {
 836              $item->options .= FEEDBACK_MULTICHOICE_HIDENOSELECT;
 837          }
 838      }
 839  
 840      public function hidenoselect($item) {
 841          if (strstr($item->options, FEEDBACK_MULTICHOICE_HIDENOSELECT)) {
 842              return true;
 843          }
 844          return false;
 845      }
 846  
 847      public function can_switch_require() {
 848          return true;
 849      }
 850  
 851      public function value_type() {
 852          return PARAM_INT;
 853      }
 854  
 855      public function value_is_array() {
 856          return true;
 857      }
 858  
 859      public function clean_input_value($value) {
 860          return clean_param_array($value, $this->value_type());
 861      }
 862  }


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