Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / gapselect / renderer.php
CommitLineData
0d24b17a 1<?php
0d24b17a
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
0d24b17a
TH
17/**
18 * Select from drop down list question renderer class.
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
0d24b17a
TH
23 */
24
9df0480d 25
b28ad86a
TH
26defined('MOODLE_INTERNAL') || die();
27
0d24b17a
TH
28require_once($CFG->dirroot . '/question/type/gapselect/rendererbase.php');
29
30
31/**
32 * Generates the output for select missing words questions.
33 *
9df0480d
TH
34 * @copyright 2011 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0d24b17a
TH
36 */
37class qtype_gapselect_renderer extends qtype_elements_embedded_in_question_text_renderer {
060e0294
TH
38 protected function embedded_element(question_attempt $qa, $place,
39 question_display_options $options) {
0d24b17a
TH
40 $question = $qa->get_question();
41 $group = $question->places[$place];
42
43 $fieldname = $question->field($place);
44
45 $value = $qa->get_last_qt_var($question->field($place));
46
47 $attributes = array(
3cb75b2b 48 'id' => $this->box_id($qa, 'p' . $place),
12d6dfc1 49 'class' => 'custom-select place' . $place,
0d24b17a 50 );
43df6cac 51 $groupclass = 'group' . $group;
0d24b17a
TH
52
53 if ($options->readonly) {
54 $attributes['disabled'] = 'disabled';
55 }
56
57 $orderedchoices = $question->get_ordered_choices($group);
58 $selectoptions = array();
59 foreach ($orderedchoices as $orderedchoicevalue => $orderedchoice) {
0d3be6d6 60 $selectoptions[$orderedchoicevalue] = format_string($orderedchoice->text);
0d24b17a
TH
61 }
62
63 $feedbackimage = '';
64 if ($options->correctness) {
65 $response = $qa->get_last_qt_data();
66 if (array_key_exists($fieldname, $response)) {
060e0294
TH
67 $fraction = (int) ($response[$fieldname] ==
68 $question->get_right_choice_for($place));
3cb75b2b 69 $attributes['class'] .= ' ' . $this->feedback_class($fraction);
0d24b17a
TH
70 $feedbackimage = $this->feedback_image($fraction);
71 }
72 }
73
3e697596 74 // Use non-breaking space instead of 'Choose...'.
060e0294 75 $selecthtml = html_writer::select($selectoptions, $qa->get_qt_field_name($fieldname),
3e697596 76 $value, '&nbsp;', $attributes) . ' ' . $feedbackimage;
43df6cac 77 return html_writer::tag('span', $selecthtml, array('class' => 'control '.$groupclass));
0d24b17a
TH
78 }
79
80}