weekly release 4.3dev
[moodle.git] / question / type / gapselect / renderer.php
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.
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/>.
17 /**
18  * Select from drop down list question renderer class.
19  *
20  * @package    qtype_gapselect
21  * @copyright  2011 The Open University
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
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.
33  *
34  * @copyright  2011 The Open University
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
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,
50         ];
51         $groupclass = 'group' . $group;
53         if ($options->readonly) {
54             $attributes['disabled'] = 'disabled';
55         }
57         $orderedchoices = $question->get_ordered_choices($group);
58         $selectoptions = [];
59         foreach ($orderedchoices as $orderedchoicevalue => $orderedchoice) {
60             $selectoptions[$orderedchoicevalue] = format_string($orderedchoice->text);
61         }
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);
71             }
72         }
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]);
80     }
82 }