General developer forum

$USER in prescsscallback function

 
Picture of Alain Bolli
$USER in prescsscallback function
Group Plugin developers

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: -
Tim at Lone Pine Koala Sanctuary
Re: $USER in prescsscallback function
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup 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?)


 
Average of ratings: -
Picture of Alain Bolli
Re: $USER in prescsscallback function
Group Plugin developers

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

 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: $USER in prescsscallback function
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

OK, the $USER will be set.

 
Average of ratings: -
Picture of Richard Oelmann
Re: $USER in prescsscallback function
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developersGroup 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. 

 
Average of ratings: -
Picture of Alain Bolli
Re: $USER in prescsscallback function
Group Plugin developers

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

 
Average of ratings: -