Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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/>. 16 17 /** 18 * Question type class for the drag-and-drop onto image question type. 19 * 20 * @package qtype_ddimageortext 21 * @copyright 2009 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once($CFG->dirroot . '/question/type/ddimageortext/questiontypebase.php'); 29 30 /** 31 * The drag-and-drop onto image question type class. 32 * 33 * @copyright 2009 The Open University 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class qtype_ddimageortext extends qtype_ddtoimage_base { 37 38 protected function make_choice($dragdata) { 39 return new qtype_ddimageortext_drag_item($dragdata->label, $dragdata->no, 40 $dragdata->draggroup, $dragdata->infinite, $dragdata->id); 41 } 42 43 protected function make_place($dropzonedata) { 44 return new qtype_ddimageortext_drop_zone($dropzonedata->label, $dropzonedata->no, 45 $dropzonedata->group, 46 $dropzonedata->xleft, $dropzonedata->ytop); 47 } 48 49 protected function make_hint($hint) { 50 return question_hint_with_parts::load_from_record($hint); 51 } 52 53 public function save_question_options($formdata) { 54 global $DB, $USER; 55 $context = $formdata->context; 56 57 $options = $DB->get_record('qtype_ddimageortext', array('questionid' => $formdata->id)); 58 if (!$options) { 59 $options = new stdClass(); 60 $options->questionid = $formdata->id; 61 $options->correctfeedback = ''; 62 $options->partiallycorrectfeedback = ''; 63 $options->incorrectfeedback = ''; 64 $options->id = $DB->insert_record('qtype_ddimageortext', $options); 65 } 66 67 $options->shuffleanswers = !empty($formdata->shuffleanswers); 68 $options = $this->save_combined_feedback_helper($options, $formdata, $context, true); 69 $this->save_hints($formdata, true); 70 $DB->update_record('qtype_ddimageortext', $options); 71 $DB->delete_records('qtype_ddimageortext_drops', array('questionid' => $formdata->id)); 72 foreach (array_keys($formdata->drops) as $dropno) { 73 if ($formdata->drops[$dropno]['choice'] == 0) { 74 continue; 75 } 76 $drop = new stdClass(); 77 $drop->questionid = $formdata->id; 78 $drop->no = $dropno + 1; 79 $drop->xleft = $formdata->drops[$dropno]['xleft']; 80 $drop->ytop = $formdata->drops[$dropno]['ytop']; 81 $drop->choice = $formdata->drops[$dropno]['choice']; 82 $drop->label = $formdata->drops[$dropno]['droplabel']; 83 84 $DB->insert_record('qtype_ddimageortext_drops', $drop); 85 } 86 87 // An array of drag no -> drag id. 88 $olddragids = $DB->get_records_menu('qtype_ddimageortext_drags', 89 array('questionid' => $formdata->id), 90 '', 'no, id'); 91 foreach (array_keys($formdata->drags) as $dragno) { 92 $info = file_get_draft_area_info($formdata->dragitem[$dragno]); 93 if ($info['filecount'] > 0 || (trim($formdata->draglabel[$dragno]) != '')) { 94 $draftitemid = $formdata->dragitem[$dragno]; 95 96 $drag = new stdClass(); 97 $drag->questionid = $formdata->id; 98 $drag->no = $dragno + 1; 99 $drag->draggroup = $formdata->drags[$dragno]['draggroup']; 100 $drag->infinite = empty($formdata->drags[$dragno]['infinite']) ? 0 : 1; 101 $drag->label = $formdata->draglabel[$dragno]; 102 103 if (isset($olddragids[$dragno + 1])) { 104 $drag->id = $olddragids[$dragno + 1]; 105 unset($olddragids[$dragno + 1]); 106 $DB->update_record('qtype_ddimageortext_drags', $drag); 107 } else { 108 $drag->id = $DB->insert_record('qtype_ddimageortext_drags', $drag); 109 } 110 111 if ($formdata->drags[$dragno]['dragitemtype'] == 'image') { 112 file_save_draft_area_files($draftitemid, $formdata->context->id, 113 'qtype_ddimageortext', 'dragimage', $drag->id, 114 array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1)); 115 } else { 116 // Delete any existing files for draggable text item type. 117 $fs = get_file_storage(); 118 $fs->delete_area_files($formdata->context->id, 'qtype_ddimageortext', 119 'dragimage', $drag->id); 120 } 121 122 } 123 124 } 125 126 if (!empty($olddragids)) { 127 list($sql, $params) = $DB->get_in_or_equal(array_values($olddragids)); 128 $DB->delete_records_select('qtype_ddimageortext_drags', "id $sql", $params); 129 } 130 file_save_draft_area_files($formdata->bgimage, $formdata->context->id, 131 'qtype_ddimageortext', 'bgimage', $formdata->id, 132 array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1)); 133 } 134 public function move_files($questionid, $oldcontextid, $newcontextid) { 135 global $DB; 136 $fs = get_file_storage(); 137 138 parent::move_files($questionid, $oldcontextid, $newcontextid); 139 $fs->move_area_files_to_new_context($oldcontextid, 140 $newcontextid, 'qtype_ddimageortext', 'bgimage', $questionid); 141 $dragids = $DB->get_records_menu('qtype_ddimageortext_drags', 142 array('questionid' => $questionid), 'id', 'id,1'); 143 foreach ($dragids as $dragid => $notused) { 144 $fs->move_area_files_to_new_context($oldcontextid, 145 $newcontextid, 'qtype_ddimageortext', 'dragimage', $dragid); 146 } 147 148 $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid); 149 $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid); 150 } 151 152 /** 153 * Delete all the files belonging to this question. 154 * @param int $questionid the question being deleted. 155 * @param int $contextid the context the question is in. 156 */ 157 158 protected function delete_files($questionid, $contextid) { 159 global $DB; 160 $fs = get_file_storage(); 161 162 parent::delete_files($questionid, $contextid); 163 164 $dragids = $DB->get_records_menu('qtype_ddimageortext_drags', 165 array('questionid' => $questionid), 'id', 'id,1'); 166 foreach ($dragids as $dragid => $notused) { 167 $fs->delete_area_files($contextid, 'qtype_ddimageortext', 'dragimage', $dragid); 168 } 169 170 $this->delete_files_in_combined_feedback($questionid, $contextid); 171 $this->delete_files_in_hints($questionid, $contextid); 172 } 173 174 175 public function export_to_xml($question, qformat_xml $format, $extra = null) { 176 $fs = get_file_storage(); 177 $contextid = $question->contextid; 178 $output = ''; 179 180 if ($question->options->shuffleanswers) { 181 $output .= " <shuffleanswers/>\n"; 182 } 183 $output .= $format->write_combined_feedback($question->options, 184 $question->id, 185 $question->contextid); 186 $files = $fs->get_area_files($contextid, 'qtype_ddimageortext', 'bgimage', $question->id); 187 $output .= " ".$this->write_files($files, 2)."\n";; 188 189 foreach ($question->options->drags as $drag) { 190 $files = 191 $fs->get_area_files($contextid, 'qtype_ddimageortext', 'dragimage', $drag->id); 192 $output .= " <drag>\n"; 193 $output .= " <no>{$drag->no}</no>\n"; 194 $output .= $format->writetext($drag->label, 3)."\n"; 195 $output .= " <draggroup>{$drag->draggroup}</draggroup>\n"; 196 if ($drag->infinite) { 197 $output .= " <infinite/>\n"; 198 } 199 $output .= $this->write_files($files, 3); 200 $output .= " </drag>\n"; 201 } 202 foreach ($question->options->drops as $drop) { 203 $output .= " <drop>\n"; 204 $output .= $format->writetext($drop->label, 3); 205 $output .= " <no>{$drop->no}</no>\n"; 206 $output .= " <choice>{$drop->choice}</choice>\n"; 207 $output .= " <xleft>{$drop->xleft}</xleft>\n"; 208 $output .= " <ytop>{$drop->ytop}</ytop>\n"; 209 $output .= " </drop>\n"; 210 } 211 212 return $output; 213 } 214 215 public function import_from_xml($data, $question, qformat_xml $format, $extra=null) { 216 if (!isset($data['@']['type']) || $data['@']['type'] != 'ddimageortext') { 217 return false; 218 } 219 220 $question = $format->import_headers($data); 221 $question->qtype = 'ddimageortext'; 222 223 $question->shuffleanswers = array_key_exists('shuffleanswers', 224 $format->getpath($data, array('#'), array())); 225 226 $filexml = $format->getpath($data, array('#', 'file'), array()); 227 $question->bgimage = $format->import_files_as_draft($filexml); 228 $drags = $data['#']['drag']; 229 $question->drags = array(); 230 231 foreach ($drags as $dragxml) { 232 $dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0); 233 $dragindex = $dragno - 1; 234 $question->drags[$dragindex] = array(); 235 $question->draglabel[$dragindex] = 236 $format->getpath($dragxml, array('#', 'text', 0, '#'), '', true); 237 $question->drags[$dragindex]['infinite'] = array_key_exists('infinite', $dragxml['#']); 238 $question->drags[$dragindex]['draggroup'] = 239 $format->getpath($dragxml, array('#', 'draggroup', 0, '#'), 1); 240 $filexml = $format->getpath($dragxml, array('#', 'file'), array()); 241 $question->dragitem[$dragindex] = $format->import_files_as_draft($filexml); 242 if (count($filexml)) { 243 $question->drags[$dragindex]['dragitemtype'] = 'image'; 244 } else { 245 $question->drags[$dragindex]['dragitemtype'] = 'word'; 246 } 247 } 248 249 $drops = $data['#']['drop']; 250 $question->drops = array(); 251 foreach ($drops as $dropxml) { 252 $dropno = $format->getpath($dropxml, array('#', 'no', 0, '#'), 0); 253 $dropindex = $dropno - 1; 254 $question->drops[$dropindex] = array(); 255 $question->drops[$dropindex]['choice'] = 256 $format->getpath($dropxml, array('#', 'choice', 0, '#'), 0); 257 $question->drops[$dropindex]['droplabel'] = 258 $format->getpath($dropxml, array('#', 'text', 0, '#'), '', true); 259 $question->drops[$dropindex]['xleft'] = 260 $format->getpath($dropxml, array('#', 'xleft', 0, '#'), ''); 261 $question->drops[$dropindex]['ytop'] = 262 $format->getpath($dropxml, array('#', 'ytop', 0, '#'), ''); 263 } 264 265 $format->import_combined_feedback($question, $data, true); 266 $format->import_hints($question, $data, true, false, 267 $format->get_format($question->questiontextformat)); 268 269 return $question; 270 } 271 272 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body