Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * @package    mod_data
20
 * @subpackage backup-moodle2
21
 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * Define all the restore steps that will be used by the restore_data_activity_task
27
 */
28
 
29
/**
30
 * Structure step to restore one data activity
31
 */
32
class restore_data_activity_structure_step extends restore_activity_structure_step {
33
 
34
    protected function define_structure() {
35
 
36
        $paths = array();
37
        $userinfo = $this->get_setting_value('userinfo');
38
 
39
        $paths[] = new restore_path_element('data', '/activity/data');
40
        $paths[] = new restore_path_element('data_field', '/activity/data/fields/field');
41
        if ($userinfo) {
42
            $paths[] = new restore_path_element('data_record', '/activity/data/records/record');
43
            $paths[] = new restore_path_element('data_content', '/activity/data/records/record/contents/content');
44
            $paths[] = new restore_path_element('data_rating', '/activity/data/records/record/ratings/rating');
45
            $paths[] = new restore_path_element('data_record_tag', '/activity/data/recordstags/tag');
46
        }
47
 
48
        // Return the paths wrapped into standard activity structure
49
        return $this->prepare_activity_structure($paths);
50
    }
51
 
52
    protected function process_data($data) {
53
        global $DB;
54
 
55
        $data = (object)$data;
56
        $oldid = $data->id;
57
        $data->course = $this->get_courseid();
58
 
59
        // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
60
        // See MDL-9367.
61
        $data->timeavailablefrom = $this->apply_date_offset($data->timeavailablefrom);
62
        $data->timeavailableto = $this->apply_date_offset($data->timeavailableto);
63
        $data->timeviewfrom = $this->apply_date_offset($data->timeviewfrom);
64
        $data->timeviewto = $this->apply_date_offset($data->timeviewto);
65
        $data->assesstimestart = $this->apply_date_offset($data->assesstimestart);
66
        $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish);
67
 
68
        if ($data->scale < 0) { // scale found, get mapping
69
            $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
70
        }
71
 
72
        // Some old backups can arrive with data->notification = null (MDL-24470)
73
        // convert them to proper column default (zero)
74
        if (is_null($data->notification)) {
75
            $data->notification = 0;
76
        }
77
 
78
        // insert the data record
79
        $newitemid = $DB->insert_record('data', $data);
80
        $this->apply_activity_instance($newitemid);
81
    }
82
 
83
    protected function process_data_field($data) {
84
        global $DB;
85
 
86
        $data = (object)$data;
87
        $oldid = $data->id;
88
 
89
        $data->dataid = $this->get_new_parentid('data');
90
        $data->type = clean_param($data->type, PARAM_ALPHA);
91
 
92
        // insert the data_fields record
93
        $newitemid = $DB->insert_record('data_fields', $data);
94
        $this->set_mapping('data_field', $oldid, $newitemid, false); // no files associated
95
    }
96
 
97
    protected function process_data_record($data) {
98
        global $DB;
99
 
100
        $data = (object)$data;
101
        $oldid = $data->id;
102
 
103
        $data->userid = $this->get_mappingid('user', $data->userid);
104
        $data->groupid = $this->get_mappingid('group', $data->groupid);
105
        $data->dataid = $this->get_new_parentid('data');
106
 
107
        // insert the data_records record
108
        $newitemid = $DB->insert_record('data_records', $data);
109
        $this->set_mapping('data_record', $oldid, $newitemid, false); // no files associated
110
    }
111
 
112
    protected function process_data_content($data) {
113
        global $DB;
114
 
115
        $data = (object)$data;
116
        $oldid = $data->id;
117
 
118
        $data->fieldid = $this->get_mappingid('data_field', $data->fieldid);
119
        $data->recordid = $this->get_new_parentid('data_record');
120
 
121
        // insert the data_content record
122
        $newitemid = $DB->insert_record('data_content', $data);
123
        $this->set_mapping('data_content', $oldid, $newitemid, true); // files by this itemname
124
    }
125
 
126
    /**
127
     * Add tags to restored records.
128
     *
129
     * @param stdClass $data Tag
130
     */
131
    protected function process_data_record_tag($data) {
132
        $data = (object)$data;
133
 
134
        if (!core_tag_tag::is_enabled('mod_data', 'data_records')) { // Tags disabled in server, nothing to process.
135
            return;
136
        }
137
 
138
        if (!$itemid = $this->get_mappingid('data_record', $data->itemid)) {
139
            // Some orphaned tag, we could not find the data record for it - ignore.
140
            return;
141
        }
142
 
143
        $tag = $data->rawname;
144
        $context = context_module::instance($this->task->get_moduleid());
145
        core_tag_tag::add_item_tag('mod_data', 'data_records', $itemid, $context, $tag);
146
    }
147
 
148
    protected function process_data_rating($data) {
149
        global $DB;
150
 
151
        $data = (object)$data;
152
 
153
        // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
154
        $data->contextid = $this->task->get_contextid();
155
        $data->itemid    = $this->get_new_parentid('data_record');
156
        if ($data->scaleid < 0) { // scale found, get mapping
157
            $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
158
        }
159
        $data->rating = $data->value;
160
        $data->userid = $this->get_mappingid('user', $data->userid);
161
 
162
        // We need to check that component and ratingarea are both set here.
163
        if (empty($data->component)) {
164
            $data->component = 'mod_data';
165
        }
166
        if (empty($data->ratingarea)) {
167
            $data->ratingarea = 'entry';
168
        }
169
 
170
        $newitemid = $DB->insert_record('rating', $data);
171
    }
172
 
173
    protected function after_execute() {
174
        global $DB;
175
        // Add data related files, no need to match by itemname (just internally handled context)
176
        $this->add_related_files('mod_data', 'intro', null);
177
        // Add content related files, matching by itemname (data_content)
178
        $this->add_related_files('mod_data', 'content', 'data_content');
179
        // Adjust the data->defaultsort field
180
        if ($defaultsort = $DB->get_field('data', 'defaultsort', array('id' => $this->get_new_parentid('data')))) {
181
            if ($defaultsort = $this->get_mappingid('data_field', $defaultsort)) {
182
                $DB->set_field('data', 'defaultsort', $defaultsort, array('id' => $this->get_new_parentid('data')));
183
            }
184
        }
185
    }
186
}