get_record("course", array("id" => $cm->course))) { print_error('coursemisconf'); } // allows for adaption for multiple modules if(! $modname = $DB->get_field('modules', 'name', array('id' => $cm->module))) { print_error('invalidmoduleid', '', '', $cm->module); } if (! $mod = $DB->get_record($modname, array("id" => $cm->instance))) { print_error('invalidcoursemodule'); } require_login($course->id, false, $cm); $context = get_context_instance(CONTEXT_MODULE, $cm->id); require_capability('mod/lesson:edit', $context); $strimportppt = get_string("importppt", "lesson"); $strlessons = get_string("modulenameplural", "lesson"); $navigation = build_navigation($strimportppt, $cm); print_header_simple("$strimportppt", " $strimportppt", $navigation); if ($form = data_submitted()) { /// Filename if (empty($_FILES['newfile'])) { // file was just uploaded notify(get_string("uploadproblem") ); } if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) { notify(get_string("uploadnofilefound") ); } else { // Valid file is found if ($rawpages = readdata($_FILES, $course->id, $modname)) { // first try to reall all of the data in $pageobjects = extract_data($rawpages, $course->id, $mod->name, $modname); // parse all the html files into objects clean_temp(); // all done with files so dump em $mod_create_objects = $modname.'_create_objects'; $mod_save_objects = $modname.'_save_objects'; $objects = $mod_create_objects($pageobjects, $mod->id); // function to preps the data to be sent to DB if(! $mod_save_objects($objects, $mod->id, $pageid)) { // sends it to DB print_error('cannotsavedata'); } } else { print_error('cannotgetdata'); } echo "
tags around each element and strip out \n which I have found to be uneccessary foreach ($pageobject->contents as $content) { $content = str_replace("\n", '', $content); $content = str_replace("\r", '', $content); $content = str_replace(' ', '', $content); // puts in returns? $content = '
'.$content.'
'; $page->contents .= $content; } return $page; } /** Saves the branchtable objects to the DB */ function lesson_save_objects($branchtables, $lessonid, $after) { global $DB; // first set up the prevpageid and nextpageid if ($after == 0) { // adding it to the top of the lesson $prevpageid = 0; // get the id of the first page. If not found, then no pages in the lesson if (!$nextpageid = $DB->get_field('lesson_pages', 'id', array('prevpageid' => 0, 'lessonid' => $lessonid))) { $nextpageid = 0; } } else { // going after an actual page $prevpageid = $after; $nextpageid = $DB->get_field('lesson_pages', 'nextpageid', array('id' => $after)); } foreach ($branchtables as $branchtable) { // set the doubly linked list $branchtable->page->nextpageid = $nextpageid; $branchtable->page->prevpageid = $prevpageid; // insert the page $id = $DB->insert_record('lesson_pages', $branchtable->page); // update the link of the page previous to the one we just updated if ($prevpageid != 0) { // if not the first page $DB->set_field("lesson_pages", "nextpageid", $id, array("id" => $prevpageid)); } // insert the answers foreach ($branchtable->answers as $answer) { $answer->pageid = $id; $DB->insert_record('lesson_answers', $answer); } $prevpageid = $id; } // all done with inserts. Now check to update our last page (this is when we import between two lesson pages) if ($nextpageid != 0) { // if the next page is not the end of lesson $DB->set_field("lesson_pages", "prevpageid", $id, array("id" => $nextpageid)); } return true; } /** Save the chapter objects to the database */ function book_save_objects($chapters, $bookid, $pageid='0') { global $DB; // nothing fancy, just save them all in order foreach ($chapters as $chapter) { $chapter->id = $DB->insert_record('book_chapters', $chapter); } return true; } ?>