// 1 - INSERT mdl_page
$sql = "INSERT INTO mdl_page
(`course`,
`name`,
`intro`,
`introformat`,
`content`,
`contentformat`,
`legacyfiles`,
`display`,
`displayoptions`,
`revision`,
`timemodified`
)
VALUES
(
'".addslashes ($_GET['TargetCourseID'])."',
'".addslashes ($_GET['PageName'])."',
'".addslashes ($_GET['PageContentHTML'])."',
1,
'".addslashes ($_GET['PageContentHTML'])."',
1,
0,
5,
'',
0,
'".time()."' )";
// a\:1\:{s\:10\:\"printintro\"\;s\:1\:\"1\"\;}
// Excute does NOT return an ID
$DB->execute($sql);
// Recover the Page ID, Execute doesnt return a value. Thanks Moodle.
$page_id = 0;
$pages = $DB->get_records_sql("SELECT id FROM mdl_page WHERE course = ".$_GET['TargetCourseID']." ORDER BY id DESC LIMIT 1");
foreach($pages as $page){
$page_id = $page->id;
}
// var_dump($page_id);
// 2 - INSERT mdl_course_module
// Before we can insert a module, we need to locate the section ID from mdl_course_sections based on user input
$section_id = 0;
$sections = $DB->get_records_sql("SELECT id, section FROM mdl_course_sections WHERE course=".$_GET['TargetCourseID']." AND section=".$_GET['LoadIntoSection']." ORDER BY id DESC LIMIT 1");
foreach($sections as $section){
$section_id = $section->id;
}
$sql = "INSERT INTO mdl_course_modules
(`course`,
`module`,
`instance`,
`section`,
`added`,
`visible`,
`visibleold`,
`showdescription`
)
VALUES
(
'".addslashes ($_GET['TargetCourseID'])."',
24,
'".$page_id."',
".$section_id.",
'".time()."',
'".addslashes ($_GET['PageIsVisible'])."',
'".addslashes ($_GET['PageIsVisible'])."',
'".addslashes ($_GET['PageIsVisible'])."')";
// a\:1\:{s\:10\:\"printintro\"\;s\:1\:\"1\"\;}
// Excute does NOT return an ID
$success = $DB->execute($sql);
// lastly we must give a Moodle Context to the new item
$module_id = 0;
$modules = $DB->get_records_sql("SELECT * FROM mdl_course_modules WHERE course=".addslashes($_GET['TargetCourseID'])." AND instance=".$page_id." ORDER BY id DESC LIMIT 1");
foreach($modules as $module){
$module_id = $module->id;
}