Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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 tool_moodlenet.
  19   *
  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
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Upgrade the plugin.
  29   *
  30   * @param int $oldversion
  31   * @return bool always true
  32   */
  33  function xmldb_tool_moodlenet_upgrade(int $oldversion) {
  34      global $CFG, $DB;
  35      if ($oldversion < 2020060500) {
  36  
  37          // Grab some of the old settings.
  38          $categoryname = get_config('tool_moodlenet', 'profile_category');
  39          $profilefield = get_config('tool_moodlenet', 'profile_field_name');
  40  
  41          // Master version only!
  42  
  43          // Find out if we have a custom profile field for moodle.net.
  44          $sql = "SELECT f.*
  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";
  48  
  49          $params = [
  50              'categoryname' => $categoryname,
  51              'name' => $profilefield
  52          ];
  53  
  54          $record = $DB->get_record_sql($sql, $params);
  55  
  56          if (!empty($record)) {
  57              $userentries = $DB->get_recordset('user_info_data', ['fieldid' => $record->id]);
  58              $recordstodelete = [];
  59              foreach ($userentries as $userentry) {
  60                  $data = (object) [
  61                      'id' => $userentry->userid,
  62                      'moodlenetprofile' => $userentry->data
  63                  ];
  64                  $DB->update_record('user', $data, true);
  65                  $recordstodelete[] = $userentry->id;
  66              }
  67              $userentries->close();
  68  
  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');
  75          }
  76  
  77          upgrade_plugin_savepoint(true, 2020060500, 'tool', 'moodlenet');
  78      }
  79  
  80      if ($oldversion < 2020061501) {
  81          // Change the domain.
  82          $defaultmoodlenet = get_config('tool_moodlenet', 'defaultmoodlenet');
  83  
  84          if ($defaultmoodlenet === 'https://home.moodle.net') {
  85              set_config('defaultmoodlenet', 'https://moodle.net', 'tool_moodlenet');
  86          }
  87  
  88          // Change the name.
  89          $defaultmoodlenetname = get_config('tool_moodlenet', 'defaultmoodlenetname');
  90  
  91          if ($defaultmoodlenetname === 'Moodle HQ MoodleNet') {
  92              set_config('defaultmoodlenetname', 'MoodleNet Central', 'tool_moodlenet');
  93          }
  94  
  95          upgrade_plugin_savepoint(true, 2020061501, 'tool', 'moodlenet');
  96      }
  97  
  98      if ($oldversion < 2020061502) {
  99          // Disable the MoodleNet integration by default till further notice.
 100          set_config('enablemoodlenet', 0, 'tool_moodlenet');
 101  
 102          upgrade_plugin_savepoint(true, 2020061502, 'tool', 'moodlenet');
 103      }
 104  
 105      // Automatically generated Moodle v3.9.0 release upgrade line.
 106      // Put any upgrade step following this.
 107  
 108      if ($oldversion < 2022021600) {
 109          // This is a special case for if MoodleNet integration has never been enabled,
 110          // or if defaultmoodlenet is not set for whatever reason.
 111          if (!get_config('tool_moodlenet', 'defaultmoodlenet')) {
 112              set_config('defaultmoodlenet', 'https://moodle.net', 'tool_moodlenet');
 113              set_config('defaultmoodlenetname', get_string('defaultmoodlenetnamevalue', 'tool_moodlenet'), 'tool_moodlenet');
 114          }
 115  
 116          // Enable MoodleNet and set it to display on activity chooser footer.
 117          // But only do this if we know for sure that the default MoodleNet is a working one.
 118          if (get_config('tool_moodlenet', 'defaultmoodlenet') == 'https://moodle.net') {
 119              set_config('enablemoodlenet', '1', 'tool_moodlenet');
 120              set_config('activitychooseractivefooter', 'tool_moodlenet');
 121  
 122              // Use an adhoc task to send a notification to admin stating MoodleNet is automatically enabled after upgrade.
 123              $notificationtask = new tool_moodlenet\task\send_enable_notification();
 124              core\task\manager::queue_adhoc_task($notificationtask);
 125          }
 126  
 127          upgrade_plugin_savepoint(true, 2022021600, 'tool', 'moodlenet');
 128      }
 129  
 130      if ($oldversion < 2022021601) {
 131  
 132          $selectsql = "moodlenetprofile IS NOT NULL AND moodlenetprofile != ''";
 133  
 134          // If there are any users with MoodleNet profile set.
 135          if ($DB->count_records_select('user', $selectsql)) {
 136              // Remove the value set for the MoodleNet profile as this format can no longer be used to authenticate
 137              // MoodleNet users.
 138              $DB->set_field_select('user', 'moodlenetprofile', '', $selectsql);
 139  
 140              // Use an adhoc task to send a notification to admin stating that the user data related to the linked
 141              // MoodleNet profiles has been removed.
 142              $notificationtask = new tool_moodlenet\task\send_mnet_profiles_data_removed_notification();
 143              core\task\manager::queue_adhoc_task($notificationtask);
 144          }
 145  
 146          upgrade_plugin_savepoint(true, 2022021601, 'tool', 'moodlenet');
 147      }
 148  
 149      // Automatically generated Moodle v4.0.0 release upgrade line.
 150      // Put any upgrade step following this.
 151  
 152      return true;
 153  }