Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / shortanswer / renderer.php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
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) {
40         $question = $qa->get_question();
41         $currentanswer = $qa->get_last_qt_var('answer');
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         );
53         if ($options->readonly) {
54             $inputattributes['readonly'] = 'readonly';
55         }
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         }
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;
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         }
85         $result = html_writer::tag('div', $questiontext, array('class' => 'qtext'));
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         }
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         }
101         return $result;
102     }
104     public function specific_feedback(question_attempt $qa) {
105         $question = $qa->get_question();
107         $answer = $question->get_matching_answer(array('answer' => $qa->get_last_qt_var('answer')));
108         if (!$answer || !$answer->feedback) {
109             return '';
110         }
112         return $question->format_text($answer->feedback, $answer->feedbackformat,
113                 $qa, 'question', 'answerfeedback', $answer->id);
114     }
116     public function correct_response(question_attempt $qa) {
117         $question = $qa->get_question();
119         $answer = $question->get_matching_answer($question->get_correct_response());
120         if (!$answer) {
121             return '';
122         }
124         return get_string('correctansweris', 'qtype_shortanswer',
125                 s($question->clean_response($answer->answer)));
126     }