Returning just a course ID in a block

Returning just a course ID in a block

by M O'S -
Number of replies: 7
Hi,

Within my block I have an "api.php" which will run some functions in the background. I need to pass through the course id in some of the functions, but I am not sure how to do this without using required_param to get the id. The user will not able to access this api.php. Is there a moodle function that will just return the id of the course the block is in?

I want to store the course id so I can run some functions in the background via cron and access the resources saved in the course folder in, for example, "/moodledata/$course->id/" etc?

Thanks.

Megan
Average of ratings: -
In reply to M O'S

Re: Returning just a course ID in a block

by M O'S -
I have solved this by simply using $COURSE->id smile
In reply to M O'S

Re: Returning just a course ID in a block

by Andrew Osiname -

Hi Megan,

Does the block appear in the course? if so then you can just use $course_id = $_GET['id']; and do a few checks for the security of value retrived. All courses urls look like so: example.com/course/view.php?id=101 where 101 is the course id. So using $course_id = $_GET['id'] will give u a value of 101in this case.

If you want to use the moodle api then use $course_id = required_param('id'); ...which probably uses $_GET['id'] somewhere inside but runs the checks for you, ie value is integer, protection against sql attacks, etc.

The only other way to do it that I can think of is maybe like so: I know it works with the user object - to get a userid at anytime just do:

global $USER;
$id = $USER->id;

so maybe you can use

global $COURSE;
$cid = $COURSE->id;

but i have never tried it before.

In reply to Andrew Osiname

Re: Returning just a course ID in a block

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Ignore the first half of the post I am replying to. It is really dangerous advice.

The only safe way to find the current course/user in a block is to use the globals.

(In Moodle 2.0 you should use $this->page->coures to access the course, instead of the globals.)

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Returning just a course ID in a block

by Andrew Osiname -

Yes $_GET['id'] isnt the safest method but thats why the developer would add security to the incoming values if they chose to use $_GET

required_param is written into the moodle API so i guess its probably safe to use - can always read through it in lib.php to make sure.

Im using moodle 1.9 and have seen required_param used in various core modules.

Average of ratings: Useful (1)
In reply to Andrew Osiname

Re: Returning just a course ID in a block

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It's not safe because blocks can appear on lots of different types of page (think stickyblocks) and not all of those pages have a URL parameter called 'id', and some of the pages that have a parameter called id refer to something other than course id.

And, most inportantly, there is an alternative ($COURSE->id) that will always work. So, use that.

Average of ratings: Useful (2)
In reply to Tim Hunt

Re: Returning just a course ID in a block

by Andrew Osiname -

Good call. Ill start using that from now on. Just out of interest, the $USER and $COURSE objects can be accessed in moodle 1.9 like

global $USER;
$id = $USER->id;

Are there any other useful objects in moodle we can call in the same way?