Merge branch 'MDL-31100' of git://github.com/timhunt/moodle
[moodle.git] / mod / workshop / form / rubric / 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 rubric
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 rubric grading strategy data
31 */
32class moodle1_workshopform_rubric_handler extends moodle1_workshopform_handler {
33
34 /** @var array legacy elements to process */
35 protected $elements = array();
36
37 /** @var array legacy rubrics records to process */
38 protected $rubrics = array();
39
40 /**
41 * Prepare to gather legacy elements info for a new workshop instance
42 */
43 public function on_elements_start() {
44 $this->elements = array();
45 $this->rubrics = array();
46 }
47
48 /**
49 * Processes one <ELEMENT>
50 */
51 public function process_legacy_element($data, $raw) {
52 $this->elements[] = $data;
53 $this->rubrics[$data['id']] = array();
54 }
55
56 /**
57 * Processes one <RUBRIC>
58 */
59 public function process_legacy_rubric($data, $raw) {
60 $this->rubrics[$data['elementid']][] = $data;
61 }
62
63 /**
64 * Processes gathered elements and rubrics
65 */
66 public function on_elements_end() {
67
68 $numofrubrics = 0;
69 foreach ($this->rubrics as $itemid => $levels) {
70 $numofrubrics += count($levels);
71 }
72
73 if ($numofrubrics == 0) {
74 $this->convert_legacy_criterion_elements();
75
76 } else {
77 $this->convert_legacy_rubric_elements();
78 }
79 }
80
81 /**
82 * Processes gathered elements coming from the legacy criterion strategy
83 *
84 * Legacy criterion strategy is converted to a rubric with single rubric item
85 * and the layout set to 'list'.
86 */
87 protected function convert_legacy_criterion_elements() {
88
89 $this->write_xml('workshopform_rubric_config', array('layout' => 'list'));
90
91 $firstelement = reset($this->elements);
92 if ($firstelement === false) {
93 // no elements defined in moodle.xml
94 return;
95 }
96
97 // write the xml describing the artificial single rubric item
98 $this->xmlwriter->begin_tag('workshopform_rubric_dimension', array('id' => $firstelement['id']));
99 $this->xmlwriter->full_tag('sort', 1);
100 $this->xmlwriter->full_tag('description', trim(get_string('dimensionnumber', 'workshopform_rubric', '')));
101 $this->xmlwriter->full_tag('descriptionformat', FORMAT_HTML);
102
103 foreach ($this->elements as $element) {
104 $this->write_xml('workshopform_rubric_level', array(
105 'id' => $element['id'],
106 'grade' => $element['maxscore'],
107 'definition' => $element['description'],
108 'definitionformat' => FORMAT_HTML
109 ), array('/workshopform_rubric_level/id'));
110 }
111
112 $this->xmlwriter->end_tag('workshopform_rubric_dimension');
113 }
114
115 /**
116 * Processes gathered elements coming from the legacy rubric strategy
117 */
118 protected function convert_legacy_rubric_elements() {
119 $this->write_xml('workshopform_rubric_config', array('layout' => 'grid'));
120
121 foreach ($this->elements as $element) {
122 $this->xmlwriter->begin_tag('workshopform_rubric_dimension', array('id' => $element['id']));
123 $this->xmlwriter->full_tag('sort', $element['elementno']);
124 $this->xmlwriter->full_tag('description', $element['description']);
125 $this->xmlwriter->full_tag('descriptionformat', FORMAT_HTML);
126
127 foreach ($this->rubrics[$element['id']] as $rubric) {
128 $fakerecord = new stdClass();
129 $fakerecord->rgrade = $rubric['rubricno'];
130 $fakerecord->eweight = $element['weight'];
131 $fakerecord->rdesc = $rubric['description'];
132 $level = (array)workshopform_rubric_upgrade_rubric_level($fakerecord, $element['id']);
133 unset($level['dimensionid']);
134 $level['id'] = $this->converter->get_nextid();
135 $this->write_xml('workshopform_rubric_level', $level, array('/workshopform_rubric_level/id'));
136 }
137
138 $this->xmlwriter->end_tag('workshopform_rubric_dimension');
139 }
140 }
141}
ad8c5aee
PS
142
143/**
144 * Transforms given record into an object to be saved into workshopform_rubric_levels
145 *
146 * This is used during Rubric 1.9 -> Rubric 2.0 conversion
147 *
148 * @param stdClass $old legacy record from joined workshop_elements_old + workshop_rubrics_old
149 * @param int $newdimensionid id of the new workshopform_rubric dimension record to be linked to
150 * @return stdclass to be saved in workshopform_rubric_levels
151 */
152function workshopform_rubric_upgrade_rubric_level(stdclass $old, $newdimensionid) {
153 $new = new stdclass();
154 $new->dimensionid = $newdimensionid;
155 $new->grade = $old->rgrade * workshopform_rubric_upgrade_weight($old->eweight);
156 $new->definition = $old->rdesc;
157 $new->definitionformat = FORMAT_HTML;
158 return $new;
159}