MDL-31100 unit tests: don't mangle UTF-8 in failure messages.
[moodle.git] / mod / workshop / form / accumulative / 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  * Provides support for the conversion of moodle1 backup to the moodle2 format
20  *
21  * @package    workshopform
22  * @subpackage accumulative
23  * @copyright  2011 David Mudrak <david@moodle.com>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot.'/mod/workshop/form/accumulative/db/upgradelib.php');
31 /**
32  * Conversion handler for the accumulative grading strategy data
33  */
34 class moodle1_workshopform_accumulative_handler extends moodle1_workshopform_handler {
36     /** @var used by {@link self::migrate_legacy_scales()} */
37     private $newscaleids = array();
39     /**
40      * Converts <ELEMENT> into <workshopform_accumulative_dimension>
41      */
42     public function process_legacy_element(array $data, array $raw) {
43         // prepare a fake record and re-use the upgrade logic
44         $fakerecord = (object)$data;
45         $newscaleid = $this->get_new_scaleid($data['scale']);
46         $converted = (array)workshopform_accumulative_upgrade_element($fakerecord, $newscaleid, 12345678);
47         unset($converted['workshopid']);
49         $converted['id'] = $data['id'];
50         $this->write_xml('workshopform_accumulative_dimension', $converted, array('/workshopform_accumulative_dimension/id'));
52         return $converted;
53     }
55     /**
56      * If needed, creates new standard (global) scale to replace the legacy workshop one and returns the mapping
57      *
58      * If the given $oldscaleid represents a scale, returns array $oldscaleid => $newscaleid that
59      * can be used as a parameter for {@link workshopform_accumulative_upgrade_element()}. Otherwise
60      * this method returns empty array.
61      *
62      * In workshop 1.x, scale field in workshop_elements had the following meaning:
63      *   0 | 2 point Yes/No scale
64      *   1 | 2 point Present/Absent scale
65      *   2 | 2 point Correct/Incorrect scale
66      *   3 | 3 point Good/Poor scale
67      *   4 | 4 point Excellent/Very Poor scale
68      *   5 | 5 point Excellent/Very Poor scale
69      *   6 | 7 point Excellent/Very Poor scale
70      *   7 | Score out of 10
71      *   8 | Score out of 20
72      *   9 | Score out of 100
73      *
74      * @see workshopform_accumulative_upgrade_scales()
75      * @param int $oldscaleid the value of the 'scale' field in the moodle.xml backup file
76      * @return array (int)oldscaleid => (int)newscaleid
77      */
78     protected function get_new_scaleid($oldscaleid) {
80         if ($oldscaleid >= 0 and $oldscaleid <= 6) {
81             // we need a new scale id
82             if (!isset($this->newscaleids[$oldscaleid])) {
83                 // this is the first time the legacy scale is used in moodle.xml
84                 // let us migrate it
85                 $scale = $this->get_new_scale_definition($oldscaleid);
86                 // other scales are already stashed - let us append a new artificial record
87                 $currentscaleids = $this->converter->get_stash_itemids('scales');
88                 if (empty($currentscaleids)) {
89                     $scale['id'] = 1;
90                 } else {
91                     $scale['id'] = max($currentscaleids) + 1;
92                 }
93                 $this->converter->set_stash('scales', $scale, $scale['id']);
94                 $this->newscaleids[$oldscaleid] = $scale['id'];
95                 // inform the workshop instance that it should annotate the new scale
96                 $inforefman = $this->parenthandler->get_inforef_manager();
97                 $inforefman->add_ref('scale', $scale['id']);
98             }
99             return array($oldscaleid => $this->newscaleids[$oldscaleid]);
101         } else {
102             // not a scale
103             return array();
104         }
105     }
107     /**
108      * Returns a definition of a legacy workshop scale
109      *
110      * @see workshopform_accumulative_upgrade_scales
111      * @param object $oldscaleid
112      * @return array
113      */
114     private function get_new_scale_definition($oldscaleid) {
116         $data = array(
117             'userid'            => 0,   // restore will remap to the current user
118             'courseid'          => 0,   // global scale
119             'description'       => '',
120             'descriptionformat' => FORMAT_HTML,
121         );
123         switch ($oldscaleid) {
124         case 0:
125             $data['name']  = get_string('scalename0', 'workshopform_accumulative');
126             $data['scale'] = implode(',', array(get_string('no'), get_string('yes')));
127             break;
128         case 1:
129             $data['name']  = get_string('scalename1', 'workshopform_accumulative');
130             $data['scale'] = implode(',', array(get_string('absent', 'workshopform_accumulative'),
131                                                 get_string('present', 'workshopform_accumulative')));
132             break;
133         case 2:
134             $data['name']  = get_string('scalename2', 'workshopform_accumulative');
135             $data['scale'] = implode(',', array(get_string('incorrect', 'workshopform_accumulative'),
136                                                 get_string('correct', 'workshopform_accumulative')));
137             break;
138         case 3:
139             $data['name']  = get_string('scalename3', 'workshopform_accumulative');
140             $data['scale'] = implode(',', array('* ' . get_string('poor', 'workshopform_accumulative'),
141                                                 '**',
142                                                 '*** ' . get_string('good', 'workshopform_accumulative')));
143             break;
144         case 4:
145             $data['name']  = get_string('scalename4', 'workshopform_accumulative');
146             $data['scale'] = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'),
147                                                 '**',
148                                                 '***',
149                                                 '**** ' . get_string('excellent', 'workshopform_accumulative')));
150             break;
151         case 5:
152             $data['name']  = get_string('scalename5', 'workshopform_accumulative');
153             $data['scale'] = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'),
154                                                 '**',
155                                                 '***',
156                                                 '****',
157                                                 '***** ' . get_string('excellent', 'workshopform_accumulative')));
158             break;
159         case 6:
160             $data['name']  = get_string('scalename6', 'workshopform_accumulative');
161             $data['scale'] = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'),
162                                                 '**',
163                                                 '***',
164                                                 '****',
165                                                 '*****',
166                                                 '******',
167                                                 '******* ' . get_string('excellent', 'workshopform_accumulative')));
168             break;
169         }
171         return $data;
172     }