/** * 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\n FROM {files}\n 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; }
/** * Simulate the book/view.php web interface page: trigger events, completion, etc... * * @param int $bookid the book instance id * @param int $chapterid the book chapter id * @return array of warnings and status result * @since Moodle 3.0 * @throws moodle_exception */ public static function view_book($bookid, $chapterid = 0) { global $DB, $CFG; require_once $CFG->dirroot . "/mod/book/lib.php"; require_once $CFG->dirroot . "/mod/book/locallib.php"; $params = self::validate_parameters(self::view_book_parameters(), array('bookid' => $bookid, 'chapterid' => $chapterid)); $bookid = $params['bookid']; $chapterid = $params['chapterid']; $warnings = array(); // Request and permission validation. $book = $DB->get_record('book', array('id' => $bookid), '*', MUST_EXIST); list($course, $cm) = get_course_and_cm_from_instance($book, 'book'); $context = context_module::instance($cm->id); self::validate_context($context); require_capability('mod/book:read', $context); $chapters = book_preload_chapters($book); $firstchapterid = 0; $lastchapterid = 0; foreach ($chapters as $ch) { if ($ch->hidden) { continue; } if (!$firstchapterid) { $firstchapterid = $ch->id; } $lastchapterid = $ch->id; } if (!$chapterid) { // Trigger the module viewed events since we are displaying the book. book_view($book, null, false, $course, $cm, $context); $chapterid = $firstchapterid; } // Check if book is empty (warning). if (!$chapterid) { $warnings[] = array('item' => 'book', 'itemid' => $book->id, 'warningcode' => '1', 'message' => get_string('nocontent', 'mod_book')); } else { $chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id)); $viewhidden = has_capability('mod/book:viewhiddenchapters', $context); if (!$chapter or $chapter->hidden and !$viewhidden) { throw new moodle_exception('errorchapter', 'mod_book'); } // Trigger the chapter viewed event. $islastchapter = $chapter->id == $lastchapterid ? true : false; book_view($book, $chapter, $islastchapter, $course, $cm, $context); } $result = array(); $result['status'] = true; $result['warnings'] = $warnings; return $result; }
// Switch hidden state. $chapter->hidden = $chapter->hidden ? 0 : 1; // Update record. $DB->update_record('book_chapters', $chapter); // Change visibility of subchapters too. if (!$chapter->subchapter) { $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, subchapter, hidden'); $found = 0; foreach ($chapters as $ch) { if ($ch->id == $chapter->id) { $found = 1; } else if ($found and $ch->subchapter) { $ch->hidden = $chapter->hidden; $DB->update_record('book_chapters', $ch); } else if ($found) { break; } } } 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, $book->id, $cm->id); book_preload_chapters($book); // fix structure $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id)); redirect('view.php?id='.$cm->id.'&chapterid='.$chapter->id);
$fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id); $DB->delete_records('book_chapters', array('id' => $ch->id)); \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $ch)->trigger(); } else { if ($found) { break; } } } } $chapters->close(); } $fs->delete_area_files($context->id, 'mod_book', 'chapter', $chapter->id); $DB->delete_records('book_chapters', array('id' => $chapter->id)); \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter)->trigger(); book_preload_chapters($book); // Fix structure. $DB->set_field('book', 'revision', $book->revision + 1, array('id' => $book->id)); redirect('view.php?id=' . $cm->id); } echo $OUTPUT->header(); echo $OUTPUT->heading($book->name); // The operation has not been confirmed yet so ask the user to do so. if ($chapter->subchapter) { $strconfirm = get_string('confchapterdelete', 'mod_book'); } else { $strconfirm = get_string('confchapterdeleteall', 'mod_book'); } echo '<br />'; $continue = new moodle_url('/mod/book/delete.php', array('id' => $cm->id, 'chapterid' => $chapter->id, 'confirm' => 1)); $cancel = new moodle_url('/mod/book/view.php', array('id' => $cm->id, 'chapterid' => $chapter->id));
/** * Serves the book attachments. Implements needed access control ;-) * * @param stdClass $course course object * @param cm_info $cm course module object * @param context $context context object * @param string $filearea file area * @param array $args extra arguments * @param bool $forcedownload whether or not force download * @param array $options additional options affecting the file serving * @return bool false if file not found, does not return if found - just send the file */ function book_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) { 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; } // Download the contents of a chapter as an html file. if ($args[0] == 'index.html') { $filename = "index.html"; // We need to rewrite the pluginfile URLs so the media filters can work. $content = file_rewrite_pluginfile_urls($chapter->content, 'webservice/pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id); $formatoptions = new stdClass(); $formatoptions->noclean = true; $formatoptions->overflowdiv = true; $formatoptions->context = $context; $content = format_text($content, $chapter->contentformat, $formatoptions); // Remove @@PLUGINFILE@@/. $options = array('reverse' => true); $content = file_rewrite_pluginfile_urls($content, 'webservice/pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id, $options); $content = str_replace('@@PLUGINFILE@@/', '', $content); $titles = ""; // Format the chapter titles. if (!$book->customtitles) { require_once __DIR__ . '/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>"; } } $content = $titles . $content; 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); } }
protected function mod_book_html($mod) { if (!$mod->uservisible) { return ""; } global $DB; $cm = get_coursemodule_from_id('book', $mod->id, 0, false, MUST_EXIST); $book = $DB->get_record('book', array('id' => $cm->instance), '*', MUST_EXIST); $chapters = book_preload_chapters($book); if ($book->intro) { $context = context_module::instance($mod->id); $content = file_rewrite_pluginfile_urls($book->intro, 'pluginfile.php', $context->id, 'mod_book', 'intro', null); $formatoptions = new stdClass(); $formatoptions->noclean = true; $formatoptions->overflowdiv = true; $formatoptions->context = $context; $content = format_text($content, $book->introformat, $formatoptions); return "<div class=summary-text>\n {$content}</div>" . $this->book_get_toc($chapters, $book, $cm); } return $this->book_get_toc($chapters, $book, $cm); }
$data->importsrc = ''; $data->content = ''; // updated later $data->contentformat = FORMAT_HTML; // updated later // make room for new page $sql = "UPDATE {book_chapters}\n SET pagenum = pagenum + 1\n WHERE bookid = ? AND pagenum >= ?"; $DB->execute($sql, array($book->id, $data->pagenum)); $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); $DB->set_field('book', 'revision', $book->revision + 1, array('id' => $book->id)); $chapter = $DB->get_record('book_chapters', array('id' => $data->id)); \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter)->trigger(); } book_preload_chapters($book); // fix structure redirect("view.php?id={$cm->id}&chapterid={$data->id}"); } } // Otherwise fill and print the form. $PAGE->set_title($book->name); $PAGE->set_heading($course->fullname); if ($chapters = book_preload_chapters($book)) { book_add_fake_block($chapters, $chapter, $book, $cm); } echo $OUTPUT->header(); echo $OUTPUT->heading($book->name); $mform->display(); echo $OUTPUT->footer();