diff --git a/mod/lesson/importppt.php b/mod/lesson/importppt.php new file mode 100644 index 00000000000..f20ae85d249 --- /dev/null +++ b/mod/lesson/importppt.php @@ -0,0 +1,543 @@ +course)) { + error("Course is misconfigured"); + } + + // allows for adaption for multiple modules + if(! $modname = get_field('modules', 'name', 'id', $cm->module)) { + error("Could not find module name"); + } + + if (! $mod = get_record($modname, "id", $cm->instance)) { + error("Course module is incorrect"); + } + + require_login($course->id, false); + + if (!isteacher($course->id)) { + error("Only the teacher can import questions!"); + } + + $strimportppt = get_string("importppt", "lesson"); + $strlessons = get_string("modulenameplural", "lesson"); + + print_header_simple("$strimportppt", " $strimportppt", + "id>$strlessons -> wwwroot/mod/$modname/view.php?id=$cm->id\">".format_string($mod->name,true)."-> $strimportppt"); + + 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)) { // first try to reall all of the data in + $pageobjects = extract_data($rawpages, $course->id, $mod->name); // 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 + error("could not save"); + } + } else { + error('could not get data'); + } + + echo "
| "; + print_string("upload"); + echo ": | "; + echo " "; + echo " |
| "; + echo " "; + 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 = '
'.$content.'
'; + $page->contents .= addslashes($content); + } + return $page; +} + +/** + Saves the branchtable objects to the DB +*/ +function lesson_save_objects($branchtables, $lessonid, $after) { + // 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 = get_field('lesson_pages', 'id', 'prevpageid', 0, 'lessonid', $lessonid)) { + $nextpageid = 0; + } + } else { + // going after an actual page + $prevpageid = $after; + $nextpageid = get_field('lesson_pages', 'nextpageid', 'id', $after); + } + + foreach ($branchtables as $branchtable) { + + // set the doubly linked list + $branchtable->page->nextpageid = $nextpageid; + $branchtable->page->prevpageid = $prevpageid; + + // insert the page + if(!$id = insert_record('lesson_pages', $branchtable->page)) { + error("insert page"); + } + + // update the link of the page previous to the one we just updated + if ($prevpageid != 0) { // if not the first page + if (!set_field("lesson_pages", "nextpageid", $id, "id", $prevpageid)) { + error("Insert page: unable to update next link $prevpageid"); + } + } + + // insert the answers + foreach ($branchtable->answers as $answer) { + $answer->pageid = $id; + if(!insert_record('lesson_answers', $answer)) { + error("insert answer $id"); + } + } + + $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 + if (!set_field("lesson_pages", "prevpageid", $id, "id", $nextpageid)) { + error("Insert page: unable to update next link $prevpageid"); + } + } + + return true; +} + +/** + Save the chapter objects to the database +*/ +function book_save_objects($chapters, $bookid, $pageid='0') { + // nothing fancy, just save them all in order + foreach ($chapters as $chapter) { + if (!$chapter->id = insert_record('book_chapters', $chapter)) { + error('Could not update your book'); + } + } + return true; +} + +?> \ No newline at end of file