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