How to get current/modules course section?

How to get current/modules course section?

by Johannes Burk -
Number of replies: 4
Picture of Core developers Picture of Plugin developers

How can I get the current section when the user is on a module specific page (e.g. mod/page/view.php?id=2). Or how to get the section a given module belongs to?

I know how to get all modules of a section but is there a simple way to determine the section a given module belongs to without searching the module in all sections?


Average of ratings: Useful (1)
In reply to Johannes Burk

Re: How to get current/modules course section?

by Cristian Martin Nieto -

You have that information in the DB table called 'course_modules'. You will need a query such as:


SELECT cm.section FROM {course_modules} cm

INNER JOIN {modules} m ON cm.module = m.id

WHERE m.name = 'NAMEOFTHEMODULE' AND cm.instance = MODULEINSTANCE


I hope it helps!

Average of ratings: Useful (1)
In reply to Johannes Burk

Re: How to get current/modules course section?

by lior gil -
Picture of Core developers

A standard module already have this data in an object usually called $cm.

Just call $cm->section to get what you need.

Average of ratings: Useful (2)
In reply to lior gil

Re: How to get current/modules course section?

by Johannes Burk -
Picture of Core developers Picture of Plugin developers

Thank you. Actually it was too obvious. Now I use $this->page->cm->section (note: this is the section number, not id)  in my renderer.

In reply to Johannes Burk

Re: How to get current/modules course section?

by Johannes Burk -
Picture of Core developers Picture of Plugin developers

Correction for my previous post: the note was wrong: (note: this is the section number, not id). Of course $cm->section is the id of the section and not the section number (see the section member documentation of the cm_info class). A Little bit confusing because section_info->section is the section number.