Merge branch 'MDL-31100' of git://github.com/timhunt/moodle
[moodle.git] / mod / workshop / form / numerrors / 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 numerrors
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
29require_once($CFG->libdir.'/gradelib.php'); // grade_floatval() called here
30
31/**
32 * Conversion handler for the numerrors grading strategy data
33 */
34class moodle1_workshopform_numerrors_handler extends moodle1_workshopform_handler {
35
36 /** @var array */
37 protected $mappings = array();
38
39 /** @var array */
40 protected $dimensions = array();
41
42 /**
43 * New workshop instance is being processed
44 */
45 public function on_elements_start() {
46 $this->mappings = array();
47 $this->dimensions = array();
48 }
49
50 /**
51 * Converts <ELEMENT> into <workshopform_numerrors_dimension> and stores it for later writing
52 *
53 * @return array to be written to workshop.xml
54 */
55 public function process_legacy_element($data, $raw) {
56
57 $workshop = $this->parenthandler->get_current_workshop();
58
59 $mapping = array();
60 $mapping['id'] = $data['id'];
61 $mapping['nonegative'] = $data['elementno'];
62 if ($workshop['grade'] == 0 or $data['maxscore'] == 0) {
63 $mapping['grade'] = 0;
64 } else {
65 $mapping['grade'] = grade_floatval($data['maxscore'] / $workshop['grade'] * 100);
66 }
67 $this->mappings[] = $mapping;
68
69 $converted = null;
70
71 if (trim($data['description']) and $data['description'] <> '@@ GRADE_MAPPING_ELEMENT @@') {
72 // prepare a fake record and re-use the upgrade logic
73 $fakerecord = (object)$data;
74 $converted = (array)workshopform_numerrors_upgrade_element($fakerecord, 12345678);
75 unset($converted['workshopid']);
76
77 $converted['id'] = $data['id'];
78 $this->dimensions[] = $converted;
79 }
80
81 return $converted;
82 }
83
84 /**
85 * Writes gathered mappings and dimensions
86 */
87 public function on_elements_end() {
88
89 foreach ($this->mappings as $mapping) {
90 $this->write_xml('workshopform_numerrors_map', $mapping, array('/workshopform_numerrors_map/id'));
91 }
92
93 foreach ($this->dimensions as $dimension) {
94 $this->write_xml('workshopform_numerrors_dimension', $dimension, array('/workshopform_numerrors_dimension/id'));
95 }
96 }
97}
ad8c5aee
PS
98
99/**
100 * Transforms a given record from workshop_elements_old into an object to be saved into workshopform_numerrors
101 *
102 * @param stdClass $old legacy record from workshop_elements_old
103 * @param int $newworkshopid id of the new workshop instance that replaced the previous one
104 * @return stdclass to be saved in workshopform_numerrors
105 */
106function workshopform_numerrors_upgrade_element(stdclass $old, $newworkshopid) {
107 $new = new stdclass();
108 $new->workshopid = $newworkshopid;
109 $new->sort = $old->elementno;
110 $new->description = $old->description;
111 $new->descriptionformat = FORMAT_HTML;
112 $new->grade0 = get_string('grade0default', 'workshopform_numerrors');
113 $new->grade1 = get_string('grade1default', 'workshopform_numerrors');
114 // calculate new weight of the element. Negative weights are not supported any more and
115 // are replaced with weight = 0. Legacy workshop did not store the raw weight but the index
116 // in the array of weights (see $WORKSHOP_EWEIGHTS in workshop 1.x)
117 // workshop 2.0 uses integer weights only (0-16) so all previous weights are multiplied by 4.
118 switch ($old->weight) {
119 case 8: $new->weight = 1; break;
120 case 9: $new->weight = 2; break;
121 case 10: $new->weight = 3; break;
122 case 11: $new->weight = 4; break;
123 case 12: $new->weight = 6; break;
124 case 13: $new->weight = 8; break;
125 case 14: $new->weight = 16; break;
126 default: $new->weight = 0;
127 }
128 return $new;
129}