General developer forum

get the courseid and groupid from the course that the user is currently acessing

 
Picture of Rodrigo Guerra
get the courseid and groupid from the course that the user is currently acessing
 

Hi all,

I need to get the courseid and groupid from the course that the user is currently acessing at the moment.

The course is a SCORM package. The user enters in the course page and click on a button, it calls a PHP page (same domain) and show these info.

I tried the solution below to get the courseID without success:

    require("config.php");
    $contextid = context_course::instance($courseid);
    echo "Context id: ".$context->id;

thanks!
rodrigo.

 
Average of ratings: -
Picture of Mark Johnson
Re: get the courseid and groupid from the course that the user is currently acessing
Core developersParticularly helpful MoodlersPlugin developers

Hi Rodrigo

Based on what you've posted above, it seems you're expecting that $courseid is made available by including config.php?  Unfortunately, this isn't the case.  You will need to pass the courseid to the script though a GET or POST parameter, then access it using require_parameter().

 
Average of ratings: -
Picture of Rodrigo Guerra
Re: get the courseid and groupid from the course that the user is currently acessing
 

hi mark thanks for the reply.

I need the courseId and groupid to use as parameter in the core_group_get_group_members() service and get the usersID for that course/group.

this way I can show a list of the users' peers for that particular course and group.

I may be wrong but I believe that there is a way to know these 2 parameters calling a php page from inside the course based on these 2 posts:


https://stackoverflow.com/questions/20576915/how-to-get-the-current-courseid-for-the-current-page-in-moodle

https://moodle.org/mod/forum/discuss.php?d=101065


on the docs they mention the global variables.... just missing the snippet code... anyone??


https://docs.moodle.org/dev/Developer_FAQ

How do I find out the currently-logged-on user?

The global object $USER, which contains the numeric $USER->id among other things.

How do I find out the current course?

The global object $COURSE, which contains the numeric $COURSE->id



if anyone knows a easier way to get the users' peers please let me know.

my ideia is:
- get the userids from core_group_get_group_members()
- get the user role from core_user_get_course_user_profiles() to split teacher/students using the role it returns.






 
Average of ratings: -
Picture of Mark Johnson
Re: get the courseid and groupid from the course that the user is currently acessing
Core developersParticularly helpful MoodlersPlugin developers

Ok, I think those examples you refer to are missing some context.

Broadly speaking, there are 2 different types of PHP file in Moodle - libraries, which are not called directly but contain classes and functions, and pages which are called directly.  Libraries will start by declaring the "MOODLE_INTERNAL" constant, while pages will start by including config.php, as in your original example.

When you include config.php, you load Moodle's configuration and core libraries, initialise the page's global object in $PAGE, as well as the user's profile and session data via $USER and $SESSION.  However, until you set the page's course by calling $PAGE->set_course() or require_login() and passing in a course id, $COURSE will not be set - there's no way of knowing what it should be. You need to pass to the page either the id of the course, of the id of the course module, or something else within the courses's context that allows you to determine which course you are within.

If you are writing a function in a library, you can get current course by doing global $COURSE; at the top of the function - as long as the function will be called within a page that has already done $PAGE->set_course().

 
Average of ratings: Useful (1)
Picture of Rodrigo Guerra
Re: get the courseid and groupid from the course that the user is currently acessing
 

Hi Mark,

I see your point, to get the course info I need to pass the course id to moodle so it loads the info using the $PAGE->set_course(), makes total sense. The point is that course id along with groupid is the variables i'm looking for.

but to the user (student) get to the course content, he logggeid in and accessed the course page. Once he is at the course page I beleive that Moodle has this $COURSE variable set with the std current course details, doesn't?

if this is true it should not be difficult to call a simple php page to get the content from this variable.


 
Average of ratings: -
Picture of Mark Johnson
Re: get the courseid and groupid from the course that the user is currently acessing
Core developersParticularly helpful MoodlersPlugin developers

Yes, when the user is on the course page, code on that page has access to the current course id.  However, if you are then calling another page, that is a separate request so the course id needs to be passed to the new page as part of that request.  The course is not stored as part of the user's session.

An example of how you might achieve what you want is to create a block plugin that displays a link on the course page.  Within the block's get_content() method you will be able to do global $COURSE (or even $this->page->course) to get current course ID.  You can then display a link in the block to your new page, passing the course id as a parameter in the URL.


 
Average of ratings: -
Picture of Rodrigo Guerra
Re: get the courseid and groupid from the course that the user is currently acessing
 

thanks for your clarification and insights mark, I will give a try on that.

 
Average of ratings: -