PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/question/type/ddimageortext/questiontype.php

https://github.com/mackensen/moodle
PHP | 277 lines | 202 code | 41 blank | 34 comment | 20 complexity | fe41a2c6b4c1a1c83b6fdf5745153127 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. /**
  26. * The drag-and-drop onto image question type class.
  27. *
  28. * @copyright 2009 The Open University
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class qtype_ddimageortext extends qtype_ddtoimage_base {
  32. protected function make_choice($dragdata) {
  33. return new qtype_ddimageortext_drag_item($dragdata->label, $dragdata->no,
  34. $dragdata->draggroup, $dragdata->infinite, $dragdata->id);
  35. }
  36. protected function make_place($dropzonedata) {
  37. return new qtype_ddimageortext_drop_zone($dropzonedata->label, $dropzonedata->no,
  38. $dropzonedata->group,
  39. $dropzonedata->xleft, $dropzonedata->ytop);
  40. }
  41. protected function make_hint($hint) {
  42. return question_hint_with_parts::load_from_record($hint);
  43. }
  44. public function save_defaults_for_new_questions(stdClass $fromform): void {
  45. parent::save_defaults_for_new_questions($fromform);
  46. $this->set_default_value('shuffleanswers', $fromform->shuffleanswers);
  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. file_save_draft_area_files($draftitemid, $formdata->context->id,
  101. 'qtype_ddimageortext', 'dragimage', $drag->id,
  102. array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
  103. } else {
  104. // Delete any existing files for draggable text item type.
  105. $fs = get_file_storage();
  106. $fs->delete_area_files($formdata->context->id, 'qtype_ddimageortext',
  107. 'dragimage', $drag->id);
  108. }
  109. }
  110. }
  111. if (!empty($olddragids)) {
  112. list($sql, $params) = $DB->get_in_or_equal(array_values($olddragids));
  113. $DB->delete_records_select('qtype_ddimageortext_drags', "id $sql", $params);
  114. }
  115. file_save_draft_area_files($formdata->bgimage, $formdata->context->id,
  116. 'qtype_ddimageortext', 'bgimage', $formdata->id,
  117. array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
  118. }
  119. public function move_files($questionid, $oldcontextid, $newcontextid) {
  120. global $DB;
  121. $fs = get_file_storage();
  122. parent::move_files($questionid, $oldcontextid, $newcontextid);
  123. $fs->move_area_files_to_new_context($oldcontextid,
  124. $newcontextid, 'qtype_ddimageortext', 'bgimage', $questionid);
  125. $dragids = $DB->get_records_menu('qtype_ddimageortext_drags',
  126. array('questionid' => $questionid), 'id', 'id,1');
  127. foreach ($dragids as $dragid => $notused) {
  128. $fs->move_area_files_to_new_context($oldcontextid,
  129. $newcontextid, 'qtype_ddimageortext', 'dragimage', $dragid);
  130. }
  131. $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
  132. $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
  133. }
  134. /**
  135. * Delete all the files belonging to this question.
  136. * @param int $questionid the question being deleted.
  137. * @param int $contextid the context the question is in.
  138. */
  139. protected function delete_files($questionid, $contextid) {
  140. global $DB;
  141. $fs = get_file_storage();
  142. parent::delete_files($questionid, $contextid);
  143. $dragids = $DB->get_records_menu('qtype_ddimageortext_drags',
  144. array('questionid' => $questionid), 'id', 'id,1');
  145. foreach ($dragids as $dragid => $notused) {
  146. $fs->delete_area_files($contextid, 'qtype_ddimageortext', 'dragimage', $dragid);
  147. }
  148. $this->delete_files_in_combined_feedback($questionid, $contextid);
  149. $this->delete_files_in_hints($questionid, $contextid);
  150. }
  151. public function export_to_xml($question, qformat_xml $format, $extra = null) {
  152. $fs = get_file_storage();
  153. $contextid = $question->contextid;
  154. $output = '';
  155. if ($question->options->shuffleanswers) {
  156. $output .= " <shuffleanswers/>\n";
  157. }
  158. $output .= $format->write_combined_feedback($question->options,
  159. $question->id,
  160. $question->contextid);
  161. $files = $fs->get_area_files($contextid, 'qtype_ddimageortext', 'bgimage', $question->id);
  162. $output .= " ".$this->write_files($files, 2)."\n";;
  163. foreach ($question->options->drags as $drag) {
  164. $files =
  165. $fs->get_area_files($contextid, 'qtype_ddimageortext', 'dragimage', $drag->id);
  166. $output .= " <drag>\n";
  167. $output .= " <no>{$drag->no}</no>\n";
  168. $output .= $format->writetext($drag->label, 3)."\n";
  169. $output .= " <draggroup>{$drag->draggroup}</draggroup>\n";
  170. if ($drag->infinite) {
  171. $output .= " <infinite/>\n";
  172. }
  173. $output .= $this->write_files($files, 3);
  174. $output .= " </drag>\n";
  175. }
  176. foreach ($question->options->drops as $drop) {
  177. $output .= " <drop>\n";
  178. $output .= $format->writetext($drop->label, 3);
  179. $output .= " <no>{$drop->no}</no>\n";
  180. $output .= " <choice>{$drop->choice}</choice>\n";
  181. $output .= " <xleft>{$drop->xleft}</xleft>\n";
  182. $output .= " <ytop>{$drop->ytop}</ytop>\n";
  183. $output .= " </drop>\n";
  184. }
  185. return $output;
  186. }
  187. public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
  188. if (!isset($data['@']['type']) || $data['@']['type'] != 'ddimageortext') {
  189. return false;
  190. }
  191. $question = $format->import_headers($data);
  192. $question->qtype = 'ddimageortext';
  193. $question->shuffleanswers = array_key_exists('shuffleanswers',
  194. $format->getpath($data, array('#'), array()));
  195. $filexml = $format->getpath($data, array('#', 'file'), array());
  196. $question->bgimage = $format->import_files_as_draft($filexml);
  197. $drags = $data['#']['drag'];
  198. $question->drags = array();
  199. foreach ($drags as $dragxml) {
  200. $dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0);
  201. $dragindex = $dragno - 1;
  202. $question->drags[$dragindex] = array();
  203. $question->draglabel[$dragindex] =
  204. $format->getpath($dragxml, array('#', 'text', 0, '#'), '', true);
  205. $question->drags[$dragindex]['infinite'] = array_key_exists('infinite', $dragxml['#']);
  206. $question->drags[$dragindex]['draggroup'] =
  207. $format->getpath($dragxml, array('#', 'draggroup', 0, '#'), 1);
  208. $filexml = $format->getpath($dragxml, array('#', 'file'), array());
  209. $question->dragitem[$dragindex] = $format->import_files_as_draft($filexml);
  210. if (count($filexml)) {
  211. $question->drags[$dragindex]['dragitemtype'] = 'image';
  212. } else {
  213. $question->drags[$dragindex]['dragitemtype'] = 'word';
  214. }
  215. }
  216. $drops = $data['#']['drop'];
  217. $question->drops = array();
  218. foreach ($drops as $dropxml) {
  219. $dropno = $format->getpath($dropxml, array('#', 'no', 0, '#'), 0);
  220. $dropindex = $dropno - 1;
  221. $question->drops[$dropindex] = array();
  222. $question->drops[$dropindex]['choice'] =
  223. $format->getpath($dropxml, array('#', 'choice', 0, '#'), 0);
  224. $question->drops[$dropindex]['droplabel'] =
  225. $format->getpath($dropxml, array('#', 'text', 0, '#'), '', true);
  226. $question->drops[$dropindex]['xleft'] =
  227. $format->getpath($dropxml, array('#', 'xleft', 0, '#'), '');
  228. $question->drops[$dropindex]['ytop'] =
  229. $format->getpath($dropxml, array('#', 'ytop', 0, '#'), '');
  230. }
  231. $format->import_combined_feedback($question, $data, true);
  232. $format->import_hints($question, $data, true, false,
  233. $format->get_format($question->questiontextformat));
  234. return $question;
  235. }
  236. }