MDL-47494 gapselect: Add lots of missing public/protected/private.
[moodle.git] / question / type / gapselect / questiontype.php
1 <?php
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/>.
18 /**
19  * Question type class for the select missing words question type.
20  *
21  * @package    qtype
22  * @subpackage gapselect
23  * @copyright  2011 The Open University
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->libdir . '/questionlib.php');
31 require_once($CFG->dirroot . '/question/engine/lib.php');
32 require_once($CFG->dirroot . '/question/format/xml/format.php');
33 require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
36 /**
37  * The select missing words question type class.
38  *
39  * @copyright  2011 The Open University
40  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  */
42 class qtype_gapselect extends qtype_gapselect_base {
43     protected function choice_options_to_feedback($choice) {
44         return $choice['choicegroup'];
45     }
47     protected function make_choice($choicedata) {
48         return new qtype_gapselect_choice($choicedata->answer, $choicedata->feedback);
49     }
51     protected function feedback_to_choice_options($feedback) {
52         return array('selectgroup' => $feedback);
53     }
56     protected function choice_group_key() {
57         return 'selectgroup';
58     }
60     public function import_from_xml($data, $question, $format, $extra=null) {
61         if (!isset($data['@']['type']) || $data['@']['type'] != 'gapselect') {
62             return false;
63         }
65         $question = $format->import_headers($data);
66         $question->qtype = 'gapselect';
68         $question->shuffleanswers = $format->trans_single(
69                 $format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
71         if (!empty($data['#']['selectoption'])) {
72             // Modern XML format.
73             $selectoptions = $data['#']['selectoption'];
74             $question->answer = array();
75             $question->selectgroup = array();
77             foreach ($data['#']['selectoption'] as $selectoptionxml) {
78                 $question->choices[] = array(
79                     'answer' => $format->getpath($selectoptionxml, array('#', 'text', 0, '#'), '', true),
80                     'choicegroup' => $format->getpath($selectoptionxml, array('#', 'group', 0, '#'), 1),
81                 );
82             }
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,
90                     'choicegroup' => $ans->feedback,
91                 );
92             }
93         }
95         $format->import_combined_feedback($question, $data, true);
96         $format->import_hints($question, $data, true);
98         return $question;
99     }
101     public function export_to_xml($question, $format, $extra = null) {
102         $output = '';
104         $output .= '    <shuffleanswers>' . $question->options->shuffleanswers . "</shuffleanswers>\n";
106         $output .= $format->write_combined_feedback($question->options);
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         }
115         return $output;
116     }
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");
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             }
141             //Now print question_answers
142             $status = question_backup_answers($bf,$preferences,$question);
143         }
144         return $status;
145     }
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;
154         //Get the gapselect array
155         $gapselects = $info['#']['SDDLS'];
157         //Iterate over oumultiresponses
158         for($i = 0; $i < sizeof($gapselects); $i++) {
159             $mul_info = $gapselects[$i];
161             //Now, build the question_gapselect record structure
162             $gapselect = new stdClass();
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             }
188             $newid = insert_record ("question_gapselect",$gapselect);
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             }
201             if (!$newid) {
202                 $status = false;
203             }
204         }
205         return $status;
206     }