MDL-62880 qtype_ddwtos: drop support for legacy import format
[moodle.git] / question / type / ddwtos / questiontype.php
CommitLineData
61381e50 1<?php
61381e50
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
61381e50
TH
17/**
18 * Question type class for the drag-and-drop words into sentences question type.
19 *
8bc1d28b
EM
20 * @package qtype_ddwtos
21 * @copyright 2009 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
23 */
24
25
9d55f4a5
TH
26defined('MOODLE_INTERNAL') || die();
27
61381e50
TH
28require_once($CFG->libdir . '/questionlib.php');
29require_once($CFG->dirroot . '/question/engine/lib.php');
30require_once($CFG->dirroot . '/question/format/xml/format.php');
61381e50
TH
31require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
32
3a4ac328 33
61381e50
TH
34/**
35 * The drag-and-drop words into sentences question type class.
36 *
3a4ac328
TH
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
39 */
40class qtype_ddwtos extends qtype_gapselect_base {
29929b5b 41 protected function choice_group_key() {
e9cd9902
TH
42 return 'draggroup';
43 }
44
29929b5b 45 protected function choice_options_to_feedback($choice) {
60cbb9ed 46 $output = new stdClass();
6c543542 47 $output->draggroup = $choice['choicegroup'];
61381e50
TH
48 $output->infinite = !empty($choice['infinite']);
49 return serialize($output);
50 }
51
29929b5b 52 protected function feedback_to_choice_options($feedback) {
61381e50 53 $feedbackobj = unserialize($feedback);
29929b5b 54 return array('draggroup' => $feedbackobj->draggroup, 'infinite' => $feedbackobj->infinite);
61381e50
TH
55 }
56
29929b5b 57 protected function make_choice($choicedata) {
e9cd9902 58 $options = unserialize($choicedata->feedback);
29929b5b
TH
59 return new qtype_ddwtos_choice(
60 $choicedata->answer, $options->draggroup, $options->infinite);
61381e50
TH
61 }
62
5b8460a3 63 public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
61381e50
TH
64 if (!isset($data['@']['type']) || $data['@']['type'] != 'ddwtos') {
65 return false;
66 }
67
68 $question = $format->import_headers($data);
69 $question->qtype = 'ddwtos';
70
71 $question->shuffleanswers = $format->trans_single(
72 $format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
73
cb8aefa6
TH
74 // Import the choices.
75 $question->answer = array();
76 $question->draggroup = array();
77 $question->infinite = array();
78
79 foreach ($data['#']['dragbox'] as $dragboxxml) {
80 $question->choices[] = array(
81 'answer' => $format->getpath($dragboxxml, array('#', 'text', 0, '#'), '', true),
82 'choicegroup' => $format->getpath($dragboxxml, array('#', 'group', 0, '#'), 1),
83 'infinite' => array_key_exists('infinite', $dragboxxml['#']),
84 );
61381e50
TH
85 }
86
87 $format->import_combined_feedback($question, $data, true);
efff972a
TH
88 $format->import_hints($question, $data, true, false,
89 $format->get_format($question->questiontextformat));
61381e50
TH
90
91 return $question;
92 }
93
5b8460a3 94 public function export_to_xml($question, qformat_xml $format, $extra = null) {
61381e50
TH
95 $output = '';
96
29929b5b
TH
97 $output .= ' <shuffleanswers>' . $question->options->shuffleanswers .
98 "</shuffleanswers>\n";
61381e50 99
efff972a
TH
100 $output .= $format->write_combined_feedback($question->options,
101 $question->id,
102 $question->contextid);
61381e50
TH
103
104 foreach ($question->options->answers as $answer) {
105 $options = unserialize($answer->feedback);
106
107 $output .= " <dragbox>\n";
108 $output .= $format->writetext($answer->answer, 3);
109 $output .= " <group>{$options->draggroup}</group>\n";
110 if ($options->infinite) {
111 $output .= " <infinite/>\n";
112 }
113 $output .= " </dragbox>\n";
114 }
115
116 return $output;
117 }
61381e50 118}