General developer forum

 
 
Picture of marwin marwin
username
 

Hi, can some one tell me how to get the username of user that is displaying in header part (logged in as...) and put it in a block?.

Cheers

Marw

 
Average of ratings: -
Greg watching STS 132 takeoff!
Re: username
 
Have a look at the $USER variable that is pretty much everywhere. $USER->username is what you're after in this case.

When I was first learning Moodle {way back 4 months ago wink } I wrote a moodle page to help me with some of the main moodle variables. You can find a copy of it at:
http://moodle.org/mod/forum/discuss.php?d=55654&parent=253937 It sure came in handy for me!

 
Average of ratings: -
David M. Coleman
Re: username
 
Hi Greg -

I uploaded your moodlevars.php page to my server. Lots of information, but I'm not sure how to get to the specific information I need.

In my case, I just want to be able to print the student's username in a php file.

How do I created a php page (i.e. username.php) that only prints our the username??

For example:

echo $USER->username;


THank you.

 
Average of ratings: -
Greg watching STS 132 takeoff!
Re: username
 
Try this:

<?php
require_once("../config.php") ; //this assumes your php file is in a subdirectory of your moodle

require_login(); //Won't do any good to 'get' a username 'til sombody's logged in.

print "<p>$USER->username</p>"; //this gets the username (login)
print "<p>$USER->firstname $USER->lastname</p>"; //This gets the first and last.
?>

And that's it!
Greg.
 
Average of ratings: -
David M. Coleman
Re: username
 
It worked perfectly. Thanks a lot Greg!
 
Average of ratings: -
Greg watching STS 132 takeoff!
Re: username
 
Happy to help.
 
Average of ratings: -
David M. Coleman
Re: username
 
Hi Greg -

Is there also a way to print which courses the user is enrolled in by accessing the info in the $USER->student array??

require_login(); helps make sure the user is enrolled in at least one course.

However, I'd like my .php file to allow access to only speficif courses.

Would I write a conditional statement that says something like:

if (you are enrolled in course_id_1, course_id_4) {
show content
} else {
sorry, you are not enrolled in this course.

}

Know what I mean?

THank.s

David
 
Average of ratings: -
Picture of Chetan Sharma
Re: username
 
Hi Greg, What if my PHP file is more inside the Moodle Sub-directory i.e. directory/sub-directory/sub-directory-2 What should i do for that in require_once("../config.php") Thanks in Advance
 
Average of ratings: -
Davo
Re: username
Group DevelopersGroup Particularly helpful Moodlers

require_once("../../config.php");

or

require_once("../../../config.php");

etc. (until you have the right number of subdirectories)

 
Average of ratings: -
Picture of Chetan Sharma
Re: username
 

Thanks a Lot...Please help me with one more thing

I want to call list of courses in which a user is enrolled to a different site ...What should I do?

I have called the username and user pic earlier with the code

<?php
require_once("../../config.php") ;
require_login(); //Won't do any good to 'get' a username 'til somebody's logged in.

echo $USER->username;
echo $USER->firstname; // and so on..


print_user_picture($USER, $course->id, $USER->picture, true, false, false);

 

Please Help me with calling the my courses list.

 
Average of ratings: -
Davo
Re: username
Group DevelopersGroup Particularly helpful Moodlers

Please stop cross posting the same question into multiple places.

I notice you seem to be asking a lot of fairly basic questions about the operation of Moodle - you may want to either read carefully through the dev docs + the existing Moodle code, to get a better feel for how it operates, or you may want to consider employing an experienced Moodle developer to write the code for you (at the moment, you seem to be asking the forums to write all of the code for you - one line at a time).

 
Average of ratings:Useful (2)
Jamie
Re: username
 
Hi Greg,
I know you wrote this two years ago, but your posts on that php page that prints out the variables was a BIG help to me smile
Many thanks!
Jamie

 
Average of ratings: -