Merge branch 'MDL-81358' of https://github.com/paulholden/moodle
[moodle.git] / question / type / gapselect / renderer.php
blob4faea7767d5fbb20419b426a6b64ac60bf721fd8
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.
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 * Select from drop down list question renderer class.
20 * @package qtype_gapselect
21 * @copyright 2011 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot . '/question/type/gapselect/rendererbase.php');
31 /**
32 * Generates the output for select missing words questions.
34 * @copyright 2011 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class qtype_gapselect_renderer extends qtype_elements_embedded_in_question_text_renderer {
38 protected function embedded_element(question_attempt $qa, $place,
39 question_display_options $options) {
40 $question = $qa->get_question();
41 $group = $question->places[$place];
43 $fieldname = $question->field($place);
45 $value = $qa->get_last_qt_var($question->field($place));
47 $attributes = [
48 'id' => $this->box_id($qa, 'p' . $place),
49 'class' => 'custom-select place' . $place,
51 $groupclass = 'group' . $group;
53 if ($options->readonly) {
54 $attributes['disabled'] = 'disabled';
57 $orderedchoices = $question->get_ordered_choices($group);
58 $selectoptions = [];
59 foreach ($orderedchoices as $orderedchoicevalue => $orderedchoice) {
60 $selectoptions[$orderedchoicevalue] = format_string($orderedchoice->text);
63 $feedbackimage = '';
64 if ($options->correctness) {
65 $response = $qa->get_last_qt_data();
66 if (array_key_exists($fieldname, $response)) {
67 $fraction = (int) ($response[$fieldname] ==
68 $question->get_right_choice_for($place));
69 $attributes['class'] .= ' ' . $this->feedback_class($fraction);
70 $feedbackimage = $this->feedback_image($fraction);
74 $label = $options->add_question_identifier_to_label(get_string('blanknumber', 'qtype_gapselect', $place));
75 // Use non-breaking space instead of 'Choose...'.
76 $selecthtml = html_writer::label($label, $attributes['id'], false, ['class' => 'sr-only']);
77 $selecthtml .= html_writer::select($selectoptions, $qa->get_qt_field_name($fieldname),
78 $value, '&nbsp;', $attributes) . ' ' . $feedbackimage;
79 return html_writer::tag('span', $selecthtml, ['class' => 'control '.$groupclass]);