Merge branch 'MDL-75909-311' of https://github.com/andrewnicols/moodle into MOODLE_31...
[moodle.git] / admin / cli / fix_orphaned_calendar_events.php
blob304b2763c4132c1405b850822b58f5c2f4710960
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.
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/>.
17 /**
18 * Fix orphaned calendar events that were broken by MDL-67494.
20 * This script will look for all the calendar events which userids
21 * where broken by a wrong upgrade step, affecting to Moodle 3.9.5
22 * and up.
24 * It performs checks to both:
25 * a) Detect if the site was affected (ran the wrong upgrade step).
26 * b) Look for orphaned calendar events, categorising them as:
27 * - standard: site / category / course / group / user events
28 * - subscription: events created via subscriptions.
29 * - action: normal action events, created to show common important dates.
30 * - override: user and group override events, particular, that some activities support.
31 * - custom: other events, not being any of the above, common or particular.
32 * By specifying it (--fix) try to recover as many broken events (missing userid) as
33 * possible. Standard, subscription, action, override events in core are fully supported but
34 * override or custom events should be fixed by each plugin as far as there isn't any standard
35 * API (plugin-wise) to launch a rebuild of the calendar events.
37 * @package core
38 * @copyright 2021 onwards Simey Lameze <simey@moodle.com>
39 * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 define('CLI_SCRIPT', true);
44 require_once(__DIR__ . '/../../config.php');
45 require_once($CFG->libdir . "/clilib.php");
46 require_once($CFG->libdir . '/db/upgradelib.php');
48 // Supported options.
49 $long = ['fix' => false, 'help' => false];
50 $short = ['f' => 'fix', 'h' => 'help'];
52 // CLI options.
53 [$options, $unrecognized] = cli_get_params($long, $short);
55 if ($unrecognized) {
56 $unrecognized = implode("\n ", $unrecognized);
57 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
60 if ($options['help']) {
61 $help = <<<EOT
62 Fix orphaned calendar events.
64 This script detects calendar events that have had their
65 userid lost. By default it will perform various checks
66 and report them, showing the site status in an easy way.
68 Also, optionally (--fix), it wil try to recover as many
69 lost userids as possible from different sources. Note that
70 this script aims to process well-know events in core,
71 leaving custom events in 3rd part plugins mostly unmodified
72 because there isn't any consistent way to regenerate them.
74 For more details: https://tracker.moodle.org/browse/MDL-71156
76 Options:
77 -h, --help Print out this help.
78 -f, --fix Fix the orphaned calendar events in the DB.
79 If not specified only check and report problems to STDERR.
81 Usage:
82 - Only report: \$ sudo -u www-data /usr/bin/php admin/cli/fix_orphaned_calendar_events.php
83 - Report and fix: \$ sudo -u www-data /usr/bin/php admin/cli/fix_orphaned_calendar_events.php -f
84 EOT;
86 cli_writeln($help);
87 die;
90 // Check various usual pre-requisites.
91 if (empty($CFG->version)) {
92 cli_error('Database is not yet installed.');
95 $admin = get_admin();
96 if (!$admin) {
97 cli_error('Error: No admin account was found.');
100 if (moodle_needs_upgrading()) {
101 cli_error('Moodle upgrade pending, script execution suspended.');
104 // Do everything as admin by default.
105 \core\session\manager::set_user($admin);
107 // Report current site status.
108 cli_heading('Checking the site status');
109 $needsfix = upgrade_calendar_site_status();
111 // Report current calendar events status.
112 cli_heading('Checking the calendar events status');
113 $info = upgrade_calendar_events_status();
114 $hasbadevents = $info['total']->bad > 0 || $info['total']->bad != $info['other']->bad;
115 $needsfix = $needsfix || $hasbadevents;
117 // If, selected, fix as many calendar events as possible.
118 if ($options['fix']) {
120 // If the report has told us that the fix was not needed... ask for confirmation!
121 if (!$needsfix) {
122 cli_writeln("This site DOES NOT NEED to run the calendar events fix.");
123 $input = cli_input('Are you completely sure that you want to run the fix? (y/N)', 'N', ['y', 'Y', 'n', 'N']);
124 if (strtolower($input) != 'y') {
125 exit(0);
127 cli_writeln("");
129 cli_heading('Fixing as many as possible calendar events');
130 upgrade_calendar_events_fix_remaining($info);
131 // Report current (after fix) calendar events status.
132 cli_heading('Checking the calendar events status (after fix)');
133 upgrade_calendar_events_status();
134 } else if ($needsfix) {
135 // Fix option was not provided but problem events have been found. Notify the user and provide info how to fix these events.
136 cli_writeln("This site NEEDS to run the calendar events fix!");
137 cli_writeln("To fix the calendar events, re-run this script with the --fix option.");