Commit f4e1849e authored by Petr Skoda's avatar Petr Skoda
Browse files

new file api support

parent 6708d7d4
......@@ -47,28 +47,24 @@ require_capability('mod/book:edit', $context);
$PAGE->set_url('/mod/book/edit.php', array('cmid'=>$cmid, 'id'=>$chapterid, 'pagenum'=>$pagenum, 'subchapter'=>$subchapter));
if ($chapterid) {
$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
} else {
$chapter = null;
}
//check all variables
unset($cmid);
unset($chapterid);
// =========================================================================
// security checks END
// =========================================================================
$mform = new book_chapter_edit_form(null, $cm);
if ($chapter) {
$chapter->cmid = $cm->id;
$mform->set_data($chapter);
if ($chapterid) {
$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
} else {
$mform->set_data(array('cmid'=>$cm->id, 'pagenum'=>($pagenum+1), 'subchapter'=>$subchapter));
$chapter = new stdClass();
$chapter->id = null;
$chapter->subchapter = $subchapter;
$chapter->pagenum = $pagenum + 1;
}
$chapter->cmid = $cm->id;
$options = array('noclean'=>true, 'subdirs'=>true, 'maxfiles'=>-1, 'maxbytes'=>0, 'context'=>$context);
$chapter = file_prepare_standard_editor($chapter, 'content', $options, $context, 'mod_book', 'chapter', $chapter->id);
$mform = new book_chapter_edit_form(null, array('chapter'=>$chapter, 'options'=>$options));
/// If data submitted, then process and store.
if ($mform->is_cancelled()) {
......@@ -81,6 +77,8 @@ if ($mform->is_cancelled()) {
} else if ($data = $mform->get_data()) {
if ($data->id) {
// store the files
$data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id);
$DB->update_record('book_chapters', $data);
add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
......@@ -88,11 +86,13 @@ if ($mform->is_cancelled()) {
} else {
/// adding new chapter
$data->bookid = $book->id;
$data->hidden = 0;
$data->timecreated = time();
$data->timemodified = time();
$data->importsrc = '';
$data->bookid = $book->id;
$data->hidden = 0;
$data->timecreated = time();
$data->timemodified = time();
$data->importsrc = '';
$data->content = ''; // updated later
$data->contentformat = FORMAT_HTML; // updated later
// make room for new page
$sql = "UPDATE {book_chapters}
......@@ -102,6 +102,10 @@ if ($mform->is_cancelled()) {
$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);
add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id.'&chapterid='.$data->id, $book->id, $cm->id);
}
......
......@@ -31,8 +31,12 @@ class book_chapter_edit_form extends moodleform {
function definition() {
global $CFG;
$chapter = $this->_customdata['chapter'];
$options = $this->_customdata['options'];
$mform = $this->_form;
$cm = $this->_customdata;
$context = $options['context'];
$mform->addElement('header', 'general', get_string('edit'));
......@@ -42,8 +46,15 @@ class book_chapter_edit_form extends moodleform {
$mform->addElement('advcheckbox', 'subchapter', get_string('subchapter', 'book'));
$mform->addElement('htmleditor', 'content', get_string('content', 'book'), array('cols'=>50, 'rows'=>30));
$mform->setType('content', PARAM_RAW);
$mform->addElement('editor', 'content_editor', get_string('content', 'book'), null, $options);
$mform->setType('content_editor', PARAM_RAW);
$mform->addRule('content_editor', get_string('required'), 'required', null, 'client');
if ($chapter->id and has_capability('mod/book:import', $context)) {
//TODO: after files
//$mform->addElement('static', 'doimport', get_string('importingchapters', 'book').':', '<a href="import.php?id='.$chapter->cmid.'">'.get_string('doimport', 'book').'</a>');
}
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
......@@ -54,22 +65,10 @@ class book_chapter_edit_form extends moodleform {
$mform->addElement('hidden', 'pagenum');
$mform->setType('pagenum', PARAM_INT);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if (false and has_capability('mod/book:import', $context)) { //TODO: after files
$mform->addElement('static', 'doimport', get_string('importingchapters', 'book').':', '<a href="import.php?id='.$cm->id.'">'.get_string('doimport', 'book').'</a>');
}
$this->add_action_buttons(true);
}
function definition_after_data() {
global $CFG;
$mform = $this->_form;
if ($mform->getElementValue('id')) {
if ($mform->elementExists('doimport')) {
$mform->removeElement('doimport');
}
}
// set the defaults
$this->set_data($chapter);
}
}
......@@ -152,7 +152,7 @@ function book_get_participants($bookid) {
/**
* This function returns if a scale is being used by one book
* it it has support for grading and scales. Commented code should be
* modified if necessary. See forum, glossary or journal modules
* modified if necessary. See book, glossary or journal modules
* as reference.
*
* @param $bookid int
......@@ -206,7 +206,7 @@ function book_supports($feature) {
* Adds module specific settings to the settings block
*
* @param settings_navigation $settings The settings navigation object
* @param navigation_node $forumnode The node to add module settings to
* @param navigation_node $booknode The node to add module settings to
* @return void
*/
function book_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $booknode) {
......@@ -240,4 +240,57 @@ function book_extend_settings_navigation(settings_navigation $settingsnav, navig
$url = new moodle_url('/mod/book/view.php', array('id'=>$params['id'], 'chapterid'=>$params['chapterid'], 'edit'=>$edit, 'sesskey'=>sesskey()));
$booknode->add($string, $url, navigation_node::TYPE_SETTING);
}
/**
* Serves the book attachments. Implements needed access control ;-)
*
* @param object $course
* @param object $cm
* @param object $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return bool false if file not found, does not return if found - justsend the file
*/
function book_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
global $CFG, $DB;
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
require_course_login($course, true, $cm);
if ($filearea !== 'chapter') {
return false;
}
if (!has_capability('mod/book:read', $context)) {
return false;
}
$chid = (int)array_shift($args);
if (!$book = $DB->get_record('book', array('id'=>$cm->instance))) {
return false;
}
if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chid, 'bookid'=>$book->id))) {
return false;
}
if ($chapter->hidden and !has_capability('mod/book:viewhiddenchapters', $context)) {
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;
}
// finally send the file
send_stored_file($file, 360, 0, false);
}
\ No newline at end of file
......@@ -31,6 +31,7 @@ define('BOOK_NUM_BULLETS', '2');
define('BOOK_NUM_INDENTED', '3');
require_once($CFG->dirroot.'/mod/book/lib.php');
require_once($CFG->libdir.'/filelib.php');
function book_get_numbering_types() {
return array (BOOK_NUM_NONE => get_string('numbering0', 'mod_book'),
......
......@@ -50,7 +50,7 @@ if ($book->disableprinting) {
//check all variables
if ($chapterid) {
//single chapter printing - only visible!
$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id, 'visible'=>1), '*', MUST_EXIST);
$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id, 'hidden'=>0), '*', MUST_EXIST);
} else {
//complete book
$chapter = false;
......@@ -74,7 +74,7 @@ $strtop = get_string('top', 'book');
if ($chapter) {
add_to_log($course->id, 'book', 'print', 'print.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
$chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id, 'visible'=>1), 'pagenum, title');
$chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id, 'hidden'=>0), 'pagenum, title');
$print = 0;
$edit = 0;
......@@ -102,7 +102,8 @@ if ($chapter) {
echo '<p class="book_chapter_title">'.$currtitle.'<br />'.$currsubtitle.'</p>';
}
}
echo format_text($chapter->content, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
$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>';
echo '</body> </html>';
......@@ -151,18 +152,16 @@ if ($chapter) {
$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) {
if (!$ch->hidden) {
echo '<div class="book_chapter"><a name="ch'.$ch->id.'"></a>';
if (!$book->customtitles) {
echo '<p class="book_chapter_title">'.$titles[$ch->id].'</p>';
}
$content = str_replace($link1, '#ch', $ch->content);
$content = str_replace($link2, '#top', $content);
echo format_text($content, $ch->contentformat, array('noclean'=>true, 'context'=>$context));
echo '</div>';
//echo '<a href="#toc">'.$strtop.'</a>';
echo '<div class="book_chapter"><a name="ch'.$ch->id.'"></a>';
if (!$book->customtitles) {
echo '<p class="book_chapter_title">'.$titles[$ch->id].'</p>';
}
$content = str_replace($link1, '#ch', $ch->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, $ch->contentformat, array('noclean'=>true, 'context'=>$context));
echo '</div>';
//echo '<a href="#toc">'.$strtop.'</a>';
}
echo '</body> </html>';
}
......
......@@ -48,8 +48,6 @@ $allowprint = has_capability('mod/book:print', $context) and !$book->disablepri
$allowexport = has_capability('mod/book:exportimscp', $context);
$viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
$PAGE->set_url('/mod/book/view.php', array('id'=>$id, 'chapterid'=>$chapterid));
if ($allowedit) {
if ($edit != -1 and confirm_sesskey()) {
$USER->editing = $edit;
......@@ -90,6 +88,7 @@ if ($chapterid == '0') { // go to first chapter if no given
}
}
$PAGE->set_url('/mod/book/view.php', array('id'=>$id, 'chapterid'=>$chapterid));
if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id))) {
error('Error reading book chapters.');
......@@ -230,7 +229,8 @@ $generateimscp = ''; //TODO after new file handling
echo '<p class="book_chapter_title">'.$currtitle.'<br />'.$currsubtitle.'</p>';
}
}
echo format_text($chapter->content, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
$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>';
echo $OUTPUT->box_end();
/// lower navigation
......@@ -241,5 +241,6 @@ $generateimscp = ''; //TODO after new file handling
</table>
<?php
echo $OUTPUT->footer();
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment