MDL-47494 gapselect: Now all the unit tests for the the ddwtos and gapselect qtypes...
[moodle.git] / question / type / gapselect / questiontype.php
CommitLineData
0d24b17a
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Question type class for the select missing words question type.
21 *
22 * @package qtype
23 * @subpackage gapselect
24 * @copyright 2011 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28
29require_once($CFG->libdir . '/questionlib.php');
30require_once($CFG->dirroot . '/question/engine/lib.php');
31require_once($CFG->dirroot . '/question/format/xml/format.php');
32
33require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
34
35/**
36 * The select missing words question type class.
37 *
38 * @copyright 2009 The Open University
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class qtype_gapselect extends qtype_gapselect_base {
42 protected function choice_options_to_feedback($choice) {
43 return $choice['selectgroup'];
44 }
45
46 protected function make_choice($choicedata) {
47 return new qtype_gapselect_choice($choicedata->answer, $choicedata->feedback);
48 }
49
50 protected function feedback_to_choice_options($feedback) {
51 return array('selectgroup' => $feedback);
52 }
53
54
55 protected function choice_group_key() {
56 return 'selectgroup';
57 }
58
59 function import_from_xml($data, $question, $format, $extra=null) {
60 if (!isset($data['@']['type']) || $data['@']['type'] != 'gapselect') {
61 return false;
62 }
63
64 $question = $format->import_headers($data);
65 $question->qtype = 'gapselect';
66
67 $question->shuffleanswers = $format->trans_single(
68 $format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
69
70 if (!empty($data['#']['selectoption'])) {
71 // Modern XML format.
72 $selectoptions = $data['#']['selectoption'];
73 $question->answer = array();
74 $question->selectgroup = array();
75
76 foreach ($data['#']['selectoption'] as $selectoptionxml) {
77 $question->choices[] = array(
78 'answer' => $format->getpath($selectoptionxml, array('#', 'text', 0, '#'), '', true),
79 'selectgroup' => $format->getpath($selectoptionxml, array('#', 'group', 0, '#'), 1),
80 );
81 }
82
83 } else {
84 // Legacy format containing PHP serialisation.
85 foreach ($data['#']['answer'] as $answerxml) {
86 $ans = $format->import_answer($answerxml);
87 $question->choices[] = array(
88 'answer' => $ans->answer,
89 'selectgroup' => $ans->feedback,
90 );
91 }
92 }
93
94 $format->import_combined_feedback($question, $data, true);
95 $format->import_hints($question, $data, true);
96
97 return $question;
98 }
99
100 function export_to_xml($question, $format, $extra = null) {
101 $output = '';
102
103 $output .= ' <shuffleanswers>' . $question->options->shuffleanswers . "</shuffleanswers>\n";
104
105 $output .= $format->write_combined_feedback($question->options);
106
107 foreach ($question->options->answers as $answer) {
108 $output .= " <selectoption>\n";
109 $output .= $format->writetext($answer->answer, 3);
110 $output .= " <group>{$answer->feedback}</group>\n";
111 $output .= " </selectoption>\n";
112 }
113
114 return $output;
115 }
116
117 /*
118 * Backup the data in the question
119 *
120 * This is used in question/backuplib.php
121 */
122 public function backup($bf, $preferences, $question, $level = 6) {
123 $status = true;
124 $gapselects = get_records("question_gapselect", "questionid", $question, "id");
125
126 //If there are gapselect
127 if ($gapselects) {
128 //Iterate over each gapselect
129 foreach ($gapselects as $gapselect) {
130 $status = fwrite ($bf,start_tag("SDDLS",$level,true));
131 //Print oumultiresponse contents
132 fwrite ($bf,full_tag("SHUFFLEANSWERS",$level+1,false,$gapselect->shuffleanswers));
133 fwrite ($bf,full_tag("CORRECTFEEDBACK",$level+1,false,$gapselect->correctfeedback));
134 fwrite ($bf,full_tag("PARTIALLYCORRECTFEEDBACK",$level+1,false,$gapselect->partiallycorrectfeedback));
135 fwrite ($bf,full_tag("INCORRECTFEEDBACK",$level+1,false,$gapselect->incorrectfeedback));
136 fwrite ($bf,full_tag("SHOWNUMCORRECT",$level+1,false,$gapselect->shownumcorrect));
137 $status = fwrite ($bf,end_tag("SDDLS",$level,true));
138 }
139
140 //Now print question_answers
141 $status = question_backup_answers($bf,$preferences,$question);
142 }
143 return $status;
144 }
145
146 /**
147 * Restores the data in the question (This is used in question/restorelib.php)
148 *
149 */
150 public function restore($old_question_id,$new_question_id,$info,$restore) {
151 $status = true;
152
153 //Get the gapselect array
154 $gapselects = $info['#']['SDDLS'];
155
156 //Iterate over oumultiresponses
157 for($i = 0; $i < sizeof($gapselects); $i++) {
158 $mul_info = $gapselects[$i];
159
160 //Now, build the question_gapselect record structure
161 $gapselect = new stdClass;
162 $gapselect->questionid = $new_question_id;
163 $gapselect->shuffleanswers = isset($mul_info['#']['SHUFFLEANSWERS']['0']['#'])?backup_todb($mul_info['#']['SHUFFLEANSWERS']['0']['#']):'';
164 if (array_key_exists("CORRECTFEEDBACK", $mul_info['#'])) {
165 $gapselect->correctfeedback = backup_todb($mul_info['#']['CORRECTFEEDBACK']['0']['#']);
166 } else {
167 $gapselect->correctfeedback = '';
168 }
169 if (array_key_exists("PARTIALLYCORRECTFEEDBACK", $mul_info['#'])) {
170 $gapselect->partiallycorrectfeedback = backup_todb($mul_info['#']['PARTIALLYCORRECTFEEDBACK']['0']['#']);
171 } else {
172 $gapselect->partiallycorrectfeedback = '';
173 }
174 if (array_key_exists("INCORRECTFEEDBACK", $mul_info['#'])) {
175 $gapselect->incorrectfeedback = backup_todb($mul_info['#']['INCORRECTFEEDBACK']['0']['#']);
176 } else {
177 $gapselect->incorrectfeedback = '';
178 }
179 if (array_key_exists('SHOWNUMCORRECT', $mul_info['#'])) {
180 $gapselect->shownumcorrect = backup_todb($mul_info['#']['SHOWNUMCORRECT']['0']['#']);
181 } else if (array_key_exists('CORRECTRESPONSESFEEDBACK', $mul_info['#'])) {
182 $gapselect->shownumcorrect = backup_todb($mul_info['#']['CORRECTRESPONSESFEEDBACK']['0']['#']);
183 } else {
184 $gapselect->shownumcorrect = 0;
185 }
186
187 $newid = insert_record ("question_gapselect",$gapselect);
188
189 //Do some output
190 if (($i+1) % 50 == 0) {
191 if (!defined('RESTORE_SILENTLY')) {
192 echo ".";
193 if (($i+1) % 1000 == 0) {
194 echo "<br />";
195 }
196 }
197 backup_flush(300);
198 }
199
200 if (!$newid) {
201 $status = false;
202 }
203 }
204 return $status;
205 }
206
207}