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

add file browser support

closes #14
parent 920fd2c6
......@@ -63,6 +63,7 @@ page.';
$string['customtitles'] = 'Custom titles';
$string['customtitles_help'] = 'Chapter titles are displayed automatically only in TOC.';
$string['chapters'] = 'Chapters';
$string['editingchapter'] = 'Editing chapter';
$string['chaptertitle'] = 'Chapter title';
$string['content'] = 'Content';
......
......@@ -339,6 +339,69 @@ function book_extend_settings_navigation(settings_navigation $settingsnav, navig
}
}
/**
* Lists all browsable file areas
* @param object $course
* @param object $cm
* @param object $context
* @return array
*/
function book_get_file_areas($course, $cm, $context) {
$areas = array();
$areas['chapter'] = get_string('chapters', 'mod_book');
return $areas;
}
/**
* File browsing support for book module ontent area.
* @param object $browser
* @param object $areas
* @param object $course
* @param object $cm
* @param object $context
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return object file_info instance or null if not found
*/
function book_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
global $CFG, $DB;
// note: book_intro handled in file_browser automatically
if (!has_capability('mod/book:read', $context)) {
return null;
}
if ($filearea !== 'chapter') {
return null;
}
require_once("$CFG->dirroot/mod/book/locallib.php");
if (is_null($itemid)) {
return new book_file_info($browser, $course, $cm, $context, $areas, $filearea, $itemid);
}
$fs = get_file_storage();
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
if (!$storedfile = $fs->get_file($context->id, 'mod_book', $filearea, $itemid, $filepath, $filename)) {
return null;
}
// modifications may be tricky - may cause caching problems
$canwrite = has_capability('mod/book:edit', $context);
$chaptername = $DB->get_field('book_chapters', 'title', array('bookid'=>$cm->instance, 'id'=>$itemid));
$chaptername = format_string($chaptername, true, array('context'=>$context));
$urlbase = $CFG->wwwroot.'/pluginfile.php';
return new file_info_stored($browser, $context, $storedfile, $urlbase, $chaptername, true, true, $canwrite, false);
}
/**
* Serves the book attachments. Implements needed access control ;-)
*
......
......@@ -319,3 +319,86 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
return $toc;
}
/**
* File browsing support class
*/
class book_file_info extends file_info {
protected $course;
protected $cm;
protected $areas;
protected $filearea;
public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
parent::__construct($browser, $context);
$this->course = $course;
$this->cm = $cm;
$this->areas = $areas;
$this->filearea = $filearea;
}
/**
* Returns list of standard virtual file/directory identification.
* The difference from stored_file parameters is that null values
* are allowed in all fields
* @return array with keys contextid, filearea, itemid, filepath and filename
*/
public function get_params() {
return array('contextid'=>$this->context->id,
'component'=>'mod_book',
'filearea' =>$this->filearea,
'itemid' =>null,
'filepath' =>null,
'filename' =>null);
}
/**
* Returns localised visible name.
* @return string
*/
public function get_visible_name() {
return $this->areas[$this->filearea];
}
/**
* Can I add new files or directories?
* @return bool
*/
public function is_writable() {
return false;
}
/**
* Is directory?
* @return bool
*/
public function is_directory() {
return true;
}
/**
* Returns list of children.
* @return array of file_info instances
*/
public function get_children() {
global $DB;
$children = array();
$chapters = $DB->get_records('book_chapters', array('bookid'=>$this->cm->instance), 'pagenum', 'id, pagenum');
foreach ($chapters as $itemid=>$unused) {
if ($child = $this->browser->get_file_info($this->context, 'mod_book', $this->filearea, $itemid)) {
$children[] = $child;
}
}
return $children;
}
/**
* Returns parent file_info instance
* @return file_info or null for root
*/
public function get_parent() {
return $this->browser->get_file_info($this->context);
}
}
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