Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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   * Adhoc task handling fixing of events that have had their userid lost.
  19   *
  20   * @package    core
  21   * @copyright  2021 onwards Simey Lameze <simey@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\task;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Class handling fixing of events that have had their userid lost.
  31   *
  32   * @package    core
  33   * @copyright  2021 onwards Simey Lameze <simey@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class calendar_fix_orphaned_events extends adhoc_task {
  37  
  38      /**
  39       * Run the task to recover the correct userid from the event.
  40       *
  41       * If the maximum number of records are updated, the task re-queues itself,
  42       * as there may be more events to be fixed.
  43       */
  44      public function execute() {
  45  
  46          // Check for problematic upgrade steps and fix orphaned records.
  47          if ($this->update_events_wrong_userid_remaining()) {
  48              // There are orphaned events to be fixed.
  49              // The task will re-queue itself until all orphaned calendar events have been fixed.
  50              \core\task\manager::queue_adhoc_task(new calendar_fix_orphaned_events());
  51          }
  52      }
  53  
  54      /**
  55       * Execute the recovery of events that have been set with userid to zero.
  56       *
  57       * @return bool Whether there are more events to be fixed.
  58       */
  59      protected function update_events_wrong_userid_remaining(): bool {
  60          global $CFG;
  61  
  62          require_once($CFG->libdir . '/db/upgradelib.php');
  63  
  64          // Default the max runtime to 60 seconds, unless overridden in config.php.
  65          $maxseconds = $CFG->calendareventsmaxseconds ?? MINSECS;
  66  
  67          // Orphaned events found, get those events so it can be recovered.
  68          $eventsinfo = upgrade_calendar_events_status();
  69  
  70          // Fix the orphaned events and returns if there are more events to be fixed.
  71          return upgrade_calendar_events_fix_remaining($eventsinfo, true, $maxseconds);
  72      }
  73  }