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
 * Ordering question type db upgrade script
19
 *
20
 * @package    qtype_ordering
21
 * @copyright  2013 Gordon Bateson (gordon.bateson@gmail.com)
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * Upgrade code for the ordering question type.
27
 *
28
 * @param int $oldversion the version we are upgrading from.
29
 */
30
function xmldb_qtype_ordering_upgrade($oldversion) {
31
    global $CFG, $DB;
32
 
33
    $dbman = $DB->get_manager();
34
 
35
    if ($oldversion < 2013062800) {
36
        $select = 'qn.*, qo.id AS questionorderingid';
37
        $from   = '{question} qn LEFT JOIN {question_ordering} qo ON qn.id = qo.question';
38
        $where  = 'qn.qtype = ? AND qo.id IS NULL';
39
        $params = ['ordering'];
40
        if ($questions = $DB->get_records_sql("SELECT $select FROM $from WHERE $where", $params)) {
41
            foreach ($questions as $question) {
42
                if ($answers = $DB->get_records('question_answers', ['question' => $question->id])) {
43
                    // Add "options" for this ordering question.
44
                    $questionordering = (object) [
45
                        'question'   => $question->id,
46
                        'logical'    => 1,
47
                        'studentsee' => min(6, count($answers)),
48
                        'correctfeedback' => '',
49
                        'partiallycorrectfeedback' => '',
50
                        'incorrectfeedback' => '',
51
                    ];
52
                    $questionordering->id = $DB->insert_record('question_ordering', $questionordering);
53
                } else {
54
                    // This is a faulty ordering question - remove it.
55
                    $DB->delete_records('question', ['id' => $question->id]);
56
                    if ($dbman->table_exists('quiz_question_instances')) {
57
                        $DB->delete_records('quiz_question_instances', ['question' => $question->id]);
58
                    }
59
                    if ($dbman->table_exists('reader_question_instances')) {
60
                        $DB->delete_records('reader_question_instances', ['question' => $question->id]);
61
                    }
62
                }
63
            }
64
        }
65
        upgrade_plugin_savepoint(true, 2013062800, 'qtype', 'ordering');
66
    }
67
 
68
    if ($oldversion < 2015011915) {
69
 
70
        // Rename "ordering" table for Moodle >= 2.5.
71
        $oldname = 'question_ordering';
72
        $newname = 'qtype_ordering_options';
73
 
74
        if ($dbman->table_exists($oldname)) {
75
            $oldtable = new xmldb_table($oldname);
76
            if ($dbman->table_exists($newname)) {
77
                $dbman->drop_table($oldtable);
78
            } else {
79
                $dbman->rename_table($oldtable, $newname);
80
            }
81
        }
82
 
83
        // Remove index on question(id) field (because we want to modify the field).
84
        $table = new xmldb_table('qtype_ordering_options');
85
        $fields = ['question', 'questionid'];
86
        foreach ($fields as $field) {
87
            if ($dbman->field_exists($table, $field)) {
88
                $index = new xmldb_index('qtypordeopti_que_uix', XMLDB_INDEX_UNIQUE, [$field]);
89
                if ($dbman->index_exists($table, $index)) {
90
                    $dbman->drop_index($table, $index);
91
                }
92
            }
93
        }
94
 
95
        // Rename "question"   -> "questionid".
96
        // Rename "logical"    -> "selecttype".
97
        // Rename "studentsee" -> "selectcount".
98
        // Add    "(xxx)feedbackformat" fields.
99
        $table = new xmldb_table('qtype_ordering_options');
100
        $fields = [
101
            'questionid' => new xmldb_field('question', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, '0', 'id'),
102
            'selecttype' => new xmldb_field('logical', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'questionid'),
103
            'selectcount' => new xmldb_field('studentsee', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'selecttype'),
104
            'correctfeedbackformat' => new xmldb_field('correctfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null,
105
                    '0', 'correctfeedback'),
106
            'incorrectfeedbackformat' => new xmldb_field('incorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL,
107
                    null, '0', 'incorrectfeedback'),
108
            'partiallycorrectfeedbackformat' => new xmldb_field('partiallycorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
109
                    XMLDB_NOTNULL, null, '0', 'partiallycorrectfeedback'),
110
        ];
111
        foreach ($fields as $newname => $field) {
112
            $oldexists = $dbman->field_exists($table, $field);
113
            $newexists = $dbman->field_exists($table, $newname);
114
            if ($field->getName() != $newname && $oldexists) {
115
                if ($newexists) {
116
                    $dbman->drop_field($table, $field);
117
                } else {
118
                    $dbman->rename_field($table, $field, $newname);
119
                    $newexists = true;
120
                }
121
                $oldexists = false;
122
            }
123
            $field->setName($newname);
124
            if ($newexists) {
125
                $dbman->change_field_type($table, $field);
126
            } else {
127
                $dbman->add_field($table, $field);
128
            }
129
        }
130
 
131
        // Make sure there are no duplicate "questionid" fields in "qtype_ordering_options" table.
132
        $select = 'questionid, COUNT(*) AS countduplicates, MAX(id) AS maxid';
133
        $from   = '{qtype_ordering_options}';
134
        $group  = 'questionid';
135
        $having = 'countduplicates > ?';
136
        $params = [1];
137
        if ($records = $DB->get_records_sql("SELECT $select FROM $from GROUP BY $group HAVING $having", $params)) {
138
            foreach ($records as $record) {
139
                $select = 'id <> ? AND questionid = ?';
140
                $params = [$record->maxid, $record->questionid];
141
                $DB->delete_records_select('qtype_ordering_options', $select, $params);
142
            }
143
        }
144
 
145
        // Restore index on questionid field.
146
        $table = new xmldb_table('qtype_ordering_options');
147
        $index = new xmldb_index('qtypordeopti_que_uix', XMLDB_INDEX_UNIQUE, ['questionid']);
148
        if (! $dbman->index_exists($table, $index)) {
149
            $dbman->add_index($table, $index);
150
        }
151
 
152
        upgrade_plugin_savepoint(true, 2015011915, 'qtype', 'ordering');
153
    }
154
 
155
    if ($oldversion < 2015110725) {
156
        $table = new xmldb_table('qtype_ordering_options');
157
        $fields = [
158
            new xmldb_field('layouttype', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, 0, 'questionid'),
159
            new xmldb_field('selecttype', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, 0, 'layouttype'),
160
        ];
161
        foreach ($fields as $field) {
162
            if ($dbman->field_exists($table, $field)) {
163
                $dbman->change_field_type($table, $field);
164
            } else {
165
                $dbman->add_field($table, $field);
166
            }
167
        }
168
        upgrade_plugin_savepoint(true, 2015110725, 'qtype', 'ordering');
169
    }
170
 
171
    if ($oldversion < 2015121734) {
172
        $table = new xmldb_table('qtype_ordering_options');
173
        $fields = [
174
            new xmldb_field('gradingtype', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, 0, 'selectcount'),
175
        ];
176
        foreach ($fields as $field) {
177
            if ($dbman->field_exists($table, $field)) {
178
                $dbman->change_field_type($table, $field);
179
            } else {
180
                $dbman->add_field($table, $field);
181
                // When adding this field to existing records,
182
                // the gradingtype is set to whatever the selecttype is.
183
                $DB->execute('UPDATE {qtype_ordering_options} SET gradingtype = selecttype', []);
184
            }
185
        }
186
        upgrade_plugin_savepoint(true, 2015121734, 'qtype', 'ordering');
187
    }
188
 
189
    if ($oldversion < 2016032949) {
190
        if ($dbman->table_exists('reader_question_instances')) {
191
            $select = 'rqi.question, COUNT(*) AS countquestion';
192
            $from   = '{reader_question_instances} rqi '.
193
                      'LEFT JOIN {question} q ON rqi.question = q.id';
194
            $where  = 'q.qtype = ?';
195
            $group  = 'rqi.question';
196
            $params = ['ordering'];
197
            if ($questions = $DB->get_records_sql("SELECT $select FROM $from WHERE $where GROUP BY $group", $params)) {
198
                $questions = array_keys($questions);
199
                list($select, $params) = $DB->get_in_or_equal($questions);
200
                $select = "questionid $select";
201
                $table = 'qtype_ordering_options';
202
                $DB->set_field_select($table, 'layouttype',  0, $select, $params); // VERTICAL.
203
                $DB->set_field_select($table, 'selecttype',  1, $select, $params); // RANDOM.
204
                $DB->set_field_select($table, 'gradingtype', 1, $select, $params); // RELATIVE.
205
 
206
                // For selectcount, we only fix the value, if it is zero (=ALL)
207
                // because Ordering questions for some low level books use 4.
208
                $select .= ' AND selectcount = ?';
209
                $params[] = 0;
210
                $DB->set_field_select($table, 'selectcount', 6, $select, $params); // Six.
211
            }
212
        }
213
        upgrade_plugin_savepoint(true, 2016032949, 'qtype', 'ordering');
214
    }
215
 
216
    if ($oldversion < 2016081655) {
217
        $table = new xmldb_table('qtype_ordering_options');
218
        $field = new xmldb_field('showgrading', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, 1, 'gradingtype');
219
        if ($dbman->field_exists($table, $field)) {
220
            $dbman->change_field_type($table, $field);
221
        } else {
222
            $dbman->add_field($table, $field);
223
        }
224
        upgrade_plugin_savepoint(true, 2016081655, 'qtype', 'ordering');
225
    }
226
 
227
    if ($oldversion < 2019071191) {
228
 
229
        // Add field "numberingstyle" to table "qtype_ordering_options".
230
        // This field was briefly called "answernumbering".
231
        $table = new xmldb_table('qtype_ordering_options');
232
        $field = new xmldb_field('answernumbering', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'none', 'showgrading');
233
        $newname = 'numberingstyle';
234
 
235
        $oldexists = $dbman->field_exists($table, $field);
236
        $newexists = $dbman->field_exists($table, $newname);
237
        if ($oldexists) {
238
            if ($newexists) {
239
                $dbman->drop_field($table, $field);
240
            } else {
241
                $dbman->rename_field($table, $field, $newname);
242
                $newexists = true;
243
            }
244
            $oldexists = false;
245
        }
246
        $field->setName($newname);
247
        if ($newexists) {
248
            $dbman->change_field_type($table, $field);
249
        } else {
250
            $dbman->add_field($table, $field);
251
        }
252
        upgrade_plugin_savepoint(true, 2019071191, 'qtype', 'ordering');
253
    }
254
 
255
    if ($oldversion < 2019073193) {
256
        $table = 'qtype_ordering_options';
257
        $field = 'numberingstyle';
258
        $select = "$field = ? OR $field = ?";
259
        $params = ['III', 'ABC'];
260
        if ($options = $DB->get_records_select($table, $select, $params, $field, "id,$field")) {
261
            foreach ($options as $option) {
262
                switch ($option->numberingstyle) {
263
                    case 'ABC':
264
                        $DB->set_field($table, $field, 'ABCD', ['id' => $option->id]);
265
                        break;
266
                    case 'III':
267
                        $DB->set_field($table, $field, 'IIII', ['id' => $option->id]);
268
                        break;
269
                    // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
270
                    // Ignore "abc", "iii", and anything else.
271
                }
272
            }
273
        }
274
        upgrade_plugin_savepoint(true, 2019073193, 'qtype', 'ordering');
275
    }
276
 
277
    if ($oldversion < 2022092000) {
278
        $table = new xmldb_table('qtype_ordering_options');
279
        $field = new xmldb_field('shownumcorrect', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, 0);
280
        if ($dbman->field_exists($table, $field)) {
281
            $dbman->change_field_type($table, $field);
282
        } else {
283
            $dbman->add_field($table, $field);
284
        }
285
        upgrade_plugin_savepoint(true, 2022092000, 'qtype', 'ordering');
286
    }
287
 
288
    if ($oldversion < 2023092911) {
289
        // The option to set "All" for the subset size ('selectcount') setting is no longer supported, therefore we need
290
        // to update all data that references this option.
291
 
292
        // The 'selectcount' column from the 'qtype_ordering_options' table currently defines "0" as its default value.
293
        // This value ("0") used to represent the removed "All" option for the 'selectcount' setting, therefore we need
294
        // to update this to a new default of "2" which is the minimum number of items required to create a subset.
295
        $table = new xmldb_table('qtype_ordering_options');
296
        $field = new xmldb_field('selectcount');
297
        $field->set_attributes(XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, 2);
298
        $dbman->change_field_default($table, $field);
299
 
300
        // We need to find all ordering question configurations that currently store the unsupported "0" (all) option
301
        // for the 'selectcount' setting and the total number of answers that are related to each of these ordering
302
        // questions.
303
        $sql = "SELECT qoo.*, COUNT(DISTINCT(qa.id)) AS answerscount
304
                FROM {qtype_ordering_options} qoo
305
                JOIN {question_answers} qa ON qa.question = qoo.questionid
306
                WHERE selectcount = :selectcount
307
                GROUP BY qoo.id";
308
        $questionoptions = $DB->get_recordset_sql($sql, ['selectcount' => 0]);
309
        foreach ($questionoptions as $questionoption) {
310
            // Update the value of the 'selectcount' configuration option for the current ordering question and set it
311
            // to the total number of answers related to this question. This way, we are making sure that the original
312
            // behavior is preserved and all existing items (answers) related to the question will be included in the
313
            // subset.
314
            $questionoption->selectcount = $questionoption->answerscount;
315
            unset($questionoption->answerscount);
316
            $DB->update_record('qtype_ordering_options', $questionoption);
317
        }
318
        $questionoptions->close();
319
 
320
        // Currently, a 'qtype_ordering_selectcount' user preference is set (or updated, if it already exists) each time
321
        // a new ordering question is created. If there are user preferences that store the removed "0" (all) option, they
322
        // need to be updated. In this case, replace it with "2" (minimum number of items required to create a subset).
323
        $DB->set_field('user_preferences', 'value', 2,
324
            ['name' => 'qtype_ordering_selectcount', 'value' => 0]);
325
 
326
        upgrade_plugin_savepoint(true, 2023092911, 'qtype', 'ordering');
327
    }
328
 
329
    if ($oldversion < 2024040401) {
330
        // The option to set "All" for the subset size ('selectcount') setting is no longer supported, therefore we need
331
        // to update all data that references this option.
332
 
333
        // The 'selectcount' column from the 'qtype_ordering_options' table currently defines "0" as its default value.
334
        // This value ("0") used to represent the removed "All" option for the 'selectcount' setting, therefore we need
335
        // to update this to a new default of "2" which is the minimum number of items required to create a subset.
336
        $table = new xmldb_table('qtype_ordering_options');
337
        $field = new xmldb_field('selectcount');
338
        $field->set_attributes(XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, 2);
339
        $dbman->change_field_default($table, $field);
340
 
341
        // We need to find all ordering question configurations that currently store the unsupported "0" (all) option
342
        // for the 'selectcount' setting and the total number of answers that are related to each of these ordering
343
        // questions.
344
        $sql = "SELECT qoo.*, COUNT(DISTINCT(qa.id)) AS answerscount
345
                FROM {qtype_ordering_options} qoo
346
                JOIN {question_answers} qa ON qa.question = qoo.questionid
347
                WHERE selectcount = :selectcount
348
                GROUP BY qoo.id";
349
        $questionoptions = $DB->get_recordset_sql($sql, ['selectcount' => 0]);
350
        foreach ($questionoptions as $questionoption) {
351
            // Update the value of the 'selectcount' configuration option for the current ordering question and set it
352
            // to the total number of answers related to this question. This way, we are making sure that the original
353
            // behavior is preserved and all existing items (answers) related to the question will be included in the
354
            // subset.
355
            $questionoption->selectcount = $questionoption->answerscount;
356
            unset($questionoption->answerscount);
357
            $DB->update_record('qtype_ordering_options', $questionoption);
358
        }
359
        $questionoptions->close();
360
 
361
        // Currently, a 'qtype_ordering_selectcount' user preference is set (or updated, if it already exists) each time
362
        // a new ordering question is created. If there are user preferences that store the removed "0" (all) option, they
363
        // need to be updated. In this case, replace it with "2" (minimum number of items required to create a subset).
364
        $DB->set_field('user_preferences', 'value', 2,
365
            ['name' => 'qtype_ordering_selectcount', 'value' => 0]);
366
 
367
        upgrade_plugin_savepoint(true, 2024040401, 'qtype', 'ordering');
368
    }
369
 
370
    // Automatically generated Moodle v4.4.0 release upgrade line.
371
    // Put any upgrade step following this.
372
 
373
    return true;
374
}