[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/numerical/ -> renderer.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  
  18  /**
  19   * Numerical question renderer class.
  20   *
  21   * @package qtype_numerical
  22   * @copyright 2009 The Open University
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  /**
  28   * Generates the output for short answer questions.
  29   *
  30   * @copyright 2009 The Open University
  31   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class qtype_numerical_renderer extends qtype_renderer {
  34      public function formulation_and_controls(question_attempt $qa,
  35              question_display_options $options) {
  36  
  37          $question = $qa->get_question();
  38          $currentanswer = $qa->get_last_qt_var('answer');
  39          if ($question->has_separate_unit_field()) {
  40              $selectedunit = $qa->get_last_qt_var('unit');
  41          } else {
  42              $selectedunit = null;
  43          }
  44  
  45          $inputname = $qa->get_qt_field_name('answer');
  46          $inputattributes = array(
  47              'type' => 'text',
  48              'name' => $inputname,
  49              'value' => $currentanswer,
  50              'id' => $inputname,
  51              'size' => 80,
  52          );
  53  
  54          if ($options->readonly) {
  55              $inputattributes['readonly'] = 'readonly';
  56          }
  57  
  58          $feedbackimg = '';
  59          if ($options->correctness) {
  60              list($value, $unit, $multiplier) = $question->ap->apply_units(
  61                      $currentanswer, $selectedunit);
  62              $answer = $question->get_matching_answer($value, $multiplier);
  63              if ($answer) {
  64                  $fraction = $question->apply_unit_penalty($answer->fraction, $answer->unitisright);
  65              } else {
  66                  $fraction = 0;
  67              }
  68              $inputattributes['class'] = $this->feedback_class($fraction);
  69              $feedbackimg = $this->feedback_image($fraction);
  70          }
  71  
  72          $questiontext = $question->format_questiontext($qa);
  73          $placeholder = false;
  74          if (preg_match('/_____+/', $questiontext, $matches)) {
  75              $placeholder = $matches[0];
  76              $inputattributes['size'] = round(strlen($placeholder) * 1.1);
  77          }
  78  
  79          $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
  80  
  81          if ($question->has_separate_unit_field()) {
  82              if ($question->unitdisplay == qtype_numerical::UNITRADIO) {
  83                  $choices = array();
  84                  $i = 1;
  85                  foreach ($question->ap->get_unit_options() as $unit) {
  86                      $id = $qa->get_qt_field_name('unit') . '_' . $i++;
  87                      $radioattrs = array('type' => 'radio', 'id' => $id, 'value' => $unit,
  88                              'name' => $qa->get_qt_field_name('unit'));
  89                      if ($unit == $selectedunit) {
  90                          $radioattrs['checked'] = 'checked';
  91                      }
  92                      $choices[] = html_writer::tag('label',
  93                              html_writer::empty_tag('input', $radioattrs) . $unit,
  94                              array('for' => $id, 'class' => 'unitchoice'));
  95                  }
  96  
  97                  $unitchoice = html_writer::tag('span', implode(' ', $choices),
  98                          array('class' => 'unitchoices'));
  99  
 100              } else if ($question->unitdisplay == qtype_numerical::UNITSELECT) {
 101                  $unitchoice = html_writer::label(get_string('selectunit', 'qtype_numerical'),
 102                          'menu' . $qa->get_qt_field_name('unit'), false, array('class' => 'accesshide'));
 103                  $unitchoice .= html_writer::select($question->ap->get_unit_options(),
 104                          $qa->get_qt_field_name('unit'), $selectedunit, array(''=>'choosedots'),
 105                          array('disabled' => $options->readonly));
 106              }
 107  
 108              if ($question->ap->are_units_before()) {
 109                  $input = $unitchoice . ' ' . $input;
 110              } else {
 111                  $input = $input . ' ' . $unitchoice;
 112              }
 113          }
 114  
 115          if ($placeholder) {
 116              $inputinplace = html_writer::tag('label', get_string('answer'),
 117                      array('for' => $inputattributes['id'], 'class' => 'accesshide'));
 118              $inputinplace .= $input;
 119              $questiontext = substr_replace($questiontext, $inputinplace,
 120                      strpos($questiontext, $placeholder), strlen($placeholder));
 121          }
 122  
 123          $result = html_writer::tag('div', $questiontext, array('class' => 'qtext'));
 124  
 125          if (!$placeholder) {
 126              $result .= html_writer::start_tag('div', array('class' => 'ablock'));
 127              $result .= html_writer::tag('label', get_string('answercolon', 'qtype_numerical'), array('for' => $inputattributes['id']));
 128              $result .= html_writer::tag('span', $input, array('class' => 'answer'));
 129              $result .= html_writer::end_tag('div');
 130          }
 131  
 132          if ($qa->get_state() == question_state::$invalid) {
 133              $result .= html_writer::nonempty_tag('div',
 134                      $question->get_validation_error(array('answer' => $currentanswer, 'unit' => $selectedunit)),
 135                      array('class' => 'validationerror'));
 136          }
 137  
 138          return $result;
 139      }
 140  
 141      public function specific_feedback(question_attempt $qa) {
 142          $question = $qa->get_question();
 143  
 144          if ($question->has_separate_unit_field()) {
 145              $selectedunit = $qa->get_last_qt_var('unit');
 146          } else {
 147              $selectedunit = null;
 148          }
 149          list($value, $unit, $multiplier) = $question->ap->apply_units(
 150                  $qa->get_last_qt_var('answer'), $selectedunit);
 151          $answer = $question->get_matching_answer($value, $multiplier);
 152  
 153          if ($answer && $answer->feedback) {
 154              $feedback = $question->format_text($answer->feedback, $answer->feedbackformat,
 155                      $qa, 'question', 'answerfeedback', $answer->id);
 156          } else {
 157              $feedback = '';
 158          }
 159  
 160          if ($question->unitgradingtype && !$question->ap->is_known_unit($unit)) {
 161              $feedback .= html_writer::tag('p', get_string('unitincorrect', 'qtype_numerical'));
 162          }
 163  
 164          return $feedback;
 165      }
 166  
 167      public function correct_response(question_attempt $qa) {
 168          $question = $qa->get_question();
 169          $answer = $question->get_correct_answer();
 170          if (!$answer) {
 171              return '';
 172          }
 173  
 174          $response = str_replace('.', $question->ap->get_point(), $answer->answer);
 175          if ($question->unitdisplay != qtype_numerical::UNITNONE) {
 176              $response = $question->ap->add_unit($response);
 177          }
 178  
 179          return get_string('correctansweris', 'qtype_shortanswer', $response);
 180      }
 181  }


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