Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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              'class' => 'form-control d-inline',
  51          );
  52  
  53          if ($options->readonly) {
  54              $inputattributes['readonly'] = 'readonly';
  55          }
  56  
  57          $feedbackimg = '';
  58          if ($options->correctness) {
  59              $answer = $question->get_matching_answer(array('answer' => $currentanswer));
  60              if ($answer) {
  61                  $fraction = $answer->fraction;
  62              } else {
  63                  $fraction = 0;
  64              }
  65              $inputattributes['class'] .= ' ' . $this->feedback_class($fraction);
  66              $feedbackimg = $this->feedback_image($fraction);
  67          }
  68  
  69          $questiontext = $question->format_questiontext($qa);
  70          $placeholder = false;
  71          if (preg_match('/_____+/', $questiontext, $matches)) {
  72              $placeholder = $matches[0];
  73              $inputattributes['size'] = round(strlen($placeholder) * 1.1);
  74          }
  75          $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
  76  
  77          if ($placeholder) {
  78              $inputinplace = html_writer::tag('label', get_string('answer'),
  79                      array('for' => $inputattributes['id'], 'class' => 'accesshide'));
  80              $inputinplace .= $input;
  81              $questiontext = substr_replace($questiontext, $inputinplace,
  82                      strpos($questiontext, $placeholder), strlen($placeholder));
  83          }
  84  
  85          $result = html_writer::tag('div', $questiontext, array('class' => 'qtext'));
  86  
  87          if (!$placeholder) {
  88              $result .= html_writer::start_tag('div', array('class' => 'ablock form-inline'));
  89              $result .= html_writer::tag('label', get_string('answer', 'qtype_shortanswer',
  90                      html_writer::tag('span', $input, array('class' => 'answer'))),
  91                      array('for' => $inputattributes['id']));
  92              $result .= html_writer::end_tag('div');
  93          }
  94  
  95          if ($qa->get_state() == question_state::$invalid) {
  96              $result .= html_writer::nonempty_tag('div',
  97                      $question->get_validation_error(array('answer' => $currentanswer)),
  98                      array('class' => 'validationerror'));
  99          }
 100  
 101          return $result;
 102      }
 103  
 104      public function specific_feedback(question_attempt $qa) {
 105          $question = $qa->get_question();
 106  
 107          $answer = $question->get_matching_answer(array('answer' => $qa->get_last_qt_var('answer')));
 108          if (!$answer || !$answer->feedback) {
 109              return '';
 110          }
 111  
 112          return $question->format_text($answer->feedback, $answer->feedbackformat,
 113                  $qa, 'question', 'answerfeedback', $answer->id);
 114      }
 115  
 116      public function correct_response(question_attempt $qa) {
 117          $question = $qa->get_question();
 118  
 119          $answer = $question->get_matching_answer($question->get_correct_response());
 120          if (!$answer) {
 121              return '';
 122          }
 123  
 124          return get_string('correctansweris', 'qtype_shortanswer',
 125                  s($question->clean_response($answer->answer)));
 126      }
 127  }