$USER variable

$USER variable

by Barry O Reilly -
Number of replies: 31
Hey everyone,
Probably a stupid question but is the $USER variable a global variable that can be used in any php file in moodle and can it tell me the user id of who is currently looking at a moodle page?

I would like to try and access information in the database in the mdl_user table and i need the id of the user to query the database.

Or if i am making no sense whatsover, here is a scenario. A user is posting a forum post, i would like to get the id of this user and then based on the id query the database. Can i do this?

Also what does optional_param do? Does it define an optional varible that can be passed as a GET value?


I'm obviously new to this!
Average of ratings: -
In reply to Barry O Reilly

Re: $USER variable

by Adam Zapletal -
$user_object = get_record('user', 'id', $USER->id);

Does that help? It will give you an object that represents the current user's information in the user database table.
In reply to Adam Zapletal

Re: $USER variable

by Barry O Reilly -
Yeah it prob does... Will work with it soon. Could you tell me what $USER->id does? What does the -> mean?
And where is $USER stored? Do i have to include a file to use $USER or anything?

Thanks Adam!
In reply to Adam Zapletal

Re: $USER variable

by Barry O Reilly -
I think you solved my problem anyway so thank you very much. Am i right in saying that $USER is an object and by putting $USER->id into get_record you are looking up the value of id of the object $USER?

And now that i have an object $user_object, which contains the whole row of information in the table user with the current users id, i can say something like
$test = $user_object->firstname ? this assigns the firstname of the user to test?

Thanks again...
In reply to Barry O Reilly

Re: $USER variable

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
In PHP -> is the operator for accessing a properly of an object. The equivalent in Java or somehting like that would be USER.id.

$USER is a global, and actually it already holds the result of doing get_record('user', 'id', $USER->id), so use use $USER and save one database access. (Actually, $USER has some extra fields added too.)

A useful function in Moodle is print_object($something). It dumps out any variable, not necessarily an object, so that you can see all the data it contains. This is very useful as you are developing code.

optional_param is a useful function for getting data out of GET or POST requests. The second argument gives a default to use if a parameter with that name was not sent to your script (you can always use null if you don't want a default). The third argument is a data type, for example PARAM_INT. This is used to clean up the input, so if you ask for an integer, you can be sure you have got an integer. This is important for avoiding security problems (http://xkcd.com/327/). There is a partner function require_param which does not take a default, and gives an error if the parameter was not set in the request.
Average of ratings: Useful (3)
In reply to Tim Hunt

Re: $USER variable

by Adam Zapletal -
Not sure how I missed that $USER already has the result of that query...

Sorry if I misled anyone! dead
Average of ratings: Useful (1)
In reply to Barry O Reilly

Re: $USER variable

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
$USER is likely to already contain most key fields of the user object - so print_object it to see if it has what you need, like Tim said.

Also note:

1) You just need to require config.php for all this stuff (and any other Moodle features) to work.

2) There is a fullname() function in Moodle that takes a user object and returns their name as it should be displayed.

3) Make sure your script has called require_login at some point, otherwise $USER might not contain a valid user id because the person looking at the page might not have logged in. (Or if you don't want to require_login, just be ready to handle this.)

4) When you do require_login it should probably not allow guest access, unless you are ok with getting a $USER object that doesn't correspond to a 'real' user.

If you aren't familiar with any of this, look up the definitions of the functions mentioned - they mostly contain help comments above the function in the code.

--sam
Average of ratings: Useful (1)
In reply to sam marshall

Re: $USER variable

by Barry O Reilly -
Thanks you very much Tim and Sam. i should have asked those questions ages ago to clear stuff up.
I should be ok now for what i'm tryin to do...
In reply to Barry O Reilly

Re: $USER variable

by Neda Shafiee -
hi,i have an scrom package and i want to reach the user id of the moodle current user who logged in.but i have a problem. in my html form i used post method to redirect to a php page to reach the user object but when i submit the html form i see the php page code instead of my desired result.what should i do?can any body help me?
In reply to Adam Zapletal

Re: $USER variable

by ankit mittal -

This solution is not working with moodle 2.2. The error I am getting is "get_record() not available anymore" When clicked on more details, I get the following.. "

Function isteacherinanycourse() was removed, please use capabilities instead!

An attempt to bring a Moodle 1.9 solution to v2.0 ending in failure"

Any suggestion, how to correct this statement.

In reply to ankit mittal

Re: $USER variable

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

The post you are replying to was written in 2008, which Moodle 1.9 was the latest release.

The database API changed in Moodle 2.0. You now need to say

global $DB;

$DB->get_record(...);

In reply to Tim Hunt

Re: $USER variable

by ankit mittal -

Great!!

It worked..

Thanks Tim.

In reply to ankit mittal

Re: $USER variable

by dan k -

hey guys, i'm wondering why can't i retrieve the description of the user using $USER->description

I can get descriptionformat but not description... any reason for that?

The other question is how do i get the user desctription from his profile?

cheers

dan

In reply to dan k

Re: $USER variable

by Will Gimenes -

Hi. Regarding this subject, I would to understand what this means:

 $USER->firstaccess

[firstaccess] => 1409755214

How can I 'decript' this into a real date?

Tks in advance.

In reply to Will Gimenes

Re: $USER variable

by Abhi puri -
Hi Will, the date returned is a UNIX timestamp, also known as epoch time.
What you need to do is convert it to human readable format using the php date function

example:
$epocht = "1409755214" ;
echo date('y-m-d', $epocht) ;

most common  (RFC232 format date)
echo date('r', $epocht) ;

you can do a lot more with the date function.
Refer to http://php.net/manual/en/function.date.php


Average of ratings: Useful (1)
In reply to Abhi puri

Re: $USER variable

by Will Gimenes -

Hi everyone!

I would like to know what kind of modification this 'timemodified' stores. 

Thx!

In reply to Will Gimenes

Re: $USER variable

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

It stores the last time that item was modified - so any modification to that table row done from the front-end, rather than directly in the database using something like phpmyadmin (you'll find that in several of the tables, not just relating to USER)

Average of ratings: Useful (1)
In reply to Richard Oelmann

Re: $USER variable

by Will Gimenes -

Nice. I am actually  trying to find some var that tells me when the theme was changed for the last time. It can include .css, .js, php .

After having this information I am going to compare that with when the current user accessed for the last time, if he accessed before the last update, I am going to show him some message.


In reply to Will Gimenes

Re: $USER variable

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

I dont think there's any way that the database can hold information about the times the theme code itself is changed, unless the version.php is bumped and the theme effectively re-installed?

You might try the mdl_config_log table which holds a timemodified for any settings in the theme (search for theme_themename if using phpmyadmin to take a look at the table)

Average of ratings: Useful (1)
In reply to Barry O Reilly

Re: $USER variable

by Marjan Milošević -

I'll just use this topic, since it is resurrected.

What is the most efficient way of getting a $USER-like object for any user (not just the one that is currently logged in)? I know I can use get_record in order to create an object containing data from mdl_user, but I need the additional fields data - which is not in mdl_user. 

$USER got that data too. Can I somehow reuse code that makes $USER for current user, in order to make $SOME_USER for given userid?

Or I should just write some SQL?



In reply to Marjan Milošević

Re: $USER variable

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

All the data in $USER is expensive to calculate. You should only get the bits you really need.

Mostly you do that using the API. E.g. has_capability has extra optional arguments you can use to check the permissions for another user.

Similarly get_/set_user_preferences

Average of ratings: Useful (2)
In reply to Barry O Reilly

Re: $USER variable

by ranu jaiswal -

Hi, 

I have to do some customization on my moodle account. I am using javascipt and writing it inside additional HTML link. IS there any way to find out current userid , courseid in that js code. If i have to use "$user_object = get_record('user', 'id', $USER->id);" than how can i write this php code inside that js code snippet can anyone pls elaborate i am very new to moodle.

In reply to ranu jaiswal

Re: $USER variable

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Take a look at the Generico plugin for a standard way of doing this type of thing.

In reply to Marcus Green

Re: $USER variable

by ranu jaiswal -

Thanx but i go through this. This is for templates i dont want to create templates i want to do coding in js like calling API , showing popup. And for this I need current userid and courseid there in my js code written inside Additinal HTML So how could i do this

In reply to ranu jaiswal

Re: $USER variable

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

$USER is a php variable and you want to pass that into javascript code - am I understanding correctly?

Then there are plenty of useful hints on the net...

http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript

But consider the fact that php is server-side code and javascript is client-side - that's not about Moodle specifically, but would be the same for any system using php (or other server side language) alongside javascript (or other client side processes/language).

For a recent project I did (the radial grades/completion widgets in flexibase/pioneer) I used the method of writing the variable to the DOM and getting it from there, but that Stackflow answer gives several methods.


In reply to Richard Oelmann

Re: $USER variable

by ranu jaiswal -

Thanx for your valuable reply I tried with that but when i wrote this code inside my js file and console the value of div.textContent than it is giving null

In reply to ranu jaiswal

Re: $USER variable

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers

Some information is available in javascript already for you to use in the javascript M.cfg object. 

On a Moodla page from the browser console, Try doing a 

console.log(M.cfg);

And see what you get. But you usually have to pass information like user id and course id to javascript from PHP to get it. Thats fairly common stuff and lots of plugins will be doing this.

 But if you do not want to develop a plugin to do this, then the Generico filter will do it for you. Generico templates are bundles of HTML/CSS/JS ( ie front end stuff) and variables can be passed in from PHP to those bundles. Its a bit of learning curve, but for prototyping, customizing a site, hit and run stuff ... it is a lot faster than building a new plugin.


Average of ratings: Useful (1)
In reply to Justin Hunt

Re: $USER variable

by ranu jaiswal -

hi Justin,

Thanx! Can you pls tell me where to upload this filter i just downloaded it and i dont have access to server side code so i am not able to upload it on location your_moodle_site/filters or i dont knw how to upload it there can you pls help

In reply to Justin Hunt

Re: $USER variable

by ranu jaiswal -

I have seen in many sites that filters are created by doing some coding in php but where to write that php code i dont know. As i only have access to the front end i,e; the moodle site only. SO i am doing changes by using javascipt which i wrote in Additional HTML. But i need to have access to current userId there So how can i access is there any other way on the client side only by which i can do that

In reply to Justin Hunt

Re: $USER variable

by ranu jaiswal -

All my problem has been solved only one is there. As there is a @@USER:firstname@@ variable which will print current user first name . How can i get userID and courseID and from where i came to know about all of ther USER:____ variable which i can use.


KIndly Reply 

Thanx

In reply to Justin Hunt

Re: $USER variable

by ranu jaiswal -

Hi Justin,

As you said php variables can be passed into generico filter how can i do this and use it in generico filter js. Can you please elaborate.

Thanx in advance

In reply to ranu jaiswal

Re: $USER variable

by Ben Laor -

Was there a solution for this? i seem to need to do it as well