From 91b890abb91e3136570015586d70e8e91d4fbcca Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Mon, 3 May 2010 05:19:38 +0000 Subject: [PATCH] MDL-22217, added section_backup and activity_backup areas --- lang/en/repository.php | 5 +- lib/file/file_browser.php | 47 ++++++++- lib/file/file_info_course.php | 3 + lib/file/file_info_coursesectionbackup.php | 106 +++++++++++++++++++++ pluginfile.php | 29 ++++++ 5 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 lib/file/file_info_coursesectionbackup.php diff --git a/lang/en/repository.php b/lang/en/repository.php index 584cfb4d53c..2b0a79d9950 100644 --- a/lang/en/repository.php +++ b/lang/en/repository.php @@ -30,8 +30,9 @@ $string['add'] = 'Add'; $string['addfile'] = 'Add...'; $string['addplugin'] = 'Add a repository plugin'; $string['allowexternallinks'] = 'Allow external links'; -$string['coursebackup'] = 'Course Backups'; -$string['sectionbackup'] = 'Section Backups'; +$string['coursebackup'] = 'Course backups'; +$string['sectionbackup'] = 'Section backups'; +$string['activitybackup'] = 'Activity backup'; $string['areacategoryintro'] = 'Category introduction'; $string['areacourseintro'] = 'Course introduction'; $string['arearoot'] = 'System'; diff --git a/lib/file/file_browser.php b/lib/file/file_browser.php index 61ad62889f6..f62fd1faed2 100644 --- a/lib/file/file_browser.php +++ b/lib/file/file_browser.php @@ -33,6 +33,7 @@ require_once("$CFG->libdir/file/file_info_user.php"); require_once("$CFG->libdir/file/file_info_coursecat.php"); require_once("$CFG->libdir/file/file_info_course.php"); require_once("$CFG->libdir/file/file_info_coursesection.php"); +require_once("$CFG->libdir/file/file_info_coursesectionbackup.php"); require_once("$CFG->libdir/file/file_info_coursefile.php"); require_once("$CFG->libdir/file/virtual_root_file.php"); @@ -317,7 +318,7 @@ class file_browser { return null; } - if (!is_null($filearea) and !in_array($filearea, array('course_intro', 'course_content', 'course_section', 'course_backup'))) { + if (!is_null($filearea) and !in_array($filearea, array('course_intro', 'course_content', 'course_section', 'course_backup', 'section_backup'))) { // file area does not exist, sorry $filearea = null; } @@ -422,6 +423,39 @@ class file_browser { } + private function get_file_info_section_backup($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { + global $CFG, $DB; + + $fs = get_file_storage(); + if (empty($itemid)) { + // list all sections + return new file_info_coursesectionbackup($this, $context, $course); + } + + if (!has_capability('moodle/backup:backupcourse', $context) and !has_capability('moodle/restore:restorecourse', $context)) { + return null; + } + + if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$itemid))) { + return null; // does not exist + } + + + $urlbase = $CFG->wwwroot.'/pluginfile.php'; + if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { + if ($filepath === '/' and $filename === '.') { + $storedfile = new virtual_root_file($context->id, $filearea, $itemid); + } else { + // not found + return null; + } + } + + $downloadable = has_capability('moodle/backup:downloadfile', $context); + $uploadable = has_capability('moodle/restore:uploadfile', $context); + return new file_info_stored($this, $context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false); + } + private function get_file_info_course_content($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) { $fs = get_file_storage(); @@ -484,15 +518,24 @@ class file_browser { and has_capability('moodle/course:managefiles', $context)) { $areas = array_merge(array($modname.'_intro'=>get_string('moduleintro')), $areas); } + + if (has_capability('moodle/backup:downloadfile', $context)) { + $areas = array_merge(array('activity_backup'=>get_string('activitybackup', 'repository')), $areas); + } + if (empty($areas)) { return null; } - if ($filearea === $modname.'_intro') { + if ($filearea === $modname.'_intro' || $filearea === 'activity_backup') { // always only itemid 0 if (!has_capability('moodle/course:managefiles', $context)) { return null; } + // need downloadfile cap when accessing activity_backup area + if ($filearea === 'activity_backup' && !has_capability('moodle/backup:downloadfile', $context)) { + return null; + } $filepath = is_null($filepath) ? '/' : $filepath; $filename = is_null($filename) ? '.' : $filename; diff --git a/lib/file/file_info_course.php b/lib/file/file_info_course.php index 00bd4918a58..4ec03929f9f 100644 --- a/lib/file/file_info_course.php +++ b/lib/file/file_info_course.php @@ -84,6 +84,9 @@ class file_info_course extends file_info { if ($child = $this->browser->get_file_info($this->context, 'course_section')) { $children[] = $child; } + if ($child = $this->browser->get_file_info($this->context, 'section_backup')) { + $children[] = $child; + } if ($child = $this->browser->get_file_info($this->context, 'course_backup', 0)) { $children[] = $child; diff --git a/lib/file/file_info_coursesectionbackup.php b/lib/file/file_info_coursesectionbackup.php new file mode 100644 index 00000000000..ae7664ab133 --- /dev/null +++ b/lib/file/file_info_coursesectionbackup.php @@ -0,0 +1,106 @@ +. + + +/** + * Utility class for browsing of course section files. + * + * @package moodlecore + * @subpackage file-browser + * @copyright 2010 Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + */ +class file_info_coursesectionbackup extends file_info { + protected $course; + + public function __construct($browser, $context, $course) { + parent::__construct($browser, $context); + $this->course = $course; + } + + /** + * 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, + 'filearea' =>'section_backup', + 'itemid' =>null, + 'filepath' =>null, + 'filename' =>null); + } + + /** + * Returns localised visible name. + * @return string + */ + public function get_visible_name() { + $format = $this->course->format; + $sectionsname = get_string('sectionbackup', 'repository'); + + return $sectionsname; + } + + /** + * 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(); + + $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section'); + foreach ($course_sections as $section) { + if ($child = $this->browser->get_file_info($this->context, 'section_backup', $section->id)) { + $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); + } +} + diff --git a/pluginfile.php b/pluginfile.php index a83f37f1bfa..3eb2e125be9 100644 --- a/pluginfile.php +++ b/pluginfile.php @@ -409,6 +409,22 @@ if (!empty($sendflashupgrader) && (($userplayerversion[0] < $requiredplayervers session_get_instance()->write_close(); // unlock session during fileserving send_stored_file($file, 60*60, 0, false); // TODO: change timeout? + } else if ($filearea === 'section_backup') { + require_login($course); + require_capability('moodle/backup:downloadfile', $context); + + $sectionid = (int)array_shift($args); + + $relativepath = '/'.implode('/', $args); + $fullpath = $context->id.'section_backup'.$sectionid.$relativepath; + + if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + send_file_not_found(); + } + + session_get_instance()->write_close(); + send_stored_file($file, 60*60, 0, false); + } else if ($filearea === 'user_profile') { $userid = (int)array_shift($args); $usercontext = get_context_instance(CONTEXT_USER, $userid); @@ -499,6 +515,19 @@ if (!empty($sendflashupgrader) && (($userplayerversion[0] < $requiredplayervers // finally send the file send_stored_file($file, $lifetime, 0); + } else if ($filearea === 'activity_backup') { + require_login($course); + require_capability('moodle/backup:downloadfile', $context); + + $relativepath = '/'.implode('/', $args); + $fullpath = $context->id.'activity_backup0'.$relativepath; + + if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + send_file_not_found(); + } + + session_get_instance()->write_close(); + send_stored_file($file, 60*60, 0, false); } $filefunction = $modname.'_pluginfile';