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!
Does that help? It will give you an object that represents the current user's information in the user database table.
And where is $USER stored? Do i have to include a file to use $USER or anything?
Thanks Adam!
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...
$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.
Sorry if I misled anyone!
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
I should be ok now for what i'm tryin to do...
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.
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(...);
Great!!
It worked..
Thanks Tim.
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
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.
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
Hi everyone!
I would like to know what kind of modification this 'timemodified' stores.
Thx!
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)
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.
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)
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?
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
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.
Take a look at the Generico plugin for a standard way of doing this type of thing.
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
$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.
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
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.
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
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
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
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
Was there a solution for this? i seem to need to do it as well