Permalink
Browse files

rewritten html import

hopefully there are not many bugs, closes #13
  • Loading branch information...
skodak committed Oct 9, 2011
1 parent 22d3a77 commit 34660c5979863c9b45038853bdb356c66d6d60da
View
11 lib.php
@@ -308,13 +308,6 @@ function book_extend_settings_navigation(settings_navigation $settingsnav, navig
$PAGE->cm->context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->instance);
}
-
- $params = $PAGE->url->params();
-
- if (empty($params['id'])) {
- return;
- }
-
$plugins = get_plugin_list('booktool');
foreach($plugins as $plugin=>$dir) {
if (file_exists("$dir/lib.php")) {
@@ -326,7 +319,9 @@ function book_extend_settings_navigation(settings_navigation $settingsnav, navig
}
}
- if (!empty($params['chapterid']) and has_capability('mod/book:edit', $PAGE->cm->context)) {
+ $params = $PAGE->url->params();
+
+ if (!empty($params['id']) and !empty($params['chapterid']) and has_capability('mod/book:edit', $PAGE->cm->context)) {
if (!empty($USER->editing)) {
$string = get_string("turneditingoff");
$edit = '0';
@@ -27,15 +27,22 @@
require_once($CFG->libdir.'/formslib.php');
-class book_import_form extends moodleform {
+class booktool_importhtml_form extends moodleform {
function definition() {
- global $CFG;
$mform = $this->_form;
$data = $this->_customdata;
$mform->addElement('header', 'general', get_string('import'));
+ $options = array(
+ //'0'=>get_string('typeonefile', 'booktool_importhtml'),
+ '1'=>get_string('typezipdirs', 'booktool_importhtml'),
+ '2'=>get_string('typezipfiles', 'booktool_importhtml'),
+ );
+ $mform->addElement('select', 'type', get_string('type', 'booktool_importhtml'), $options);
+ $mform->setDefault('type', 2);
+
$mform->addElement('filepicker', 'importfile', get_string('file'));
$mform->addRule('importfile', null, 'required');
@@ -45,17 +52,36 @@ function definition() {
$mform->addElement('hidden', 'chapterid');
$mform->setType('chapterid', PARAM_INT);
- $this->add_action_buttons(true, get_string('import', 'mod_book'));
+ $this->add_action_buttons(true, get_string('doimport', 'booktool_importhtml'));
$this->set_data($data);
}
function validation($data, $files) {
- global $CFG;
+ global $USER;
+
+ if ($errors = parent::validation($data, $files)) {
+ return $errors;
+ }
- $errors = parent::validation($data, $files);
+ $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
+ $fs = get_file_storage();
- //TODO: validate package
+ if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['importfile'], 'id', false)) {
+ $errors['importfile'] = get_string('required');
+ return $errors;
+ } else {
+ $file = reset($files);
+ if ($file->get_mimetype() != 'application/zip') {
+ $errors['importfile'] = get_string('invalidfiletype', 'error', $file->get_filename());
+ // better delete current file, it is not usable anyway
+ $fs->delete_area_files($usercontext->id, 'user', 'draft', $data['importfile']);
+ } else {
+ if (!$chpterfiles = toolbook_importhtml_get_chapter_files($file, $data['type'])) {
+ $errors['importfile'] = get_string('errornochapters', 'booktool_importhtml');
+ }
+ }
+ }
return $errors;
}
View
@@ -23,11 +23,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-die('TODO');
-
-require('../../config.php');
-require_once($CFG->dirroot.'/mod/book/locallib.php');
-require_once('import_form.php');
+require(dirname(__FILE__).'/../../../../config.php');
+require_once(dirname(__FILE__).'/locallib.php');
+require_once(dirname(__FILE__).'/import_form.php');
$id = required_param('id', PARAM_INT); // Course Module ID
$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
@@ -39,9 +37,9 @@
require_login($course, false, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
-require_capability('mod/book:import', $context);
+require_capability('booktool/importhtml:import', $context);
-$PAGE->set_url('/mod/book/import.php', array('id'=>$id, 'chapterid'=>$chapterid));
+$PAGE->set_url('/mod/book/tool/importhtml/index.php', array('id'=>$id, 'chapterid'=>$chapterid));
if ($chapterid) {
if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id))) {
@@ -59,7 +57,7 @@
$strbook = get_string('modulename', 'mod_book');
$strbooks = get_string('modulenameplural', 'mod_book');
-$mform = new book_import_form(null, array('id'=>$id, 'chapterid'=>$chapterid));
+$mform = new booktool_importhtml_form(null, array('id'=>$id, 'chapterid'=>$chapterid));
/// If data submitted, then process and store.
if ($mform->is_cancelled()) {
@@ -70,81 +68,25 @@
}
} else if ($data = $mform->get_data()) {
-
-
-
-/*
-
- $coursebase = $CFG->dataroot.'/'.$book->course;
-
- $reference = book_prepare_link($data->reference);
-
- if ($reference == '') {
- $base = $coursebase;
- } else {
- $base = $coursebase.'/'.$reference;
- }
-
- //prepare list of html files in $refs
- $refs = array();
- $htmlpat = '/\.html$|\.htm$/i';
- if (is_dir($base)) { //import whole directory
- $basedir = opendir($base);
- while ($file = readdir($basedir)) {
- $path = $base.'/'.$file;
- if (filetype($path) == 'file' and preg_match($htmlpat, $file)) {
- $refs[] = str_replace($coursebase, '', $path);
- }
- }
- asort($refs);
- } else if (is_file($base)) { //import single file
- $refs[] = '/'.$reference;
- } else { //what is it???
- error('Incorrect file/directory specified!');
+ echo $OUTPUT->header();
+ echo $OUTPUT->heading(get_string('importingchapters', 'booktool_importhtml'));
+
+ // 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(get_context_instance(CONTEXT_USER, $USER->id)->id, 'user', 'draft', $draftid, 'id DESC', false)) {
+ redirect($PAGE->url);
}
+ $file = reset($files);
+ toolbook_importhtml_import_chapters($file, $data->type, $book, $context);
- print_header("$course->shortname: $book->name", $course->fullname, $navigation);
-
- //import files
- print_box_start('generalbox boxaligncenter centerpara');
- echo '<strong>'.get_string('importing', 'book').':</strong>';
- echo '<table cellpadding="2" cellspacing="2" border="1">';
- book_preload_chapters($book); // fix structure
- foreach($refs as $ref) {
- $chapter = book_read_chapter($coursebase, $ref);
- if ($chapter) {
- $chapter->bookid = $book->id;
- $chapter->pagenum = $DB->count_records('book_chapters', array('bookid'=>$book->id)+1);
- $chapter->timecreated = time();
- $chapter->timemodified = time();
- echo "imsrc:".$chapter->importsrc;
- if (($data->subchapter) || preg_match('/_sub\.htm/i', $chapter->importsrc)) { //if filename or directory starts with sub_* treat as subdirecotories
- $chapter->subchapter = 1;
- } else {
- $chapter->subchapter = 0;
- }
- $chapter->id = $DB->insert_record('book_chapters', $chapter);
-
- add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
- add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
- }
- }
- echo '</table><br />';
- echo '<strong>'.get_string('relinking', 'book').':</strong>';
- echo '<table cellpadding="2" cellspacing="2" border="1">';
- //relink whole book = all chapters
- book_relink($cm->id, $book->id, $course->id);
- echo '</table><br />';
- print_box_end();
- print_continue('view.php?id='.$cm->id);
- print_footer($course);
+ echo $OUTPUT->continue_button(new moodle_url('/mod/book/view.php', array('id'=>$id)));
+ echo $OUTPUT->footer();
die;
-
- */
}
echo $OUTPUT->header();
-echo $OUTPUT->heading(get_string('importingchapters', 'mod_book'));
+echo $OUTPUT->heading(get_string('import', 'booktool_importhtml'));
$mform->display();
@@ -26,12 +26,14 @@
defined('MOODLE_INTERNAL') || die;
$string['doimport'] = 'Import';
-$string['import'] = 'Import';
+$string['errornochapters'] = 'Can not find chapters in selected file';
+$string['import'] = 'Import from HTML';
$string['importhtml:import'] = 'Import chapters';
-$string['importinfo'] = 'Import selected HTML file or directory.<br />Chapters are sorted alphabetically using file names.<br />Files named \'sub_*.*\' are always imported as subchapters.';
$string['importing'] = 'Importing';
$string['importingchapters'] = 'Importing chapters into book';
-$string['import_help'] = 'You can import a single HTML file or every HTML file in a direcory. Relative file links are converted to absolute chapter links. Images, flash and Java are relinked too.';
-$string['maindirectory'] = 'Main directory';
$string['pluginname'] = 'Book HTML import';
$string['relinking'] = 'Relinking';
+$string['type'] = 'Type';
+$string['typeonefile'] = 'One HTML file with headings as chapters';
+$string['typezipfiles'] = 'Each HTML file represents one chapter';
+$string['typezipdirs'] = 'Each directory represents one chapter';
View
@@ -36,16 +36,8 @@ function booktool_importhtml_extend_settings_navigation(settings_navigation $set
$PAGE->cm->context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->instance);
}
-
- $params = $PAGE->url->params();
-
- if (empty($params['id'])) {
- return;
- }
-
-
if (has_capability('booktool/importhtml:import', $PAGE->cm->context)) {
- //TODO
- //$doimport = ($allowimport and $edit) ? '<div>(<a href="import.php?id='.$cm->id.'">'.get_string('doimport', 'book').'</a>)</div>' : '';
+ $url = new moodle_url('/mod/book/tool/importhtml/index.php', array('id'=>$PAGE->cm->id));
+ $booknode->add(get_string('import', 'booktool_importhtml'), $url, navigation_node::TYPE_SETTING, null, null, null);
}
}
Oops, something went wrong.

0 comments on commit 34660c5

Please sign in to comment.