Merge branch 'MDL-31100' of git://github.com/timhunt/moodle
[moodle.git] / mod / workshop / form / accumulative / backup / moodle1 / lib.php
CommitLineData
30d5190a
DM
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
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 */
26
27defined('MOODLE_INTERNAL') || die();
28
30d5190a
DM
29/**
30 * Conversion handler for the accumulative grading strategy data
31 */
32class moodle1_workshopform_accumulative_handler extends moodle1_workshopform_handler {
33
34 /** @var used by {@link self::migrate_legacy_scales()} */
35 private $newscaleids = array();
36
37 /**
38 * Converts <ELEMENT> into <workshopform_accumulative_dimension>
39 */
40 public function process_legacy_element(array $data, array $raw) {
41 // prepare a fake record and re-use the upgrade logic
42 $fakerecord = (object)$data;
43 $newscaleid = $this->get_new_scaleid($data['scale']);
44 $converted = (array)workshopform_accumulative_upgrade_element($fakerecord, $newscaleid, 12345678);
45 unset($converted['workshopid']);
46
47 $converted['id'] = $data['id'];
48 $this->write_xml('workshopform_accumulative_dimension', $converted, array('/workshopform_accumulative_dimension/id'));
49
50 return $converted;
51 }
52
53 /**
54 * If needed, creates new standard (global) scale to replace the legacy workshop one and returns the mapping
55 *
56 * If the given $oldscaleid represents a scale, returns array $oldscaleid => $newscaleid that
57 * can be used as a parameter for {@link workshopform_accumulative_upgrade_element()}. Otherwise
58 * this method returns empty array.
59 *
60 * In workshop 1.x, scale field in workshop_elements had the following meaning:
61 * 0 | 2 point Yes/No scale
62 * 1 | 2 point Present/Absent scale
63 * 2 | 2 point Correct/Incorrect scale
64 * 3 | 3 point Good/Poor scale
65 * 4 | 4 point Excellent/Very Poor scale
66 * 5 | 5 point Excellent/Very Poor scale
67 * 6 | 7 point Excellent/Very Poor scale
68 * 7 | Score out of 10
69 * 8 | Score out of 20
70 * 9 | Score out of 100
71 *
72 * @see workshopform_accumulative_upgrade_scales()
73 * @param int $oldscaleid the value of the 'scale' field in the moodle.xml backup file
74 * @return array (int)oldscaleid => (int)newscaleid
75 */
76 protected function get_new_scaleid($oldscaleid) {
77
78 if ($oldscaleid >= 0 and $oldscaleid <= 6) {
79 // we need a new scale id
80 if (!isset($this->newscaleids[$oldscaleid])) {
81 // this is the first time the legacy scale is used in moodle.xml
82 // let us migrate it
83 $scale = $this->get_new_scale_definition($oldscaleid);
84 // other scales are already stashed - let us append a new artificial record
85 $currentscaleids = $this->converter->get_stash_itemids('scales');
86 if (empty($currentscaleids)) {
87 $scale['id'] = 1;
88 } else {
89 $scale['id'] = max($currentscaleids) + 1;
90 }
91 $this->converter->set_stash('scales', $scale, $scale['id']);
92 $this->newscaleids[$oldscaleid] = $scale['id'];
93 // inform the workshop instance that it should annotate the new scale
94 $inforefman = $this->parenthandler->get_inforef_manager();
95 $inforefman->add_ref('scale', $scale['id']);
96 }
97 return array($oldscaleid => $this->newscaleids[$oldscaleid]);
98
99 } else {
100 // not a scale
101 return array();
102 }
103 }
104
105 /**
106 * Returns a definition of a legacy workshop scale
107 *
108 * @see workshopform_accumulative_upgrade_scales
109 * @param object $oldscaleid
110 * @return array
111 */
112 private function get_new_scale_definition($oldscaleid) {
113
114 $data = array(
115 'userid' => 0, // restore will remap to the current user
116 'courseid' => 0, // global scale
117 'description' => '',
118 'descriptionformat' => FORMAT_HTML,
119 );
120
121 switch ($oldscaleid) {
122 case 0:
123 $data['name'] = get_string('scalename0', 'workshopform_accumulative');
124 $data['scale'] = implode(',', array(get_string('no'), get_string('yes')));
125 break;
126 case 1:
127 $data['name'] = get_string('scalename1', 'workshopform_accumulative');
128 $data['scale'] = implode(',', array(get_string('absent', 'workshopform_accumulative'),
129 get_string('present', 'workshopform_accumulative')));
130 break;
131 case 2:
132 $data['name'] = get_string('scalename2', 'workshopform_accumulative');
133 $data['scale'] = implode(',', array(get_string('incorrect', 'workshopform_accumulative'),
134 get_string('correct', 'workshopform_accumulative')));
135 break;
136 case 3:
137 $data['name'] = get_string('scalename3', 'workshopform_accumulative');
138 $data['scale'] = implode(',', array('* ' . get_string('poor', 'workshopform_accumulative'),
139 '**',
140 '*** ' . get_string('good', 'workshopform_accumulative')));
141 break;
142 case 4:
143 $data['name'] = get_string('scalename4', 'workshopform_accumulative');
144 $data['scale'] = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'),
145 '**',
146 '***',
147 '**** ' . get_string('excellent', 'workshopform_accumulative')));
148 break;
149 case 5:
150 $data['name'] = get_string('scalename5', 'workshopform_accumulative');
151 $data['scale'] = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'),
152 '**',
153 '***',
154 '****',
155 '***** ' . get_string('excellent', 'workshopform_accumulative')));
156 break;
157 case 6:
158 $data['name'] = get_string('scalename6', 'workshopform_accumulative');
159 $data['scale'] = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'),
160 '**',
161 '***',
162 '****',
163 '*****',
164 '******',
165 '******* ' . get_string('excellent', 'workshopform_accumulative')));
166 break;
167 }
168
169 return $data;
170 }
171}
ad8c5aee
PS
172
173/**
174 * Transforms a given record from workshop_elements_old into an object to be saved into workshopform_accumulative
175 *
176 * @param stdClass $old legacy record from workshop_elements_old
177 * @param array $newscaleids mapping from old scale types into new standard ones
178 * @param int $newworkshopid id of the new workshop instance that replaced the previous one
179 * @return stdclass to be saved in workshopform_accumulative
180 */
181function workshopform_accumulative_upgrade_element(stdclass $old, array $newscaleids, $newworkshopid) {
182 $new = new stdclass();
183 $new->workshopid = $newworkshopid;
184 $new->sort = $old->elementno;
185 $new->description = $old->description;
186 $new->descriptionformat = FORMAT_HTML;
187 // calculate new grade/scale of the element
188 if ($old->scale >= 0 and $old->scale <= 6 and isset($newscaleids[$old->scale])) {
189 $new->grade = -$newscaleids[$old->scale];
190 } elseif ($old->scale == 7) {
191 $new->grade = 10;
192 } elseif ($old->scale == 8) {
193 $new->grade = 20;
194 } elseif ($old->scale == 9) {
195 $new->grade = 100;
196 } else {
197 $new->grade = 0; // something is wrong
198 }
199 // calculate new weight of the element. Negative weights are not supported any more and
200 // are replaced with weight = 0. Legacy workshop did not store the raw weight but the index
201 // in the array of weights (see $WORKSHOP_EWEIGHTS in workshop 1.x)
202 // workshop 2.0 uses integer weights only (0-16) so all previous weights are multiplied by 4.
203 switch ($old->weight) {
204 case 8: $new->weight = 1; break;
205 case 9: $new->weight = 2; break;
206 case 10: $new->weight = 3; break;
207 case 11: $new->weight = 4; break;
208 case 12: $new->weight = 6; break;
209 case 13: $new->weight = 8; break;
210 case 14: $new->weight = 16; break;
211 default: $new->weight = 0;
212 }
213 return $new;
214}