General developer forum

Programmatically specify module position in section?

 
Picture of Christos Savva
Programmatically specify module position in section?
Plugin developers

Hello, I am adding programmatically some labels to all courses we have in Moodle using add_moduleinfo() but this adds the module at the end of the section.


I know that the position of the module is specified in table mdl_course_sections and its specified in the column sequence.


Is there a function where I can specify the position of the module without directly touching the DB? 

If there is no Moodle function that does that, is it wise update the sequence using the $DB->update_records() function?

 
Average of ratings: -
Picture of Sam Chaffee
Re: Programmatically specify module position in section?
Core developersParticularly helpful Moodlers

I would not recommend updating the sequence directly via the DB.

Have you checked out the function course_add_cm_to_section() in course/lib.php? The last parameter (optional) it takes is $beforemod, the id of the the activity that you want the new one placed before. Then it does all the messing about with the sequence for you.

Cheers,

Sam

 
Average of ratings: -
Picture of Christos Savva
Re: Programmatically specify module position in section?
Plugin developers

Hi Sam, thank you for the suggestion I will try it out


Two questions


1.  Regarding the function the $cmid should be the id of the label that I will add before calling course_add_cm_to_section?


function course_add_cm_to_section($courseorid, $cmid, $sectionnum, $beforemod = null) {


2. How can I get a list of modules in section 0?


My code should be


$moduleinfo = add_moduleinfo($moduleObject);
//here i should somehow get a list of modules in section 0
//eg the id of second module is 3
course_add_cm_to_section($courseorid, $moduleinfo->id, 0, 3);

 
Average of ratings: -
Picture of Sam Chaffee
Re: Programmatically specify module position in section?
Core developersParticularly helpful Moodlers

No problem. I looked again and that may not actually be your best bet if you're using add_moduleinfo. Maybe look at moveto_module as well, that may actually be better.

To answer your questions: 

1) $cmid is the ID of the entry in the course_modules table so I believe that the object returned from add_moduleinfo will have that as the property coursemodule.

2) get_fast_modinfo will return a course_modinfo object that will have a property called sections that is an array indexed by section num (e.g. 0, 1, 2) of course_module ids in order.

Cheers,

Sam

 
Average of ratings: -
Picture of Christos Savva
Re: Programmatically specify module position in section?
Plugin developers

Thanks for everything Sam! You really helped me a lot. How did you find these information regarding these functions? Is there a documentation somewhere?


In any case here is my complete code for future reference


//...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);


 
Average of ratings: -