Merge branch 'MDL-48717-master' of git://github.com/jleyva/moodle

This commit is contained in:
Dan Poltawski 2015-03-16 15:28:50 +00:00
commit 448a80c6e9
2 changed files with 243 additions and 13 deletions

View File

@ -425,21 +425,57 @@ function book_pluginfile($course, $cm, $context, $filearea, $args, $forcedownloa
return false;
}
$fs = get_file_storage();
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_book/chapter/$chid/$relativepath";
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// Download the contents of a chapter as an html file.
if ($args[0] == 'index.html') {
$filename = "index.html";
// Nasty hack because we do not have file revisions in book yet.
$lifetime = $CFG->filelifetime;
if ($lifetime > 60*10) {
$lifetime = 60*10;
}
// Remove @@PLUGINFILE@@/.
$content = str_replace('@@PLUGINFILE@@/', '', $chapter->content);
// finally send the file
send_stored_file($file, $lifetime, 0, $forcedownload, $options);
$titles = "";
// Format the chapter titles.
if (!$book->customtitles) {
require_once(dirname(__FILE__).'/locallib.php');
$chapters = book_preload_chapters($book);
if (!$chapter->subchapter) {
$currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
// Note that we can't use the $OUTPUT->heading() in WS_SERVER mode.
$titles = "<h3>$currtitle</h3>";
} else {
$currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
$currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
// Note that we can't use the $OUTPUT->heading() in WS_SERVER mode.
$titles = "<h3>$currtitle</h3>";
$titles .= "<h4>$currsubtitle</h4>";
}
}
$formatoptions = new stdClass;
$formatoptions->noclean = true;
$formatoptions->overflowdiv = true;
$formatoptions->context = $context;
$content = $titles . format_text($content, $chapter->contentformat, $formatoptions);
send_file($content, $filename, 0, 0, true, true);
} else {
$fs = get_file_storage();
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_book/chapter/$chid/$relativepath";
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// Nasty hack because we do not have file revisions in book yet.
$lifetime = $CFG->filelifetime;
if ($lifetime > 60 * 10) {
$lifetime = 60 * 10;
}
// Finally send the file.
send_stored_file($file, $lifetime, 0, $forcedownload, $options);
}
}
/**
@ -454,3 +490,111 @@ function book_page_type_list($pagetype, $parentcontext, $currentcontext) {
$module_pagetype = array('mod-book-*'=>get_string('page-mod-book-x', 'mod_book'));
return $module_pagetype;
}
/**
* Export book resource contents
*
* @param stdClass $cm Course module object
* @param string $baseurl Base URL for file downloads
* @return array of file content
*/
function book_export_contents($cm, $baseurl) {
global $DB;
$contents = array();
$context = context_module::instance($cm->id);
$book = $DB->get_record('book', array('id' => $cm->instance), '*', MUST_EXIST);
$fs = get_file_storage();
$chapters = $DB->get_records('book_chapters', array('bookid' => $book->id), 'pagenum');
$structure = array();
$currentchapter = 0;
foreach ($chapters as $chapter) {
if ($chapter->hidden) {
continue;
}
// Generate the book structure.
$thischapter = array(
"title" => format_string($chapter->title, true, array('context' => $context)),
"href" => $chapter->id . "/index.html",
"level" => 0,
"subitems" => array()
);
// Main chapter.
if (!$chapter->subchapter) {
$currentchapter = $chapter->pagenum;
$structure[$currentchapter] = $thischapter;
} else {
// Subchapter.
$thischapter['level'] = 1;
$structure[$currentchapter]["subitems"][] = $thischapter;
}
// Export the chapter contents.
// Main content (html).
$filename = 'index.html';
$chapterindexfile = array();
$chapterindexfile['type'] = 'file';
$chapterindexfile['filename'] = $filename;
// Each chapter in a subdirectory.
$chapterindexfile['filepath'] = "/{$chapter->id}/";
$chapterindexfile['filesize'] = 0;
$chapterindexfile['fileurl'] = moodle_url::make_webservice_pluginfile_url(
$context->id, 'mod_book', 'chapter', $chapter->id, '/', 'index.html')->out(false);
$chapterindexfile['timecreated'] = $book->timecreated;
$chapterindexfile['timemodified'] = $book->timemodified;
$chapterindexfile['content'] = format_string($chapter->title, true, array('context' => $context));
$chapterindexfile['sortorder'] = 0;
$chapterindexfile['userid'] = null;
$chapterindexfile['author'] = null;
$chapterindexfile['license'] = null;
$contents[] = $chapterindexfile;
// Chapter files (images usually).
$files = $fs->get_area_files($context->id, 'mod_book', 'chapter', $chapter->id, 'sortorder DESC, id ASC', false);
foreach ($files as $fileinfo) {
$file = array();
$file['type'] = 'file';
$file['filename'] = $fileinfo->get_filename();
$file['filepath'] = "/{$chapter->id}/";
$file['filesize'] = $fileinfo->get_filesize();
$file['fileurl'] = moodle_url::make_webservice_pluginfile_url(
$context->id, 'mod_book', 'chapter', $chapter->id,
$fileinfo->get_filepath(), $fileinfo->get_filename())->out(false);
$file['timecreated'] = $fileinfo->get_timecreated();
$file['timemodified'] = $fileinfo->get_timemodified();
$file['sortorder'] = $fileinfo->get_sortorder();
$file['userid'] = $fileinfo->get_userid();
$file['author'] = $fileinfo->get_author();
$file['license'] = $fileinfo->get_license();
$contents[] = $file;
}
}
// First content is the structure in encoded JSON format.
$structurefile = array();
$structurefile['type'] = 'content';
$structurefile['filename'] = 'structure';
$structurefile['filepath'] = "/";
$structurefile['filesize'] = 0;
$structurefile['fileurl'] = null;
$structurefile['timecreated'] = $book->timecreated;
$structurefile['timemodified'] = $book->timemodified;
$structurefile['content'] = json_encode(array_values($structure));
$structurefile['sortorder'] = 0;
$structurefile['userid'] = null;
$structurefile['author'] = null;
$structurefile['license'] = null;
// Add it as first element.
array_unshift($contents, $structurefile);
return $contents;
}

View File

@ -0,0 +1,86 @@
<?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/>.
/**
* Unit tests for (some of) mod/book/lib.php.
*
* @package mod_book
* @category phpunit
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/book/lib.php');
/**
* Unit tests for (some of) mod/book/lib.php.
*
* @package mod_book
* @category phpunit
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_book_lib_testcase extends advanced_testcase {
public function test_export_contents() {
global $DB;
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
// Test book with 3 chapters.
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
$cm = get_coursemodule_from_id('book', $book->cmid);
$bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
$chapter1 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 1));
$chapter2 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 2));
$subchapter = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 3, "subchapter" => 1));
$chapter3 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 4, "hidden" => 1));
$this->setUser($user);
$contents = book_export_contents($cm, '');
// The hidden chapter must not be included, and additional page with the structure must be included.
$this->assertCount(4, $contents);
$this->assertEquals('structure', $contents[0]['filename']);
$this->assertEquals('index.html', $contents[1]['filename']);
$this->assertEquals('Chapter 1', $contents[1]['content']);
$this->assertEquals('index.html', $contents[2]['filename']);
$this->assertEquals('Chapter 2', $contents[2]['content']);
$this->assertEquals('index.html', $contents[3]['filename']);
$this->assertEquals('Chapter 3', $contents[3]['content']);
// Test empty book.
$emptybook = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
$cm = get_coursemodule_from_id('book', $emptybook->cmid);
$contents = book_export_contents($cm, '');
$this->assertCount(1, $contents);
$this->assertEquals('structure', $contents[0]['filename']);
$this->assertEquals(json_encode(array()), $contents[0]['content']);
}
}