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