get course user is currently in

get course user is currently in

by Daniel Schleuter -
Number of replies: 2
Hey,

i made myself a new php file and want to get the course id of the course where the user is currently in.
I appreciate, if someone could help me out!

Greetings,
Daniel
Average of ratings: -
In reply to Daniel Schleuter

Re: get course user is currently in

by Trevor Furtado -
Wužiwarjo-wobraz wo Plugin developers
You can use the global $COURSE, which basically contains the same information as the mdl_course column in the database ($COURSE->id, $COURSE->shortname, try print_r($COURSE) to see all properties available).

https://docs.moodle.org/dev/Tutorial
https://docs.moodle.org/dev/Developer_FAQ#How_do_I_find_out_the_current_course.3F
In reply to Daniel Schleuter

Re: get course user is currently in

by Davo Smith -
Wužiwarjo-wobraz wo Core developers Wužiwarjo-wobraz wo Particularly helpful Moodlers Wužiwarjo-wobraz wo Peer reviewers Wužiwarjo-wobraz wo Plugin developers

If you are just creating a single PHP file on it's own and not checking any parameters, then the user is not in any course at all (imagine, for a moment, the user has 3 tabs open, with 3 different courses displayed - if you open a 4th tab with your script in it, then which course is the user in?)

When you link to your script, you are going to need to include some sort of URL param to indicate which course (or activity, etc.) this page access relates to, you can then do:

$courseid = required_param('courseid', PARAM_INT);
require_login($courseid);

After that, you can use $PAGE->course or $COURSE as the course that the user is part of.