Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/lesson/importppt.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00034 require_once("../../config.php");
00035 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
00036 require_once($CFG->dirroot.'/mod/lesson/importpptlib.php');
00037 
00038 $id     = required_param('id', PARAM_INT);         // Course Module ID
00039 $pageid = optional_param('pageid', '', PARAM_INT); // Page ID
00040 
00041 $url = new moodle_url('/mod/lesson/importppt.php', array('id'=>$id));
00042 if ($pageid !== '') {
00043     $url->param('pageid', $pageid);
00044 }
00045 $PAGE->set_url($url);
00046 
00047 $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);;
00048 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
00049 $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
00050 
00051 $modname = 'lesson';
00052 $mod = $cm;
00053 require_login($course, false, $cm);
00054 
00055 require_login($course->id, false, $cm);
00056 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00057 require_capability('mod/lesson:edit', $context);
00058 
00059 $strimportppt = get_string("importppt", "lesson");
00060 $strlessons = get_string("modulenameplural", "lesson");
00061 
00062 $data = new stdClass;
00063 $data->id = $cm->id;
00064 $data->pageid = $pageid;
00065 $mform = new lesson_importppt_form();
00066 $mform->set_data($data);
00067 
00068 if ($data = $mform->get_data()) {
00069     $manager = lesson_page_type_manager::get($lesson);
00070     if (!$filename = $mform->get_new_filename('pptzip')) {
00071         print_error('invalidfile', 'lesson');
00072     }
00073     if (!$package = $mform->save_stored_file('pptzip', $context->id, 'mod_lesson', 'ppt_imports', $lesson->id, '/', $filename, true)) {
00074         print_error('unabletosavefile', 'lesson');
00075     }
00076     // extract package content
00077     $packer = get_file_packer('application/zip');
00078     $package->extract_to_storage($packer, $context->id, 'mod_lesson', 'imported_files', $lesson->id, '/');
00079 
00080     $fs = get_file_storage();
00081     if ($files = $fs->get_area_files($context->id, 'mod_lesson', 'imported_files', $lesson->id)) {
00082 
00083         $pages = array();
00084         foreach ($files as $key=>$file) {
00085             if ($file->get_mimetype() != 'text/html') {
00086                 continue;
00087             }
00088             $filenameinfo = pathinfo($file->get_filepath().$file->get_filename());
00089 
00090             $page = new stdClass;
00091             $page->title = '';
00092             $page->contents = array();
00093             $page->images = array();
00094             $page->source = $filenameinfo['basename'];
00095 
00096             $string = strip_tags($file->get_content(),'<div><img>');
00097             $imgs = array();
00098             preg_match_all("/<img[^>]*(src\=\"(".$filenameinfo['filename']."\_image[^>^\"]*)\"[^>]*)>/i", $string, $imgs);
00099             foreach ($imgs[2] as $img) {
00100                 $imagename = basename($img);
00101                 foreach ($files as $file) {
00102                     if ($imagename === $file->get_filename()) {
00103                         $page->images[] = clone($file);
00104                     }
00105                 }
00106             }
00107 
00108             $matches = array();
00109             // this will look for a non nested tag that is closed
00110             // want to allow <b><i>(maybe more) tags but when we do that
00111             // the preg_match messes up.
00112             preg_match_all("/(<([\w]+)[^>]*>)([^<\\2>]*)(<\/\\2>)/", $string, $matches);
00113             $countmatches = count($matches[1]);
00114             for($i = 0; $i < $countmatches; $i++) { // go through all of our div matches
00115 
00116                 $class = lesson_importppt_isolate_class($matches[1][$i]); // first step in isolating the class
00117 
00118                 // check for any static classes
00119                 switch ($class) {
00120                     case 'T':  // class T is used for Titles
00121                         $page->title = $matches[3][$i];
00122                         break;
00123                     case 'B':  // I would guess that all bullet lists would start with B then go to B1, B2, etc
00124                     case 'B1': // B1-B4 are just insurance, should just hit B and all be taken care of
00125                     case 'B2':
00126                     case 'B3':
00127                     case 'B4':
00128                         $page->contents[] = lesson_importppt_build_list($matches, '<ul>', $i, 0);  // this is a recursive function that will grab all the bullets and rebuild the list in html
00129                         break;
00130                     default:
00131                         if ($matches[3][$i] != '&#13;') {  // odd crap generated... sigh
00132                             if (substr($matches[3][$i], 0, 1) == ':') {  // check for leading :    ... hate MS ...
00133                                 $page->contents[] = substr($matches[3][$i], 1);  // get rid of :
00134                             } else {
00135                                 $page->contents[] = $matches[3][$i];
00136                             }
00137                         }
00138                         break;
00139                 }
00140             }
00141             $pages[] = $page;
00142         }
00143 
00144         $branchtables = lesson_create_objects($pages, $lesson->id);
00145 
00146         // first set up the prevpageid and nextpageid
00147         if (empty($pageid)) { // adding it to the top of the lesson
00148             $prevpageid = 0;
00149             // get the id of the first page.  If not found, then no pages in the lesson
00150             if (!$nextpageid = $DB->get_field('lesson_pages', 'id', array('prevpageid' => 0, 'lessonid' => $lesson->id))) {
00151                 $nextpageid = 0;
00152             }
00153         } else {
00154             // going after an actual page
00155             $prevpageid = $pageid;
00156             $nextpageid = $DB->get_field('lesson_pages', 'nextpageid', array('id' => $pageid));
00157         }
00158 
00159         foreach ($branchtables as $branchtable) {
00160 
00161             // set the doubly linked list
00162             $branchtable->page->nextpageid = $nextpageid;
00163             $branchtable->page->prevpageid = $prevpageid;
00164 
00165             // insert the page
00166             $id = $DB->insert_record('lesson_pages', $branchtable->page);
00167 
00168             if (!empty($branchtable->page->images)) {
00169                 $changes = array('contextid'=>$context->id, 'component'=>'mod_lesson', 'filearea'=>'page_contents', 'itemid'=>$id, 'timemodified'=>time());
00170                 foreach ($branchtable->page->images as $image) {
00171                     $fs->create_file_from_storedfile($changes, $image);
00172                 }
00173             }
00174 
00175             // update the link of the page previous to the one we just updated
00176             if ($prevpageid != 0) {  // if not the first page
00177                 $DB->set_field("lesson_pages", "nextpageid", $id, array("id" => $prevpageid));
00178             }
00179 
00180             // insert the answers
00181             foreach ($branchtable->answers as $answer) {
00182                 $answer->pageid = $id;
00183                 $DB->insert_record('lesson_answers', $answer);
00184             }
00185 
00186             $prevpageid = $id;
00187         }
00188 
00189         // all done with inserts.  Now check to update our last page (this is when we import between two lesson pages)
00190         if ($nextpageid != 0) {  // if the next page is not the end of lesson
00191             $DB->set_field("lesson_pages", "prevpageid", $id, array("id" => $nextpageid));
00192         }
00193     }
00194 
00195     // Remove all unzipped files!
00196     $fs->delete_area_files($context->id, 'mod_lesson', 'imported_files', $lesson->id);
00197 
00198     redirect("$CFG->wwwroot/mod/$modname/view.php?id=$cm->id", get_string('pptsuccessfullimport', 'lesson'), 5);
00199 }
00200 
00201 $PAGE->navbar->add($strimportppt);
00202 $PAGE->set_title($strimportppt);
00203 $PAGE->set_heading($strimportppt);
00204 echo $OUTPUT->header();
00205 
00207 echo $OUTPUT->heading_with_help($strimportppt, 'importppt', 'lesson');
00208 echo $OUTPUT->box_start('generalbox boxaligncenter');
00209 $mform->display();
00210 echo $OUTPUT->box_end();
00211 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations