$USER in prescsscallback function

$USER in prescsscallback function

by Alain Bolli -
Number of replies: 5

Hi all,

I'm creating a child theme from Boost.

I would like to have some user preferences so that each user can choose the primary color.

So I modified the theme_boost_get_pre_scss($theme) function and added at the beginning :

global $USER, $DB;

$user = $DB->get_record('user', array('id' => $USER->id)); 

But $USER->id equals 0.

Is $USER available in all functions ?

This function is in my theme's lib.php file. In this lib.php file there is another function that uses $USER without problems.

Best,

Alain

Average of ratings: -
In reply to Alain Bolli

Re: $USER in prescsscallback function

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

$USER is available almost everywhere, but:

  1. $USER is already a full object, so doing get_record on the 'user' table is a complete waste of time.
  2. A few scripts that are involved in serving things like CSS and files are performance critical, and so are heavily optimised. (For example, since started the user's session is quite slow, they try to avoid doing so if it is not needed.) This might be such a script. (What is the URL when your function is called? What does that script do?)


In reply to Tim Hunt

Re: $USER in prescsscallback function

by Alain Bolli -

Hi Tim,

The function is called on every page from lib/outputlib.php. It ads scss lines at the beginning of main scss file.

Best,

Alain

In reply to Alain Bolli

Re: $USER in prescsscallback function

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

OK, the $USER will be set.

In reply to Tim Hunt

Re: $USER in prescsscallback function

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

But scss is processed and cached. What you are trying to do sounds like it would attempt to call the user setting and force the page to serve up the scss and process it every page load (adding the user specific scss to it). 

If that's the case, you will be looking at a huge performance hit. Or, could you explain how you get around that? I'm looking to do something vaguely similar, but had only considered adding user specific css because of the performance issues. 

In reply to Richard Oelmann

Re: $USER in prescsscallback function

by Alain Bolli -

Hi,

As I could not use $USER, I changed my idea.

I want to let user choose between 3 colorsets in my theme. So know, I look for this preference only in the layout page. Than I add a class to body : colorset1 or colorset2 ou colorset3.

So, the css is unique and does not change for each user. It can be cached without problems.

Best,

Alain