Merge branch 'MDL-29058' of git://github.com/timhunt/moodle
[moodle.git] / question / type / calculated / backup / moodle1 / lib.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  * @package    qtype
20  * @subpackage calculated
21  * @copyright  2011 David Mudrak <david@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 /**
28  * Calculated question type conversion handler
29  */
30 class moodle1_qtype_calculated_handler extends moodle1_qtype_handler {
32     /**
33      * @return array
34      */
35     public function get_question_subpaths() {
36         return array(
37             'ANSWERS/ANSWER',
38             'CALCULATED',
39             'CALCULATED/NUMERICAL_UNITS/NUMERICAL_UNIT',
40             'CALCULATED/DATASET_DEFINITIONS/DATASET_DEFINITION',
41             'CALCULATED/DATASET_DEFINITIONS/DATASET_DEFINITION/DATASET_ITEMS/DATASET_ITEM'
42         );
43     }
45     /**
46      * Appends the calculated specific information to the question
47      */
48     public function process_question(array $data, array $raw) {
50         // convert and write the answers first
51         if (isset($data['answers'])) {
52             $this->write_answers($data['answers'], $this->pluginname);
53         }
55         // convert and write the numerical units and numerical options
56         if (isset($data['calculated'][0]['numerical_units'])) {
57             $numericalunits   = $data['calculated'][0]['numerical_units'];
58             $numericaloptions = $this->get_default_numerical_options($data['oldquestiontextformat']);
59         } else {
60             $numericalunits   = array();
61             $numericaloptions = array();
62         }
63         $this->write_numerical_units($numericalunits);
64         $this->write_numerical_options($numericaloptions);
66         // write dataset_definitions
67         if (isset($data['calculated'][0]['dataset_definitions']['dataset_definition'])) {
68             $datasetdefinitions = $data['calculated'][0]['dataset_definitions']['dataset_definition'];
69         } else {
70             $datasetdefinitions = array();
71         }
72         $this->write_dataset_definitions($datasetdefinitions);
74         // write calculated_records
75         $this->xmlwriter->begin_tag('calculated_records');
76         foreach ($data['calculated'] as $calculatedrecord) {
77             $record = array(
78                 'id'                  => $this->converter->get_nextid(),
79                 'answer'              => $calculatedrecord['answer'],
80                 'tolerance'           => $calculatedrecord['tolerance'],
81                 'tolerancetype'       => $calculatedrecord['tolerancetype'],
82                 'correctanswerlength' => $calculatedrecord['correctanswerlength'],
83                 'correctanswerformat' => $calculatedrecord['correctanswerformat']
84             );
85             $this->write_xml('calculated_record', $record, array('/calculated_record/id'));
86         }
87         $this->xmlwriter->end_tag('calculated_records');
89         // write calculated_options
90         $options = array(
91             'calculate_option' => array(
92                 'id'                             => $this->converter->get_nextid(),
93                 'synchronize'                    => 0,
94                 'single'                         => 0,
95                 'shuffleanswers'                 => 0,
96                 'correctfeedback'                => null,
97                 'correctfeedbackformat'          => FORMAT_HTML,
98                 'partiallycorrectfeedback'       => null,
99                 'partiallycorrectfeedbackformat' => FORMAT_HTML,
100                 'incorrectfeedback'              => null,
101                 'incorrectfeedbackformat'        => FORMAT_HTML,
102                 'answernumbering'                => 'abc'
103             )
104         );
105         $this->write_xml('calculated_options', $options, array('/calculated_options/calculate_option/id'));
106     }