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