MDL-22217, added section_backup and activity_backup areas

This commit is contained in:
Dongsheng Cai 2010-05-03 05:19:38 +00:00
parent 8f87420a92
commit 91b890abb9
5 changed files with 186 additions and 4 deletions

View File

@ -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';

View File

@ -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;

View File

@ -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;

View File

@ -0,0 +1,106 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Utility class for browsing of course section files.
*
* @package moodlecore
* @subpackage file-browser
* @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
* @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);
}
}

View File

@ -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';