Bulk update course image

Bulk update course image

by Anthony Brodard -
Number of replies: 3

Hello Moodle community !


My users used the same - big - image in almost all their courses, as course image. That's leading into an important loading time on pages showing many courses.

I'm trying to find a way to replace in bulk this image by a resized one. I think the best approach is to change the reference directly on the database. I found a way to do it, but I would make sure I will not break anything by manipulating the database like that.

So, I started to upload the resized image as a Course Image on an existing course. So I can retrieve all its parameters on the database.

Now, looking into `mdl_files` tables, I can retrieve the original image several times, and the new one 1 time.

My plan is to update all entries matching the original contenthash, and replace by the new one data. Here is the query:

UPDATE public.mdl_files 

SET contenthash='<new hash>', filename='<new filename>', filesize='<new filesize>'

WHERE contenthash='<old hash>', component='course', filearea='overviewfiles'

Is this update safe ? Is there a better way to do it ?

Thank you !

Average of ratings: -
In reply to Anthony Brodard

Re: Bulk update course image

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Not safe to mess with the files table! Put the image somewhere on the server and use a static URL instead.
Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: Bulk update course image

by Anthony Brodard -
Hey Gareth,

Thanks for this fast answer!
Ok about files table, I'll keep it safe =)
About using a static URL, do you know if there's a way to do the bulk update ? Or should we do it manually on all impacted courses ?
In reply to Anthony Brodard

Re: Bulk update course image

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Humm, good question. I'm not sure. But... thinking about it, then replacing the file in the MoodleData folder might work, then you can alter the values in the files table... but! Test, test and test again on a test server that if breaks is not an issue!