Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * @package    mod_scorm
19
 * @subpackage backup-moodle2
20
 * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
/**
25
 * Define all the restore steps that will be used by the restore_scorm_activity_task
26
 */
27
 
28
/**
29
 * Structure step to restore one scorm activity
30
 */
31
class restore_scorm_activity_structure_step extends restore_activity_structure_step {
32
 
33
    protected function define_structure() {
34
 
35
        $paths = array();
36
        $userinfo = $this->get_setting_value('userinfo');
37
 
38
        $paths[] = new restore_path_element('scorm', '/activity/scorm');
39
        $paths[] = new restore_path_element('scorm_sco', '/activity/scorm/scoes/sco');
40
        $paths[] = new restore_path_element('scorm_sco_data', '/activity/scorm/scoes/sco/sco_datas/sco_data');
41
        $paths[] = new restore_path_element('scorm_seq_objective', '/activity/scorm/scoes/sco/seq_objectives/seq_objective');
42
        $paths[] = new restore_path_element('scorm_seq_rolluprule', '/activity/scorm/scoes/sco/seq_rolluprules/seq_rolluprule');
43
        $paths[] = new restore_path_element('scorm_seq_rolluprulecond', '/activity/scorm/scoes/sco/seq_rollupruleconds/seq_rolluprulecond');
44
        $paths[] = new restore_path_element('scorm_seq_rulecond', '/activity/scorm/scoes/sco/seq_ruleconds/seq_rulecond');
45
        $paths[] = new restore_path_element('scorm_seq_rulecond_data', '/activity/scorm/scoes/sco/seq_rulecond_datas/seq_rulecond_data');
46
 
47
        $paths[] = new restore_path_element('scorm_seq_mapinfo', '/activity/scorm/scoes/sco/seq_objectives/seq_objective/seq_mapinfos/seq_mapinfo');
48
        if ($userinfo) {
49
            $paths[] = new restore_path_element('scorm_sco_track', '/activity/scorm/scoes/sco/sco_tracks/sco_track');
50
        }
51
 
52
        // Return the paths wrapped into standard activity structure
53
        return $this->prepare_activity_structure($paths);
54
    }
55
 
56
    protected function process_scorm($data) {
57
        global $DB;
58
 
59
        $data = (object)$data;
60
        $oldid = $data->id;
61
 
62
        $data->course = $this->get_courseid();
63
 
64
        // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
65
        // See MDL-9367.
66
        $data->timeopen = $this->apply_date_offset($data->timeopen);
67
        $data->timeclose = $this->apply_date_offset($data->timeclose);
68
 
69
        if (!isset($data->completionstatusallscos)) {
70
            $data->completionstatusallscos = false;
71
        }
72
        // insert the scorm record
73
        $newitemid = $DB->insert_record('scorm', $data);
74
        // immediately after inserting "activity" record, call this
75
        $this->apply_activity_instance($newitemid);
76
    }
77
 
78
    protected function process_scorm_sco($data) {
79
        global $DB;
80
 
81
        $data = (object)$data;
82
 
83
        $oldid = $data->id;
84
        $data->scorm = $this->get_new_parentid('scorm');
85
 
86
        $newitemid = $DB->insert_record('scorm_scoes', $data);
87
        $this->set_mapping('scorm_sco', $oldid, $newitemid);
88
    }
89
 
90
    protected function process_scorm_sco_data($data) {
91
        global $DB;
92
 
93
        $data = (object)$data;
94
        $oldid = $data->id;
95
        $data->scoid = $this->get_new_parentid('scorm_sco');
96
 
97
        $newitemid = $DB->insert_record('scorm_scoes_data', $data);
98
        // No need to save this mapping as far as nothing depend on it
99
        // (child paths, file areas nor links decoder)
100
    }
101
 
102
    protected function process_scorm_seq_objective($data) {
103
        global $DB;
104
 
105
        $data = (object)$data;
106
        $oldid = $data->id;
107
        $data->scoid = $this->get_new_parentid('scorm_sco');
108
 
109
        $newitemid = $DB->insert_record('scorm_seq_objective', $data);
110
        $this->set_mapping('scorm_seq_objective', $oldid, $newitemid);
111
    }
112
 
113
    protected function process_scorm_seq_rolluprule($data) {
114
        global $DB;
115
 
116
        $data = (object)$data;
117
        $oldid = $data->id;
118
        $data->scoid = $this->get_new_parentid('scorm_sco');
119
 
120
        $newitemid = $DB->insert_record('scorm_seq_rolluprule', $data);
121
        $this->set_mapping('scorm_seq_rolluprule', $oldid, $newitemid);
122
    }
123
 
124
    protected function process_scorm_seq_rolluprulecond($data) {
125
        global $DB;
126
 
127
        $data = (object)$data;
128
        $oldid = $data->id;
129
        $data->scoid = $this->get_new_parentid('scorm_sco');
130
        $data->ruleconditions = $this->get_new_parentid('scorm_seq_rolluprule');
131
 
132
        $newitemid = $DB->insert_record('scorm_seq_rolluprulecond', $data);
133
        // No need to save this mapping as far as nothing depend on it
134
        // (child paths, file areas nor links decoder)
135
    }
136
 
137
    protected function process_scorm_seq_rulecond($data) {
138
        global $DB;
139
 
140
        $data = (object)$data;
141
        $oldid = $data->id;
142
        $data->scoid = $this->get_new_parentid('scorm_sco');
143
 
144
        $newitemid = $DB->insert_record('scorm_seq_ruleconds', $data);
145
        $this->set_mapping('scorm_seq_ruleconds', $oldid, $newitemid);
146
    }
147
 
148
    protected function process_scorm_seq_rulecond_data($data) {
149
        global $DB;
150
 
151
        $data = (object)$data;
152
        $oldid = $data->id;
153
        $data->scoid = $this->get_new_parentid('scorm_sco');
154
        $data->ruleconditions = $this->get_new_parentid('scorm_seq_ruleconds');
155
 
156
        $newitemid = $DB->insert_record('scorm_seq_rulecond', $data);
157
        // No need to save this mapping as far as nothing depend on it
158
        // (child paths, file areas nor links decoder)
159
    }
160
 
161
 
162
 
163
    protected function process_scorm_seq_mapinfo($data) {
164
        global $DB;
165
 
166
        $data = (object)$data;
167
        $oldid = $data->id;
168
        $data->scoid = $this->get_new_parentid('scorm_sco');
169
        $data->objectiveid = $this->get_new_parentid('scorm_seq_objective');
170
        $newitemid = $DB->insert_record('scorm_scoes_data', $data);
171
        // No need to save this mapping as far as nothing depend on it
172
        // (child paths, file areas nor links decoder)
173
    }
174
 
175
    protected function process_scorm_sco_track($data) {
176
        global $DB, $CFG;
177
        require_once($CFG->dirroot.'/mod/scorm/locallib.php');
178
        $data = (object)$data;
179
        $attemptobject = scorm_get_attempt($this->get_mappingid('user', $data->userid),
180
                                           $this->get_new_parentid('scorm'),
181
                                           $data->attempt);
182
        $data->scoid = $this->get_new_parentid('scorm_sco');
183
        $data->userid = $this->get_mappingid('user', $data->userid);
184
        $data->attemptid = $attemptobject->id;
185
        $data->elementid = scorm_get_elementid($data->element);
186
 
187
        $DB->insert_record('scorm_scoes_value', $data);
188
        // No need to save this mapping as far as nothing depend on it.
189
    }
190
 
191
    protected function after_execute() {
192
        global $DB;
193
 
194
        // Add scorm related files, no need to match by itemname (just internally handled context)
195
        $this->add_related_files('mod_scorm', 'intro', null);
196
        $this->add_related_files('mod_scorm', 'content', null);
197
        $this->add_related_files('mod_scorm', 'package', null);
198
 
199
        // Fix launch param in scorm table to use new sco id.
200
        $scormid = $this->get_new_parentid('scorm');
201
        $scorm = $DB->get_record('scorm', array('id' => $scormid));
202
        $scorm->launch = $this->get_mappingid('scorm_sco', $scorm->launch, '');
203
 
204
        if (!empty($scorm->launch)) {
205
            // Check that this sco has a valid launch value.
206
            $scolaunch = $DB->get_field('scorm_scoes', 'launch', array('id' => $scorm->launch));
207
            if (empty($scolaunch)) {
208
                // This is not a valid sco - set to empty so we can find a valid launch sco.
209
                $scorm->launch = '';
210
            }
211
        }
212
 
213
        if (empty($scorm->launch)) {
214
            // This scorm has an invalid launch param - we need to calculate it and get the first launchable sco.
215
            $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
216
            // We use get_records here as we need to pass a limit in the query that works cross db.
217
            $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scormid), 'sortorder', 'id', 0, 1);
218
            if (!empty($scoes)) {
219
                $sco = reset($scoes); // We only care about the first record - the above query only returns one.
220
                $scorm->launch = $sco->id;
221
            }
222
        }
223
        if (!empty($scorm->launch)) {
224
            $DB->update_record('scorm', $scorm);
225
        }
226
    }
227
}