Commit e81c01b6 authored by Eloy Lafuente's avatar Eloy Lafuente
Browse files

Subtree merged git://github.com/skodak/moodle-mod_book.git (master branch) into mod/book

parents 4f7f2a88 dcb83c34
# See gitignore(5) man page for more details
#
*~
*.swp
/tags
/TAGS
/cscope.*
/.patches/
/.idea/
/nbproject/
CVS
.DS_Store
/.settings/
/.project
/.buildpath
/.cache
Book module for Moodle (http://moodle.org/) - Copyright (C) 2004-2011 Petr Skoda (http://skodak.org/)
The Book module makes it easy to create multi-page resources with a book-like format. This module can be used to build complete book-like websites inside of your Moodle course.
This module was developed for Technical University of Liberec (Czech Republic). Many ideas and code were taken from other Moodle modules and Moodle itself
This program 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.
This program 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: http://www.gnu.org/copyleft/gpl.html
Created by:
* Petr Skoda (skodak) - most of the coding & design
* Mojmir Volf, Eloy Lafuente, Antonio Vicent and others
Project page:
* https://github.com/skodak/moodle-mod_book
* http://moodle.org/plugins/view.php?plugin=mod_book
Installation:
* http://docs.moodle.org/20/en/Installing_contributed_modules_or_plugins
Issue tracker:
* https://github.com/skodak/moodle-mod_book/issues?milestone=&labels=
Intentionally omitted features:
* more chapter levels - it would encourage teachers to write too much complex and long books, better use standard standalone HTML editor and import it as Resource. DocBook format is another suitable solution.
* TOC hiding in normal view - instead use printer friendly view
* PDF export - there is no elegant way AFAIK to convert HTML to PDF, use virtual PDF printer or better use DocBook format for authoring
* detailed student tracking (postponed till officially supported)
* export as zipped set of HTML pages - instead use browser command Save page as... in print view
Future:
* No more development planned
<?php
// This file is part of Book module for 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/>.
/**
* Provides support for the conversion of moodle1 backup to the moodle2 format
*
* @package mod
* @subpackage book
* @copyright 2011 Tõnis Tartes <t6nis20@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Book conversion handler
*/
class moodle1_mod_book_handler extends moodle1_mod_handler {
/** @var moodle1_file_manager */
protected $fileman = null;
/** @var int cmid */
protected $moduleid = null;
/**
* Declare the paths in moodle.xml we are able to convert
*
* The method returns list of {@link convert_path} instances. For each path returned,
* at least one of on_xxx_start(), process_xxx() and on_xxx_end() methods must be
* defined. The method process_xxx() is not executed if the associated path element is
* empty (i.e. it contains none elements or sub-paths only).
*
* Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK does not
* actually exist in the file. The last element with the module name was
* appended by the moodle1_converter class.
*
* @return array of {@link convert_path} instances
*/
public function get_paths() {
return array(
new convert_path('book', '/MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK',
array(
'renamefields' => array(
'summary' => 'intro',
),
'newfields' => array(
'introformat' => FORMAT_MOODLE,
),
'dropfields' => array(
'disableprinting'
),
)
),
new convert_path('book_chapters', '/MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK/CHAPTERS/CHAPTER',
array(
'newfields' => array(
'contentformat' => FORMAT_HTML,
),
)
),
);
}
/**
* This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK
* data available
* @param array $data
*/
public function process_book($data) {
global $CFG;
// get the course module id and context id
$instanceid = $data['id'];
$cminfo = $this->get_cminfo($instanceid);
$this->moduleid = $cminfo['id'];
$contextid = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
// replay the upgrade step 2009042006
if ($CFG->texteditors !== 'textarea') {
$data['intro'] = text_to_html($data['intro'], false, false, true);
$data['introformat'] = FORMAT_HTML;
}
// get a fresh new file manager for this instance
$this->fileman = $this->converter->get_file_manager($contextid, 'mod_book');
// convert course files embedded into the intro
$this->fileman->filearea = 'intro';
$this->fileman->itemid = 0;
$data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
// start writing book.xml
$this->open_xml_writer("activities/book_{$this->moduleid}/book.xml");
$this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
'modulename' => 'book', 'contextid' => $contextid));
$this->xmlwriter->begin_tag('book', array('id' => $instanceid));
foreach ($data as $field => $value) {
if ($field <> 'id') {
$this->xmlwriter->full_tag($field, $value);
}
}
}
/**
* This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK/CHAPTERS/CHAPTER
* data available
* @param array $data
*/
public function process_book_chapters($data) {
$this->write_xml('chapter', $data, array('/chapter/id'));
// convert chapter files
$this->fileman->filearea = 'chapter';
$this->fileman->itemid = $data['id'];
$data['content'] = moodle1_converter::migrate_referenced_files($data['content'], $this->fileman);
}
/**
* This is executed when the parser reaches the <OPTIONS> opening element
*/
public function on_book_chapters_start() {
$this->xmlwriter->begin_tag('chapters');
}
/**
* This is executed when the parser reaches the closing </OPTIONS> element
*/
public function on_book_chapters_end() {
$this->xmlwriter->end_tag('chapters');
}
/**
* This is executed when we reach the closing </MOD> tag of our 'book' path
*/
public function on_book_end() {
// finalize book.xml
$this->xmlwriter->end_tag('book');
$this->xmlwriter->end_tag('activity');
$this->close_xml_writer();
// write inforef.xml
$this->open_xml_writer("activities/book_{$this->moduleid}/inforef.xml");
$this->xmlwriter->begin_tag('inforef');
$this->xmlwriter->begin_tag('fileref');
foreach ($this->fileman->get_fileids() as $fileid) {
$this->write_xml('file', array('id' => $fileid));
}
$this->xmlwriter->end_tag('fileref');
$this->xmlwriter->end_tag('inforef');
$this->close_xml_writer();
}
}
<?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/>.
/**
* Description of book backup task
*
* @package mod
* @subpackage book
* @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/mod/book/backup/moodle2/backup_book_stepslib.php'); // Because it exists (must)
require_once($CFG->dirroot.'/mod/book/backup/moodle2/backup_book_settingslib.php'); // Because it exists (optional)
/**
* book backup task that provides all the settings and steps to perform one
* complete backup of the activity
*/
class backup_book_activity_task extends backup_activity_task {
/**
* Define (add) particular settings this activity can have
*
* @return void
*/
protected function define_my_settings() {
// No particular settings for this activity
}
/**
* Define (add) particular steps this activity can have
*
* @return void
*/
protected function define_my_steps() {
// book only has one structure step
$this->add_step(new backup_book_activity_structure_step('book_structure', 'book.xml'));
}
/**
* Code the transformations to perform in the activity in
* order to get transportable (encoded) links
*
* @param string $content
* @return string encoded content
*/
static public function encode_content_links($content) {
global $CFG;
$base = preg_quote($CFG->wwwroot, "/");
// Link to the list of books
$search = "/($base\/mod\/book\/index.php\?id=)([0-9]+)/";
$content = preg_replace($search, '$@BOOKINDEX*$2@$', $content);
// Link to book view by moduleid
$search = "/($base\/mod\/book\/view.php\?id=)([0-9]+)(&|&amp;)chapterid=([0-9]+)/";
$content = preg_replace($search, '$@BOOKVIEWBYIDCH*$2*$4@$', $content);
$search = "/($base\/mod\/book\/view.php\?id=)([0-9]+)/";
$content = preg_replace($search, '$@BOOKVIEWBYID*$2@$', $content);
// Link to book view by bookid
$search = "/($base\/mod\/book\/view.php\?b=)([0-9]+)(&|&amp;)chapterid=([0-9]+)/";
$content = preg_replace($search, '$@BOOKVIEWBYBCH*$2*$4@$', $content);
$search = "/($base\/mod\/book\/view.php\?b=)([0-9]+)/";
$content = preg_replace($search, '$@BOOKVIEWBYB*$2@$', $content);
return $content;
}
}
<?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/>.
/**
* Description of book backup settings
*
* @package mod
* @subpackage book
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
// This activity has not particular settings but the inherited from the generic
// backup_activity_task so here there isn't any class definition, like the ones
// existing in /backup/moodle2/backup_settingslib.php (activities section)
<?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/>.
/**
* Define all the backup steps that will be used by the backup_book_activity_task
*
* @package mod
* @subpackage book
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Define the complete book structure for backup, with file and id annotations
*/
class backup_book_activity_structure_step extends backup_activity_structure_step {
protected function define_structure() {
// Define each element separated
$book = new backup_nested_element('book', array('id'), array('name', 'intro', 'introformat', 'numbering', 'customtitles', 'timecreated', 'timemodified'));
$chapters = new backup_nested_element('chapters');
$chapter = new backup_nested_element('chapter', array('id'), array('pagenum', 'subchapter', 'title', 'content', 'contentformat', 'hidden', 'timemcreated', 'timemodified', 'importsrc',));
$book->add_child($chapters);
$chapters->add_child($chapter);
// Define sources
$book->set_source_table('book', array('id' => backup::VAR_ACTIVITYID));
$chapter->set_source_table('book_chapters', array('bookid' => backup::VAR_PARENTID));
// Define file annotations
$book->annotate_files('mod_book', 'intro', null); // This file area hasn't itemid
$chapter->annotate_files('mod_book', 'chapter', 'id');
// Return the root element (book), wrapped into standard activity structure
return $this->prepare_activity_structure($book);
}
}
<?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/>.
/**
* Define all the restore tasks
*
* @package mod
* @subpackage book
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/book/backup/moodle2/restore_book_stepslib.php'); // Because it exists (must)
/**
* book restore task that provides all the settings and steps to perform one
* complete restore of the activity
*/
class restore_book_activity_task extends restore_activity_task {
/**
* Define (add) particular settings this activity can have
*
* @return void
*/
protected function define_my_settings() {
// No particular settings for this activity
}
/**
* Define (add) particular steps this activity can have
*
* @return void
*/
protected function define_my_steps() {
// Choice only has one structure step
$this->add_step(new restore_book_activity_structure_step('book_structure', 'book.xml'));
}
/**
* Define the contents in the activity that must be
* processed by the link decoder
*
* @return array
*/
static public function define_decode_contents() {
$contents = array();
$contents[] = new restore_decode_content('book', array('intro'), 'book');
$contents[] = new restore_decode_content('book_chapters', array('content'), 'book_chapter');
return $contents;
}
/**
* Define the decoding rules for links belonging
* to the activity to be executed by the link decoder
*
* @return array
*/
static public function define_decode_rules() {
$rules = array();
// List of books in course
$rules[] = new restore_decode_rule('BOOKINDEX', '/mod/book/index.php?id=$1', 'course');
// book by cm->id
$rules[] = new restore_decode_rule('BOOKVIEWBYID', '/mod/book/view.php?id=$1', 'course_module');
$rules[] = new restore_decode_rule('BOOKVIEWBYIDCH', '/mod/book/view.php?id=$1&amp;chapterid=$2', array('course_module', 'book_chapter'));
// book by book->id
$rules[] = new restore_decode_rule('BOOKVIEWBYB', '/mod/book/view.php?b=$1', 'book');
$rules[] = new restore_decode_rule('BOOKVIEWBYBCH', '/mod/book/view.php?b=$1&amp;chapterid=$2', array('book', 'book_chapter'));
return $rules;
}
/**
* Define the restore log rules that will be applied
* by the {@link restore_logs_processor} when restoring
* book logs. It must return one array
* of {@link restore_log_rule} objects
*
* @return array
*/
static public function define_restore_log_rules() {
$rules = array();
$rules[] = new restore_log_rule('book', 'update', 'view.php?id={course_module}', '{book}');
$rules[] = new restore_log_rule('book', 'view', 'view.php?id={course_module}', '{book}');
$rules[] = new restore_log_rule('book', 'view all', 'view.php?id={course_module}', '{book}');
$rules[] = new restore_log_rule('book', 'print', 'view.php?id={course_module}', '{book}');
return $rules;
}
/**
* Define the restore log rules that will be applied
* by the {@link restore_logs_processor} when restoring
* course logs. It must return one array
* of {@link restore_log_rule} objects
*
* Note this rules are applied when restoring course logs
* by the restore final task, but are defined here at
* activity level. All them are rules not linked to any module instance (cmid = 0)
*
* @return array
*/
static public function define_restore_log_rules_for_course() {
$rules = array();
return $rules;
}
}
<?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/>.
/**
* Define all the restore steps that will be used by the restore_book_activity_task
*
* @package mod
* @subpackage book
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Structure step to restore one book activity
*/
class restore_book_activity_structure_step extends restore_activity_structure_step {
protected function define_structure() {
$paths = array();
$paths[] = new restore_path_element('book', '/activity/book');
$paths[] = new restore_path_element('book_chapter', '/activity/book/chapters/chapter');
// Return the paths wrapped into standard activity structure
return $this->prepare_activity_structure($paths);
}
protected function process_book($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->course = $this->get_courseid();
$newitemid = $DB->insert_record('book', $data);
$this->apply_activity_instance($newitemid);
}
protected function process_book_chapter($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->course = $this->get_courseid();
$data->bookid = $this->get_new_parentid('book');
$newitemid = $DB->insert_record('book_chapters', $data);
$this->set_mapping('book_chapter', $oldid, $newitemid, true);
}
protected function after_execute() {
global $DB;
// Add book related files
$this->add_related_files('mod_book', 'intro', null);
$this->add_related_files('mod_book', 'chapter', 'book_chapter');