I don't know if there is a proper API. Looks like rebuild_course_cache may be what you're looking for. Here is some of our code which does that (all GPL so do what you want... although you may be able to observe that it perhaps isn't very clean code anyway) which you may be able to adapt. I think I included all the required functions! Hope this helps,
--sam
/**
* Adds a new link to the end of the top section of the course.
*
* The link should be set up so that it goes directly to that URL, rather than
* using any of the framing options.
* @param int $courseid ID of course
* @param string $title Title (text) of link
* @param string $url URL of link
* @param int $indent Optional indentation value for link
*/
public function add_link($courseid,$title,$url, $indent=0) {
// Get section 0
$section = get_record('course_sections', 'course', $courseid, 'section', 0);
if(!$section) {
throw new Exception("Couldn't find section 0 for course $courseid");
}
$this->add_link_to_section($courseid, $section, $title, $url, $indent);
}
/**
* Adds a new link to the section of the course.
*
* The link should be set up so that it goes directly to that URL, rather than
* using any of the framing options.
* @param int $courseid ID of course
* @param object $section section object
* @param string $title Title (text) of link
* @param string $url URL of link
* @param int $indent Optional indentation value for link
* @param string $variables Optional list of variables e.g. 'courseid=course'
*/
// Function moved here from oucourseformat.php
private function add_link_to_section($courseid, $section, $website, $url, $indent=0,$variables='') {
$timenow = time();
// create resource record
$resource = new stdClass;
$resource->course = $courseid;
$resource->name = $website;
$resource->type = 'file';
$resource->reference = $url;
$resource->timemodified = $timenow;
$resource->summary='';
$resource->popup='';
$resource->alltext=$variables;
if ($newresourceid = insert_record('resource', $resource, true)) { // Set up new resource
// create course modules record
$mod = new stdClass;
$mod->course = $courseid;
// Find link ID
global $CFG;
$rs=get_recordset_sql("select id from ".$CFG->prefix."modules where name='resource'",false);
if(!$moduleid=$rs->fields['id']) {
throw new Exception('Resource module not found');
}
$mod->module = $moduleid;
$mod->section = $section->section; // was $section->id; logical but incorrect!
$mod->added = $timenow;
$mod->instance = $newresourceid;
// Add indentation value if supplied
if ($indent) {
$mod->indent = $indent;
}
$this->create_and_link_course_modules($mod);
rebuild_course_cache($courseid);
} else {
global $db;
throw new Exception('Error adding resource record '.$db->ErrorMsg());
}
}
/**
* Adds a course module and links to section.
*
* @param object $mod module object
* @throws Exception if anything goes wrong
*/
// Function moved here from oucourseformat.php
// Code cribbed from course/mod.php 'case "add":'
public function create_and_link_course_modules($mod) {
// course_modules and course_sections each contain a reference
// to each other, so we have to update one of them twice.
// Note: This is unbelievable!!! $mod->section MUST BE section number (not id)
// Adds course_module with section number, add_mod_to_section uses
// section number (& course id) to get section id, which is returned
// course module record then updated to replace section number by id!!!
if (! $mod->coursemodule = add_course_module($mod) ) {
throw new Exception("Could not add a new course module");
}
if (! $sectionid = add_mod_to_section($mod) ) {
throw new Exception("Could not add the new course module to that section");
}
if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
throw new Exception("Could not update the course module with the correct section");
}
if (!isset($mod->visible)) { // We get the section's visible field status
$mod->visible = get_field("course_sections","visible","id",$sectionid);
}
// make sure visibility is set correctly (in particular in calendar)
if(!set_coursemodule_visible($mod->coursemodule, $mod->visible)) {
throw new Exception("Could not set visibility");
}
/*
add_to_log($course->id, "course", "add mod",
"../mod/$mod->modulename/view.php?id=$mod->coursemodule",
"$mod->modulename $mod->instance");
add_to_log($course->id, $mod->modulename, "add",
"view.php?id=$mod->coursemodule",
"$mod->instance", $mod->coursemodule);
*/
}