2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Drag-and-drop onto image question renderer class.
20 * @package qtype_ddimageortext
21 * @copyright 2010 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Generates the output for drag-and-drop onto image questions.
31 * @copyright 2010 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class qtype_ddtoimage_renderer_base
extends qtype_with_combined_feedback_renderer
{
36 public function clear_wrong(question_attempt
$qa) {
37 $question = $qa->get_question();
38 $response = $qa->get_last_qt_data();
40 if (!empty($response)) {
41 $cleanresponse = $question->clear_wrong_from_response($response);
43 $cleanresponse = $response;
45 $cleanresponsehtml = '';
46 foreach ($cleanresponse as $fieldname => $value) {
47 list (, $html) = $this->hidden_field_for_qt_var($qa, $fieldname, $value);
48 $cleanresponsehtml .= $html;
50 return $cleanresponsehtml;
53 public function formulation_and_controls(question_attempt
$qa,
54 question_display_options
$options) {
57 $question = $qa->get_question();
58 $response = $qa->get_last_qt_data();
60 $questiontext = $question->format_questiontext($qa);
62 $output = html_writer
::tag('div', $questiontext, array('class' => 'qtext'));
64 $bgimage = self
::get_url_for_image($qa, 'bgimage');
66 $img = html_writer
::empty_tag('img', array(
67 'src' => $bgimage, 'class' => 'dropbackground',
68 'alt' => get_string('dropbackground', 'qtype_ddimageortext')));
70 $droparea = html_writer
::tag('div', $img, array('class' => 'droparea'));
73 foreach ($question->choices
as $groupno => $group) {
74 $dragimagehomesgroup = '';
75 $orderedgroup = $question->get_ordered_choices($groupno);
76 foreach ($orderedgroup as $choiceno => $dragimage) {
77 $dragimageurl = self
::get_url_for_image($qa, 'dragimage', $dragimage->id
);
78 $classes = array("group{$groupno}",
80 "dragitemhomes{$dragimage->no}",
82 if ($dragimage->infinite
) {
83 $classes[] = 'infinite';
85 if ($dragimageurl === null) {
86 $classes[] = 'yui3-cssfonts';
87 $dragimagehomesgroup .= html_writer
::tag('div', $dragimage->text
,
88 array('src' => $dragimageurl, 'class' => join(' ', $classes)));
90 $dragimagehomesgroup .= html_writer
::empty_tag('img',
91 array('src' => $dragimageurl, 'alt' => $dragimage->text
,
92 'class' => join(' ', $classes)));
95 $dragimagehomes .= html_writer
::tag('div', $dragimagehomesgroup,
96 array('class' => 'dragitemgroup' . $groupno));
99 $dragitemsclass = 'dragitems';
100 if ($options->readonly
) {
101 $dragitemsclass .= ' readonly';
103 $dragitems = html_writer
::tag('div', $dragimagehomes, array('class' => $dragitemsclass));
104 $dropzones = html_writer
::tag('div', '', array('class' => 'dropzones'));
107 foreach ($question->places
as $placeno => $place) {
108 $varname = $question->field($placeno);
109 list($fieldname, $html) = $this->hidden_field_for_qt_var($qa, $varname);
111 $question->places
[$placeno]->fieldname
= $fieldname;
113 $output .= html_writer
::tag('div',
114 $droparea . $dragitems . $dropzones . $hiddens, array('class' => 'ddarea'));
115 $topnode = 'div#q'.$qa->get_slot().' div.ddarea';
116 $params = array('drops' => $question->places
,
117 'topnode' => $topnode,
118 'readonly' => $options->readonly
);
120 $PAGE->requires
->string_for_js('blank', 'qtype_ddimageortext');
121 $PAGE->requires
->yui_module('moodle-qtype_ddimageortext-dd',
122 'M.qtype_ddimageortext.init_question',
125 if ($qa->get_state() == question_state
::$invalid) {
126 $output .= html_writer
::nonempty_tag('div',
127 $question->get_validation_error($qa->get_last_qt_data()),
128 array('class' => 'validationerror'));
134 * Returns the URL for an image
136 * @param object $qa Question attempt object
137 * @param string $filearea File area descriptor
138 * @param int $itemid Item id to get
139 * @return string Output url, or null if not found
141 protected static function get_url_for_image(question_attempt
$qa, $filearea, $itemid = 0) {
142 $question = $qa->get_question();
143 $qubaid = $qa->get_usage_id();
144 $slot = $qa->get_slot();
145 $fs = get_file_storage();
146 if ($filearea == 'bgimage') {
147 $itemid = $question->id
;
149 $componentname = $question->qtype
->plugin_name();
150 $draftfiles = $fs->get_area_files($question->contextid
, $componentname,
151 $filearea, $itemid, 'id');
153 foreach ($draftfiles as $file) {
154 if ($file->is_directory()) {
157 $url = moodle_url
::make_pluginfile_url($question->contextid
, $componentname,
158 $filearea, "$qubaid/$slot/{$itemid}", '/',
159 $file->get_filename());
167 * Returns a hidden field for a qt variable
169 * @param object $qa Question attempt object
170 * @param string $varname The hidden var name
171 * @param string $value The hidden value
172 * @param array $classes Any additional css classes to apply
173 * @return array Array with field name and the html of the tag
175 protected function hidden_field_for_qt_var(question_attempt
$qa, $varname, $value = null,
177 if ($value === null) {
178 $value = $qa->get_last_qt_var($varname);
180 $fieldname = $qa->get_qt_field_name($varname);
181 $attributes = array('type' => 'hidden',
182 'id' => str_replace(':', '_', $fieldname),
183 'name' => $fieldname,
185 if ($classes !== null) {
186 $attributes['class'] = join(' ', $classes);
188 return array($fieldname, html_writer
::empty_tag('input', $attributes)."\n");
191 public function specific_feedback(question_attempt
$qa) {
192 return $this->combined_feedback($qa);
195 public function correct_response(question_attempt
$qa) {