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