0

after managing to add modules to Moodle programmaticaly (see my old question here) I now also need to add the module at a specific place.

Lets take for example a sample course in Moodle, I have:

Section 0
 - Module 1
 - --> ADD NEW MODULE HERE <--
 - Module 2
Section 1
 - Module 1
 - Module 2
Section 2

So I need to add the new module I create programatically inbetween module 1 and 2 of section 0.

I know that the order of modules come from table mdl_course_sections and its specified in the column sequence where the ids of the modules exist in comma separated values

Is there a function in Moodle that does that? Set the sequence of a section? I don't want to mess with the DB directly.

Christos312
  • 466
  • 6
  • 18

1 Answers1

0

After I got some help from Moodle community (thanks Sam Chaffee) here is the solution

//...Do stuff to create module object...
// ...

$moduleinfo = add_moduleinfo($newlabel, $section);

//lets get all sections and modules of the course
$coursemodinfo = get_fast_modinfo($course->id, 0, false);

//get all sections of the course
$coursesections = $coursemodinfo->get_sections();

//get the first section, get_section will return an array of sections so I get position 0 to get section 1
$section01 = $coursesections[0];

//get the first module in the section so we can add on top of it
$firstmodid = $section01[0];

//We need to get cm_info for the specific mod
$mod = $coursemodinfo->get_cm($moduleinfo->coursemodule);

//we also need section_info for the section we want to move to
$sectioninfo = $coursemodinfo->get_section_info(0);

//move the newly created module to section 01 but before $firstmodid
moveto_module($mod, $sectioninfo, $firstmodid);
Christos312
  • 466
  • 6
  • 18