From 6aef525df448dec2ced24bba13e9ee2b5c847766 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Thu, 26 Aug 2010 06:19:46 +0000 Subject: [PATCH] "MDL-23910, improved restore pre-ui, removed treeview, list backup files in tables and sorted by created time" --- backup/backupfilesedit.php | 74 ++++++++++++++ backup/backupfilesedit_form.php | 39 ++++++++ backup/restorefile.php | 72 +++++++------- backup/util/ui/renderer.php | 168 ++++++++++---------------------- lang/en/backup.php | 8 +- 5 files changed, 206 insertions(+), 155 deletions(-) create mode 100755 backup/backupfilesedit.php create mode 100644 backup/backupfilesedit_form.php diff --git a/backup/backupfilesedit.php b/backup/backupfilesedit.php new file mode 100755 index 00000000000..f0b87cfec43 --- /dev/null +++ b/backup/backupfilesedit.php @@ -0,0 +1,74 @@ +. + +/** + * Manage backup files + * @package moodlecore + * @copyright 2010 Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once('../config.php'); +require_once(dirname(__FILE__) . '/backupfilesedit_form.php'); +require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); +require_once($CFG->dirroot . '/repository/lib.php'); + +// current context +$contextid = required_param('contextid', PARAM_INT); +// file parameters +$component = optional_param('component', null, PARAM_ALPHAEXT); +$filearea = optional_param('filearea', null, PARAM_ALPHAEXT); +$returnurl = optional_param('returnurl', null, PARAM_URL); + +list($context, $course, $cm) = get_context_info_array($contextid); + +$filecontext = $context; + +$url = new moodle_url('/backup/backupfilesedit.php', array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea)); + +require_login($course); +require_capability('moodle/restore:uploadfile', $context); + +$PAGE->set_url($url); +$PAGE->set_context($context); +$PAGE->set_title(get_string('managefiles', 'backup')); +$PAGE->set_heading(get_string('managefiles', 'backup')); +$PAGE->set_pagelayout('admin'); +$browser = get_file_browser(); + +$data = new object(); +$options = array('subdirs'=>0, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL); +file_prepare_standard_filemanager($data, 'files', $options, $filecontext, $component, $filearea, 0); +$form = new backup_files_edit_form(null, array('data'=>$data, 'contextid'=>$contextid, 'filearea'=>$filearea, 'component'=>$component, 'returnurl'=>$returnurl)); + +if ($form->is_cancelled()) { + redirect($returnurl); +} + +$data = $form->get_data(); +if ($data) { + $formdata = file_postupdate_standard_filemanager($data, 'files', $options, $filecontext, $component, $filearea, 0); + redirect($returnurl); +} + +echo $OUTPUT->header(); + +echo $OUTPUT->container_start(); +$form->display(); +echo $OUTPUT->container_end(); + +echo $OUTPUT->footer(); diff --git a/backup/backupfilesedit_form.php b/backup/backupfilesedit_form.php new file mode 100644 index 00000000000..e9b81d4c86f --- /dev/null +++ b/backup/backupfilesedit_form.php @@ -0,0 +1,39 @@ +. + +/** + * Manage backup files + * @package moodlecore + * @copyright 2010 Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +require_once($CFG->libdir.'/formslib.php'); + +class backup_files_edit_form extends moodleform { + function definition() { + $mform =& $this->_form; + $contextid = $this->_customdata['contextid']; + $options = array('subdirs'=>0, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL); + $mform->addElement('filemanager', 'files_filemanager', get_string('files'), null, $options); + $mform->addElement('hidden', 'contextid', $this->_customdata['contextid']); + $mform->addElement('hidden', 'filearea', $this->_customdata['filearea']); + $mform->addElement('hidden', 'component', $this->_customdata['component']); + $mform->addElement('hidden', 'returnurl', $this->_customdata['returnurl']); + $this->add_action_buttons(true, get_string('savechanges')); + $this->set_data($this->_customdata['data']); + } +} diff --git a/backup/restorefile.php b/backup/restorefile.php index 8b3ae977b8d..e9b406a3e0d 100755 --- a/backup/restorefile.php +++ b/backup/restorefile.php @@ -28,6 +28,7 @@ require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); // current context $contextid = required_param('contextid', PARAM_INT); +$filecontextid = optional_param('filecontextid', 0, PARAM_INT); // action $action = optional_param('action', '', PARAM_ALPHA); // file parameters @@ -40,38 +41,27 @@ $filename = optional_param('filename', null, PARAM_FILE); 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); +// will be used when restore +if (!empty($filecontextid)) { + $filecontext = get_context_instance_by_id($filecontextid); } $url = new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid)); switch ($context->contextlevel) { - case CONTEXT_COURSE: - $heading = get_string('restorecourse', 'backup'); - break; case CONTEXT_MODULE: $heading = get_string('restoreactivity', 'backup'); break; + case CONTEXT_COURSE: default: - print_error('invalidcontext'); - die; + $heading = get_string('restorecourse', 'backup'); } require_login($course); require_capability('moodle/restore:restorecourse', $context); -$PAGE->set_url($url); -$PAGE->set_context($context); -$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') { if ($fileinfo = $browser->get_file_info($filecontext, $component, $filearea, $itemid, $filepath, $filename)) { @@ -86,6 +76,12 @@ if ($action == 'choosebackupfile') { die; } +$PAGE->set_url($url); +$PAGE->set_context($context); +$PAGE->set_title(get_string('course') . ': ' . $course->fullname); +$PAGE->set_heading($heading); +$PAGE->set_pagelayout('admin'); + $form = new course_restore_form(null, array('contextid'=>$contextid)); $data = $form->get_data(); if ($data && has_capability('moodle/restore:uploadfile', $context)) { @@ -97,28 +93,10 @@ if ($data && has_capability('moodle/restore:uploadfile', $context)) { die; } -$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($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')); @@ -127,4 +105,30 @@ if (has_capability('moodle/restore:uploadfile', $context)) { echo $OUTPUT->container_end(); } +echo $OUTPUT->heading_with_help(get_string('choosefilefromcoursebackup', 'backup'), 'choosefilefromcoursebackup', 'backup'); +echo $OUTPUT->container_start(); +$treeview_options = array(); +$treeview_options['filecontext'] = $context; +$treeview_options['currentcontext'] = $context; +$treeview_options['component'] = 'backup'; +$treeview_options['context'] = $context; +$treeview_options['filearea'] = 'course'; +$renderer = $PAGE->get_renderer('core', 'backup'); +echo $renderer->backup_files_viewer($treeview_options); +echo $OUTPUT->container_end(); + + +echo $OUTPUT->heading_with_help(get_string('choosefilefromuserbackup', 'backup'), 'choosefilefromuserbackup', 'backup'); +echo $OUTPUT->container_start(); +$treeview_options = array(); +$user_context = get_context_instance(CONTEXT_USER, $USER->id); +$treeview_options['filecontext'] = $user_context; +$treeview_options['currentcontext'] = $context; +$treeview_options['component'] = 'user'; +$treeview_options['context'] = 'backup'; +$treeview_options['filearea'] = 'backup'; +$renderer = $PAGE->get_renderer('core', 'backup'); +echo $renderer->backup_files_viewer($treeview_options); +echo $OUTPUT->container_end(); + echo $OUTPUT->footer(); diff --git a/backup/util/ui/renderer.php b/backup/util/ui/renderer.php index 6bde586af67..68614a8090f 100644 --- a/backup/util/ui/renderer.php +++ b/backup/util/ui/renderer.php @@ -388,8 +388,8 @@ class core_backup_renderer extends plugin_renderer_base { * @return string */ public function backup_files_viewer(array $options = null) { - $tree = new backup_files_viewer($options); - return $this->render($tree); + $files = new backup_files_viewer($options); + return $this->render($files); } /** @@ -399,49 +399,43 @@ class core_backup_renderer extends plugin_renderer_base { * @param backup_files_viewer $tree * @return string */ - 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(); - $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); + public function render_backup_files_viewer(backup_files_viewer $viewer) { + global $CFG; + $files = $viewer->files; - $html = '
'; - foreach($tree->path as $path) { - $html .= $path; - $html .= ' / '; - } - $html .= '
'; + $table = new html_table(); + $table->head = array(get_string('filename', 'backup'), get_string('time'), get_string('size'), get_string('download'), get_string('restore')); + $table->align = array('left', 'left', 'left', 'center', 'left', 'center'); + $table->width = '100%'; + $table->data = array(); - $html .= '
'; - if (empty($tree->tree)) { - $html .= get_string('nofilesavailable', 'repository'); - } else { - $html .= '
    '; - foreach($tree->tree as $node) { - $link_attributes = array(); - if (!empty($node['isdir'])) { - $class = ' class="file-tree-folder"'; - $restore_link = ''; - } else { - $class = ' class="file-tree-file"'; - $link_attributes['target'] = '_blank'; - $restore_link = html_writer::link($node['restoreurl'], get_string('restore', 'moodle'), $link_attributes); - } - $html .= '
  • '; - $html .= html_writer::link($node['url'], $node['filename'], $link_attributes); - // when js is off, use this restore link - // otherwise, yui treeview will generate a restore link in js - $html .= ' '.$restore_link; - $html .= '
  • '; + foreach ($files as $file) { + if ($file->is_directory()) { + continue; } - $html .= '
'; + $fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename(), true); + $params = array(); + $params['action'] = 'choosebackupfile'; + $params['filename'] = $file->get_filename(); + $params['filepath'] = $file->get_filepath(); + $params['component'] = $file->get_component(); + $params['filearea'] = $file->get_filearea(); + $params['filecontextid'] = $file->get_contextid(); + $params['contextid'] = $viewer->currentcontext->id; + $params['itemid'] = $file->get_itemid(); + $restoreurl = new moodle_url('/backup/restorefile.php', $params); + $table->data[] = array( + $file->get_filename(), + userdate($file->get_timemodified()), + display_size($file->get_filesize()), + html_writer::link($fileurl, get_string('download')), + html_writer::link($restoreurl, get_string('restore')), + ); } - $html .= '
'; + + $html = html_writer::table($table); + $html .= $this->output->single_button(new moodle_url('/backup/backupfilesedit.php', array('contextid'=>$viewer->filecontext->id, 'filearea'=>$viewer->filearea, 'component'=>$viewer->component, 'returnurl'=>$this->page->url->out())), get_string('managefiles', 'backup'), 'post'); + return $html; } @@ -557,7 +551,7 @@ class core_backup_renderer extends plugin_renderer_base { /** * Renders a restore category search object - * + * * @param restore_category_search $component * @return string */ @@ -610,6 +604,7 @@ class core_backup_renderer extends plugin_renderer_base { return $output; } } + /** * Data structure representing backup files viewer * @@ -618,8 +613,11 @@ class core_backup_renderer extends plugin_renderer_base { * @since Moodle 2.0 */ class backup_files_viewer implements renderable { - public $tree; - public $path; + public $files; + public $filecontext; + public $component; + public $filearea; + public $currentcontext; /** * Constructor of backup_files_viewer class @@ -627,80 +625,12 @@ class backup_files_viewer implements renderable { */ 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(); - - $level = $parent_info; - $this->path = array(); - while ($level) { - $params = $level->get_params(); - $context = get_context_instance_by_id($params['contextid']); - // lock user in course level - if ($context->contextlevel == CONTEXT_COURSECAT or $context->contextlevel == CONTEXT_SYSTEM) { - break; - } - $url = new moodle_url('/backup/restorefile.php', $params); - $this->path[] = html_writer::link($url->out(false), $level->get_visible_name()); - $level = $level->get_parent(); - } - $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(); - $mimetype = $child->get_mimetype(); - $params = $child->get_params(); - $fileitem = array( - 'params' => $params, - 'filename' => $child->get_visible_name(), - '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 (!$is_coursebackup and !$is_userbackup) { - continue; - } - $fileitem['isdir'] = true; - // link to this folder - $folderurl = new moodle_url('/backup/restorefile.php', $params); - $fileitem['url'] = $folderurl->out(false); - } else { - $restoreurl = new moodle_url('/backup/restorefile.php', array_merge($params, array('action'=>'choosebackupfile'))); - // link to this file - $fileitem['url'] = $child->get_url(); - $fileitem['restoreurl'] = $restoreurl->out(false); - } - $this->tree[] = $fileitem; - } + $fs = get_file_storage(); + $this->currentcontext = $options['currentcontext']; + $this->filecontext = $options['filecontext']; + $this->component = $options['component']; + $this->filearea = $options['filearea']; + $files = $fs->get_area_files($this->filecontext->id, $this->component, $this->filearea, false, 'timecreated'); + $this->files = array_reverse($files); } } diff --git a/lang/en/backup.php b/lang/en/backup.php index f5eaf636575..7a204bd4614 100644 --- a/lang/en/backup.php +++ b/lang/en/backup.php @@ -46,7 +46,10 @@ $string['backuptype'] = 'Type'; $string['backuptypecourse'] = 'Course'; $string['backupversion'] = 'Backup version'; $string['cannotfindassignablerole'] = 'The {$a} role in the backup file cannot be mapped to any of the roles that you are allowed to assign.'; -$string['choosefile'] = 'Choose an existing backup file'; +$string['choosefilefromcoursebackup'] = 'Choose an existing backup file from course bacup area'; +$string['choosefilefromcoursebackup_help'] = 'When backup courses using default settings, backup files will be stored here'; +$string['choosefilefromuserbackup'] = 'Choose an existing backup file from private backup area'; +$string['choosefilefromuserbackup_help'] = 'When backup courses with "Anonymize user information" option ticked, backup files will be stored here'; $string['configgeneralactivities'] = 'Sets the default for including activities in a backup.'; $string['configgeneralanonymize'] = 'If enabled all information pertaining to users will be anonymised by default.'; $string['configgeneralblocks'] = 'Sets the default for including blocks in a backup.'; @@ -117,6 +120,7 @@ $string['locked'] = 'Locked'; $string['lockedbypermission'] = 'You don\'t have sufficient permissions to change this setting'; $string['lockedbyconfig'] = 'This setting has been locked by the default backup settings'; $string['lockedbyhierarchy'] = 'Locked by dependencies'; +$string['managefiles'] = 'Manage backup files'; $string['moodleversion'] = 'Moodle version'; $string['nomatchingcourses'] = 'There are no courses to display'; $string['originalwwwroot'] = 'URL of backup'; @@ -179,4 +183,4 @@ $string['setting_course_fullname'] = 'Course name'; $string['setting_course_shortname'] = 'Course short name'; $string['setting_course_startdate'] = 'Course startdate'; $string['totalcategorysearchresults'] = 'Total categories: {$a}'; -$string['totalcoursesearchresults'] = 'Total courses: {$a}'; \ No newline at end of file +$string['totalcoursesearchresults'] = 'Total courses: {$a}';