public function test_import_chapters_events() { $course = $this->getDataGenerator()->create_course(); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); $context = context_module::instance($book->cmid); $record = new stdClass(); $record->contextid = $context->id; $record->component = 'phpunit'; $record->filearea = 'test'; $record->itemid = 0; $record->filepath = '/'; $record->filename = 'chapters.zip'; $fs = get_file_storage(); $file = $fs->create_file_from_pathname($record, __DIR__ . '/fixtures/chapters.zip'); // Importing the chapters. $sink = $this->redirectEvents(); toolbook_importhtml_import_chapters($file, 2, $book, $context, false); $events = $sink->get_events(); // Checking the results. $this->assertCount(5, $events); foreach ($events as $event) { $this->assertInstanceOf('\\mod_book\\event\\chapter_created', $event); $this->assertEquals($context, $event->get_context()); $chapter = $event->get_record_snapshot('book_chapters', $event->objectid); $this->assertNotEmpty($chapter); $this->assertEventContextNotUsed($event); } }
$strbooks = get_string('modulenameplural', 'mod_book'); $mform = new booktool_importhtml_form(null, array('id' => $id, 'chapterid' => $chapterid)); // If data submitted, then process and store. if ($mform->is_cancelled()) { if (empty($chapter->id)) { redirect($CFG->wwwroot . "/mod/book/view.php?id={$cm->id}"); } else { redirect($CFG->wwwroot . "/mod/book/view.php?id={$cm->id}&chapterid={$chapter->id}"); } } else { if ($data = $mform->get_data()) { echo $OUTPUT->header(); echo $OUTPUT->heading($book->name); echo $OUTPUT->heading(get_string('importingchapters', 'booktool_importhtml'), 3); // this is a bloody hack - children do not try this at home! $fs = get_file_storage(); $draftid = file_get_submitted_draft_itemid('importfile'); if (!($files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $draftid, 'id DESC', false))) { redirect($PAGE->url); } $file = reset($files); toolbook_importhtml_import_chapters($file, $data->type, $book, $context); echo $OUTPUT->continue_button(new moodle_url('/mod/book/view.php', array('id' => $id))); echo $OUTPUT->footer(); die; } } echo $OUTPUT->header(); echo $OUTPUT->heading($book->name); $mform->display(); echo $OUTPUT->footer();
/** * Import HTML pages from a Word file * * @param string $wordfilename Word file * @param stdClass $book * @param context_module $context * @param bool $splitonsubheadings * @return void */ function booktool_wordimport_import_word($wordfilename, $book, $context, $splitonsubheadings) { // Convert the Word file content into XHTML and an array of images. $imagesforzipping = array(); $htmlcontent = booktool_wordimport_convert_to_xhtml($wordfilename, $imagesforzipping); // Create a temporary Zip file to store the HTML and images for feeding to import function. $zipfilename = dirname($wordfilename) . DIRECTORY_SEPARATOR . basename($wordfilename, ".tmp") . ".zip"; $zipfile = new ZipArchive(); if (!$zipfile->open($zipfilename, ZipArchive::CREATE)) { // Cannot open zip file. throw new moodle_exception('cannotopenzip', 'error'); } // Add any images to the Zip file. if (count($imagesforzipping) > 0) { foreach ($imagesforzipping as $imagename => $imagedata) { $zipfile->addFromString($imagename, $imagedata); } } // Split the single HTML file into multiple chapters based on h1 elements. $h1matches = null; $chaptermatches = null; // Grab title and contents of each section. $chaptermatches = preg_split('#<h3>.*</h3>#isU', $htmlcontent); preg_match_all('#<h3>(.*)</h3>#i', $htmlcontent, $h1matches); // @codingStandardsIgnoreLine debugging(__FUNCTION__ . ":" . __LINE__ . ": n chapters = " . count($chaptermatches), DEBUG_WORDIMPORT); // If no h1 elements are present, treat the whole file as a single chapter. if (count($chaptermatches) == 1) { $zipfile->addFromString("index.htm", $htmlcontent); } // Create a separate HTML file in the Zip file for each section of content. for ($i = 1; $i < count($chaptermatches); $i++) { $chaptitle = $h1matches[1][$i - 1]; $chapcontent = $chaptermatches[$i]; $chapfilename = sprintf("index%02d.htm", $i); // Remove the closing HTML markup from the last section. if ($i == count($chaptermatches) - 1) { $chapcontent = substr($chapcontent, 0, strpos($chapcontent, "</div></body>")); } if ($splitonsubheadings) { // Save each subsection as a separate HTML file with a '_sub.htm' suffix. $h2matches = null; $subchaptermatches = null; // Grab title and contents of each subsection. preg_match_all('#<h4>(.*)</h4>#i', $chapcontent, $h2matches); $subchaptermatches = preg_split('#<h4>.*</h4>#isU', $chapcontent); // First save the initial chapter content. $chapcontent = $subchaptermatches[0]; $chapfilename = sprintf("index%02d_00.htm", $i); $htmlfilecontent = "<html><head><title>{$chaptitle}</title></head>" . "<body>{$chapcontent}</body></html>"; $zipfile->addFromString($chapfilename, $htmlfilecontent); // Save each subsection to a separate file. for ($j = 1; $j < count($subchaptermatches); $j++) { $subchaptitle = $h2matches[1][$j - 1]; $subchapcontent = $subchaptermatches[$j]; $subsectionfilename = sprintf("index%02d_%02d_sub.htm", $i, $j); $htmlfilecontent = "<html><head><title>{$subchaptitle}</title></head>" . "<body>{$subchapcontent}</body></html>"; $zipfile->addFromString($subsectionfilename, $htmlfilecontent); } } else { // Save each section as a HTML file. $htmlfilecontent = "<html><head><title>{$chaptitle}</title></head>" . "<body>{$chapcontent}</body></html>"; $zipfile->addFromString($chapfilename, $htmlfilecontent); } } $zipfile->close(); // Add the Zip file to the file storage area. $fs = get_file_storage(); $zipfilerecord = array('contextid' => $context->id, 'component' => 'user', 'filearea' => 'draft', 'itemid' => $book->revision, 'filepath' => "/", 'filename' => basename($zipfilename)); $zipfile = $fs->create_file_from_pathname($zipfilerecord, $zipfilename); // Call the standard HTML import function to really import the content. // Argument 2, value 2 = Each HTML file represents 1 chapter. toolbook_importhtml_import_chapters($zipfile, 2, $book, $context); }