Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 * Upgrade script for the scorm module.
19
 *
20
 * @package    mod_scorm
21
 * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * @global moodle_database $DB
27
 * @param int $oldversion
28
 * @return bool
29
 */
30
function xmldb_scorm_upgrade($oldversion) {
31
    global $DB, $OUTPUT;
32
 
33
    $dbman = $DB->get_manager();
34
 
35
    // Automatically generated Moodle v4.2.0 release upgrade line.
36
    // Put any upgrade step following this.
37
 
38
    // New table structure for scorm_scoes_track.
39
    if ($oldversion < 2023042401) {
40
        // Define table scorm_attempt to be created.
41
        $table = new xmldb_table('scorm_attempt');
42
 
43
        // Adding fields to table scorm_attempt.
44
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
45
        $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
46
        $table->add_field('scormid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
47
        $table->add_field('attempt', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '1');
48
 
49
        // Adding keys to table scorm_attempt.
50
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
51
        $table->add_key('user', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
52
        $table->add_key('scorm', XMLDB_KEY_FOREIGN, ['scormid'], 'scorm', ['id']);
53
 
54
        // Conditionally launch create table for scorm_attempt.
55
        if (!$dbman->table_exists($table)) {
56
            $dbman->create_table($table);
57
        }
58
 
59
        // Define table scorm_element to be created.
60
        $table = new xmldb_table('scorm_element');
61
 
62
        // Adding fields to table scorm_element.
63
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
64
        $table->add_field('element', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
65
 
66
        // Adding keys to table scorm_element.
67
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
68
 
69
        // Adding indexes to table scorm_element.
70
        $table->add_index('element', XMLDB_INDEX_UNIQUE, ['element']);
71
 
72
        // Conditionally launch create table for scorm_element.
73
        if (!$dbman->table_exists($table)) {
74
            $dbman->create_table($table);
75
        }
76
 
77
        // Define table scorm_scoes_value to be created.
78
        $table = new xmldb_table('scorm_scoes_value');
79
 
80
        // Adding fields to table scorm_scoes_value.
81
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
82
        $table->add_field('scoid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
83
        $table->add_field('attemptid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
84
        $table->add_field('elementid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
85
        $table->add_field('value', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
86
        $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
87
 
88
        // Adding keys to table scorm_scoes_value.
89
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
90
        $table->add_key('scoe', XMLDB_KEY_FOREIGN, ['scoid'], 'scorm_scoes', ['id']);
91
        $table->add_key('attempt', XMLDB_KEY_FOREIGN, ['attemptid'], 'scorm_attempt', ['id']);
92
        $table->add_key('element', XMLDB_KEY_FOREIGN, ['elementid'], 'scorm_element', ['id']);
93
 
94
        // Conditionally launch create table for scorm_scoes_value.
95
        if (!$dbman->table_exists($table)) {
96
            $dbman->create_table($table);
97
        }
98
 
99
        upgrade_mod_savepoint(true, 2023042401, 'scorm');
100
    }
101
 
102
    if ($oldversion < 2023042402) {
103
        $trans = $DB->start_delegated_transaction();
104
 
105
        // First grab all elements and store those.
106
        $sql = "INSERT INTO {scorm_element} (element)
107
                    SELECT DISTINCT element FROM {scorm_scoes_track}";
108
        $DB->execute($sql);
109
 
110
        // Now store all data in the scorm_attempt table.
111
        $sql = "INSERT INTO {scorm_attempt} (userid, scormid, attempt)
112
                    SELECT DISTINCT userid, scormid, attempt FROM {scorm_scoes_track}";
113
        $DB->execute($sql);
114
 
115
        $trans->allow_commit();
116
        // Scorm savepoint reached.
117
        upgrade_mod_savepoint(true, 2023042402, 'scorm');
118
    }
119
    if ($oldversion < 2023042403) {
120
        // Now store all translated data in the scorm_scoes_value table.
121
        $total = $DB->count_records('scorm_scoes_track');
122
        if ($total > 500000) {
123
            // This site has a large number of user track records, lets warn that this next part may take some time.
124
            $notification = new \core\output\notification(
125
                get_string('largetrackupgrade', 'scorm', format_float($total, 0)),
126
                \core\output\notification::NOTIFY_WARNING
127
            );
128
            $notification->set_show_closebutton(false);
129
            echo $OUTPUT->render($notification);
130
        }
131
 
132
        // We don't need a progress bar - just run the fastest option possible.
133
        $sql = "INSERT INTO {scorm_scoes_value} (attemptid, scoid, elementid, value, timemodified)
134
                SELECT a.id as attemptid, t.scoid as scoid, e.id as elementid, t.value as value, t.timemodified
135
                  FROM {scorm_scoes_track} t
136
                  JOIN {scorm_element} e ON e.element = t.element
137
                  JOIN {scorm_attempt} a ON (t.userid = a.userid AND t.scormid = a.scormid AND a.attempt = t.attempt)";
138
        $DB->execute($sql);
139
 
140
        // Drop old table scorm_scoes_track.
141
        $table = new xmldb_table('scorm_scoes_track');
142
 
143
        // Conditionally launch drop table for scorm_scoes_track.
144
        if ($dbman->table_exists($table)) {
145
            $dbman->drop_table($table);
146
        }
147
 
148
        // Scorm savepoint reached.
149
        upgrade_mod_savepoint(true, 2023042403, 'scorm');
150
    }
151
 
152
    // Automatically generated Moodle v4.3.0 release upgrade line.
153
    // Put any upgrade step following this.
154
 
155
    if ($oldversion < 2023100901) {
156
        // MDL-79967 - fix up any possible activity completion states since the upgrade to 2023042403.
157
        // Get timestamp of when this site updated to version 2023042403.
158
        $upgraded = $DB->get_field_sql("SELECT min(timemodified)
159
                                          FROM {upgrade_log}
160
                                         WHERE plugin = 'mod_scorm' AND version = '2023042403'");
161
        if (empty($upgraded)) {
162
            // The code causing this regression landed upstream 28 Jul 2023, if a site has done a fresh install since then,
163
            // the upgrade step won't exist - set it to 20th July so we don't end up dealing with too many attempts.
164
            $upgraded = 1689811200; // 20 July 2023 12AM.
165
        }
166
        // Don't bother triggering this next step if the upgrade completed within the last hour.
167
        if (time() - HOURSECS > $upgraded) {
168
            // Get all attempts that have occurred since the upgrade.
169
            $sql = "SELECT DISTINCT s.id, sa.userid
170
                      FROM {scorm} s
171
                      JOIN {scorm_attempt} sa ON sa.scormid = s.id
172
                      JOIN {scorm_scoes_value} sv on sv.attemptid = sa.id
173
                      WHERE sv.timemodified > ?";
174
            $scorms = $DB->get_recordset_sql($sql, [$upgraded]);
175
            foreach ($scorms as $scorm) {
176
                // Run an ad-hoc task to update the grades.
177
                $task = new \mod_scorm\task\update_grades();
178
                $task->set_custom_data([
179
                    'scormid' => $scorm->id,
180
                    'userid' => $scorm->userid,
181
                ]);
182
                \core\task\manager::queue_adhoc_task($task, true);
183
            }
184
            $scorms->close();
185
        }
186
 
187
        // Scorm savepoint reached.
188
        upgrade_mod_savepoint(true, 2023100901, 'scorm');
189
    }
190
    // Automatically generated Moodle v4.4.0 release upgrade line.
191
    // Put any upgrade step following this.
192
 
1441 ariadna 193
    // Automatically generated Moodle v4.5.0 release upgrade line.
194
    // Put any upgrade step following this.
195
 
196
    // Automatically generated Moodle v5.0.0 release upgrade line.
197
    // Put any upgrade step following this.
198
 
1 efrain 199
    return true;
200
}