MDL-47494 gapselect: work-in-progress converting the ddwtos and gapselect qtypes.
[moodle.git] / question / type / gapselect / rendererbase.php
CommitLineData
0d24b17a
TH
1<?php
2/**
3 * Generates the output for question types where the question includes embedded interactive elements in the
4 * question text.
5 *
6 * @copyright 2010 The Open University
7 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
8 */
9abstract class qtype_elements_embedded_in_question_text_renderer extends qtype_with_combined_feedback_renderer {
10 public function formulation_and_controls(question_attempt $qa,
11 question_display_options $options) {
12
13 $question = $qa->get_question();
14
15 $questiontext = '';
16 foreach ($question->textfragments as $i => $fragment) {
17 if ($i > 0) {
18 $questiontext .= $this->embedded_element($qa, $i, $options);
19 }
20 $questiontext .= $fragment;
21 }
22
23
24 $result = '';
25 $result .= html_writer::tag('div', $question->format_text($questiontext),
26 array('class' => $this->qtext_classname(), 'id' => $qa->get_qt_field_name('')));
27
28 $result .= $this->post_qtext_elements($qa, $options);
29
30 if ($qa->get_state() == question_state::$invalid) {
31 $result .= html_writer::nonempty_tag('div',
32 $question->get_validation_error($qa->get_last_qt_data()),
33 array('class' => 'validationerror'));
34 }
35
36 return $result;
37 }
38
39 protected function qtext_classname() {
40 return 'qtext';
41 }
42
43 protected abstract function embedded_element(question_attempt $qa, $place, question_display_options $options);
44
45 protected function post_qtext_elements(question_attempt $qa, question_display_options $options) {
46 return '';
47 }
48
49 protected function box_id(question_attempt $qa, $place, $group) {
50 return $qa->get_qt_field_name($place) . '_' . $group;
51 }
52
53 public function specific_feedback(question_attempt $qa) {
54 return $this->combined_feedback($qa);
55 }
56
57 public function correct_response(question_attempt $qa) {
58 $question = $qa->get_question();
59
60 $correctanswer = '';
61 foreach ($question->textfragments as $i => $fragment) {
62 if ($i > 0) {
63 $group = $question->places[$i];
64 $choice = $question->choices[$group][$question->rightchoices[$i]];
65 $correctanswer .= '[' . str_replace('-', '&#x2011;',
66 $choice->text) . ']';
67 }
68 $correctanswer .= $fragment;
69 }
70
71 if (!empty($correctanswer)) {
72 return get_string('correctansweris', 'qtype_gapselect', $correctanswer);
73 }
74 }
75}