$USER in prescsscallback function

$USER in prescsscallback function

ved Alain Bolli -
Antal besvarelser: 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

Gennemsnitsbedømmelse: -
I svar til Alain Bolli

Re: $USER in prescsscallback function

ved Tim Hunt -
Billede af Core developers Billede af Documentation writers Billede af Particularly helpful Moodlers Billede af Peer reviewers Billede af 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?)


I svar til Tim Hunt

Re: $USER in prescsscallback function

ved 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

I svar til Alain Bolli

Re: $USER in prescsscallback function

ved Tim Hunt -
Billede af Core developers Billede af Documentation writers Billede af Particularly helpful Moodlers Billede af Peer reviewers Billede af Plugin developers

OK, the $USER will be set.

I svar til Tim Hunt

Re: $USER in prescsscallback function

ved Richard Oelmann -
Billede af Core developers Billede af Particularly helpful Moodlers Billede af Plugin developers Billede af 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. 

I svar til Richard Oelmann

Re: $USER in prescsscallback function

ved 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