Commit | Line | Data |
---|---|---|
0d24b17a | 1 | <?php |
f7dd2d44 TH |
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 | * Base class for rendering question types like this one. | |
19 | * | |
9df0480d | 20 | * @package qtype |
f7dd2d44 | 21 | * @subpackage gapselect |
9df0480d TH |
22 | * @copyright 2011 The Open University |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
f7dd2d44 TH |
24 | */ |
25 | ||
26 | ||
b28ad86a TH |
27 | defined('MOODLE_INTERNAL') || die(); |
28 | ||
29 | ||
0d24b17a | 30 | /** |
060e0294 TH |
31 | * Generates the output for question types where the question includes embedded |
32 | * interactive elements in the question text. | |
0d24b17a | 33 | * |
9df0480d TH |
34 | * @copyright 2011 The Open University |
35 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
0d24b17a | 36 | */ |
060e0294 TH |
37 | abstract class qtype_elements_embedded_in_question_text_renderer |
38 | extends qtype_with_combined_feedback_renderer { | |
0d24b17a TH |
39 | public function formulation_and_controls(question_attempt $qa, |
40 | question_display_options $options) { | |
41 | ||
42 | $question = $qa->get_question(); | |
43 | ||
44 | $questiontext = ''; | |
45 | foreach ($question->textfragments as $i => $fragment) { | |
46 | if ($i > 0) { | |
47 | $questiontext .= $this->embedded_element($qa, $i, $options); | |
48 | } | |
49 | $questiontext .= $fragment; | |
50 | } | |
51 | ||
0d24b17a | 52 | $result = ''; |
43df6cac | 53 | $result .= html_writer::tag('div', $question->format_text($questiontext, |
4ffe7ac4 | 54 | $question->questiontextformat, $qa, 'question', 'questiontext', $question->id), |
0f5bb175 | 55 | array('class' => $this->qtext_classname(), 'id' => $this->qtext_id($qa))); |
0d24b17a TH |
56 | |
57 | $result .= $this->post_qtext_elements($qa, $options); | |
58 | ||
59 | if ($qa->get_state() == question_state::$invalid) { | |
60 | $result .= html_writer::nonempty_tag('div', | |
61 | $question->get_validation_error($qa->get_last_qt_data()), | |
62 | array('class' => 'validationerror')); | |
63 | } | |
64 | ||
65 | return $result; | |
66 | } | |
67 | ||
68 | protected function qtext_classname() { | |
69 | return 'qtext'; | |
70 | } | |
71 | ||
0f5bb175 JP |
72 | protected function qtext_id($qa) { |
73 | return str_replace(':', '_', $qa->get_qt_field_name('')); | |
74 | } | |
75 | ||
060e0294 TH |
76 | protected abstract function embedded_element(question_attempt $qa, $place, |
77 | question_display_options $options); | |
0d24b17a | 78 | |
060e0294 TH |
79 | protected function post_qtext_elements(question_attempt $qa, |
80 | question_display_options $options) { | |
0d24b17a TH |
81 | return ''; |
82 | } | |
83 | ||
33be3558 JP |
84 | protected function box_id(question_attempt $qa, $place) { |
85 | return str_replace(':', '_', $qa->get_qt_field_name($place)); | |
0d24b17a TH |
86 | } |
87 | ||
88 | public function specific_feedback(question_attempt $qa) { | |
89 | return $this->combined_feedback($qa); | |
90 | } | |
91 | ||
92 | public function correct_response(question_attempt $qa) { | |
93 | $question = $qa->get_question(); | |
94 | ||
95 | $correctanswer = ''; | |
96 | foreach ($question->textfragments as $i => $fragment) { | |
97 | if ($i > 0) { | |
98 | $group = $question->places[$i]; | |
99 | $choice = $question->choices[$group][$question->rightchoices[$i]]; | |
100 | $correctanswer .= '[' . str_replace('-', '‑', | |
101 | $choice->text) . ']'; | |
102 | } | |
103 | $correctanswer .= $fragment; | |
104 | } | |
105 | ||
106 | if (!empty($correctanswer)) { | |
107 | return get_string('correctansweris', 'qtype_gapselect', $correctanswer); | |
108 | } | |
109 | } | |
110 | } |