First, I added the sectionid as a parameter for the command.
list($options, $unrecognized) = cli_get_params(array(
'courseid' => false,
'sectionid' => false, // this is the added parameter
'courseshortname' => '',
'destination' => '',
'help' => false,
), array('h' => 'help'));
Second, I added the sectionid as an additional condition to be queried in fetching a record from the course_sections table. Take note that both courseid and sectionid parameters should be present. The command will throw an invalidrecord exception if the pair doesn't exist in the course_sections table.
if($options['sectionid'] && $options['courseid']) {
$section = $DB->get_record('course_sections', array('id' => $options['sectionid'], 'course' => $options['courseid']), '*', MUST_EXIST);
} else if($options['courseid']) {
// get course record by ID
} else {
// get course record by shortname
}
Third, I added a condition wherein the backup controller will use the section type and ID, instead of course.
if($section) {
$bc = new backup_controller(
backup::TYPE_1SECTION, $section->id,
backup::FORMAT_MOODLE, backup::INTERACTIVE_YES,
backup::MODE_GENERAL, $admin->id);
}
else {
$bc = new backup_controller(
backup::TYPE_1COURSE, $course->id,
backup::FORMAT_MOODLE, backup::INTERACTIVE_YES,
backup::MODE_GENERAL, $admin->id);
}
That's basically what I did. The implementation can be improved though.