mirror of
https://github.com/moodle/moodle.git
synced 2025-08-09 19:06:41 +02:00

The machinery to fix orphaned calendar events that were broken by MDL-67494. The solution consists of: 1) Upgrade step that checks if this site has executed the problematic upgrade steps and if positive, it will schedule a new run for calendar_fix_orphaned_events adhoc task. 2) Adhoc task that will self-spawn calling the recovery machinery, running until all the orphaned calendar events are fixed. It also sets the maximum runtime of 60 seconds. It is also possible to override that number by specifing the desired number setting the ->calendareventsmaxseconds in your config.php 3) CLI script that will look for all the calendar events which userids where broken by a wrong upgrade step, affecting to Moodle 3.9.5 and up. It performs checks to both: a) Detect if the site was affected (ran the wrong upgrade step). b) Look for orphaned calendar events, categorising them as: - standard: site / category / course / group / user events - subscription: events created via subscriptions. - action: normal action events, created to show common important dates. - override: user and group override events, particular, that some activities support. - custom: other events, not being any of the above, common or particular. By specifying it (--fix) try to recover as many broken events (missing userid) as possible. Standard, subscription, action, override events in core are fully supported but override or custom events should be fixed by each plugin as far as there isn't any standard API (plugin-wise) to launch a rebuild of the calendar events. 4) Unit tests and helper functions to generate calendar events. We have decided to keep the tests simple, testing only true and false and not using data generators because the code is purely to recover the calendar events and won't turn into an API or something and also due to the urgency of this issue. The helpers have been created in calendar/tests/helpers.php since there are no data generators for calendar.
73 lines
2.6 KiB
PHP
73 lines
2.6 KiB
PHP
<?php
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
* Adhoc task handling fixing of events that have had their userid lost.
|
|
*
|
|
* @package core
|
|
* @copyright 2021 onwards Simey Lameze <simey@moodle.com>
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
namespace core\task;
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
/**
|
|
* Class handling fixing of events that have had their userid lost.
|
|
*
|
|
* @package core
|
|
* @copyright 2021 onwards Simey Lameze <simey@moodle.com>
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
class calendar_fix_orphaned_events extends adhoc_task {
|
|
|
|
/**
|
|
* Run the task to recover the correct userid from the event.
|
|
*
|
|
* If the maximum number of records are updated, the task re-queues itself,
|
|
* as there may be more events to be fixed.
|
|
*/
|
|
public function execute() {
|
|
|
|
// Check for problematic upgrade steps and fix orphaned records.
|
|
if ($this->update_events_wrong_userid_remaining()) {
|
|
// There are orphaned events to be fixed.
|
|
// The task will re-queue itself until all orphaned calendar events have been fixed.
|
|
\core\task\manager::queue_adhoc_task(new calendar_fix_orphaned_events());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Execute the recovery of events that have been set with userid to zero.
|
|
*
|
|
* @return bool Whether there are more events to be fixed.
|
|
*/
|
|
protected function update_events_wrong_userid_remaining(): bool {
|
|
global $CFG;
|
|
|
|
require_once($CFG->libdir . '/db/upgradelib.php');
|
|
|
|
// Default the max runtime to 60 seconds, unless overridden in config.php.
|
|
$maxseconds = $CFG->calendareventsmaxseconds ?? MINSECS;
|
|
|
|
// Orphaned events found, get those events so it can be recovered.
|
|
$eventsinfo = upgrade_calendar_events_status();
|
|
|
|
// Fix the orphaned events and returns if there are more events to be fixed.
|
|
return upgrade_calendar_events_fix_remaining($eventsinfo, true, $maxseconds);
|
|
}
|
|
}
|