MDL-27452 Workshop 1.9 backups conversion support
[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
29require_once($CFG->dirroot.'/mod/workshop/form/rubric/db/upgradelib.php');
30
31/**
32 * Conversion handler for the rubric grading strategy data
33 */
34class moodle1_workshopform_rubric_handler extends moodle1_workshopform_handler {
35
36 /** @var array legacy elements to process */
37 protected $elements = array();
38
39 /** @var array legacy rubrics records to process */
40 protected $rubrics = array();
41
42 /**
43 * Prepare to gather legacy elements info for a new workshop instance
44 */
45 public function on_elements_start() {
46 $this->elements = array();
47 $this->rubrics = array();
48 }
49
50 /**
51 * Processes one <ELEMENT>
52 */
53 public function process_legacy_element($data, $raw) {
54 $this->elements[] = $data;
55 $this->rubrics[$data['id']] = array();
56 }
57
58 /**
59 * Processes one <RUBRIC>
60 */
61 public function process_legacy_rubric($data, $raw) {
62 $this->rubrics[$data['elementid']][] = $data;
63 }
64
65 /**
66 * Processes gathered elements and rubrics
67 */
68 public function on_elements_end() {
69
70 $numofrubrics = 0;
71 foreach ($this->rubrics as $itemid => $levels) {
72 $numofrubrics += count($levels);
73 }
74
75 if ($numofrubrics == 0) {
76 $this->convert_legacy_criterion_elements();
77
78 } else {
79 $this->convert_legacy_rubric_elements();
80 }
81 }
82
83 /**
84 * Processes gathered elements coming from the legacy criterion strategy
85 *
86 * Legacy criterion strategy is converted to a rubric with single rubric item
87 * and the layout set to 'list'.
88 */
89 protected function convert_legacy_criterion_elements() {
90
91 $this->write_xml('workshopform_rubric_config', array('layout' => 'list'));
92
93 $firstelement = reset($this->elements);
94 if ($firstelement === false) {
95 // no elements defined in moodle.xml
96 return;
97 }
98
99 // write the xml describing the artificial single rubric item
100 $this->xmlwriter->begin_tag('workshopform_rubric_dimension', array('id' => $firstelement['id']));
101 $this->xmlwriter->full_tag('sort', 1);
102 $this->xmlwriter->full_tag('description', trim(get_string('dimensionnumber', 'workshopform_rubric', '')));
103 $this->xmlwriter->full_tag('descriptionformat', FORMAT_HTML);
104
105 foreach ($this->elements as $element) {
106 $this->write_xml('workshopform_rubric_level', array(
107 'id' => $element['id'],
108 'grade' => $element['maxscore'],
109 'definition' => $element['description'],
110 'definitionformat' => FORMAT_HTML
111 ), array('/workshopform_rubric_level/id'));
112 }
113
114 $this->xmlwriter->end_tag('workshopform_rubric_dimension');
115 }
116
117 /**
118 * Processes gathered elements coming from the legacy rubric strategy
119 */
120 protected function convert_legacy_rubric_elements() {
121 $this->write_xml('workshopform_rubric_config', array('layout' => 'grid'));
122
123 foreach ($this->elements as $element) {
124 $this->xmlwriter->begin_tag('workshopform_rubric_dimension', array('id' => $element['id']));
125 $this->xmlwriter->full_tag('sort', $element['elementno']);
126 $this->xmlwriter->full_tag('description', $element['description']);
127 $this->xmlwriter->full_tag('descriptionformat', FORMAT_HTML);
128
129 foreach ($this->rubrics[$element['id']] as $rubric) {
130 $fakerecord = new stdClass();
131 $fakerecord->rgrade = $rubric['rubricno'];
132 $fakerecord->eweight = $element['weight'];
133 $fakerecord->rdesc = $rubric['description'];
134 $level = (array)workshopform_rubric_upgrade_rubric_level($fakerecord, $element['id']);
135 unset($level['dimensionid']);
136 $level['id'] = $this->converter->get_nextid();
137 $this->write_xml('workshopform_rubric_level', $level, array('/workshopform_rubric_level/id'));
138 }
139
140 $this->xmlwriter->end_tag('workshopform_rubric_dimension');
141 }
142 }
143}