Fixing Theme Assignment Errors in Moodle User Profiles

When managing user settings in Moodle, you may encounter an issue where editing a user's profile results in an error if the theme column in the database is set to NULL. Moodle expects the theme value to either be empty (i.e., an empty string) or contain a valid theme name. A NULL value can cause problems, particularly when updating user preferences. In this blog post, we’ll walk through how to troubleshoot and resolve this issue by checking and fixing the database.
Error
Exception - theme_delete_used_in_context_cache(): Argument #2 ($oldtheme) must be of type string, null given, called in [dirroot]/user/editadvanced_form.php on line 248
Understanding the Problem
The root of the issue lies in the mdl_user table within your Moodle database. The theme column stores the preferred theme for each user. If this column is set to NULL, Moodle may throw errors when attempting to edit a user's profile. To avoid such issues, the theme column should either:
- Be an empty string (
'') if no specific theme is assigned. - Contain the name of a valid theme (e.g.,
boost,classic).
By ensuring that the theme column is never NULL, you can prevent these errors from occurring.
Note: The column is set to default NULL. This could be a problem from Moodle.
Step 1: Checking the Database
To identify users with a NULL value in the theme column, you can run the following SQL query in phpMyAdmin or any other MySQL client:
SELECT id, username, email, theme
FROM mdl_user
WHERE theme IS NULL;Or use this query to show a summary of all theme assignments.
SELECT theme, COUNT(*) AS user_count
FROM mdl_user
GROUP BY theme
WITH ROLLUP;
This query will return a list of all users whose theme column is currently set to NULL. If the result set is not empty, it confirms that some users have a NULL value in their theme column.
You can also count the number of affected users using this query:
SELECT COUNT(*) AS null_theme_count
FROM mdl_user
WHERE theme IS NULL;
Step 2: Fixing the Issue
Once you’ve identified the users with NULL values in the theme column, you can update these entries to ensure they are empty strings (''). Run the following SQL query to perform the update:
UPDATE mdl_user
SET theme = ''
WHERE theme IS NULL;
This query replaces all NULL values in the theme column with an empty string. After running this query, verify that no users still have a NULL value in the theme column by re-running the first query:
SELECT id, username, email, theme
FROM mdl_user
WHERE theme IS NULL;
If the query returns no rows, the update was successful.
Optional Step: Assigning a Default Theme
If you prefer to assign a default theme to users instead of leaving the theme column empty, you can retrieve the site-wide default theme from the mdl_config table:
SELECT value AS default_theme
FROM mdl_config
WHERE name = 'theme';
Then, update the theme column for users with an empty string to the default theme. Replace <default_theme> with the name of your default theme (e.g., boost):
UPDATE mdl_user
SET theme = '<default_theme>'
WHERE theme = '';
For example:
UPDATE mdl_user
SET theme = 'boost'
WHERE theme = '';
Step 3: Clearing Caches
After making changes to the database, it’s important to clear Moodle’s caches to ensure the updates take effect. You can do this by running the following command in your terminal:
php admin/cli/purge_caches.php
Further notes...
By ensuring that the theme column in the mdl_user table is never NULL, you can eliminate errors related to user profile editing in Moodle.
