Skip to content

Commit 91b890a

Browse files
author
Dongsheng Cai
committed
MDL-22217, added section_backup and activity_backup areas
1 parent 8f87420 commit 91b890a

File tree

5 files changed

+186
-4
lines changed

5 files changed

+186
-4
lines changed

lang/en/repository.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
$string['addfile'] = 'Add...';
3131
$string['addplugin'] = 'Add a repository plugin';
3232
$string['allowexternallinks'] = 'Allow external links';
33-
$string['coursebackup'] = 'Course Backups';
34-
$string['sectionbackup'] = 'Section Backups';
33+
$string['coursebackup'] = 'Course backups';
34+
$string['sectionbackup'] = 'Section backups';
35+
$string['activitybackup'] = 'Activity backup';
3536
$string['areacategoryintro'] = 'Category introduction';
3637
$string['areacourseintro'] = 'Course introduction';
3738
$string['arearoot'] = 'System';

lib/file/file_browser.php

+45-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
require_once("$CFG->libdir/file/file_info_coursecat.php");
3434
require_once("$CFG->libdir/file/file_info_course.php");
3535
require_once("$CFG->libdir/file/file_info_coursesection.php");
36+
require_once("$CFG->libdir/file/file_info_coursesectionbackup.php");
3637
require_once("$CFG->libdir/file/file_info_coursefile.php");
3738
require_once("$CFG->libdir/file/virtual_root_file.php");
3839

@@ -317,7 +318,7 @@ private function get_file_info_course($context, $filearea=null, $itemid=null, $f
317318
return null;
318319
}
319320

320-
if (!is_null($filearea) and !in_array($filearea, array('course_intro', 'course_content', 'course_section', 'course_backup'))) {
321+
if (!is_null($filearea) and !in_array($filearea, array('course_intro', 'course_content', 'course_section', 'course_backup', 'section_backup'))) {
321322
// file area does not exist, sorry
322323
$filearea = null;
323324
}
@@ -422,6 +423,39 @@ private function get_file_info_course_backup($course, $context, $filearea=null,
422423

423424
}
424425

426+
private function get_file_info_section_backup($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
427+
global $CFG, $DB;
428+
429+
$fs = get_file_storage();
430+
if (empty($itemid)) {
431+
// list all sections
432+
return new file_info_coursesectionbackup($this, $context, $course);
433+
}
434+
435+
if (!has_capability('moodle/backup:backupcourse', $context) and !has_capability('moodle/restore:restorecourse', $context)) {
436+
return null;
437+
}
438+
439+
if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$itemid))) {
440+
return null; // does not exist
441+
}
442+
443+
444+
$urlbase = $CFG->wwwroot.'/pluginfile.php';
445+
if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) {
446+
if ($filepath === '/' and $filename === '.') {
447+
$storedfile = new virtual_root_file($context->id, $filearea, $itemid);
448+
} else {
449+
// not found
450+
return null;
451+
}
452+
}
453+
454+
$downloadable = has_capability('moodle/backup:downloadfile', $context);
455+
$uploadable = has_capability('moodle/restore:uploadfile', $context);
456+
return new file_info_stored($this, $context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
457+
}
458+
425459
private function get_file_info_course_content($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
426460
$fs = get_file_storage();
427461

@@ -484,15 +518,24 @@ private function get_file_info_module($context, $filearea=null, $itemid=null, $f
484518
and has_capability('moodle/course:managefiles', $context)) {
485519
$areas = array_merge(array($modname.'_intro'=>get_string('moduleintro')), $areas);
486520
}
521+
522+
if (has_capability('moodle/backup:downloadfile', $context)) {
523+
$areas = array_merge(array('activity_backup'=>get_string('activitybackup', 'repository')), $areas);
524+
}
525+
487526
if (empty($areas)) {
488527
return null;
489528
}
490529

491-
if ($filearea === $modname.'_intro') {
530+
if ($filearea === $modname.'_intro' || $filearea === 'activity_backup') {
492531
// always only itemid 0
493532
if (!has_capability('moodle/course:managefiles', $context)) {
494533
return null;
495534
}
535+
// need downloadfile cap when accessing activity_backup area
536+
if ($filearea === 'activity_backup' && !has_capability('moodle/backup:downloadfile', $context)) {
537+
return null;
538+
}
496539

497540
$filepath = is_null($filepath) ? '/' : $filepath;
498541
$filename = is_null($filename) ? '.' : $filename;

lib/file/file_info_course.php

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public function get_children() {
8484
if ($child = $this->browser->get_file_info($this->context, 'course_section')) {
8585
$children[] = $child;
8686
}
87+
if ($child = $this->browser->get_file_info($this->context, 'section_backup')) {
88+
$children[] = $child;
89+
}
8790

8891
if ($child = $this->browser->get_file_info($this->context, 'course_backup', 0)) {
8992
$children[] = $child;
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
// This file is part of Moodle - http://moodle.org/
4+
//
5+
// Moodle is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Moodle is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17+
18+
19+
/**
20+
* Utility class for browsing of course section files.
21+
*
22+
* @package moodlecore
23+
* @subpackage file-browser
24+
* @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
25+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
*/
27+
28+
/**
29+
*/
30+
class file_info_coursesectionbackup extends file_info {
31+
protected $course;
32+
33+
public function __construct($browser, $context, $course) {
34+
parent::__construct($browser, $context);
35+
$this->course = $course;
36+
}
37+
38+
/**
39+
* Returns list of standard virtual file/directory identification.
40+
* The difference from stored_file parameters is that null values
41+
* are allowed in all fields
42+
* @return array with keys contextid, filearea, itemid, filepath and filename
43+
*/
44+
public function get_params() {
45+
return array('contextid'=>$this->context->id,
46+
'filearea' =>'section_backup',
47+
'itemid' =>null,
48+
'filepath' =>null,
49+
'filename' =>null);
50+
}
51+
52+
/**
53+
* Returns localised visible name.
54+
* @return string
55+
*/
56+
public function get_visible_name() {
57+
$format = $this->course->format;
58+
$sectionsname = get_string('sectionbackup', 'repository');
59+
60+
return $sectionsname;
61+
}
62+
63+
/**
64+
* Can I add new files or directories?
65+
* @return bool
66+
*/
67+
public function is_writable() {
68+
return false;
69+
}
70+
71+
/**
72+
* Is directory?
73+
* @return bool
74+
*/
75+
public function is_directory() {
76+
return true;
77+
}
78+
79+
/**
80+
* Returns list of children.
81+
* @return array of file_info instances
82+
*/
83+
public function get_children() {
84+
global $DB;
85+
86+
$children = array();
87+
88+
$course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
89+
foreach ($course_sections as $section) {
90+
if ($child = $this->browser->get_file_info($this->context, 'section_backup', $section->id)) {
91+
$children[] = $child;
92+
}
93+
}
94+
95+
return $children;
96+
}
97+
98+
/**
99+
* Returns parent file_info instance
100+
* @return file_info or null for root
101+
*/
102+
public function get_parent() {
103+
return $this->browser->get_file_info($this->context);
104+
}
105+
}
106+

pluginfile.php

+29
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,22 @@
409409
session_get_instance()->write_close(); // unlock session during fileserving
410410
send_stored_file($file, 60*60, 0, false); // TODO: change timeout?
411411
412+
} else if ($filearea === 'section_backup') {
413+
require_login($course);
414+
require_capability('moodle/backup:downloadfile', $context);
415+
416+
$sectionid = (int)array_shift($args);
417+
418+
$relativepath = '/'.implode('/', $args);
419+
$fullpath = $context->id.'section_backup'.$sectionid.$relativepath;
420+
421+
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
422+
send_file_not_found();
423+
}
424+
425+
session_get_instance()->write_close();
426+
send_stored_file($file, 60*60, 0, false);
427+
412428
} else if ($filearea === 'user_profile') {
413429
$userid = (int)array_shift($args);
414430
$usercontext = get_context_instance(CONTEXT_USER, $userid);
@@ -499,6 +515,19 @@
499515

500516
// finally send the file
501517
send_stored_file($file, $lifetime, 0);
518+
} else if ($filearea === 'activity_backup') {
519+
require_login($course);
520+
require_capability('moodle/backup:downloadfile', $context);
521+
522+
$relativepath = '/'.implode('/', $args);
523+
$fullpath = $context->id.'activity_backup0'.$relativepath;
524+
525+
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
526+
send_file_not_found();
527+
}
528+
529+
session_get_instance()->write_close();
530+
send_stored_file($file, 60*60, 0, false);
502531
}
503532

504533
$filefunction = $modname.'_pluginfile';

0 commit comments

Comments
 (0)