the course->modinfo field contains one "summary" of all the activities
in the course. It's a serialised array of data and it's used to get some important information (visibility, groupmode, sections
..) and it's used to render the course main page faster.
From your posts above it seems that you are, manually, creating courses and adding sections and activities to them.
If everything has been properly populated, all you have to do, in php is to execute the function:
(where $courseid is the id field of the course that you've created.
Once executed, the course->modinfo will contain the required information and all the activities will appear ok in the course.
But remember that everything (course_modules, course_sections, activity tables...) have to be properly and coherently in place before calling such function.