2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This file contains backup and restore output renderers
20 * @package core_backup
21 * @copyright 2010 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die;
28 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
29 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
30 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
33 * The primary renderer for the backup.
35 * Can be retrieved with the following code:
37 * $renderer = $PAGE->get_renderer('core', 'backup');
40 * @package core_backup
41 * @copyright 2010 Sam Hemelryk
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class core_backup_renderer extends plugin_renderer_base {
47 * Same site notification display.
51 private $samesitenotification = '';
54 * Renderers a progress bar for the backup or restore given the items that make it up.
56 * @param array $items An array of items
59 public function progress_bar(array $items) {
60 foreach ($items as &$item) {
61 $text = $item['text'];
63 if (array_key_exists('link', $item)) {
64 $link = $item['link'];
66 $item = html_writer::link($link, $text, $item);
68 $item = html_writer::tag('span', $text, $item);
71 return html_writer::tag('div', join(get_separator(), $items), array('class' => 'backup_progress clearfix'));
75 * The backup and restore pages may display a log (if any) in a scrolling box.
77 * @param string $loghtml Log content in HTML format
78 * @return string HTML content that shows the log
80 public function log_display($loghtml) {
81 $out = html_writer::start_div('backup_log');
82 $out .= $this->output->heading(get_string('backuplog', 'backup'));
83 $out .= html_writer::start_div('backup_log_contents');
85 $out .= html_writer::end_div();
86 $out .= html_writer::end_div();
91 * Set the same site backup notification.
94 public function set_samesite_notification() {
95 $this->samesitenotification = $this->output->notification(get_string('samesitenotification', 'backup'), 'info');
99 * Get the same site backup notification.
102 public function get_samesite_notification() {
103 return $this->samesitenotification;
107 * Prints a dependency notification
109 * @param string $message
112 public function dependency_notification($message) {
113 return html_writer::tag('div', $message, array('class' => 'notification dependencies_enforced'));
117 * Displays the details of a backup file
119 * @param stdClass $details
120 * @param moodle_url $nextstageurl
123 public function backup_details($details, $nextstageurl) {
124 $yestick = $this->output->pix_icon('i/valid', get_string('yes'));
125 $notick = $this->output->pix_icon('i/invalid', get_string('no'));
127 $html = html_writer::start_tag('div', array('class' => 'backup-restore'));
129 $html .= html_writer::start_tag('div', array('class' => 'backup-section'));
130 $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, array('class' => 'header'));
131 $html .= $this->backup_detail_pair(get_string('backuptype', 'backup'), get_string('backuptype'.$details->type, 'backup'));
132 $html .= $this->backup_detail_pair(get_string('backupformat', 'backup'), get_string('backupformat'.$details->format, 'backup'));
133 $html .= $this->backup_detail_pair(get_string('backupmode', 'backup'), get_string('backupmode'.$details->mode, 'backup'));
134 $html .= $this->backup_detail_pair(get_string('backupdate', 'backup'), userdate($details->backup_date));
135 $html .= $this->backup_detail_pair(get_string('moodleversion', 'backup'),
136 html_writer::tag('span', $details->moodle_release, array('class' => 'moodle_release')).
137 html_writer::tag('span', '['.$details->moodle_version.']', array('class' => 'moodle_version sub-detail')));
138 $html .= $this->backup_detail_pair(get_string('backupversion', 'backup'),
139 html_writer::tag('span', $details->backup_release, array('class' => 'moodle_release')).
140 html_writer::tag('span', '['.$details->backup_version.']', array('class' => 'moodle_version sub-detail')));
141 $html .= $this->backup_detail_pair(get_string('originalwwwroot', 'backup'),
142 html_writer::tag('span', $details->original_wwwroot, array('class' => 'originalwwwroot')).
143 html_writer::tag('span', '['.$details->original_site_identifier_hash.']', array('class' => 'sitehash sub-detail')));
144 if (!empty($details->include_file_references_to_external_content)) {
146 if (backup_general_helper::backup_is_samesite($details)) {
147 $message = $yestick . ' ' . get_string('filereferencessamesite', 'backup');
149 $message = $notick . ' ' . get_string('filereferencesnotsamesite', 'backup');
151 $html .= $this->backup_detail_pair(get_string('includefilereferences', 'backup'), $message);
154 $html .= html_writer::end_tag('div');
156 $html .= html_writer::start_tag('div', array('class' => 'backup-section settings-section'));
157 $html .= $this->output->heading(get_string('backupsettings', 'backup'), 2, array('class' => 'header'));
158 foreach ($details->root_settings as $label => $value) {
159 if ($label == 'filename' or $label == 'user_files') {
162 $html .= $this->backup_detail_pair(get_string('rootsetting'.str_replace('_', '', $label), 'backup'), $value ? $yestick : $notick);
164 $html .= html_writer::end_tag('div');
166 if ($details->type === 'course') {
167 $html .= html_writer::start_tag('div', array('class' => 'backup-section'));
168 $html .= $this->output->heading(get_string('backupcoursedetails', 'backup'), 2, array('class' => 'header'));
169 $html .= $this->backup_detail_pair(get_string('coursetitle', 'backup'), $details->course->title);
170 $html .= $this->backup_detail_pair(get_string('courseid', 'backup'), $details->course->courseid);
172 // Warning users about front page backups.
173 if ($details->original_course_format === 'site') {
174 $html .= $this->backup_detail_pair(get_string('type_format', 'plugin'), get_string('sitecourseformatwarning', 'backup'));
176 $html .= html_writer::start_tag('div', array('class' => 'backup-sub-section'));
177 $html .= $this->output->heading(get_string('backupcoursesections', 'backup'), 3, array('class' => 'subheader'));
178 foreach ($details->sections as $key => $section) {
179 $included = $key.'_included';
180 $userinfo = $key.'_userinfo';
181 if ($section->settings[$included] && $section->settings[$userinfo]) {
182 $value = get_string('sectionincanduser', 'backup');
183 } else if ($section->settings[$included]) {
184 $value = get_string('sectioninc', 'backup');
188 $html .= $this->backup_detail_pair(get_string('backupcoursesection', 'backup', $section->title), $value);
190 foreach ($details->activities as $activitykey => $activity) {
191 if ($activity->sectionid != $section->sectionid) {
195 $table = new html_table();
196 $table->head = array(get_string('module', 'backup'), get_string('title', 'backup'), get_string('userinfo', 'backup'));
197 $table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
198 $table->align = array('left', 'left', 'center');
199 $table->attributes = array('class' => 'activitytable generaltable');
200 $table->data = array();
202 $name = get_string('pluginname', $activity->modulename);
203 $icon = new image_icon('icon', $name, $activity->modulename, array('class' => 'iconlarge icon-pre'));
204 $table->data[] = array(
205 $this->output->render($icon).$name,
207 ($activity->settings[$activitykey.'_userinfo']) ? $yestick : $notick,
210 if (!empty($table)) {
211 $html .= $this->backup_detail_pair(get_string('sectionactivities', 'backup'), html_writer::table($table));
215 $html .= html_writer::end_tag('div');
216 $html .= html_writer::end_tag('div');
219 $html .= $this->continue_button($nextstageurl, 'post');
220 $html .= html_writer::end_tag('div');
226 * Displays the general information about a backup file with non-standard format
228 * @param moodle_url $nextstageurl URL to send user to
229 * @param array $details basic info about the file (format, type)
230 * @return string HTML code to display
232 public function backup_details_nonstandard($nextstageurl, array $details) {
234 $html = html_writer::start_tag('div', array('class' => 'backup-restore nonstandardformat'));
235 $html .= html_writer::start_tag('div', array('class' => 'backup-section'));
236 $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, 'header');
237 $html .= $this->output->box(get_string('backupdetailsnonstandardinfo', 'backup'), 'noticebox');
238 $html .= $this->backup_detail_pair(
239 get_string('backupformat', 'backup'),
240 get_string('backupformat'.$details['format'], 'backup'));
241 $html .= $this->backup_detail_pair(
242 get_string('backuptype', 'backup'),
243 get_string('backuptype'.$details['type'], 'backup'));
244 $html .= html_writer::end_tag('div');
245 $html .= $this->continue_button($nextstageurl, 'post');
246 $html .= html_writer::end_tag('div');
252 * Displays the general information about a backup file with unknown format
254 * @param moodle_url $nextstageurl URL to send user to
255 * @return string HTML code to display
257 public function backup_details_unknown(moodle_url $nextstageurl) {
259 $html = html_writer::start_div('unknownformat');
260 $html .= $this->output->heading(get_string('errorinvalidformat', 'backup'), 2);
261 $html .= $this->output->notification(get_string('errorinvalidformatinfo', 'backup'), 'notifyproblem');
262 $html .= $this->continue_button($nextstageurl, 'post');
263 $html .= html_writer::end_div();
269 * Displays a course selector for restore
271 * @param moodle_url $nextstageurl
272 * @param bool $wholecourse true if we are restoring whole course (as with backup::TYPE_1COURSE), false otherwise
273 * @param restore_category_search $categories
274 * @param restore_course_search $courses
275 * @param int $currentcourse
278 public function course_selector(moodle_url $nextstageurl, $wholecourse = true, restore_category_search $categories = null,
279 restore_course_search $courses = null, $currentcourse = null) {
281 require_once($CFG->dirroot.'/course/lib.php');
283 // These variables are used to check if the form using this function was submitted.
284 $target = optional_param('target', false, PARAM_INT);
285 $targetid = optional_param('targetid', null, PARAM_INT);
287 // Check if they submitted the form but did not provide all the data we need.
288 $missingdata = false;
289 if ($target and is_null($targetid)) {
293 $nextstageurl->param('sesskey', sesskey());
295 $form = html_writer::start_tag('form', array('method' => 'post', 'action' => $nextstageurl->out_omit_querystring(),
296 'class' => 'mform'));
297 foreach ($nextstageurl->params() as $key => $value) {
298 $form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
301 $hasrestoreoption = false;
303 $html = html_writer::start_tag('div', array('class' => 'backup-course-selector backup-restore'));
304 if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) {
306 $hasrestoreoption = true;
308 $html .= html_writer::start_tag('div', array('class' => 'bcs-new-course backup-section'));
309 $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class' => 'header'));
310 $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target',
311 backup::TARGET_NEW_COURSE, array('checked' => 'checked'));
312 $selectacategoryhtml = $this->backup_detail_pair(get_string('selectacategory', 'backup'), $this->render($categories));
313 // Display the category selection as required if the form was submitted but this data was not supplied.
314 if ($missingdata && $target == backup::TARGET_NEW_COURSE) {
315 $html .= html_writer::span(get_string('required'), 'error');
316 $html .= html_writer::start_tag('fieldset', array('class' => 'error'));
317 $html .= $selectacategoryhtml;
318 $html .= html_writer::end_tag('fieldset');
320 $html .= $selectacategoryhtml;
322 $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
323 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', $attrs));
324 $html .= html_writer::end_tag('div');
325 $html .= html_writer::end_tag('form');
328 if ($wholecourse && !empty($currentcourse)) {
330 $hasrestoreoption = true;
332 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'targetid', 'value' => $currentcourse));
333 $html .= html_writer::start_tag('div', array('class' => 'bcs-current-course backup-section'));
334 $html .= $this->output->heading(get_string('restoretocurrentcourse', 'backup'), 2, array('class' => 'header'));
335 $html .= $this->backup_detail_input(get_string('restoretocurrentcourseadding', 'backup'), 'radio', 'target',
336 backup::TARGET_CURRENT_ADDING, array('checked' => 'checked'));
337 $html .= $this->backup_detail_input(get_string('restoretocurrentcoursedeleting', 'backup'), 'radio', 'target',
338 backup::TARGET_CURRENT_DELETING);
339 $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
340 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', $attrs));
341 $html .= html_writer::end_tag('div');
342 $html .= html_writer::end_tag('form');
345 // If we are restoring an activity, then include the current course.
347 $courses->invalidate_results(); // Clean list of courses.
348 $courses->set_include_currentcourse();
350 if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search())) {
352 $hasrestoreoption = true;
354 $html .= html_writer::start_tag('div', array('class' => 'bcs-existing-course backup-section'));
355 $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class' => 'header'));
357 $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target',
358 backup::TARGET_EXISTING_ADDING, array('checked' => 'checked'));
359 $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target',
360 backup::TARGET_EXISTING_DELETING);
362 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'target', 'value' => backup::TARGET_EXISTING_ADDING));
364 $selectacoursehtml = $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
365 // Display the course selection as required if the form was submitted but this data was not supplied.
366 if ($missingdata && $target == backup::TARGET_EXISTING_ADDING) {
367 $html .= html_writer::span(get_string('required'), 'error');
368 $html .= html_writer::start_tag('fieldset', array('class' => 'error'));
369 $html .= $selectacoursehtml;
370 $html .= html_writer::end_tag('fieldset');
372 $html .= $selectacoursehtml;
374 $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
375 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', $attrs));
376 $html .= html_writer::end_tag('div');
377 $html .= html_writer::end_tag('form');
380 if (!$hasrestoreoption) {
381 echo $this->output->notification(get_string('norestoreoptions', 'backup'));
384 $html .= html_writer::end_tag('div');
389 * Displays the import course selector
391 * @param moodle_url $nextstageurl
392 * @param import_course_search $courses
395 public function import_course_selector(moodle_url $nextstageurl, import_course_search $courses = null) {
396 $html = html_writer::start_tag('div', array('class' => 'import-course-selector backup-restore'));
397 $html .= html_writer::start_tag('form', array('method' => 'post', 'action' => $nextstageurl->out_omit_querystring()));
398 foreach ($nextstageurl->params() as $key => $value) {
399 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
401 // We only allow import adding for now. Enforce it here.
402 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'target', 'value' => backup::TARGET_CURRENT_ADDING));
403 $html .= html_writer::start_tag('div', array('class' => 'ics-existing-course backup-section'));
404 $html .= $this->output->heading(get_string('importdatafrom'), 2, array('class' => 'header'));
405 $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
406 $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
407 $html .= html_writer::start_tag('div', array('class' => 'mt-3'));
408 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', $attrs));
409 $html .= html_writer::end_tag('div');
410 $html .= html_writer::end_tag('div');
411 $html .= html_writer::end_tag('form');
412 $html .= html_writer::end_tag('div');
417 * Creates a detailed pairing (key + value)
419 * @staticvar int $count
420 * @param string $label
421 * @param string $value
424 protected function backup_detail_pair($label, $value) {
427 $html = html_writer::start_tag('div', array('class' => 'detail-pair'));
428 $html .= html_writer::tag('label', $label, array('class' => 'detail-pair-label', 'for' => 'detail-pair-value-'.$count));
429 $html .= html_writer::tag('div', $value, array('class' => 'detail-pair-value pl-2', 'name' => 'detail-pair-value-'.$count));
430 $html .= html_writer::end_tag('div');
435 * Created a detailed pairing with an input
437 * @param string $label
438 * @param string $type
439 * @param string $name
440 * @param string $value
441 * @param array $attributes
442 * @param string|null $description
445 protected function backup_detail_input($label, $type, $name, $value, array $attributes = array(), $description = null) {
446 if (!empty($description)) {
447 $description = html_writer::tag('span', $description, array('class' => 'description'));
451 return $this->backup_detail_pair(
453 html_writer::empty_tag('input', $attributes + array('name' => $name, 'type' => $type, 'value' => $value)) . $description
458 * Creates a detailed pairing with a select
460 * @param string $label
461 * @param string $name
462 * @param array $options
463 * @param string $selected
464 * @param bool $nothing
465 * @param array $attributes
466 * @param string|null $description
469 protected function backup_detail_select($label, $name, $options, $selected = '', $nothing = false, array $attributes = array(), $description = null) {
470 if (!empty ($description)) {
471 $description = html_writer::tag('span', $description, array('class' => 'description'));
475 return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
479 * Displays precheck notices
481 * @param array $results
484 public function precheck_notices($results) {
485 $output = html_writer::start_tag('div', array('class' => 'restore-precheck-notices'));
486 if (array_key_exists('errors', $results)) {
487 foreach ($results['errors'] as $error) {
488 $output .= $this->output->notification($error);
491 if (array_key_exists('warnings', $results)) {
492 foreach ($results['warnings'] as $warning) {
493 $output .= $this->output->notification($warning, 'notifyproblem');
496 return $output.html_writer::end_tag('div');
500 * Displays substage buttons
502 * @param bool $haserrors
505 public function substage_buttons($haserrors) {
506 $output = html_writer::start_tag('div', array('continuebutton'));
508 $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
509 $output .= html_writer::empty_tag('input', $attrs);
511 $attrs = array('type' => 'submit', 'name' => 'cancel', 'value' => get_string('cancel'), 'class' => 'btn btn-secondary');
512 $output .= html_writer::empty_tag('input', $attrs);
513 $output .= html_writer::end_tag('div');
518 * Displays a role mapping interface
520 * @param array $rolemappings
521 * @param array $roles
524 public function role_mappings($rolemappings, $roles) {
525 $roles[0] = get_string('none');
526 $output = html_writer::start_tag('div', array('class' => 'restore-rolemappings'));
527 $output .= $this->output->heading(get_string('restorerolemappings', 'backup'), 2);
528 foreach ($rolemappings as $id => $mapping) {
529 $label = $mapping->name;
530 $name = 'mapping'.$id;
531 $selected = $mapping->targetroleid;
532 $output .= $this->backup_detail_select($label, $name, $roles, $mapping->targetroleid, false, array(), $mapping->description);
534 $output .= html_writer::end_tag('div');
539 * Displays a continue button
541 * @param string|moodle_url $url
542 * @param string $method
545 public function continue_button($url, $method = 'post') {
546 if (!($url instanceof moodle_url)) {
547 $url = new moodle_url($url);
549 if ($method != 'post') {
552 $url->param('sesskey', sesskey());
553 $button = new single_button($url, get_string('continue'), $method, true);
554 $button->class = 'continuebutton';
555 return $this->render($button);
558 * Print a backup files tree
559 * @param array $options
562 public function backup_files_viewer(array $options = null) {
563 $files = new backup_files_viewer($options);
564 return $this->render($files);
568 * Generate the status indicator markup for display in the
569 * backup restore file area UI.
571 * @param int $statuscode The status code of the backup.
572 * @param string $backupid The backup record id.
573 * @return string|boolean $status The status indicator for the operation.
575 public function get_status_display($statuscode, $backupid, $restoreid=null, $operation='backup') {
576 if ($statuscode == backup::STATUS_AWAITING
577 || $statuscode == backup::STATUS_EXECUTING
578 || $statuscode == backup::STATUS_REQUIRE_CONV) { // In progress.
579 $progresssetup = array(
580 'backupid' => $backupid,
581 'restoreid' => $restoreid,
582 'operation' => $operation,
585 $status = $this->render_from_template('core/async_backup_progress', $progresssetup);
586 } else if ($statuscode == backup::STATUS_FINISHED_ERR) { // Error.
587 $icon = $this->output->render(new \pix_icon('i/delete', get_string('failed', 'backup')));
588 $status = \html_writer::span($icon, 'action-icon');
589 } else if ($statuscode == backup::STATUS_FINISHED_OK) { // Complete.
590 $icon = $this->output->render(new \pix_icon('i/checked', get_string('successful', 'backup')));
591 $status = \html_writer::span($icon, 'action-icon');
598 * Displays a backup files viewer
600 * @global stdClass $USER
601 * @param backup_files_viewer $viewer
604 public function render_backup_files_viewer(backup_files_viewer $viewer) {
606 $files = $viewer->files;
608 $async = async_helper::is_async_enabled();
611 get_string('filename', 'backup'),
614 get_string('download'),
615 get_string('restore'));
617 $tablehead[] = get_string('status', 'backup');
620 $table = new html_table();
621 $table->attributes['class'] = 'backup-files-table generaltable';
622 $table->head = $tablehead;
623 $table->width = '100%';
624 $table->data = array();
626 // First add in progress asynchronous backups.
627 // Only if asynchronous backups are enabled.
628 // Also only render async status in correct area. Courese OR activity (not both).
630 && (($viewer->filearea == 'course' && $viewer->currentcontext->contextlevel == CONTEXT_COURSE)
631 || ($viewer->filearea == 'activity' && $viewer->currentcontext->contextlevel == CONTEXT_MODULE))
633 $table->data = \async_helper::get_async_backups($this, $viewer->currentcontext->instanceid);
636 // Add completed backups.
637 foreach ($files as $file) {
638 if ($file->is_directory()) {
641 $fileurl = moodle_url::make_pluginfile_url(
642 $file->get_contextid(),
643 $file->get_component(),
644 $file->get_filearea(),
646 $file->get_filepath(),
647 $file->get_filename(),
651 $params['action'] = 'choosebackupfile';
652 $params['filename'] = $file->get_filename();
653 $params['filepath'] = $file->get_filepath();
654 $params['component'] = $file->get_component();
655 $params['filearea'] = $file->get_filearea();
656 $params['filecontextid'] = $file->get_contextid();
657 $params['contextid'] = $viewer->currentcontext->id;
658 $params['itemid'] = $file->get_itemid();
659 $restoreurl = new moodle_url('/backup/restorefile.php', $params);
660 $restorelink = html_writer::link($restoreurl, get_string('restore'));
661 $downloadlink = html_writer::link($fileurl, get_string('download'));
663 // Conditional display of the restore and download links, initially only for the 'automated' filearea.
664 if ($params['filearea'] == 'automated') {
665 if (!has_capability('moodle/restore:viewautomatedfilearea', $viewer->currentcontext)) {
668 if (!can_download_from_backup_filearea($params['filearea'], $viewer->currentcontext)) {
673 $file->get_filename(),
674 userdate ($file->get_timemodified()),
675 display_size ($file->get_filesize()),
680 $tabledata[] = $this->get_status_display(backup::STATUS_FINISHED_OK, null);
683 $table->data[] = $tabledata;
686 $html = html_writer::table($table);
688 // For automated backups, the ability to manage backup files is controlled by the ability to download them.
689 // All files must be from the same file area in a backup_files_viewer.
690 $canmanagebackups = true;
691 if ($viewer->filearea == 'automated') {
692 if (!can_download_from_backup_filearea($viewer->filearea, $viewer->currentcontext)) {
693 $canmanagebackups = false;
697 if ($canmanagebackups) {
698 $html .= $this->output->single_button(
699 new moodle_url('/backup/backupfilesedit.php', array(
700 'currentcontext' => $viewer->currentcontext->id,
701 'contextid' => $viewer->filecontext->id,
702 'filearea' => $viewer->filearea,
703 'component' => $viewer->component,
704 'returnurl' => $this->page->url->out())
706 get_string('managefiles', 'backup'),
715 * Renders a restore course search object
717 * @param restore_course_search $component
720 public function render_restore_course_search(restore_course_search $component) {
721 $url = $component->get_url();
723 $output = html_writer::start_tag('div', array('class' => 'restore-course-search form-inline mb-1'));
724 $output .= html_writer::start_tag('div', array('class' => 'rcs-results table-sm w-75'));
726 $table = new html_table();
727 $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
728 $table->data = array();
729 if ($component->get_count() !== 0) {
730 foreach ($component->get_results() as $course) {
731 $row = new html_table_row();
732 $row->attributes['class'] = 'rcs-course';
733 if (!$course->visible) {
734 $row->attributes['class'] .= ' dimmed';
737 html_writer::empty_tag('input', array('type' => 'radio', 'name' => 'targetid', 'value' => $course->id)),
738 format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
739 format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
741 $table->data[] = $row;
743 if ($component->has_more_results()) {
744 $cell = new html_table_cell(get_string('moreresults', 'backup'));
746 $cell->attributes['class'] = 'notifyproblem';
747 $row = new html_table_row(array($cell));
748 $row->attributes['class'] = 'rcs-course';
749 $table->data[] = $row;
752 $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
754 $cell->attributes['class'] = 'notifyproblem';
755 $row = new html_table_row(array($cell));
756 $row->attributes['class'] = 'rcs-course';
757 $table->data[] = $row;
759 $output .= html_writer::table($table);
760 $output .= html_writer::end_tag('div');
762 $output .= html_writer::start_tag('div', array('class' => 'rcs-search'));
765 'name' => restore_course_search::$VAR_SEARCH,
766 'value' => $component->get_search(),
767 'class' => 'form-control'
769 $output .= html_writer::empty_tag('input', $attrs);
772 'name' => 'searchcourses',
773 'value' => get_string('search'),
774 'class' => 'btn btn-secondary'
776 $output .= html_writer::empty_tag('input', $attrs);
777 $output .= html_writer::end_tag('div');
779 $output .= html_writer::end_tag('div');
784 * Renders an import course search object
786 * @param import_course_search $component
789 public function render_import_course_search(import_course_search $component) {
790 $url = $component->get_url();
792 $output = html_writer::start_tag('div', array('class' => 'import-course-search'));
793 if ($component->get_count() === 0) {
794 $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
796 $output .= html_writer::start_tag('div', array('class' => 'ics-search form-inline'));
799 'name' => restore_course_search::$VAR_SEARCH,
800 'value' => $component->get_search(),
801 'class' => 'form-control'
803 $output .= html_writer::empty_tag('input', $attrs);
806 'name' => 'searchcourses',
807 'value' => get_string('search'),
808 'class' => 'btn btn-secondary ml-1'
810 $output .= html_writer::empty_tag('input', $attrs);
811 $output .= html_writer::end_tag('div');
813 $output .= html_writer::end_tag('div');
818 if ($component->has_more_results()) {
819 $countstr = get_string('morecoursesearchresults', 'backup', $component->get_count());
821 $countstr = get_string('totalcoursesearchresults', 'backup', $component->get_count());
824 $output .= html_writer::tag('div', $countstr, array('class' => 'ics-totalresults'));
825 $output .= html_writer::start_tag('div', array('class' => 'ics-results'));
827 $table = new html_table();
828 $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
829 $table->data = array();
830 foreach ($component->get_results() as $course) {
831 $row = new html_table_row();
832 $row->attributes['class'] = 'ics-course';
833 if (!$course->visible) {
834 $row->attributes['class'] .= ' dimmed';
837 html_writer::empty_tag('input', array('type' => 'radio', 'name' => 'importid', 'value' => $course->id)),
838 format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
839 format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
841 $table->data[] = $row;
843 if ($component->has_more_results()) {
844 $cell = new html_table_cell(get_string('moreresults', 'backup'));
846 $cell->attributes['class'] = 'notifyproblem';
847 $row = new html_table_row(array($cell));
848 $row->attributes['class'] = 'rcs-course';
849 $table->data[] = $row;
851 $output .= html_writer::table($table);
852 $output .= html_writer::end_tag('div');
854 $output .= html_writer::start_tag('div', array('class' => 'ics-search form-inline'));
857 'name' => restore_course_search::$VAR_SEARCH,
858 'value' => $component->get_search(),
859 'class' => 'form-control');
860 $output .= html_writer::empty_tag('input', $attrs);
863 'name' => 'searchcourses',
864 'value' => get_string('search'),
865 'class' => 'btn btn-secondary ml-1'
867 $output .= html_writer::empty_tag('input', $attrs);
868 $output .= html_writer::end_tag('div');
870 $output .= html_writer::end_tag('div');
875 * Renders a restore category search object
877 * @param restore_category_search $component
880 public function render_restore_category_search(restore_category_search $component) {
881 $url = $component->get_url();
883 $output = html_writer::start_tag('div', array('class' => 'restore-course-search form-inline mb-1'));
884 $output .= html_writer::start_tag('div', array('class' => 'rcs-results table-sm w-75'));
886 $table = new html_table();
887 $table->head = array('', get_string('name'), get_string('description'));
888 $table->data = array();
890 if ($component->get_count() !== 0) {
891 foreach ($component->get_results() as $category) {
892 $row = new html_table_row();
893 $row->attributes['class'] = 'rcs-course';
894 if (!$category->visible) {
895 $row->attributes['class'] .= ' dimmed';
897 $context = context_coursecat::instance($category->id);
899 html_writer::empty_tag('input', array('type' => 'radio', 'name' => 'targetid', 'value' => $category->id)),
900 format_string($category->name, true, array('context' => context_coursecat::instance($category->id))),
901 format_text(file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id,
902 'coursecat', 'description', null), $category->descriptionformat, array('overflowdiv' => true))
904 $table->data[] = $row;
906 if ($component->has_more_results()) {
907 $cell = new html_table_cell(get_string('moreresults', 'backup'));
908 $cell->attributes['class'] = 'notifyproblem';
910 $row = new html_table_row(array($cell));
911 $row->attributes['class'] = 'rcs-course';
912 $table->data[] = $row;
915 $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
917 $cell->attributes['class'] = 'notifyproblem';
918 $row = new html_table_row(array($cell));
919 $row->attributes['class'] = 'rcs-course';
920 $table->data[] = $row;
922 $output .= html_writer::table($table);
923 $output .= html_writer::end_tag('div');
925 $output .= html_writer::start_tag('div', array('class' => 'rcs-search'));
928 'name' => restore_category_search::$VAR_SEARCH,
929 'value' => $component->get_search(),
930 'class' => 'form-control'
932 $output .= html_writer::empty_tag('input', $attrs);
935 'name' => 'searchcourses',
936 'value' => get_string('search'),
937 'class' => 'btn btn-secondary'
939 $output .= html_writer::empty_tag('input', $attrs);
940 $output .= html_writer::end_tag('div');
942 $output .= html_writer::end_tag('div');
947 * Get markup to render table for all of a users async
948 * in progress restores.
950 * @param int $userid The Moodle user id.
951 * @param \context $context The Moodle context for these restores.
952 * @return string $html The table HTML.
954 public function restore_progress_viewer ($userid, $context) {
955 $tablehead = array(get_string('course'), get_string('time'), get_string('status', 'backup'));
957 $table = new html_table();
958 $table->attributes['class'] = 'backup-files-table generaltable';
959 $table->head = $tablehead;
960 $tabledata = array();
962 // Get all in progress async restores for this user.
963 $restores = \async_helper::get_async_restores($userid);
965 // For each backup get, new item name, time restore created and progress.
966 foreach ($restores as $restore) {
968 $restorename = \async_helper::get_restore_name($context);
969 $timecreated = $restore->timecreated;
970 $status = $this->get_status_display($restore->status, $restore->backupid, $restore->backupid, null, 'restore');
972 $tablerow = array($restorename, userdate($timecreated), $status);
973 $tabledata[] = $tablerow;
976 $table->data = $tabledata;
977 $html = html_writer::table($table);
983 * Get markup to render table for all of a users course copies.
985 * @param int $userid The Moodle user id.
986 * @param int $courseid The id of the course to get the backups for.
987 * @return string $html The table HTML.
989 public function copy_progress_viewer(int $userid, int $courseid): string {
991 get_string('copysource', 'backup'),
992 get_string('copydest', 'backup'),
994 get_string('copyop', 'backup'),
995 get_string('status', 'backup')
998 $table = new html_table();
999 $table->attributes['class'] = 'backup-files-table generaltable';
1000 $table->head = $tablehead;
1002 $tabledata = array();
1004 // Get all in progress course copies for this user.
1005 $copies = \core_backup\copy\copy::get_copies($userid, $courseid);
1007 foreach ($copies as $copy) {
1008 $sourceurl = new \moodle_url('/course/view.php', array('id' => $copy->sourceid));
1011 html_writer::link($sourceurl, $copy->source),
1013 userdate($copy->time),
1014 get_string($copy->operation),
1015 $this->get_status_display($copy->status, $copy->backupid, $copy->restoreid, $copy->operation)
1017 $tabledata[] = $tablerow;
1020 $table->data = $tabledata;
1021 $html = html_writer::table($table);
1028 * Data structure representing backup files viewer
1030 * @copyright 2010 Dongsheng Cai
1031 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1034 class backup_files_viewer implements renderable {
1044 public $filecontext;
1059 public $currentcontext;
1062 * Constructor of backup_files_viewer class
1063 * @param array $options
1065 public function __construct(array $options = null) {
1067 $fs = get_file_storage();
1068 $this->currentcontext = $options['currentcontext'];
1069 $this->filecontext = $options['filecontext'];
1070 $this->component = $options['component'];
1071 $this->filearea = $options['filearea'];
1072 $files = $fs->get_area_files($this->filecontext->id, $this->component, $this->filearea, false, 'timecreated');
1073 $this->files = array_reverse($files);