Skip to content

Commit

Permalink
MDL-22915, added user backup to tree, added capability check to file …
Browse files Browse the repository at this point in the history
…picker form
  • Loading branch information
Dongsheng Cai committed Jul 28, 2010
1 parent 588a395 commit 30ad8cd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 24 deletions.
46 changes: 35 additions & 11 deletions backup/restorefile.php
Expand Up @@ -26,6 +26,7 @@
require_once(dirname(__FILE__) . '/restorefile_form.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');

// current context
$contextid = required_param('contextid', PARAM_INT);
// action
$action = optional_param('action', '', PARAM_ALPHA);
Expand All @@ -39,6 +40,13 @@

list($context, $course, $cm) = get_context_info_array($contextid);

$filecontext = $context;
// files in user context
if ($filearea == 'backup' && $component == 'user') {
// XXX: hacky, to get user context
$filecontext = get_context_instance(CONTEXT_USER, $USER->id);
}

$url = new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid));

switch ($context->contextlevel) {
Expand All @@ -48,7 +56,9 @@
case CONTEXT_MODULE:
$heading = get_string('restoreactivity', 'backup');
break;
// TODO
default:
print_error('invalidcontext');
die;
}


Expand All @@ -60,11 +70,11 @@
$PAGE->set_title(get_string('course') . ': ' . $course->fullname);
$PAGE->set_heading($heading);
$PAGE->set_pagelayout('admin');
$browser = get_file_browser();

// choose the backup file from backup files tree
if ($action == 'choosebackupfile') {
$browser = get_file_browser();
if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
if ($fileinfo = $browser->get_file_info($filecontext, $component, $filearea, $itemid, $filepath, $filename)) {
$filename = restore_controller::get_tempdir_name($course->id, $USER->id);
$pathname = "$CFG->dataroot/temp/backup/".$filename;
$fileinfo->copy_to_pathname($pathname);
Expand All @@ -78,7 +88,7 @@

$form = new course_restore_form(null, array('contextid'=>$contextid));
$data = $form->get_data();
if ($data) {
if ($data && has_capability('moodle/restore:uploadfile', $context)) {
$filename = restore_controller::get_tempdir_name($course->id, $USER->id);
$pathname = "$CFG->dataroot/temp/backup/".$filename;
$form->save_file('backupfile', $pathname);
Expand All @@ -87,20 +97,34 @@
die;
}

$browser = get_file_browser();
$fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
$treeview_options = array();
$treeview_options['context'] = $context;
$treeview_options['filecontext'] = $filecontext;
$treeview_options['component'] = $component;
$treeview_options['filearea'] = $filearea;
$treeview_options['itemid'] = $itemid;
$treeview_options['filepath'] = $filepath;
$treeview_options['filename'] = $filename;
if (empty($component) && empty($filearea)) {
$treeview_options['show_user_backup'] = true;
}


echo $OUTPUT->header();

echo $OUTPUT->heading(get_string('choosefile', 'backup'));
echo $OUTPUT->container_start();
$renderer = $PAGE->get_renderer('core', 'backup');
echo $renderer->backup_files_viewer($fileinfo, array());
echo $OUTPUT->container_end();

echo $OUTPUT->heading(get_string('importfile', 'backup'));
echo $OUTPUT->container_start();
$form->display();
echo $renderer->backup_files_viewer($treeview_options);
echo $OUTPUT->container_end();

// require uploadfile cap to use file picker
if (has_capability('moodle/restore:uploadfile', $context)) {
echo $OUTPUT->heading(get_string('importfile', 'backup'));
echo $OUTPUT->container_start();
$form->display();
echo $OUTPUT->container_end();
}

echo $OUTPUT->footer();
14 changes: 13 additions & 1 deletion backup/util/ui/module.js
Expand Up @@ -12,6 +12,11 @@ M.core_backup_files_tree = {
params['itemid'] = this.get_param(url, 'itemid', -1);
params['filepath'] = this.get_param(url, 'filepath', null);
params['filename'] = this.get_param(url, 'filename', null);
if (params['filearea'] == 'backup' && params['component'] == 'user') {
// XXX: the id in params['contextid'] is current context
// request file list, so should be user context
params['contextid'] = this.usercontextid;
}
var scope = this;
params['sesskey']=M.cfg.sesskey;
var cfg = {
Expand All @@ -37,6 +42,10 @@ M.core_backup_files_tree = {
n.isLeaf = false;
} else {
var params = data[i].params;
if (params['filearea'] == 'backup' && params['component'] == 'user') {
// XXX: display the restore link, so should be context id
params['contextid'] = scope.currentcontextid;
}
params.action = 'choosebackupfile';
var restoreurl = M.cfg.wwwroot+'/backup/restorefile.php?'+build_querystring(params);
var info = {label: data[i].filename, 'href': data[i].url, 'restoreurl': restoreurl};
Expand All @@ -60,7 +69,10 @@ M.core_backup_files_tree = {
};
this.y3.io(api, cfg);
},
init : function(Y, htmlid){
init : function(Y, options){
var htmlid = options.htmlid;
this.usercontextid = options.usercontextid;
this.currentcontextid = options.currentcontextid;
var tree = new YAHOO.widget.TreeView(htmlid);
tree.setDynamicLoad(this.dynload);
var root = tree.getRoot();
Expand Down
53 changes: 41 additions & 12 deletions backup/util/ui/renderer.php
Expand Up @@ -76,7 +76,7 @@ public function backup_details($details, $nextstageurl) {
$html .= $this->backup_detail_pair(get_string('backupformat', 'backup'), get_string('backupformat'.$details->format, 'backup'));
$html .= $this->backup_detail_pair(get_string('backupmode', 'backup'), get_string('backupmode'.$details->mode, 'backup'));
$html .= $this->backup_detail_pair(get_string('backupdate', 'backup'), userdate($details->backup_date));
$html .= $this->backup_detail_pair(get_string('moodleversion', 'backup'),
$html .= $this->backup_detail_pair(get_string('moodleversion', 'backup'),
html_writer::tag('span', $details->moodle_release, array('class'=>'moodle_release')).
html_writer::tag('span', '['.$details->moodle_version.']', array('class'=>'moodle_version sub-detail')));
$html .= $this->backup_detail_pair(get_string('backupversion', 'backup'),
Expand Down Expand Up @@ -135,7 +135,7 @@ public function backup_details($details, $nextstageurl) {
if (!empty($table)) {
$html .= $this->backup_detail_pair(get_string('sectionactivities','backup'), html_writer::table($table));
}

}
$html .= html_writer::end_tag('div');
$html .= html_writer::end_tag('div');
Expand Down Expand Up @@ -283,19 +283,24 @@ public function continue_button($url, $method='post') {
}
/**
* Print a backup files tree
* @param file_info $fileinfo
* @param array $options
* @return string
*/
public function backup_files_viewer(file_info $fileinfo, array $options = null) {
$tree = new backup_files_viewer($fileinfo, $options);
public function backup_files_viewer(array $options = null) {
$tree = new backup_files_viewer($options);
return $this->render($tree);
}

public function render_backup_files_viewer(backup_files_viewer $tree) {
global $USER;
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
$options = new stdclass;
$module = array('name'=>'backup_files_tree', 'fullpath'=>'/backup/util/ui/module.js', 'requires'=>array('yui2-treeview', 'yui2-json'), 'strings'=>array(array('restore', 'moodle')));
$htmlid = 'backup-treeview-'.uniqid();
$this->page->requires->js_init_call('M.core_backup_files_tree.init', array($htmlid), false, $module);
$options->htmlid = $htmlid;
$options->usercontextid = $user_context->id;
$options->currentcontextid = $tree->options['context']->id;
$this->page->requires->js_init_call('M.core_backup_files_tree.init', array($options), false, $module);

$html = '<div>';
foreach($tree->path as $path) {
Expand Down Expand Up @@ -376,7 +381,7 @@ public function render_restore_course_search(restore_course_search $component) {
$output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
$output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
$output .= html_writer::end_tag('div');

$output .= html_writer::end_tag('div');
return $output;
}
Expand Down Expand Up @@ -443,14 +448,20 @@ class backup_files_viewer implements renderable {

/**
* Constructor of backup_files_viewer class
* @param file_info $file_info
* @param array $options
*/
public function __construct(file_info $file_info, array $options = null) {
global $CFG;
public function __construct(array $options = null) {
global $CFG, $USER;
$browser = get_file_browser();
$file_info = $browser->get_file_info($options['filecontext'], $options['component'], $options['filearea'], $options['itemid'], $options['filepath'], $options['filename']);
$this->options = (array)$options;

$this->tree = array();
if (!$file_info) {
$this->path = array();
$this->tree = array();
return;
}
$children = $file_info->get_children();
$parent_info = $file_info->get_parent();

Expand All @@ -470,6 +481,18 @@ public function __construct(file_info $file_info, array $options = null) {
$this->path = array_reverse($this->path);
$this->path[] = $file_info->get_visible_name();

$this->add_to_tree($children);

if (!empty($options['show_user_backup'])) {
$browser = get_file_browser();
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
$fileinfo = $browser->get_file_info($user_context, null, null, null, null, null);
$children = $fileinfo->get_children();
$this->add_to_tree($children);
}
}

function add_to_tree($children) {
foreach ($children as $child) {
$filedate = $child->get_timemodified();
$filesize = $child->get_filesize();
Expand All @@ -481,9 +504,15 @@ public function __construct(file_info $file_info, array $options = null) {
'filedate' => $filedate ? userdate($filedate) : '',
'filesize' => $filesize ? display_size($filesize) : ''
);
$is_coursebackup = ($params['component'] == 'backup' && in_array($params['filearea'], array('course', 'section', 'activity', 'backup')));
$is_userbackup = ($params['component'] == 'user' && $params['filearea'] == 'backup');
if ($is_userbackup) {
// XXX: hacky, current context
$params['contextid'] = $this->options['context']->id;
}
if ($child->is_directory()) {
// ignore all other fileares except backup_course backup_section and backup_activity
if ($params['component'] != 'backup' or !in_array($params['filearea'], array('course', 'section', 'activity'))) {
if (!$is_coursebackup and !$is_userbackup) {
continue;
}
$fileitem['isdir'] = true;
Expand All @@ -499,4 +528,4 @@ public function __construct(file_info $file_info, array $options = null) {
$this->tree[] = $fileitem;
}
}
}
}

0 comments on commit 30ad8cd

Please sign in to comment.