Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
moodle
moodle
Commits
dd9b772b
Commit
dd9b772b
authored
Oct 08, 2011
by
Petr Skoda
Browse files
add file browser support
closes #14
parent
920fd2c6
Changes
3
Hide whitespace changes
Inline
Side-by-side
lang/en/book.php
View file @
dd9b772b
...
...
@@ -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'
;
...
...
lib.php
View file @
dd9b772b
...
...
@@ -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 ;-)
*
...
...
locallib.php
View file @
dd9b772b
...
...
@@ -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
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment