MDL-43385 booktool_print: Improve print output
authorMihail Geshoski <mihail@moodle.com>
Tue, 5 Feb 2019 08:10:31 +0000 (16:10 +0800)
committerMihail Geshoski <mihail@moodle.com>
Tue, 5 Feb 2019 08:10:31 +0000 (16:10 +0800)
mod/book/locallib.php
mod/book/tool/print/classes/output/print_book_chapter_page.php [new file with mode: 0644]
mod/book/tool/print/classes/output/print_book_page.php [new file with mode: 0644]
mod/book/tool/print/classes/output/renderer.php [new file with mode: 0644]
mod/book/tool/print/index.php
mod/book/tool/print/locallib.php
mod/book/tool/print/print.css
mod/book/tool/print/templates/print_book.mustache [new file with mode: 0644]
mod/book/tool/print/templates/print_book_chapter.mustache [new file with mode: 0644]

index 52cb3d4..3005c70 100644 (file)
@@ -52,7 +52,7 @@ define ('BOOK_LINK_TEXT', '2');
 /**
  * Preload book chapters and fix toc structure if necessary.
  *
- * Returns array of chapters with standard 'pagenum', 'id, pagenum, subchapter, title, hidden'
+ * Returns array of chapters with standard 'pagenum', 'id, pagenum, subchapter, title, content, contentformat, hidden'
  * and extra 'parent, number, subchapters, prev, next'.
  * Please note the content/text of chapters is not included.
  *
@@ -61,7 +61,8 @@ define ('BOOK_LINK_TEXT', '2');
  */
 function book_preload_chapters($book) {
     global $DB;
-    $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, pagenum, subchapter, title, hidden');
+    $chapters = $DB->get_records('book_chapters', array('bookid' => $book->id), 'pagenum', 'id, pagenum,
+            subchapter, title, content, contentformat, hidden');
     if (!$chapters) {
         return array();
     }
diff --git a/mod/book/tool/print/classes/output/print_book_chapter_page.php b/mod/book/tool/print/classes/output/print_book_chapter_page.php
new file mode 100644 (file)
index 0000000..cd70be8
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Class containing data for the view book page.
+ *
+ * @package    booktool_print
+ * @copyright  2019 Mihail Geshoski
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace booktool_print\output;
+
+defined('MOODLE_INTERNAL') || die();
+
+use renderable;
+use renderer_base;
+use stdClass;
+use templatable;
+use context_module;
+
+/**
+ * Class containing data for the print book page.
+ *
+ * @copyright  2019 Mihail Geshoski
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class print_book_chapter_page implements renderable, templatable {
+
+    /**
+     * @var object $book The book object.
+     */
+    protected $book;
+
+    /**
+     * @var object $cm The course module object.
+     */
+    protected $cm;
+
+    /**
+     * @var object $chapter The book chapter object.
+     */
+    protected $chapter;
+
+    /**
+     * Construct this renderable.
+     *
+     * @param object $book The book
+     * @param object $cm The course module
+     * @param object $chapter The book chapter
+     */
+    public function __construct($book, $cm, $chapter) {
+        $this->book = $book;
+        $this->cm = $cm;
+        $this->chapter = $chapter;
+    }
+
+    /**
+     * Export this data so it can be used as the context for a mustache template.
+     *
+     * @param renderer_base $output
+     * @return stdClass
+     */
+    public function export_for_template(renderer_base $output) {
+        global $OUTPUT;
+
+        $context = context_module::instance($this->cm->id);
+        $chapters = book_preload_chapters($this->book);
+
+        $data = new stdClass();
+        // Print dialog link.
+        $data->printdialoglink = $output->render_print_book_chapter_dialog_link();
+        $data->booktitle = $OUTPUT->heading(format_string($this->book->name, true,
+                array('context' => $context)), 1);
+        if (!$this->book->customtitles) {
+            // If the current chapter is a subchapter, get the title of the parent chapter.
+            if ($this->chapter->subchapter) {
+                $parentchaptertitle = book_get_chapter_title($chapters[$this->chapter->id]->parent, $chapters,
+                        $this->book, $context);
+                $data->parentchaptertitle = $OUTPUT->heading(format_string($parentchaptertitle, true,
+                        array('context' => $context)), 2);
+            }
+        }
+
+        list($chaptercontent, $chaptervisible) = $output->render_print_book_chapter($this->chapter, $chapters,
+                $this->book, $this->cm);
+        $chapter = new stdClass();
+        $chapter->content = $chaptercontent;
+        $chapter->visible = $chaptervisible;
+        $data->chapter = $chapter;
+
+        return $data;
+    }
+}
diff --git a/mod/book/tool/print/classes/output/print_book_page.php b/mod/book/tool/print/classes/output/print_book_page.php
new file mode 100644 (file)
index 0000000..3dd5936
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Class containing data for the view book page.
+ *
+ * @package    booktool_print
+ * @copyright  2019 Mihail Geshoski
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace booktool_print\output;
+
+defined('MOODLE_INTERNAL') || die();
+
+use moodle_url;
+use renderable;
+use renderer_base;
+use stdClass;
+use templatable;
+use context_module;
+
+/**
+ * Class containing data for the print book page.
+ *
+ * @copyright  2019 Mihail Geshoski
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class print_book_page implements renderable, templatable {
+
+    /**
+     * @var object $book The book object.
+     */
+    protected $book;
+
+    /**
+     * @var object $cm The course module object.
+     */
+    protected $cm;
+
+    /**
+     * Construct this renderable.
+     *
+     * @param object $book The book
+     * @param object $cm The course module
+     */
+    public function __construct($book, $cm) {
+        $this->book = $book;
+        $this->cm = $cm;
+    }
+
+    /**
+     * Export this data so it can be used as the context for a mustache template.
+     *
+     * @param renderer_base $output
+     * @return stdClass
+     */
+    public function export_for_template(renderer_base $output) {
+        global $OUTPUT, $CFG, $SITE, $USER;
+
+        $context = context_module::instance($this->cm->id);
+        $chapters = book_preload_chapters($this->book);
+        $course = get_course($this->book->course);
+
+        $data = new stdClass();
+        // Print dialog link.
+        $data->printdialoglink = $output->render_print_book_dialog_link();
+        $data->booktitle = $OUTPUT->heading(format_string($this->book->name, true,
+                array('context' => $context)), 1);
+        $data->bookintro = format_text($this->book->intro, $this->book->introformat,
+                array('noclean' => true, 'context' => $context));
+        $data->sitelink = \html_writer::link(new moodle_url($CFG->wwwroot),
+                format_string($SITE->fullname, true, array('context' => $context)));
+        $data->coursename = format_string($course->fullname, true, array('context' => $context));
+        $data->modulename = format_string($this->book->name, true, array('context' => $context));
+        $data->username = fullname($USER, true);
+        $data->printdate = userdate(time());
+        $data->toc = $output->render_print_book_toc($chapters, $this->book, $this->cm);
+        foreach ($chapters as $ch) {
+            list($chaptercontent, $chaptervisible) = $output->render_print_book_chapter($ch, $chapters, $this->book,
+                    $this->cm);
+            $chapter = new stdClass();
+            $chapter->content = $chaptercontent;
+            $chapter->visible = $chaptervisible;
+            $data->chapters[] = $chapter;
+        }
+
+        return $data;
+    }
+}
diff --git a/mod/book/tool/print/classes/output/renderer.php b/mod/book/tool/print/classes/output/renderer.php
new file mode 100644 (file)
index 0000000..21c2932
--- /dev/null
@@ -0,0 +1,210 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Defines the renderer for the book print tool.
+ *
+ * @package    booktool_print
+ * @copyright  2019 Mihail Geshoski
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace booktool_print\output;
+
+defined('MOODLE_INTERNAL') || die();
+
+use plugin_renderer_base;
+use html_writer;
+use context_module;
+use moodle_url;
+use moodle_exception;
+
+/**
+ * The renderer for the book print tool.
+ *
+ * @copyright  2019 Mihail Geshoski
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class renderer extends plugin_renderer_base {
+
+    /**
+     * Render the print book page.
+     *
+     * @param print_book_page $page
+     * @return string html for the page
+     * @throws moodle_exception
+     */
+    public function render_print_book_page(print_book_page $page) {
+        $data = $page->export_for_template($this);
+        return parent::render_from_template('booktool_print/print_book', $data);
+    }
+
+    /**
+     * Render the print book chapter page.
+     *
+     * @param print_book_chapter_page $page
+     * @return string html for the page
+     * @throws moodle_exception
+     */
+    public function render_print_book_chapter_page(print_book_chapter_page $page) {
+        $data = $page->export_for_template($this);
+        return parent::render_from_template('booktool_print/print_book_chapter', $data);
+    }
+
+    /**
+     * Render the print book chapter link.
+     *
+     * @return string html for the link
+     */
+    public function render_print_book_chapter_dialog_link() {
+        $printtext = get_string('printchapter', 'booktool_print');
+        $printicon = $this->output->pix_icon('chapter', $printtext, 'booktool_print',
+                array('class' => 'icon'));
+        $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'hidden-print');
+        return html_writer::link('#', $printicon . $printtext, $printlinkatt);
+    }
+
+    /**
+     * Render the print book link.
+     *
+     * @return string html for the link
+     */
+    public function render_print_book_dialog_link() {
+        $printtext = get_string('printbook', 'booktool_print');
+        $printicon = $this->output->pix_icon('book', $printtext, 'booktool_print',
+            array('class' => 'icon'));
+        $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'hidden-print');
+        return html_writer::link('#', $printicon . $printtext, $printlinkatt);
+    }
+
+    /**
+     * Render the print book table of contents.
+     *
+     * @param array $chapters Array of book chapters
+     * @param object $book The book object
+     * @param object $cm The curse module object
+     * @return string html for the TOC
+     */
+    public function render_print_book_toc($chapters, $book, $cm) {
+
+        $first = true;
+        $titles = array();
+
+        $context = context_module::instance($cm->id);
+
+        $toc = ''; // Representation of toc (HTML).
+
+        switch ($book->numbering) {
+            case BOOK_NUM_NONE:
+                $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none'));
+                break;
+            case BOOK_NUM_NUMBERS:
+                $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered'));
+                break;
+            case BOOK_NUM_BULLETS:
+                $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets'));
+                break;
+            case BOOK_NUM_INDENTED:
+                $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented'));
+                break;
+        }
+
+        $toc .= html_writer::tag('a', '', array('name' => 'toc')); // Representation of toc (HTML).
+
+        $toc .= html_writer::tag('h2', get_string('toc', 'mod_book'), ['class' => 'text-center p-b-2']);
+        $toc .= html_writer::start_tag('ul');
+        foreach ($chapters as $ch) {
+            if (!$ch->hidden) {
+                $title = book_get_chapter_title($ch->id, $chapters, $book, $context);
+                if (!$ch->subchapter) {
+
+                    if ($first) {
+                        $toc .= html_writer::start_tag('li');
+                    } else {
+                        $toc .= html_writer::end_tag('ul');
+                        $toc .= html_writer::end_tag('li');
+                        $toc .= html_writer::start_tag('li');
+                    }
+
+                } else {
+
+                    if ($first) {
+                        $toc .= html_writer::start_tag('li');
+                        $toc .= html_writer::start_tag('ul');
+                        $toc .= html_writer::start_tag('li');
+                    } else {
+                        $toc .= html_writer::start_tag('li');
+                    }
+
+                }
+                $titles[$ch->id] = $title;
+
+                if (!$ch->subchapter) {
+                    $toc .= html_writer::link(new moodle_url('#ch' . $ch->id), $title,
+                            array('title' => s($title), 'class' => 'font-weight-bold text-decoration-none'));
+                    $toc .= html_writer::start_tag('ul');
+                } else {
+                    $toc .= html_writer::link(new moodle_url('#ch' . $ch->id), $title,
+                            array('title' => s($title), 'class' => 'text-decoration-none'));
+                    $toc .= html_writer::end_tag('li');
+                }
+                $first = false;
+            }
+        }
+
+        $toc .= html_writer::end_tag('ul');
+        $toc .= html_writer::end_tag('li');
+        $toc .= html_writer::end_tag('ul');
+        $toc .= html_writer::end_tag('div');
+
+        $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
+
+        return $toc;
+    }
+
+    /**
+     * Render the print book chapter.
+     *
+     * @param object $chapter The book chapter object
+     * @param array $chapters The array of book chapters
+     * @param object $book The book object
+     * @param object $cm The course module object
+     * @return array The array containing the content of the book chapter and visibility information
+     */
+    public function render_print_book_chapter($chapter, $chapters, $book, $cm) {
+        global $OUTPUT;
+
+        $context = context_module::instance($cm->id);
+        $title = book_get_chapter_title($chapter->id, $chapters, $book, $context);
+
+        $chaptervisible = $chapter->hidden ? false : true;
+
+        $bookchapter = '';
+        $bookchapter .= html_writer::start_div('book_chapter p-t-1', ['id' => 'ch' . $chapter->id]);
+        if (!$book->customtitles) {
+            if (!$chapter->subchapter) {
+                $bookchapter .= $OUTPUT->heading($title, 2, 'text-center p-b-2');
+            } else {
+                $bookchapter .= $OUTPUT->heading($title, 3, 'text-center p-b-2');
+            }
+        }
+
+        $bookchapter .= format_text($chapter->content, $chapter->contentformat, array('noclean' => true, 'context' => $context));
+        $bookchapter .= html_writer::end_div();
+
+        return array($bookchapter, $chaptervisible);
+    }
+}
index ac86e91..a704d80 100644 (file)
@@ -33,8 +33,8 @@ $chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
 // =========================================================================
 
 $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
-$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
-$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
+$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
+$book = $DB->get_record('book', array('id' => $cm->instance), '*', MUST_EXIST);
 
 require_course_login($course, true, $cm);
 
@@ -45,13 +45,14 @@ require_capability('booktool/print:print', $context);
 // Check all variables.
 if ($chapterid) {
     // Single chapter printing - only visible!
-    $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
+    $chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id), '*',
+            MUST_EXIST);
 } else {
     // Complete book.
     $chapter = false;
 }
 
-$PAGE->set_url('/mod/book/print.php', array('id'=>$id, 'chapterid'=>$chapterid));
+$PAGE->set_url('/mod/book/print.php', array('id' => $id, 'chapterid' => $chapterid));
 
 // Use "embedded" instead of "print" because Bootstrapbase shows top
 // header bar and navbar even on print style - which is inconsistent
@@ -71,112 +72,28 @@ $strbook  = get_string('modulename', 'mod_book');
 $strtop   = get_string('top', 'mod_book');
 
 // Page header.
-$strtitle = format_string($book->name, true, array('context'=>$context));
+$strtitle = format_string($book->name, true, array('context' => $context));
 $PAGE->set_title($strtitle);
 $PAGE->set_heading($strtitle);
 $PAGE->requires->css('/mod/book/tool/print/print.css');
 
+$renderer = $PAGE->get_renderer('booktool_print');
+
 // Begin page output.
 echo $OUTPUT->header();
 
 if ($chapter) {
-
     if ($chapter->hidden) {
         require_capability('mod/book:viewhiddenchapters', $context);
     }
     \booktool_print\event\chapter_printed::create_from_chapter($book, $context, $chapter)->trigger();
-
-    // Print dialog link.
-    $printtext = get_string('printchapter', 'booktool_print');
-    $printicon = $OUTPUT->pix_icon('chapter', $printtext, 'booktool_print', array('class' => 'icon'));
-    $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'hidden-print');
-    echo html_writer::link('#', $printicon.$printtext, $printlinkatt);
-    ?>
-    <a name="top"></a>
-    <?php
-    echo $OUTPUT->heading(format_string($book->name, true, array('context'=>$context)), 1);
-    ?>
-    <div class="chapter">
-    <?php
-    if (!$book->customtitles) {
-        if (!$chapter->subchapter) {
-            $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
-            echo $OUTPUT->heading($currtitle);
-        } else {
-            $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
-            $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
-            echo $OUTPUT->heading($currtitle);
-            echo $OUTPUT->heading($currsubtitle, 3);
-        }
-    }
-
-    $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
-    echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
-    echo '</div>';
-
+    $page = new booktool_print\output\print_book_chapter_page($book, $cm, $chapter);
 } else {
     \booktool_print\event\book_printed::create_from_book($book, $context)->trigger();
-
-    $allchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum');
-    $book->intro = file_rewrite_pluginfile_urls($book->intro, 'pluginfile.php', $context->id, 'mod_book', 'intro', null);
-
-    // Print dialog link.
-    $printtext = get_string('printbook', 'booktool_print');
-    $printicon = $OUTPUT->pix_icon('book', $printtext, 'booktool_print', array('class' => 'icon'));
-    $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'hidden-print');
-    echo html_writer::link('#', $printicon.$printtext, $printlinkatt);
-    ?>
-    <a name="top"></a>
-    <?php
-    echo $OUTPUT->heading(format_string($book->name, true, array('context'=>$context)), 1);
-    ?>
-    <p class="book_summary"><?php echo format_text($book->intro, $book->introformat, array('noclean'=>true, 'context'=>$context)) ?></p>
-    <div class="book_info"><table>
-    <tr>
-    <td><?php echo get_string('site') ?>:</td>
-    <td><a href="<?php echo $CFG->wwwroot ?>"><?php echo format_string($SITE->fullname, true, array('context'=>$context)) ?></a></td>
-    </tr><tr>
-    <td><?php echo get_string('course') ?>:</td>
-    <td><?php echo format_string($course->fullname, true, array('context'=>$context)) ?></td>
-    </tr><tr>
-    <td><?php echo get_string('modulename', 'mod_book') ?>:</td>
-    <td><?php echo format_string($book->name, true, array('context'=>$context)) ?></td>
-    </tr><tr>
-    <td><?php echo get_string('printedby', 'booktool_print') ?>:</td>
-    <td><?php echo fullname($USER, true) ?></td>
-    </tr><tr>
-    <td><?php echo get_string('printdate', 'booktool_print') ?>:</td>
-    <td><?php echo userdate(time()) ?></td>
-    </tr>
-    </table></div>
-
-    <?php
-    list($toc, $titles) = booktool_print_get_toc($chapters, $book, $cm);
-    echo $toc;
-    // chapters
-    $link1 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id.'&chapterid=';
-    $link2 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id;
-    foreach ($chapters as $ch) {
-        $chapter = $allchapters[$ch->id];
-        if ($chapter->hidden) {
-            continue;
-        }
-        echo '<div class="book_chapter"><a name="ch'.$ch->id.'"></a>';
-        if (!$book->customtitles) {
-            if (!$chapter->subchapter) {
-                echo $OUTPUT->heading($titles[$ch->id]);
-            } else {
-                echo $OUTPUT->heading($titles[$ch->id], 3);
-            }
-        }
-        $content = str_replace($link1, '#ch', $chapter->content);
-        $content = str_replace($link2, '#top', $content);
-        $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $ch->id);
-        echo format_text($content, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
-        echo '</div>';
-        // echo '<a href="#toc">'.$strtop.'</a>';
-    }
+    $page = new booktool_print\output\print_book_page($book, $cm);
 }
 
+echo $renderer->render($page);
+
 // Finish page output.
 echo $OUTPUT->footer();
\ No newline at end of file
index b8d0670..169caf3 100644 (file)
@@ -30,12 +30,16 @@ require_once($CFG->dirroot.'/mod/book/locallib.php');
 /**
  * Generate toc structure and titles
  *
+ * @deprecated since Moodle 3.7
  * @param array $chapters
  * @param stdClass $book
  * @param stdClass $cm
  * @return array
  */
 function booktool_print_get_toc($chapters, $book, $cm) {
+    debugging('booktool_print_get_toc() is deprecated. Please use booktool_print renderer
+            function render_print_book_toc().', DEBUG_DEVELOPER);
+
     $first = true;
     $titles = array();
 
index 7b733c0..c28fe48 100644 (file)
@@ -1,48 +1,3 @@
-#page-mod-book-print {
-    color: #000;
-    background-color: #fff;
-    font-family: "Times New Roman", Times, serif;
-    font-size: 1em;
-    font-weight: normal;
-    text-decoration: none;
-}
-
-#page-mod-book-print {
-    margin-left: 50px;
-    margin-right: 10px;
-}
-
-#page-mod-book-print h1,
-#page-mod-book-print h2,
-#page-mod-book-print h3,
-#page-mod-book-print h4,
-#page-mod-book-print h5,
-#page-mod-book-print h6 {
-    page-break-after: avoid;
-    page-break-inside: avoid;
-    margin-left: 0;
-}
-
-/* just some hack - ignore user defined <font> */
-#page-mod-book-print font {
-    color: #000;
-    background-color: #eee;
-    font-family: "Times New Roman", Times, serif;
-    font-size: 1em;
-    font-weight: normal;
-    text-decoration: none;
-}
-
-#page-mod-book-print .book_summary {
-    text-align: center;
-    margin-bottom: 120px;
-    margin-left: -40px;
-}
-
-#page-mod-book-print .book_chapter {
-    page-break-before: always;
-}
-
 /* ===== TOC numbering styles ===== */
 
 /* numbering == NONE */
     padding-left: 0;
 }
 
+#page-mod-book-print .book .book_chapter,
+#page-mod-book-print .book .book_description {
+    page-break-before: always;
+}
+
+#page-mod-book-print {
+    padding-top: 0;
+}
 
 /* Exclude elements from printing. */
-
 @media print {
     .hidden-print {
         display: none !important; /* stylelint-disable-line declaration-no-important */
     }
+    .book .book_title {
+        margin-top: 45%;
+    }
 }
diff --git a/mod/book/tool/print/templates/print_book.mustache b/mod/book/tool/print/templates/print_book.mustache
new file mode 100644 (file)
index 0000000..b6a0214
--- /dev/null
@@ -0,0 +1,137 @@
+{{!
+    This file is part of Moodle - http://moodle.org/
+
+    Moodle is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Moodle is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more comments.
+
+    You should have received a copy of the GNU General Public License
+    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+}}
+{{!
+    @template booktool_print/print_book
+
+    Print book page.
+
+    Classes required for JS:
+    * none
+
+    Data attributes required for JS:
+    * none
+
+    * printdialoglink string The URL pointing to the print book chapter popup window.
+    * booktitle string The HTML of the book title.
+    * bookintro string The book description.
+    * sitelink string The HTML link pointing to the site's dashboard.
+    * coursename string The name of the course.
+    * modulename string The name of the module.
+    * username string The user name.
+    * printdate string The print date.
+    * chaptertitle string The title of the chapter.
+    * toc object The HTML of the table of contents.
+    * chapters array The array of book chapters, their content and visibility information.
+
+    Example context (json):
+    {
+        "printdialoglink": "<a>Print book</a>",
+        "booktitle": "<h2>Book title</h2>",
+        "bookintro": "Book description",
+        "sitelink" : "<a>Site title</a>",
+        "coursename" : "Course name",
+        "modulename" : "Module name",
+        "username" : "User name",
+        "printdate" : "Tuesday, 22 January 2019, 10:17 AM",
+        "chaptertitle": "Chapter title",
+        "toc" : "<div>Table of contents</div>",
+        "chapters": [
+            {
+                "content": "<div>Chapter1 content</div>",
+                "visible": "true"
+            },
+            {
+                "content": "<div>Chapter2 content</div>",
+                "visible": "false"
+            }
+        ]
+    }
+}}
+<div class="book p-4">
+    <div class="text-right">{{{ printdialoglink }}}</div>
+    <div class="text-center p-b-1 book_title">{{{ booktitle }}}</div>
+    <div class="book_info w-100 p-t-3 d-inline-block">
+        <div class="w-50 pull-left">
+            <table>
+                <tr>
+                    <td>
+                        {{# str }} site {{/ str }}:
+                    </td>
+                    <td class="p-l-1">
+                        {{{ sitelink }}}
+                    </td>
+                </tr>
+                <tr>
+                    <td>
+                        {{# str }} course {{/ str }}:
+                    </td>
+                    <td class="p-l-1">
+                        {{{ coursename }}}
+                    </td>
+                </tr>
+                <tr>
+                    <td>
+                        {{# str }} modulename, mod_book {{/ str }}:
+                    </td>
+                    <td class="p-l-1">
+                        {{{ modulename }}}
+                    </td>
+                </tr>
+            </table>
+        </div>
+        <div class="w-50 pull-left">
+            <table class="pull-right">
+                <tr>
+                    <td>
+                        {{# str }} printedby, booktool_print {{/ str }}:
+                    </td>
+                    <td class="p-l-1">
+                        {{{ username }}}
+                    </td>
+                </tr>
+                <tr>
+                    <td>
+                        {{# str }} printdate, booktool_print {{/ str }}:
+                    </td>
+                    <td class="p-l-1">
+                        {{{ printdate }}}
+                    </td>
+                </tr>
+            </table>
+        </div>
+    </div>
+    {{#bookintro}}
+    <div class="w-100 book_description">
+        <div class="p-b-2 p-t-2">
+            <h2 class="text-center p-b-2">{{#str}} description {{/str}}</h2>
+             <p class="book_summary">{{{ bookintro }}}</p>
+        </div>
+    </div>
+    {{/bookintro}}
+    <div class="w-100">
+        <div class="p-b-2 p-t-2">{{{ toc }}}</div>
+    </div>
+    <div class="w-100">
+     {{#chapters}}
+        {{#visible }}
+            <div class="p-b-2">
+            {{{ content }}}
+            </div>
+        {{/visible}}
+    {{/chapters}}
+    </div>
+</div>
diff --git a/mod/book/tool/print/templates/print_book_chapter.mustache b/mod/book/tool/print/templates/print_book_chapter.mustache
new file mode 100644 (file)
index 0000000..bfcc41d
--- /dev/null
@@ -0,0 +1,56 @@
+{{!
+    This file is part of Moodle - http://moodle.org/
+
+    Moodle is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Moodle is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more comments.
+
+    You should have received a copy of the GNU General Public License
+    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+}}
+{{!
+    @template booktool_print/print_book_chapter
+
+    Print book chapter page.
+
+    Classes required for JS:
+    * none
+
+    Data attributes required for JS:
+    * none
+
+    Context variables required for this template:
+    * printdialoglink string The URL pointing to the print book chapter popup window.
+    * booktitle string The HTML of the book title.
+    * parentchaptertitle string The HTML of the parent chapter title (if exists).
+    * chapter object The object containing the HTML of the chapter content and chapter's visibility.
+
+    Example context (json):
+    {
+        "printdialoglink": "<a>Print book</a>",
+        "booktitle": "<h2>Book title</h2>",
+        "parentchaptertitle": "<h2>Parent chapter title</h2>",
+        "chapter": {
+            "content": "<div>Chapter content</div>",
+            "visible": "true"
+        }
+    }
+}}
+<div class="chapter col-12 p-4">
+    <div class="text-right">{{{ printdialoglink }}}</div>
+    <div class="text-center p-b-2">{{{ booktitle }}}</div>
+    <div class="chapter">
+        {{#parentchaptertitle}}
+         <div class="text-center">
+            {{{parentchaptertitle}}}
+         </div>
+        {{/parentchaptertitle}}
+        {{{chapter.content}}}
+    </div>
+</div>