Sabuj Kundu 10th Jun 2013

How to get user object in Moodle


global $USER, $COURSE;
$userid = $USER->id;

//dump user object
print_r ($USER);

Get enrolled users of a course by course id


global $USER, $COURSE;
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
$students = get_role_users(5, $context);
//dump students object
print_r ($students );

Ger enrolled courses of a user by user id


global $USER, $COURSE;
$courses = enrol_get_users_courses($USER->id,true, '*', 'visible DESC,sortorder ASC');

//use * in place of * for short data list for each course id,fullname,shortname,idnumber,format
print_r ($courses );