2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Upgrade script for tool_moodlenet.
20 * @package tool_moodlenet
21 * @copyright 2020 Adrian Greeve <adrian@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
30 * @param int $oldversion
31 * @return bool always true
33 function xmldb_tool_moodlenet_upgrade(int $oldversion) {
35 if ($oldversion < 2020060500) {
37 // Grab some of the old settings.
38 $categoryname = get_config('tool_moodlenet', 'profile_category');
39 $profilefield = get_config('tool_moodlenet', 'profile_field_name');
41 // Master version only!
43 // Find out if we have a custom profile field for moodle.net.
45 FROM {user_info_field} f
46 JOIN {user_info_category} c ON c.id = f.categoryid and c.name = :categoryname
47 WHERE f.shortname = :name";
50 'categoryname' => $categoryname,
51 'name' => $profilefield
54 $record = $DB->get_record_sql($sql, $params);
56 if (!empty($record)) {
57 $userentries = $DB->get_recordset('user_info_data', ['fieldid' => $record->id]);
58 $recordstodelete = [];
59 foreach ($userentries as $userentry) {
61 'id' => $userentry->userid,
62 'moodlenetprofile' => $userentry->data
64 $DB->update_record('user', $data, true);
65 $recordstodelete[] = $userentry->id;
67 $userentries->close();
69 // Remove the user profile data, fields, and category.
70 $DB->delete_records_list('user_info_data', 'id', $recordstodelete);
71 $DB->delete_records('user_info_field', ['id' => $record->id]);
72 $DB->delete_records('user_info_category', ['name' => $categoryname]);
73 unset_config('profile_field_name', 'tool_moodlenet');
74 unset_config('profile_category', 'tool_moodlenet');
77 upgrade_plugin_savepoint(true, 2020060500, 'tool', 'moodlenet');
80 if ($oldversion < 2020061501) {
82 $defaultmoodlenet = get_config('tool_moodlenet', 'defaultmoodlenet');
84 if ($defaultmoodlenet === 'https://home.moodle.net') {
85 set_config('defaultmoodlenet', 'https://moodle.net', 'tool_moodlenet');
89 $defaultmoodlenetname = get_config('tool_moodlenet', 'defaultmoodlenetname');
91 if ($defaultmoodlenetname === 'Moodle HQ MoodleNet') {
92 set_config('defaultmoodlenetname', 'MoodleNet Central', 'tool_moodlenet');
95 upgrade_plugin_savepoint(true, 2020061501, 'tool', 'moodlenet');