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.

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

Upgrade helper functions This file is used for special upgrade functions - for example groups and gradebook. These functions must use SQL and database related functions only- no other Moodle API, because it might depend on db structures that are not yet present during upgrade. (Do not use functions from accesslib.php, grades classes or group functions at all!)

Copyright: 2007 Petr Skoda (http://skodak.org)
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 1826 lines (75 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 27 functions


Functions that are not part of a class:

upgrade_mysql_get_supported_tables()   X-Ref
Returns all non-view and non-temp tables with sane names.
Prints list of non-supported tables using $OUTPUT->notification()

return: array

upgrade_group_members_only($groupingid, $availability)   X-Ref
Using data for a single course-module that has groupmembersonly enabled,
returns the new availability value that incorporates the correct
groupmembersonly option.

Included as a function so that it can be shared between upgrade and restore,
and unit-tested.

param: int $groupingid Grouping id for the course-module (0 if none)
param: string $availability Availability JSON data for the module (null if none)
return: string New value for availability for the module

upgrade_extra_credit_weightoverride($onlycourseid = 0)   X-Ref
Marks all courses with changes in extra credit weight calculation

Used during upgrade and in course restore process

This upgrade script is needed because we changed the algorithm for calculating the automatic weights of extra
credit items and want to prevent changes in the existing student grades.

param: int $onlycourseid

upgrade_calculated_grade_items($courseid = null)   X-Ref
Marks all courses that require calculated grade items be updated.

Used during upgrade and in course restore process.

This upgrade script is needed because the calculated grade items were stuck with a maximum of 100 and could be changed.
This flags the courses that are affected and the grade book is frozen to retain grade integrity.

param: int $courseid Specify a course ID to run this script on just one course.

make_default_scale()   X-Ref
This function creates a default separated/connected scale
so there's something in the database.  The locations of
strings and files is a bit odd, but this is because we
need to maintain backward compatibility with many different
existing language translations and older sites.

return: void

make_competence_scale()   X-Ref
Create another default scale.

param: int $oldversion
return: bool always true

upgrade_course_letter_boundary($courseid = null)   X-Ref
Marks all courses that require rounded grade items be updated.

Used during upgrade and in course restore process.

This upgrade script is needed because it has been decided that if a grade is rounded up, and it will changed a letter
grade or satisfy a course completion grade criteria, then it should be set as so, and the letter will be awarded and or
the course completion grade will be awarded.

param: int $courseid Specify a course ID to run this script on just one course.

upgrade_letter_boundary_needs_freeze($context)   X-Ref
Checks the letter boundary of the provided context to see if it needs freezing.
Each letter boundary is tested to see if receiving that boundary number will
result in achieving the cosponsoring letter.

param: object $context Context object
return: bool if the letter boundary for this context should be frozen.

upgrade_standardise_score($rawgrade, $sourcemin, $sourcemax, $targetmin, $targetmax)   X-Ref
Given a float value situated between a source minimum and a source maximum, converts it to the
corresponding value situated between a target minimum and a target maximum. Thanks to Darlene
for the formula :-)

param: float $rawgrade
param: float $sourcemin
param: float $sourcemax
param: float $targetmin
param: float $targetmax
return: float Converted value

upgrade_fix_serialized_objects($serializeddata)   X-Ref
Provides a way to check and update a serialized string that uses the deprecated object class.

param: string $serializeddata Serialized string which may contain the now deprecated object.
return: array Returns an array where the first variable is a bool with a status of whether the initial data was changed

upgrade_delete_orphaned_file_records()   X-Ref
Deletes file records which have their repository deleted.


upgrade_core_licenses()   X-Ref
Upgrade core licenses shipped with Moodle.


upgrade_calendar_site_status(bool $output = true)   X-Ref
Detects if the site may need to get the calendar events fixed or no. With optional output.

param: bool $output true if the function must output information, false if not.
return: bool true if the site needs to run the fixes, false if not.

upgrade_calendar_events_status(bool $output = true)   X-Ref
Detects the calendar events needing to be fixed. With optional output.

param: bool $output true if the function must output information, false if not.
return: stdClass[] an array of event types (as keys) with total and bad counters, plus sql to retrieve them.

upgrade_calendar_events_fix_remaining(array $info, bool $output = true, int $maxseconds = 0)   X-Ref
Detects the calendar events needing to be fixed. With optional output.

param: stdClass[] an array of event types (as keys) with total and bad counters, plus sql to retrieve them.
param: bool $output true if the function must output information, false if not.
param: int $maxseconds Number of seconds the function will run as max, with zero meaning no limit.
return: bool true if the function has not finished fixing everything, false if it has finished.

upgrade_calendar_events_mtrace(string $string, bool $output)   X-Ref
Wrapper over mtrace() to allow a few more things to be specified.

param: string $string string to output.
param: bool $output true to perform the output, false to avoid it.

upgrade_calendar_events_get_teacherid(int $courseid)   X-Ref
Get a valid editing teacher for a given courseid

param: int $courseid The course to look for editing teachers.
return: int A user id of an editing teacher or, if missing, the admin userid.

upgrade_calendar_standard_events_fix(stdClass $info, bool $output = true, int $endtime = 0)   X-Ref
Detects the calendar standard events needing to be fixed. With optional output.

param: stdClass $info an object with total and bad counters, plus sql to retrieve them.
param: bool $output true if the function must output information, false if not.
param: int $endtime cutoff time when the process must stop (0 means no cutoff).
return: bool true if the function has not finished fixing everything, false if it has finished.

upgrade_calendar_subscription_events_fix(stdClass $info, bool $output = true, int $endtime = 0)   X-Ref
Detects the calendar subscription events needing to be fixed. With optional output.

param: stdClass $info an object with total and bad counters, plus sql to retrieve them.
param: bool $output true if the function must output information, false if not.
param: int $endtime cutoff time when the process must stop (0 means no cutoff).
return: bool true if the function has not finished fixing everything, false if it has finished.

upgrade_calendar_action_events_fix(stdClass $info, bool $output = true, int $endtime = 0)   X-Ref
Detects the calendar action events needing to be fixed. With optional output.

param: stdClass $info an object with total and bad counters, plus sql to retrieve them.
param: bool $output true if the function must output information, false if not.
param: int $endtime cutoff time when the process must stop (0 means no cutoff).
return: bool true if the function has not finished fixing everything, false if it has finished.

upgrade_calendar_override_events_fix(stdClass $info, bool $output = true, int $endtime = 0)   X-Ref
Detects the calendar override events needing to be fixed. With optional output.

param: stdClass $info an object with total and bad counters, plus sql to retrieve them.
param: bool $output true if the function must output information, false if not.
param: int $endtime cutoff time when the process must stop (0 means no cutoff).
return: bool true if the function has not finished fixing everything, false if it has finished.

upgrade_add_item_to_usermenu(string $menuitem)   X-Ref
Add a new item at the end of the usermenu.

param: string $menuitem

upgrade_block_set_defaultregion(string $blockname,string $pagename,string $pagetypepattern,string $newdefaultregion)   X-Ref
Update all instances of a block shown on a pagetype to a new default region, adding missing block instances where
none is found.

Note: This is intended as a helper to add blocks to all instances of the standard my-page. It will only work where
the subpagepattern is a string representation of an integer. If there are any string values this will not work.

param: string $blockname The block name, without the block_ frankenstyle component
param: string $pagename The type of my-page to match
param: string $pagetypepattern The page type pattern to match for the block
param: string $newdefaultregion The new region to set

upgrade_block_delete_instances(string $blockname,string $pagename,string $pagetypepattern)   X-Ref
Remove all instances of a block on pages of the specified pagetypepattern.

Note: This is intended as a helper to add blocks to all instances of the standard my-page. It will only work where
the subpagepattern is a string representation of an integer. If there are any string values this will not work.

param: string $blockname The block name, without the block_ frankenstyle component
param: string $pagename The type of my-page to match
param: string $pagetypepattern This is typically used on the 'my-index'

upgrade_block_set_my_user_parent_context(string $blockname,string $pagename,string $pagetypepattern)   X-Ref
Update the block instance parentcontext to point to the correct user context id for the specified block on a my page.

param: string $blockname
param: string $pagename
param: string $pagetypepattern

upgrade_fix_file_timestamps()   X-Ref
Fix the timestamps for files where their timestamps are older
than the directory listing that they are contained in.


upgrade_add_foreign_key_and_indexes()   X-Ref
Upgrade helper to add foreign keys and indexes for MDL-49795