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
 * Custom sort order upgrade script.
19
 *
20
 * @package   qbank_columnsortorder
21
 * @copyright 2024 The Open University
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
use core_question\local\bank\column_base;
26
 
27
/**
28
 * Upgrade the plugin.
29
 *
30
 * @param int $oldversion the version of this plugin we are upgrading from.
31
 * @return bool success/failure.
32
 */
33
function xmldb_qbank_columnsortorder_upgrade(int $oldversion): bool {
34
    global $DB;
35
 
36
    if ($oldversion < 2024042201) {
37
        // Before Moodle 4.3, config_plugins settings for qbank_columnsortorder (disabledcol, enabledcol) had a value like
38
        // qbank_statistics\columns\facility_index,qbank_statistics\columns\discriminative_efficiency, ...
39
        // In Moodle 4.3, the values are stored as qbank_statistics\columns\discriminative_efficiency-discriminative_efficiency.
40
        // So updating the old values to match the new format.
41
        // Update the columns records for qbank_columnsortorder plugin.
1441 ariadna 42
        $pluginconfigs = $DB->get_records('config_plugins', ['plugin' => 'qbank_columnsortorder'], 'name');
1 efrain 43
 
44
        foreach ($pluginconfigs as $config) {
1441 ariadna 45
            if (!in_array($config->name, ['hiddencols', 'enabledcol', 'disabledcol'])) {
1 efrain 46
                continue;
47
            }
48
            $fields = explode(',', $config->value);
49
            $updatedcols = [];
50
            foreach ($fields as $columnclass) {
51
                // Columns config that are already in the correct format, could be ignored.
52
                if (str_contains($columnclass, column_base::ID_SEPARATOR)) {
53
                    continue;
54
                }
55
 
56
                $classbits = explode('\\', $columnclass);
57
                $columnname = end($classbits);
58
 
59
                // The custom fields are to be in the format e.g., qbank_customfields\custom_field_column-test.
60
                if (str_contains($columnclass, 'custom_field_column')) {
61
                    array_pop($classbits);
62
                }
63
 
64
                $updatedcols[] = implode('\\', $classbits) . column_base::ID_SEPARATOR . $columnname;
65
            }
66
            $updatedconfig = implode(',', $updatedcols);
67
            set_config($config->name, $updatedconfig, 'qbank_columnsortorder');
68
        }
69
 
70
        // Custom sort order savepoint reached.
71
        upgrade_plugin_savepoint(true, 2024042201, 'qbank', 'columnsortorder');
72
    }
73
 
1441 ariadna 74
    if ($oldversion < 2024051000) {
1 efrain 75
        // Remove plugin entry created by previously incorrect 2024042201 savepoint.
76
        $DB->delete_records('config_plugins', ['plugin' => 'qbank_qbank_columnsortorder']);
1441 ariadna 77
        upgrade_plugin_savepoint(true, 2024051000, 'qbank', 'columnsortorder');
1 efrain 78
    }
79
 
1441 ariadna 80
    // Automatically generated Moodle v4.5.0 release upgrade line.
81
    // Put any upgrade step following this.
82
 
83
    if ($oldversion < 2024100701) {
84
        // When upgrading to version 2024042201, if there were any values for colsize in qbank_columnsortorder plugin,
85
        // they were getting incorrectly updated, resulting in corrupted colsize value,
86
        // e.g., '"width":"30"}-"width":"30"},"width":"180"}-"width":"180"}' and thus breaking the question bank page.
87
        $pluginconfig = $DB->get_record('config_plugins', ['plugin' => 'qbank_columnsortorder', 'name' => 'colsize']);
88
        $pattern = '/"width":"[^"]*"}-"width":"[^"]*"}/';
89
        if ($pluginconfig && preg_match($pattern, $pluginconfig->value)) {
90
            $DB->delete_records('config_plugins', ['plugin' => 'qbank_columnsortorder', 'name' => 'colsize']);
91
        }
92
        upgrade_plugin_savepoint(true, 2024100701, 'qbank', 'columnsortorder');
93
    }
94
 
95
    // Automatically generated Moodle v5.0.0 release upgrade line.
96
    // Put any upgrade step following this.
97
 
1 efrain 98
    return true;
99
}