Re-sort Courses
local_resort_courses
Admin-only local plugin that automatically re-sorts the courses in a course category (by full name, short name, ID number or start date) whenever a course is created or updated, with an optional scheduled task to re-sort all categories and settings to skip selected categories, optionally recursively.
The plugin has no user-facing pages, forms, AJAX endpoints or web services. Its only entry points are two core event observers (course_created and course_updated), a scheduled task that is disabled by default, and an admin settings page gated by $hassiteconfig. No user-controlled input reaches SQL: the ORDER BY clause comes from a fixed switch over admin-configured integer constants and falls back to a safe default, and every other query uses $DB with array conditions.
The only finding is low severity and about efficiency. Every course create or update rewrites sortorder on every course in the category, even rows whose value has not changed, and then runs the site-wide fix_course_sortorder() and a course cache purge. This can slow down saves in large categories, but it is not a security issue.
The Privacy API is implemented correctly as a null provider. The plugin uses current namespaced core APIs (\core\url, \core_cache\helper, core_course_category), ships Behat coverage, and bundles no third-party code.
local_resort_courses is a small admin-configured plugin, about 400 lines of PHP. It keeps courses in each category in a fixed order. It listens to core's course_created and course_updated events and rewrites the sortorder of every course in the affected category. An optional scheduled task, disabled by default, re-sorts all categories.
The security posture is clean. There are no pages or endpoints a user can call directly, and the settings page sits behind $hassiteconfig. The SQL sort clause is chosen from a hardcoded switch on an admin-selected integer, so nothing user-supplied reaches a query. The event class, scheduled task and null privacy provider all follow core conventions and use current namespaced APIs.
The single finding is a low-severity efficiency concern. Every course save issues one UPDATE per course in the category, even when the order has not changed, and then runs the site-wide fix_course_sortorder() and a changesincourse cache purge. On sites with very large categories or bulk course updates, this adds avoidable write load to every save.
Findings
resort_course_category() runs synchronously on every course_created and course_updated event. It loads every course in the category and calls $DB->set_field() once per course, whether or not that course's sortorder actually changes. After that it calls fix_course_sortorder(), which scans all categories and runs a grouped count across the whole course table, and it purges the changesincourse cache. Each save also writes a courses_sorted log entry. In a category with thousands of courses, or during bulk operations that fire course_updated many times (CSV uploads, restores, sync tools), this turns each save into thousands of writes plus a site-wide maintenance pass.
Low risk. Only users who can already create or edit courses can trigger it, and the result is correct; the cost is extra write load and slower saves on large sites. Skipping unchanged rows removes almost all of that load in the common case where an edit does not change the order.
The observer is registered in db/events.php for both course creation and update. Core fires course_updated from update_course() and other course-editing paths, so the plugin's work runs inside the request that saves the course.
$courses = $DB->get_records('course', ['category' => $category->id], $sortsql, 'id');
// If there are any courses in the category.
if (count($courses) > 0) {
// Do the resorting.
$i = 1;
foreach ($courses as $course) {
$DB->set_field('course', 'sortorder', $category->sortorder + $i, ['id' => $course->id]);
$i++;
}
Fetch the current sortorder and write only the rows that change. If nothing changed, skip the cleanup and the log event too:
$courses = $DB->get_records('course', ['category' => $category->id], $sortsql, 'id, sortorder');
$changed = false;
$i = 1;
foreach ($courses as $course) {
$newsortorder = $category->sortorder + $i;
if ((int)$course->sortorder !== $newsortorder) {
$DB->set_field('course', 'sortorder', $newsortorder, ['id' => $course->id]);
$changed = true;
}
$i++;
}
if ($changed) {
// Cleanup and log event as before.
}
Raw course table query is intentional and correct. resort_course_category() reads {course} directly rather than through get_courses(). The inline comment explains why: the re-sort must cover hidden courses that the triggering user (for example, a teacher) cannot see. This is the right choice for a background ordering job, and only course ids are selected.
Sorting differs slightly from core's core_course_category::resort_courses(). The plugin sorts on raw lower(fullname) / lower(shortname) in SQL. Core sorts on the format_string()-rendered value. With multilang filters or HTML in course names, the order may differ from what users see on screen. This is a documented design decision, not a defect.