Moodle | Fetch activities in same order in which they are listed on Course Page

| By Webner

There is a functionality on Moodle course page that activities can be dragged up and down to change their order after turning on the edit button. If in the code you need to pull the activities in same order for a course then following code can help you :

$mods = $modinfo->get_cms();
$sections = $modinfo->get_section_info_all();
foreach ($modinfo->sections as $section)
foreach ($section as $modid)
 foreach($mods as $mod)
$modcopy = new stdClass();$modcopy->id = $mod->id;
$modcopy->name = $mod->modname;
$modcopy->course = $courseObj->id;
$modcopy->module = $mod->module;
$modcopy->modname = $mod->name;
$modcopy->completionstate = '';
$modcopy->instance = $mod->instance;

By this we have obtained an array “$modlist“ that comprises of all the activities based on the section order present on the course page.

Leave a Reply

Your email address will not be published. Required fields are marked *