Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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', $options->add_question_identifier_to_label(get_string('answer')),
  79                      array('for' => $inputattributes['id'], 'class' => 'sr-only'));
  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              $answerspan = html_writer::tag('span', $input, array('class' => 'answer'));
  90              $label = $options->add_question_identifier_to_label(get_string('answercolon', 'qtype_numerical'), true);
  91              $result .= html_writer::tag('label', $label . $answerspan,
  92                      array('for' => $inputattributes['id']));
  93              $result .= html_writer::end_tag('div');
  94          }
  95  
  96          if ($qa->get_state() == question_state::$invalid) {
  97              $result .= html_writer::nonempty_tag('div',
  98                      $question->get_validation_error(array('answer' => $currentanswer)),
  99                      array('class' => 'validationerror'));
 100          }
 101  
 102          return $result;
 103      }
 104  
 105      public function specific_feedback(question_attempt $qa) {
 106          $question = $qa->get_question();
 107  
 108          $answer = $question->get_matching_answer(array('answer' => $qa->get_last_qt_var('answer')));
 109          if (!$answer || !$answer->feedback) {
 110              return '';
 111          }
 112  
 113          return $question->format_text($answer->feedback, $answer->feedbackformat,
 114                  $qa, 'question', 'answerfeedback', $answer->id);
 115      }
 116  
 117      public function correct_response(question_attempt $qa) {
 118          $question = $qa->get_question();
 119  
 120          $answer = $question->get_matching_answer($question->get_correct_response());
 121          if (!$answer) {
 122              return '';
 123          }
 124  
 125          return get_string('correctansweris', 'qtype_shortanswer',
 126                  s($question->clean_response($answer->answer)));
 127      }
 128  }