$USER in prescsscallback function

$USER in prescsscallback function

από Alain Bolli -
Αριθμός απαντήσεων: 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

Μέσος όρος βαθμολογίας: -
Σε απάντηση σε Alain Bolli

Re: $USER in prescsscallback function

από Tim Hunt -
Φωτογραφία Core developers Φωτογραφία Documentation writers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Peer reviewers Φωτογραφία 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?)


Σε απάντηση σε Tim Hunt

Re: $USER in prescsscallback function

από 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

Σε απάντηση σε Alain Bolli

Re: $USER in prescsscallback function

από Tim Hunt -
Φωτογραφία Core developers Φωτογραφία Documentation writers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Peer reviewers Φωτογραφία Plugin developers

OK, the $USER will be set.

Σε απάντηση σε Tim Hunt

Re: $USER in prescsscallback function

από Richard Oelmann -
Φωτογραφία Core developers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Plugin developers Φωτογραφία 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. 

Σε απάντηση σε Richard Oelmann

Re: $USER in prescsscallback function

από 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