PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/ddimageortext/questiontype.php

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