Skip to content

Commit

Permalink
MDL-22217, added section_backup and activity_backup areas
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongsheng Cai committed May 3, 2010
1 parent 8f87420 commit 91b890a
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lang/en/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
$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';
Expand Down
47 changes: 45 additions & 2 deletions lib/file/file_browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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");

Expand Down Expand Up @@ -317,7 +318,7 @@ private function get_file_info_course($context, $filearea=null, $itemid=null, $f
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;
}
Expand Down Expand Up @@ -422,6 +423,39 @@ private function get_file_info_course_backup($course, $context, $filearea=null,

}

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();

Expand Down Expand Up @@ -484,15 +518,24 @@ private function get_file_info_module($context, $filearea=null, $itemid=null, $f
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;
Expand Down
3 changes: 3 additions & 0 deletions lib/file/file_info_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public function get_children() {
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;
Expand Down
106 changes: 106 additions & 0 deletions lib/file/file_info_coursesectionbackup.php
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
* @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);
}
}

29 changes: 29 additions & 0 deletions pluginfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,22 @@
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);
Expand Down Expand Up @@ -499,6 +515,19 @@

// 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';
Expand Down

0 comments on commit 91b890a

Please sign in to comment.