email_html.mustache in custom theme for specific user

email_html.mustache in custom theme for specific user

by Matthew Terentjevs -
Number of replies: 5

Dear all, 

I am wondering if anyone had experience of using custom email_html.mustache in custom theme that is not Moodle's main system theme. 

User case: 

System theme is set to boost. Individual user's theme is set to a custom theme. When user is logged in and e-mails are generated by user's action within the theme all e-mails are styled correctly using email_html.mustache supplied with user's custom theme. 

However, when e-mails are generated by cron and etc..., with user logged out - all e-mails sent to the user use default system theme ignoring user's or cohort's theme settings. 

Did anyone else encountered this and was able to resolve this in Moodle 3.9?


Average of ratings: -
In reply to Matthew Terentjevs

Re: email_html.mustache in custom theme for specific user

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers
Emails sent by cron etc are sent by the system and not by the user account, so will not pick up a user theme only the site one.
In reply to Richard Oelmann

Re: email_html.mustache in custom theme for specific user

by Matthew Terentjevs -

Thanks, Richard! 

That is what I thought... Will try and see if I will be able to get additional logic in the e-mail function to check affected user's theme first and force the correct template.... smile 

Matt. 

In reply to Matthew Terentjevs

Re: email_html.mustache in custom theme for specific user

by Matthew Terentjevs -

I am wondering if anyone could help me understand how Moodle selects the theme to pull .mustache templates from. 

I can see moodlelib.php  $messagehtml = $renderer->render_from_template('core/email_html', $context); calls a renderer using 'core/email_html' but struggling to see where theme name comes into the function. How does Moodle know which theme to use for 'core/email_html'?

Is there a function, that says, based on the page context theme is "boost" and this is where you need to render the mustache template from?

Trying to find it, so I could check user who the e-mail is directed to, check user's selected theme and use the theme instead of the system one. Potentially can check for a course theme if the e-mail is course related and it's in the e-mail.

In reply to Matthew Terentjevs

Re: email_html.mustache in custom theme for specific user

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Logic is in '/lib/classes/output/mustache_template_finder.php'.
In reply to Gareth J Barnard

Re: email_html.mustache in custom theme for specific user

by Matthew Terentjevs -
Thanks, Gareth.

I was able to resolve my specific issue, by adding the below to the moodlelib.php function email_to_user:

/hack to ensure user gets cron emails based on their preferred team/
if($PAGE->bodyid === "page-admin-cli-cron" and !empty($user->theme)) {
$PAGE->force_theme($user->theme);
}
/hack to ensure user gets cron emails based on their preferred team/

It should check if a user has a preferred theme and if e-mail is generated by cron, let's use a preferred theme, rather than system's default...