.
/**
* Book imscp export lib
*
* @package booktool_exportimscp
* @copyright 2001-3001 Antonio Vicent {@link http://ludens.es}
* @copyright 2001-3001 Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @copyright 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(dirname(__FILE__).'/lib.php');
require_once($CFG->dirroot.'/mod/book/locallib.php');
/**
* Export one book as IMSCP package
*
* @param stdClass $book book instance
* @param context_module $context
* @return bool|stored_file
*/
function booktool_exportimscp_build_package($book, $context) {
global $DB;
$fs = get_file_storage();
if ($packagefile = $fs->get_file($context->id, 'booktool_exportimscp', 'package', $book->revision, '/', 'imscp.zip')) {
return $packagefile;
}
// fix structure and test if chapters present
if (!book_preload_chapters($book)) {
print_error('nochapters', 'booktool_exportimscp');
}
// prepare temp area with package contents
booktool_exportimscp_prepare_files($book, $context);
$packer = get_file_packer('application/zip');
$areafiles = $fs->get_area_files($context->id, 'booktool_exportimscp', 'temp', $book->revision, "sortorder, itemid, filepath, filename", false);
$files = array();
foreach ($areafiles as $file) {
$path = $file->get_filepath().$file->get_filename();
$path = ltrim($path, '/');
$files[$path] = $file;
}
unset($areafiles);
$packagefile = $packer->archive_to_storage($files, $context->id, 'booktool_exportimscp', 'package', $book->revision, '/', 'imscp.zip');
// drop temp area
$fs->delete_area_files($context->id, 'booktool_exportimscp', 'temp', $book->revision);
// delete older versions
$sql = "SELECT DISTINCT itemid
FROM {files}
WHERE contextid = :contextid AND component = 'booktool_exportimscp' AND itemid < :revision";
$params = array('contextid'=>$context->id, 'revision'=>$book->revision);
$revisions = $DB->get_records_sql($sql, $params);
foreach ($revisions as $rev => $unused) {
$fs->delete_area_files($context->id, 'booktool_exportimscp', 'temp', $rev);
$fs->delete_area_files($context->id, 'booktool_exportimscp', 'package', $rev);
}
return $packagefile;
}
/**
* Prepare temp area with the files used by book html contents
*
* @param stdClass $book book instance
* @param context_module $context
*/
function booktool_exportimscp_prepare_files($book, $context) {
global $CFG, $DB;
$fs = get_file_storage();
$temp_file_record = array('contextid'=>$context->id, 'component'=>'booktool_exportimscp', 'filearea'=>'temp', 'itemid'=>$book->revision);
$chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum');
$chapterresources = array();
foreach ($chapters as $chapter) {
$chapterresources[$chapter->id] = array();
$files = $fs->get_area_files($context->id, 'mod_book', 'chapter', $chapter->id, "sortorder, itemid, filepath, filename", false);
foreach ($files as $file) {
$temp_file_record['filepath'] = '/'.$chapter->pagenum.$file->get_filepath();
$fs->create_file_from_storedfile($temp_file_record, $file);
$chapterresources[$chapter->id][] = $chapter->pagenum.$file->get_filepath().$file->get_filename();
}
if ($file = $fs->get_file($context->id, 'booktool_exportimscp', 'temp', $book->revision, "/$chapter->pagenum/", 'index.html')) {
// this should not exist
$file->delete();
}
$content = booktool_exportimscp_chapter_content($chapter, $context);
$index_file_record = array('contextid'=>$context->id, 'component'=>'booktool_exportimscp', 'filearea'=>'temp',
'itemid'=>$book->revision, 'filepath'=>"/$chapter->pagenum/", 'filename'=>'index.html');
$fs->create_file_from_string($index_file_record, $content);
}
$css_file_record = array('contextid'=>$context->id, 'component'=>'booktool_exportimscp', 'filearea'=>'temp',
'itemid'=>$book->revision, 'filepath'=>"/css/", 'filename'=>'styles.css');
$fs->create_file_from_pathname($css_file_record, dirname(__FILE__).'/imscp.css');
// Init imsmanifest and others
$imsmanifest = '';
$imsitems = '';
$imsresources = '';
// Moodle and Book version
$moodle_release = $CFG->release;
$moodle_version = $CFG->version;
$book_version = get_config('mod_book', 'version');
$bookname = format_string($book->name, true, array('context'=>$context));
// Load manifest header
$imsmanifest .= '