Ejemplo n.º 1
0
    public function test_chapter_created() {
        // There is no proper API to call to generate chapters for a book, so what we are
        // doing here is simply making sure that the events returns the right information.

        $course = $this->getDataGenerator()->create_course();
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
        $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');

        $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));

        $params = array(
            'context' => context_module::instance($book->cmid),
            'objectid' => $chapter->id
        );
        $event = \mod_book\event\chapter_created::create($params);

        // Triggering and capturing the event.
        $sink = $this->redirectEvents();
        $event->trigger();
        $events = $sink->get_events();
        $this->assertCount(1, $events);
        $event = reset($events);

        // Checking that the event contains the expected values.
        $this->assertInstanceOf('\mod_book\event\chapter_created', $event);
        $this->assertEquals(context_module::instance($book->id), $event->get_context());
        $this->assertEquals($chapter->id, $event->objectid);
        $expected = array($course->id, 'book', 'add chapter', 'view.php?id='.$book->cmid.'&chapterid='.$chapter->id,
            $chapter->id, $book->cmid);
        $this->assertEventLegacyLogData($expected, $event);
        $this->assertEventContextNotUsed($event);
    }
Ejemplo n.º 2
0
            $data->importsrc = '';
            $data->content = '';
            // updated later
            $data->contentformat = FORMAT_HTML;
            // updated later
            // make room for new page
            $sql = "UPDATE {book_chapters}\n                   SET pagenum = pagenum + 1\n                 WHERE bookid = ? AND pagenum >= ?";
            $DB->execute($sql, array($book->id, $data->pagenum));
            $data->id = $DB->insert_record('book_chapters', $data);
            // store the files
            $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id);
            $DB->update_record('book_chapters', $data);
            $DB->set_field('book', 'revision', $book->revision + 1, array('id' => $book->id));
            add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id=' . $cm->id, 'book ' . $book->id);
            $params = array('context' => $context, 'objectid' => $data->id);
            $event = \mod_book\event\chapter_created::create($params);
            $event->add_record_snapshot('book_chapters', $data);
            $event->trigger();
        }
        book_preload_chapters($book);
        // fix structure
        redirect("view.php?id={$cm->id}&chapterid={$data->id}");
    }
}
// Otherwise fill and print the form.
$PAGE->set_title($book->name);
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading($book->name);
$mform->display();
echo $OUTPUT->footer();
Ejemplo n.º 3
0
            $data->timemodified = time();
            $data->importsrc = '';
            $data->content = '';
            // updated later
            $data->contentformat = FORMAT_HTML;
            // updated later
            // make room for new page
            $sql = "UPDATE {book_chapters}\n                   SET pagenum = pagenum + 1\n                 WHERE bookid = ? AND pagenum >= ?";
            $DB->execute($sql, array($book->id, $data->pagenum));
            $data->id = $DB->insert_record('book_chapters', $data);
            // store the files
            $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id);
            $DB->update_record('book_chapters', $data);
            $DB->set_field('book', 'revision', $book->revision + 1, array('id' => $book->id));
            $chapter = $DB->get_record('book_chapters', array('id' => $data->id));
            \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter)->trigger();
        }
        book_preload_chapters($book);
        // fix structure
        redirect("view.php?id={$cm->id}&chapterid={$data->id}");
    }
}
// Otherwise fill and print the form.
$PAGE->set_title($book->name);
$PAGE->set_heading($course->fullname);
if ($chapters = book_preload_chapters($book)) {
    book_add_fake_block($chapters, $chapter, $book, $cm);
}
echo $OUTPUT->header();
echo $OUTPUT->heading($book->name);
$mform->display();
Ejemplo n.º 4
0
/**
 * Import HTML pages packaged into one zip archive
 *
 * @param stored_file $package
 * @param string $type type of the package ('typezipdirs' or 'typezipfiles')
 * @param stdClass $book
 * @param context_module $context
 * @param bool $verbose
 */
function toolbook_importhtml_import_chapters($package, $type, $book, $context, $verbose = true)
{
    global $DB, $OUTPUT;
    $fs = get_file_storage();
    $chapterfiles = toolbook_importhtml_get_chapter_files($package, $type);
    $packer = get_file_packer('application/zip');
    $fs->delete_area_files($context->id, 'mod_book', 'importhtmltemp', 0);
    $package->extract_to_storage($packer, $context->id, 'mod_book', 'importhtmltemp', 0, '/');
    // $datafiles = $fs->get_area_files($context->id, 'mod_book', 'importhtmltemp', 0, 'id', false);
    // echo "<pre>";p(var_export($datafiles, true));
    $chapters = array();
    if ($verbose) {
        echo $OUTPUT->notification(get_string('importing', 'booktool_importhtml'), 'notifysuccess');
    }
    if ($type == 0) {
        $chapterfile = reset($chapterfiles);
        if ($file = $fs->get_file_by_hash(sha1("{$context->id}/mod_book/importhtmltemp/0/{$chapterfile->pathname}"))) {
            $htmlcontent = toolbook_importhtml_fix_encoding($file->get_content());
            $htmlchapters = toolbook_importhtml_parse_headings(toolbook_importhtml_parse_body($htmlcontent));
            // TODO: process h1 as main chapter and h2 as subchapters
        }
    } else {
        foreach ($chapterfiles as $chapterfile) {
            if ($file = $fs->get_file_by_hash(sha1("/{$context->id}/mod_book/importhtmltemp/0/{$chapterfile->pathname}"))) {
                $chapter = new stdClass();
                $htmlcontent = toolbook_importhtml_fix_encoding($file->get_content());
                $chapter->bookid = $book->id;
                $chapter->pagenum = $DB->get_field_sql('SELECT MAX(pagenum) FROM {book_chapters} WHERE bookid = ?', array($book->id)) + 1;
                $chapter->importsrc = '/' . $chapterfile->pathname;
                $chapter->content = toolbook_importhtml_parse_styles($htmlcontent);
                $chapter->content .= toolbook_importhtml_parse_body($htmlcontent);
                $chapter->title = toolbook_importhtml_parse_title($htmlcontent, $chapterfile->pathname);
                $chapter->contentformat = FORMAT_HTML;
                $chapter->hidden = 0;
                $chapter->timecreated = time();
                $chapter->timemodified = time();
                if (preg_match('/_sub(\\/|\\.htm)/i', $chapter->importsrc)) {
                    // If filename or directory ends with *_sub treat as subchapters
                    $chapter->subchapter = 1;
                } else {
                    $chapter->subchapter = 0;
                }
                $chapter->id = $DB->insert_record('book_chapters', $chapter);
                $chapter = $DB->get_record('book_chapters', array('id' => $chapter->id));
                $chapters[$chapter->id] = $chapter;
                \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter)->trigger();
            }
        }
    }
    if ($verbose) {
        echo $OUTPUT->notification(get_string('relinking', 'booktool_importhtml'), 'notifysuccess');
    }
    $allchapters = $DB->get_records('book_chapters', array('bookid' => $book->id), 'pagenum');
    foreach ($chapters as $chapter) {
        // find references to all files and copy them + relink them
        $matches = null;
        if (preg_match_all('/(src|codebase|name|href)\\s*=\\s*"([^"]+)"/i', $chapter->content, $matches)) {
            $file_record = array('contextid' => $context->id, 'component' => 'mod_book', 'filearea' => 'chapter', 'itemid' => $chapter->id);
            foreach ($matches[0] as $i => $match) {
                $filepath = dirname($chapter->importsrc) . '/' . $matches[2][$i];
                $filepath = toolbook_importhtml_fix_path($filepath);
                if (strtolower($matches[1][$i]) === 'href') {
                    // skip linked html files, we will try chapter relinking later
                    foreach ($allchapters as $target) {
                        if ($target->importsrc === $filepath) {
                            continue 2;
                        }
                    }
                }
                if ($file = $fs->get_file_by_hash(sha1("/{$context->id}/mod_book/importhtmltemp/0{$filepath}"))) {
                    if (!($oldfile = $fs->get_file_by_hash(sha1("/{$context->id}/mod_book/chapter/{$chapter->id}{$filepath}")))) {
                        $fs->create_file_from_storedfile($file_record, $file);
                    }
                    $chapter->content = str_replace($match, $matches[1][$i] . '="@@PLUGINFILE@@' . $filepath . '"', $chapter->content);
                }
            }
            $DB->set_field('book_chapters', 'content', $chapter->content, array('id' => $chapter->id));
        }
    }
    unset($chapters);
    $allchapters = $DB->get_records('book_chapters', array('bookid' => $book->id), 'pagenum');
    foreach ($allchapters as $chapter) {
        $newcontent = $chapter->content;
        $matches = null;
        if (preg_match_all('/(href)\\s*=\\s*"([^"]+)"/i', $chapter->content, $matches)) {
            foreach ($matches[0] as $i => $match) {
                if (strpos($matches[2][$i], ':') !== false or strpos($matches[2][$i], '@') !== false) {
                    // it is either absolute or pluginfile link
                    continue;
                }
                $chapterpath = dirname($chapter->importsrc) . '/' . $matches[2][$i];
                $chapterpath = toolbook_importhtml_fix_path($chapterpath);
                foreach ($allchapters as $target) {
                    if ($target->importsrc === $chapterpath) {
                        $newcontent = str_replace($match, 'href="' . new moodle_url('/mod/book/view.php', array('id' => $context->instanceid, 'chapterid' => $target->id)) . '"', $newcontent);
                    }
                }
            }
        }
        if ($newcontent !== $chapter->content) {
            $DB->set_field('book_chapters', 'content', $newcontent, array('id' => $chapter->id));
        }
    }
    $fs->delete_area_files($context->id, 'mod_book', 'importhtmltemp', 0);
    // update the revision flag - this takes a long time, better to refetch the current value
    $book = $DB->get_record('book', array('id' => $book->id));
    $DB->set_field('book', 'revision', $book->revision + 1, array('id' => $book->id));
}