[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/shortanswer/ -> 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   * Short answer question renderer class.
  19   *
  20   * @package    qtype
  21   * @subpackage shortanswer
  22   * @copyright  2009 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * Generates the output for short answer questions.
  32   *
  33   * @copyright  2009 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qtype_shortanswer_renderer extends qtype_renderer {
  37      public function formulation_and_controls(question_attempt $qa,
  38              question_display_options $options) {
  39  
  40          $question = $qa->get_question();
  41          $currentanswer = $qa->get_last_qt_var('answer');
  42  
  43          $inputname = $qa->get_qt_field_name('answer');
  44          $inputattributes = array(
  45              'type' => 'text',
  46              'name' => $inputname,
  47              'value' => $currentanswer,
  48              'id' => $inputname,
  49              'size' => 80,
  50          );
  51  
  52          if ($options->readonly) {
  53              $inputattributes['readonly'] = 'readonly';
  54          }
  55  
  56          $feedbackimg = '';
  57          if ($options->correctness) {
  58              $answer = $question->get_matching_answer(array('answer' => $currentanswer));
  59              if ($answer) {
  60                  $fraction = $answer->fraction;
  61              } else {
  62                  $fraction = 0;
  63              }
  64              $inputattributes['class'] = $this->feedback_class($fraction);
  65              $feedbackimg = $this->feedback_image($fraction);
  66          }
  67  
  68          $questiontext = $question->format_questiontext($qa);
  69          $placeholder = false;
  70          if (preg_match('/_____+/', $questiontext, $matches)) {
  71              $placeholder = $matches[0];
  72              $inputattributes['size'] = round(strlen($placeholder) * 1.1);
  73          }
  74          $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
  75  
  76          if ($placeholder) {
  77              $inputinplace = html_writer::tag('label', get_string('answer'),
  78                      array('for' => $inputattributes['id'], 'class' => 'accesshide'));
  79              $inputinplace .= $input;
  80              $questiontext = substr_replace($questiontext, $inputinplace,
  81                      strpos($questiontext, $placeholder), strlen($placeholder));
  82          }
  83  
  84          $result = html_writer::tag('div', $questiontext, array('class' => 'qtext'));
  85  
  86          if (!$placeholder) {
  87              $result .= html_writer::start_tag('div', array('class' => 'ablock'));
  88              $result .= html_writer::tag('label', get_string('answer', 'qtype_shortanswer',
  89                      html_writer::tag('span', $input, array('class' => 'answer'))),
  90                      array('for' => $inputattributes['id']));
  91              $result .= html_writer::end_tag('div');
  92          }
  93  
  94          if ($qa->get_state() == question_state::$invalid) {
  95              $result .= html_writer::nonempty_tag('div',
  96                      $question->get_validation_error(array('answer' => $currentanswer)),
  97                      array('class' => 'validationerror'));
  98          }
  99  
 100          return $result;
 101      }
 102  
 103      public function specific_feedback(question_attempt $qa) {
 104          $question = $qa->get_question();
 105  
 106          $answer = $question->get_matching_answer(array('answer' => $qa->get_last_qt_var('answer')));
 107          if (!$answer || !$answer->feedback) {
 108              return '';
 109          }
 110  
 111          return $question->format_text($answer->feedback, $answer->feedbackformat,
 112                  $qa, 'question', 'answerfeedback', $answer->id);
 113      }
 114  
 115      public function correct_response(question_attempt $qa) {
 116          $question = $qa->get_question();
 117  
 118          $answer = $question->get_matching_answer($question->get_correct_response());
 119          if (!$answer) {
 120              return '';
 121          }
 122  
 123          return get_string('correctansweris', 'qtype_shortanswer',
 124                  s($question->clean_response($answer->answer)));
 125      }
 126  }


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