Skip to content

Commit

Permalink
Merge branch 'MDL-79915-main' of https://github.com/sarjona/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Nov 29, 2023
2 parents 3bdbedb + 9e3acd9 commit d3d853b
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 132 deletions.
20 changes: 18 additions & 2 deletions backup/backupfilesedit.php
Expand Up @@ -66,7 +66,23 @@
// Set the restore course node active in the settings navigation block.
navigation_node::override_active_url(new moodle_url('/backup/restorefile.php', ['contextid' => $contextid]));

$title = get_string('managefiles', 'backup');
switch($filearea) {
case 'activity':
$title = get_string('managefiles_activity', 'backup');
break;
case 'course':
$title = get_string('managefiles_course', 'backup');
break;
case 'backup':
$title = get_string('managefiles_backup', 'backup');
break;
case 'automated':
$title = get_string('managefiles_automated', 'backup');
break;
default:
$title = get_string('managefiles', 'backup');
}

$PAGE->navbar->add($title);
$PAGE->set_title($title);
$PAGE->set_pagelayout('admin');
Expand All @@ -91,7 +107,7 @@
\backup_helper::print_coursereuse_selector('restore');

echo $OUTPUT->container_start();
echo $OUTPUT->heading($title);
echo $OUTPUT->heading($title, 2, 'mt-4');
$form->display();
echo $OUTPUT->container_end();

Expand Down
Expand Up @@ -9,7 +9,7 @@ Feature: Backup and restore of the question that was tagged
@javascript @_file_upload
Scenario: Restore the quiz containing the question that was tagged
Given I am on the "Course 1" "restore" page logged in as "admin"
And I press "Manage backup files"
And I press "Manage course backups"
And I upload "backup/moodle2/tests/fixtures/test_tags_backup.mbz" file to "Files" filemanager
And I press "Save changes"
And I restore "test_tags_backup.mbz" backup into a new course using this options:
Expand Down
82 changes: 38 additions & 44 deletions backup/restorefile.php
Expand Up @@ -149,64 +149,58 @@
echo $OUTPUT->container_end();
}

// Activity backup area.
if ($context->contextlevel == CONTEXT_MODULE) {
echo $OUTPUT->heading_with_help(get_string('choosefilefromactivitybackup', 'backup'), 'choosefilefromuserbackup', 'backup');
echo $OUTPUT->container_start();
$treeview_options = array();
$user_context = context_user::instance($USER->id);
$treeview_options['filecontext'] = $context;
$treeview_options['currentcontext'] = $context;
$treeview_options['component'] = 'backup';
$treeview_options['context'] = $context;
$treeview_options['filearea'] = 'activity';
$treeviewoptions = [
'filecontext' => $context,
'currentcontext' => $context,
'component' => 'backup',
'context' => $context,
'filearea' => 'activity',
];
$renderer = $PAGE->get_renderer('core', 'backup');
echo $renderer->backup_files_viewer($treeview_options);
echo $OUTPUT->container_end();
echo $renderer->backup_files_viewer($treeviewoptions);
// Update the course context with the proper value, because $context contains the module context.
$coursecontext = \context_course::instance($course->id);
} else {
$coursecontext = $context;
}

echo $OUTPUT->heading_with_help(get_string('choosefilefromcoursebackup', 'backup'), 'choosefilefromcoursebackup', 'backup');
echo $OUTPUT->container_start();
$treeview_options = array();
$treeview_options['filecontext'] = $coursecontext;
$treeview_options['currentcontext'] = $context;
$treeview_options['component'] = 'backup';
$treeview_options['context'] = $context;
$treeview_options['filearea'] = 'course';
// Course backup area.
$treeviewoptions = [
'filecontext' => $coursecontext,
'currentcontext' => $context,
'component' => 'backup',
'context' => $context,
'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 = context_user::instance($USER->id);
$treeview_options['filecontext'] = $user_context;
$treeview_options['currentcontext'] = $context;
$treeview_options['component'] = 'user';
$treeview_options['context'] = 'backup';
$treeview_options['filearea'] = 'backup';
echo $renderer->backup_files_viewer($treeviewoptions);

// Private backup area.
$usercontext = context_user::instance($USER->id);
$treeviewoptions = [
'filecontext' => $usercontext,
'currentcontext' => $context,
'component' => 'user',
'context' => 'backup',
'filearea' => 'backup',
];
$renderer = $PAGE->get_renderer('core', 'backup');
echo $renderer->backup_files_viewer($treeview_options);
echo $OUTPUT->container_end();
echo $renderer->backup_files_viewer($treeviewoptions);

// Automated backup area.
$automatedbackups = get_config('backup', 'backup_auto_active');
if (!empty($automatedbackups)) {
echo $OUTPUT->heading_with_help(get_string('choosefilefromautomatedbackup', 'backup'), 'choosefilefromautomatedbackup', 'backup');
echo $OUTPUT->container_start();
$treeview_options = array();
$user_context = context_user::instance($USER->id);
$treeview_options['filecontext'] = $context;
$treeview_options['currentcontext'] = $context;
$treeview_options['component'] = 'backup';
$treeview_options['context'] = $context;
$treeview_options['filearea'] = 'automated';
$treeviewoptions = [
'filecontext' => $context,
'currentcontext' => $context,
'component' => 'backup',
'context' => $context,
'filearea' => 'automated',
];
$renderer = $PAGE->get_renderer('core', 'backup');
echo $renderer->backup_files_viewer($treeview_options);
echo $OUTPUT->container_end();
echo $renderer->backup_files_viewer($treeviewoptions);
}

// In progress course restores.
Expand Down
2 changes: 1 addition & 1 deletion backup/restorefile_form.php
Expand Up @@ -29,7 +29,7 @@ function definition() {
$contextid = $this->_customdata['contextid'];
$mform->addElement('hidden', 'contextid', $contextid);
$mform->setType('contextid', PARAM_INT);
$mform->addElement('filepicker', 'backupfile', get_string('files'));
$mform->addElement('filepicker', 'backupfile', get_string('backupfile', 'backup'));
$mform->addRule('backupfile', get_string('required'), 'required');
$submit_string = get_string('restore');
$this->add_action_buttons(false, $submit_string);
Expand Down
191 changes: 120 additions & 71 deletions backup/util/ui/renderer.php
Expand Up @@ -621,91 +621,140 @@ public function get_status_display($statuscode, $backupid, $restoreid=null, $ope
public function render_backup_files_viewer(backup_files_viewer $viewer) {

$files = $viewer->files;
$filestodisplay = false;
foreach ($files as $file) {
if (!$file->is_directory()) {
$filestodisplay = true;
break;
}
}

$async = \async_helper::is_async_enabled();

$tablehead = array(
switch($viewer->filearea) {
case 'activity':
$title = get_string('choosefilefromactivitybackup', 'backup');
$description = get_string('choosefilefromactivitybackup_help', 'backup');
$button = get_string('managefiles_activity', 'backup');
$nofilesstring = get_string('restorenofilesbackuparea_activity', 'backup');
break;
case 'course':
$title = get_string('choosefilefromcoursebackup', 'backup');
$description = get_string('choosefilefromcoursebackup_help', 'backup');
$button = get_string('managefiles_course', 'backup');
$nofilesstring = get_string('restorenofilesbackuparea_course', 'backup');
break;
case 'backup':
$title = get_string('choosefilefromuserbackup', 'backup');
$description = get_string('choosefilefromuserbackup_help', 'backup');
$button = get_string('managefiles_backup', 'backup');
$nofilesstring = get_string('restorenofilesbackuparea_backup', 'backup');
break;
case 'automated':
$title = get_string('choosefilefromautomatedbackup', 'backup');
$description = get_string('choosefilefromautomatedbackup_help', 'backup');
$button = get_string('managefiles_automated', 'backup');
$nofilesstring = get_string('restorenofilesbackuparea_automated', 'backup');
break;
default:
$title = '';
$description = '';
$button = get_string('managefiles', 'backup');
$nofilesstring = get_string('restorenofilesbackuparea', 'backup');
}

$html = html_writer::tag('h3', $title, ['class' => 'mt-6']);
$html .= html_writer::tag('div', $description, ['class' => 'mb-3']);

if ($filestodisplay || $async) {
$tablehead = [
get_string('filename', 'backup'),
get_string('time'),
get_string('size'),
get_string('download'),
get_string('restore'));
if ($async) {
$tablehead[] = get_string('status', 'backup');
}

$table = new html_table();
$table->attributes['class'] = 'backup-files-table generaltable';
$table->head = $tablehead;
$table->width = '100%';
$table->data = [];

// First add in progress asynchronous backups.
// Only if asynchronous backups are enabled.
if ($async) {
$tabledata = [];
$backups = \async_helper::get_async_backups($viewer->filearea, $viewer->filecontext->instanceid);
// For each backup get, new item name, time restore created and progress.
foreach ($backups as $backup) {
$status = $this->get_status_display($backup->status, $backup->backupid);
$timecreated = $backup->timecreated;
$tablerow = [$backup->filename, userdate($timecreated), '-', '-', '-', $status];
$tabledata[] = $tablerow;
get_string('restore'),
];
if ($async) {
$tablehead[] = get_string('status', 'backup');
}
$table->data = $tabledata;
}

// Add completed backups.
foreach ($files as $file) {
if ($file->is_directory()) {
continue;
$table = new html_table();
$table->attributes['class'] = 'backup-files-table generaltable';
$table->head = $tablehead;
$table->width = '100%';
$table->data = [];

// First add in progress asynchronous backups.
// Only if asynchronous backups are enabled.
if ($async) {
$tabledata = [];
$backups = \async_helper::get_async_backups($viewer->filearea, $viewer->filecontext->instanceid);
// For each backup get, new item name, time restore created and progress.
foreach ($backups as $backup) {
$status = $this->get_status_display($backup->status, $backup->backupid);
$timecreated = $backup->timecreated;
$tablerow = [$backup->filename, userdate($timecreated), '-', '-', '-', $status];
$tabledata[] = $tablerow;
}
$table->data = $tabledata;
}
$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);
$restorelink = html_writer::link($restoreurl, get_string('restore'));
$downloadlink = html_writer::link($fileurl, get_string('download'));

// Conditional display of the restore and download links, initially only for the 'automated' filearea.
if ($params['filearea'] == 'automated') {
if (!has_capability('moodle/restore:viewautomatedfilearea', $viewer->currentcontext)) {
$restorelink = '';

// Add completed backups.
foreach ($files as $file) {
if ($file->is_directory()) {
continue;
}
if (!can_download_from_backup_filearea($params['filearea'], $viewer->currentcontext)) {
$downloadlink = '';
$fileurl = moodle_url::make_pluginfile_url(
$file->get_contextid(),
$file->get_component(),
$file->get_filearea(),
null,
$file->get_filepath(),
$file->get_filename(),
true
);
$params = [];
$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);
$restorelink = html_writer::link($restoreurl, get_string('restore'));
$downloadlink = html_writer::link($fileurl, get_string('download'));

// Conditional display of the restore and download links, initially only for the 'automated' filearea.
if ($params['filearea'] == 'automated') {
if (!has_capability('moodle/restore:viewautomatedfilearea', $viewer->currentcontext)) {
$restorelink = '';
}
if (!can_download_from_backup_filearea($params['filearea'], $viewer->currentcontext)) {
$downloadlink = '';
}
}
}
$tabledata = array(
$file->get_filename(),
userdate ($file->get_timemodified()),
display_size ($file->get_filesize()),
$downloadlink,
$restorelink
);
if ($async) {
$tabledata[] = $this->get_status_display(backup::STATUS_FINISHED_OK, null);
$tabledata = [
$file->get_filename(),
userdate ($file->get_timemodified()),
display_size ($file->get_filesize()),
$downloadlink,
$restorelink,
];
if ($async) {
$tabledata[] = $this->get_status_display(backup::STATUS_FINISHED_OK, null);
}

$table->data[] = $tabledata;
}

$table->data[] = $tabledata;
}
$html .= html_writer::table($table);
} else {
// There are no files to display.
$html .= $this->notification($nofilesstring, 'notifymessage');

$html = html_writer::table($table);
}

// For automated backups, the ability to manage backup files is controlled by the ability to download them.
// All files must be from the same file area in a backup_files_viewer.
Expand All @@ -725,7 +774,7 @@ public function render_backup_files_viewer(backup_files_viewer $viewer) {
'component' => $viewer->component,
'returnurl' => $this->page->url->out())
),
get_string('managefiles', 'backup'),
$button,
'post'
);
}
Expand Down
2 changes: 1 addition & 1 deletion completion/tests/behat/backup_restore_completion.feature
Expand Up @@ -32,7 +32,7 @@ Feature: Backup and restore the activity with the completion
@javascript @_file_upload
Scenario: Restore the legacy assignment with completion condition.
Given I am on the "Course 1" "restore" page logged in as "admin"
And I press "Manage backup files"
And I press "Manage course backups"
And I upload "completion/tests/fixtures/legacy_course_completion.mbz" file to "Files" filemanager
And I press "Save changes"
And I restore "legacy_course_completion.mbz" backup into a new course using this options:
Expand Down

0 comments on commit d3d853b

Please sign in to comment.