MDL-47494 gapselect: Add @package and GPL boiler-plate to files in /question.
[moodle.git] / question / type / gapselect / rendererbase.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Base class for rendering question types like this one.
20  *
21  * @package qtype
22  * @subpackage gapselect
23  * @copyright 2011 The Open University
24  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
28 /**
29  * Generates the output for question types where the question includes embedded interactive elements in the
30  * question text.
31  *
32  * @copyright 2011 The Open University
33  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34  */
35 abstract class qtype_elements_embedded_in_question_text_renderer extends qtype_with_combined_feedback_renderer {
36     public function formulation_and_controls(question_attempt $qa,
37             question_display_options $options) {
39         $question = $qa->get_question();
41         $questiontext = '';
42         foreach ($question->textfragments as $i => $fragment) {
43             if ($i > 0) {
44                 $questiontext .= $this->embedded_element($qa, $i, $options);
45             }
46             $questiontext .= $fragment;
47         }
49         $result = '';
50         $result .= html_writer::tag('div', $question->format_text($questiontext,
51                 $qa, 'question', 'questiontext', $question->id),
52                 array('class' => $this->qtext_classname(), 'id' => $qa->get_qt_field_name('')));
54         $result .= $this->post_qtext_elements($qa, $options);
56         if ($qa->get_state() == question_state::$invalid) {
57             $result .= html_writer::nonempty_tag('div',
58                     $question->get_validation_error($qa->get_last_qt_data()),
59                     array('class' => 'validationerror'));
60         }
62         return $result;
63     }
65     protected function qtext_classname() {
66         return 'qtext';
67     }
69     protected abstract function embedded_element(question_attempt $qa, $place, question_display_options $options);
71     protected function post_qtext_elements(question_attempt $qa, question_display_options $options) {
72         return '';
73     }
75     protected function box_id(question_attempt $qa, $place, $group) {
76         return $qa->get_qt_field_name($place) . '_' . $group;
77     }
79     public function specific_feedback(question_attempt $qa) {
80         return $this->combined_feedback($qa);
81     }
83     public function correct_response(question_attempt $qa) {
84         $question = $qa->get_question();
86         $correctanswer = '';
87         foreach ($question->textfragments as $i => $fragment) {
88             if ($i > 0) {
89                 $group = $question->places[$i];
90                 $choice = $question->choices[$group][$question->rightchoices[$i]];
91                 $correctanswer .= '[' . str_replace('-', '&#x2011;',
92                         $choice->text) . ']';
93             }
94             $correctanswer .= $fragment;
95         }
97         if (!empty($correctanswer)) {
98             return get_string('correctansweris', 'qtype_gapselect', $correctanswer);
99         }
100     }