Skip to content

Instantly share code, notes, and snippets.

@stronk7
Created April 6, 2021 23:22
Show Gist options
  • Save stronk7/39f925bd1c56c3543c2875136553965f to your computer and use it in GitHub Desktop.
Save stronk7/39f925bd1c56c3543c2875136553965f to your computer and use it in GitHub Desktop.
diff --git a/admin/cli/fix_orphaned_calendar_events.php b/admin/cli/fix_orphaned_calendar_events.php
index 8a203b74391..20f83d5170a 100644
--- a/admin/cli/fix_orphaned_calendar_events.php
+++ b/admin/cli/fix_orphaned_calendar_events.php
@@ -113,7 +113,7 @@ if (moodle_needs_upgrading()) {
// Report current site status.
cli_heading('Checking the site status');
-upgrade_calendar_site_status();
+$needsfix = upgrade_calendar_site_status();
// Report current calendar events status.
cli_heading('Checking the calendar events status');
@@ -121,6 +121,16 @@ $info = upgrade_calendar_events_status();
// If, selected, fix as many calendar events as possible.
if ($options['fix']) {
+
+ // If the report has told us that the fix was not needed... ask for confirmation!
+ if (!$needsfix) {
+ cli_writeln("This site DOES NOT NEED to run the calendar events fix.");
+ $input = cli_input('Are you completely sure that you want to run the fix? (y/N)', 'N', ['y', 'Y', 'n', 'N']);
+ if (strtolower($input) != 'y') {
+ exit(0);
+ }
+ cli_writeln("");
+ }
cli_heading('Fixing as many as possible calendar events');
upgrade_calendar_events_fix($info);
// Report current (after fix) calendar events status.
@@ -604,7 +614,10 @@ function upgrade_calendar_override_events_fix(stdClass $info, bool $output = tru
$DB->delete_records('event', ['modulename' => $module->modulename, 'instance' => $module->instance]);
// Get the activity record.
- $activityrecord = $DB->get_record($module->modulename, ['id' => $module->instance]);
+ if (!$activityrecord = $DB->get_record($module->modulename, ['id' => $module->instance])) {
+ // Orphaned calendar event (activity doesn't exists), skip.
+ continue;
+ }
// Let's rebuild it by calling to each module API.
switch ($module->modulename) {
@@ -619,6 +632,14 @@ function upgrade_calendar_override_events_fix(stdClass $info, bool $output = tru
break;
}
+ // Sometimes, some (group) overrides are created without userid, when that happens, they deserve
+ // some user (teacher or admin). This doesn't affect to groups calendar events behaviour,
+ // but allows counters to detect already processed group overrides and makes things
+ // consistent.
+ $DB->set_field_select('event', 'userid', upgrade_calendar_events_get_teacherid($activityrecord->course),
+ 'modulename = ? AND instance = ? and priority != 0 and userid = 0',
+ ['modulename' => $module->modulename, 'instance' => $module->instance]);
+
// Cutoff time, let's exit.
if ($endtime && $endtime <= time()) {
$status = 'Remaining override events pending';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment