MDL-69454 core_search: consistent backup search
[moodle.git] / backup / util / ui / renderer.php
CommitLineData
1904e9b3 1<?php
1904e9b3
SH
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * This file contains backup and restore output renderers
19 *
c866fe44 20 * @package core_backup
1904e9b3
SH
21 * @copyright 2010 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
2cd901a4
MP
25defined('MOODLE_INTERNAL') || die;
26
27global $CFG;
28require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
29require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
30require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
31
1904e9b3
SH
32/**
33 * The primary renderer for the backup.
34 *
35 * Can be retrieved with the following code:
36 * <?php
c866fe44 37 * $renderer = $PAGE->get_renderer('core', 'backup');
1904e9b3
SH
38 * ?>
39 *
c866fe44 40 * @package core_backup
1904e9b3
SH
41 * @copyright 2010 Sam Hemelryk
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class core_backup_renderer extends plugin_renderer_base {
c866fe44 45
d7e4481e
MP
46 /**
47 * Same site notification display.
48 *
49 * @var string
50 */
51 private $samesitenotification = '';
52
1904e9b3 53 /**
c866fe44
SH
54 * Renderers a progress bar for the backup or restore given the items that make it up.
55 *
1904e9b3
SH
56 * @param array $items An array of items
57 * @return string
58 */
59 public function progress_bar(array $items) {
60 foreach ($items as &$item) {
61 $text = $item['text'];
62 unset($item['text']);
4886029c
SH
63 if (array_key_exists('link', $item)) {
64 $link = $item['link'];
65 unset($item['link']);
66 $item = html_writer::link($link, $text, $item);
67 } else {
68 $item = html_writer::tag('span', $text, $item);
69 }
1904e9b3 70 }
c866fe44 71 return html_writer::tag('div', join(get_separator(), $items), array('class' => 'backup_progress clearfix'));
1904e9b3 72 }
77b3d9df 73
74 /**
75 * The backup and restore pages may display a log (if any) in a scrolling box.
76 *
77 * @param string $loghtml Log content in HTML format
78 * @return string HTML content that shows the log
79 */
80 public function log_display($loghtml) {
77b3d9df 81 $out = html_writer::start_div('backup_log');
2cd901a4 82 $out .= $this->output->heading(get_string('backuplog', 'backup'));
77b3d9df 83 $out .= html_writer::start_div('backup_log_contents');
84 $out .= $loghtml;
85 $out .= html_writer::end_div();
86 $out .= html_writer::end_div();
87 return $out;
88 }
89
d7e4481e
MP
90 /**
91 * Set the same site backup notification.
92 *
93 */
94 public function set_samesite_notification() {
95 $this->samesitenotification = $this->output->notification(get_string('samesitenotification', 'backup'), 'info');
96 }
97
98 /**
99 * Get the same site backup notification.
100 *
101 */
102 public function get_samesite_notification() {
103 return $this->samesitenotification;
104 }
105
1904e9b3
SH
106 /**
107 * Prints a dependency notification
c866fe44 108 *
1904e9b3
SH
109 * @param string $message
110 * @return string
111 */
112 public function dependency_notification($message) {
c866fe44 113 return html_writer::tag('div', $message, array('class' => 'notification dependencies_enforced'));
1904e9b3 114 }
785d6603 115
ad93ddd5
SH
116 /**
117 * Displays the details of a backup file
118 *
119 * @param stdClass $details
120 * @param moodle_url $nextstageurl
121 * @return string
122 */
785d6603 123 public function backup_details($details, $nextstageurl) {
c3847443 124 $yestick = $this->output->pix_icon('i/valid', get_string('yes'));
be885f12 125 $notick = $this->output->pix_icon('i/invalid', get_string('no'));
785d6603 126
c866fe44 127 $html = html_writer::start_tag('div', array('class' => 'backup-restore'));
785d6603 128
c866fe44
SH
129 $html .= html_writer::start_tag('div', array('class' => 'backup-section'));
130 $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, array('class' => 'header'));
785d6603
SH
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));
30ad8cdd 135 $html .= $this->backup_detail_pair(get_string('moodleversion', 'backup'),
c866fe44
SH
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')));
785d6603 138 $html .= $this->backup_detail_pair(get_string('backupversion', 'backup'),
c866fe44
SH
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')));
785d6603 141 $html .= $this->backup_detail_pair(get_string('originalwwwroot', 'backup'),
c866fe44
SH
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')));
67233725
DC
144 if (!empty($details->include_file_references_to_external_content)) {
145 $message = '';
146 if (backup_general_helper::backup_is_samesite($details)) {
147 $message = $yestick . ' ' . get_string('filereferencessamesite', 'backup');
148 } else {
149 $message = $notick . ' ' . get_string('filereferencesnotsamesite', 'backup');
150 }
151 $html .= $this->backup_detail_pair(get_string('includefilereferences', 'backup'), $message);
152 }
153
785d6603
SH
154 $html .= html_writer::end_tag('div');
155
c866fe44 156 $html .= html_writer::start_tag('div', array('class' => 'backup-section settings-section'));
2e8491a5 157 $html .= $this->output->heading(get_string('backupsettings', 'backup'), 2, array('class' => 'header'));
c866fe44 158 foreach ($details->root_settings as $label => $value) {
ac6dc09c
EL
159 if ($label == 'filename' or $label == 'user_files') {
160 continue;
161 }
c866fe44 162 $html .= $this->backup_detail_pair(get_string('rootsetting'.str_replace('_', '', $label), 'backup'), $value ? $yestick : $notick);
785d6603
SH
163 }
164 $html .= html_writer::end_tag('div');
165
1b42bc07 166 if ($details->type === 'course') {
c866fe44
SH
167 $html .= html_writer::start_tag('div', array('class' => 'backup-section'));
168 $html .= $this->output->heading(get_string('backupcoursedetails', 'backup'), 2, array('class' => 'header'));
1b42bc07
DM
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);
785d6603 171
99e434f2
DM
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'));
175 }
c866fe44
SH
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) {
1b42bc07
DM
179 $included = $key.'_included';
180 $userinfo = $key.'_userinfo';
181 if ($section->settings[$included] && $section->settings[$userinfo]) {
c866fe44 182 $value = get_string('sectionincanduser', 'backup');
1b42bc07 183 } else if ($section->settings[$included]) {
c866fe44 184 $value = get_string('sectioninc', 'backup');
1b42bc07 185 } else {
785d6603
SH
186 continue;
187 }
1b42bc07
DM
188 $html .= $this->backup_detail_pair(get_string('backupcoursesection', 'backup', $section->title), $value);
189 $table = null;
c866fe44 190 foreach ($details->activities as $activitykey => $activity) {
1b42bc07
DM
191 if ($activity->sectionid != $section->sectionid) {
192 continue;
193 }
194 if (empty($table)) {
195 $table = new html_table();
c866fe44 196 $table->head = array(get_string('module', 'backup'), get_string('title', 'backup'), get_string('userinfo', 'backup'));
1b42bc07 197 $table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
c866fe44
SH
198 $table->align = array('left', 'left', 'center');
199 $table->attributes = array('class' => 'activitytable generaltable');
1b42bc07
DM
200 $table->data = array();
201 }
202 $name = get_string('pluginname', $activity->modulename);
3e6adcd6 203 $icon = new image_icon('icon', $name, $activity->modulename, array('class' => 'iconlarge icon-pre'));
1b42bc07 204 $table->data[] = array(
d9f95015 205 $this->output->render($icon).$name,
1b42bc07 206 $activity->title,
c866fe44 207 ($activity->settings[$activitykey.'_userinfo']) ? $yestick : $notick,
1b42bc07
DM
208 );
209 }
210 if (!empty($table)) {
c866fe44 211 $html .= $this->backup_detail_pair(get_string('sectionactivities', 'backup'), html_writer::table($table));
785d6603 212 }
30ad8cdd 213
1b42bc07
DM
214 }
215 $html .= html_writer::end_tag('div');
216 $html .= html_writer::end_tag('div');
785d6603 217 }
785d6603 218
292b987c 219 $html .= $this->continue_button($nextstageurl, 'post');
83a9c866 220 $html .= html_writer::end_tag('div');
785d6603
SH
221
222 return $html;
223 }
224
f666f149
DM
225 /**
226 * Displays the general information about a backup file with non-standard format
227 *
f666f149 228 * @param moodle_url $nextstageurl URL to send user to
71d1751c 229 * @param array $details basic info about the file (format, type)
f666f149
DM
230 * @return string HTML code to display
231 */
71d1751c 232 public function backup_details_nonstandard($nextstageurl, array $details) {
f666f149
DM
233
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');
71d1751c 237 $html .= $this->output->box(get_string('backupdetailsnonstandardinfo', 'backup'), 'noticebox');
f666f149
DM
238 $html .= $this->backup_detail_pair(
239 get_string('backupformat', 'backup'),
71d1751c
DM
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'));
f666f149 244 $html .= html_writer::end_tag('div');
292b987c 245 $html .= $this->continue_button($nextstageurl, 'post');
f666f149
DM
246 $html .= html_writer::end_tag('div');
247
248 return $html;
249 }
250
251 /**
252 * Displays the general information about a backup file with unknown format
253 *
254 * @param moodle_url $nextstageurl URL to send user to
255 * @return string HTML code to display
256 */
257 public function backup_details_unknown(moodle_url $nextstageurl) {
258
a1071322 259 $html = html_writer::start_div('unknownformat');
9b2a2ffe
DS
260 $html .= $this->output->heading(get_string('errorinvalidformat', 'backup'), 2);
261 $html .= $this->output->notification(get_string('errorinvalidformatinfo', 'backup'), 'notifyproblem');
292b987c 262 $html .= $this->continue_button($nextstageurl, 'post');
a1071322 263 $html .= html_writer::end_div();
f666f149
DM
264
265 return $html;
266 }
267
ad93ddd5
SH
268 /**
269 * Displays a course selector for restore
270 *
271 * @param moodle_url $nextstageurl
3a076721 272 * @param bool $wholecourse true if we are restoring whole course (as with backup::TYPE_1COURSE), false otherwise
ad93ddd5
SH
273 * @param restore_category_search $categories
274 * @param restore_course_search $courses
275 * @param int $currentcourse
276 * @return string
277 */
c866fe44
SH
278 public function course_selector(moodle_url $nextstageurl, $wholecourse = true, restore_category_search $categories = null,
279 restore_course_search $courses = null, $currentcourse = null) {
1dcd0d34 280 global $CFG;
785d6603
SH
281 require_once($CFG->dirroot.'/course/lib.php');
282
c5f0d9f5
MN
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);
286
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)) {
290 $missingdata = true;
291 }
292
785d6603
SH
293 $nextstageurl->param('sesskey', sesskey());
294
c5f0d9f5
MN
295 $form = html_writer::start_tag('form', array('method' => 'post', 'action' => $nextstageurl->out_omit_querystring(),
296 'class' => 'mform'));
c866fe44
SH
297 foreach ($nextstageurl->params() as $key => $value) {
298 $form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
785d6603
SH
299 }
300
33d34b4b 301 $hasrestoreoption = false;
785d6603 302
c866fe44 303 $html = html_writer::start_tag('div', array('class' => 'backup-course-selector backup-restore'));
d62c5561 304 if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) {
c866fe44 305 // New course.
33d34b4b 306 $hasrestoreoption = true;
39bc4c6f 307 $html .= $form;
c866fe44
SH
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'));
c5f0d9f5
MN
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');
319 } else {
320 $html .= $selectacategoryhtml;
321 }
fe3db0ca
DW
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));
39bc4c6f
SH
324 $html .= html_writer::end_tag('div');
325 $html .= html_writer::end_tag('form');
326 }
785d6603 327
3a076721 328 if ($wholecourse && !empty($currentcourse)) {
c866fe44 329 // Current course.
33d34b4b 330 $hasrestoreoption = true;
10618448 331 $html .= $form;
c866fe44
SH
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);
fe3db0ca
DW
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));
10618448
SH
341 $html .= html_writer::end_tag('div');
342 $html .= html_writer::end_tag('form');
343 }
344
17fd194c
MN
345 // If we are restoring an activity, then include the current course.
346 if (!$wholecourse) {
347 $courses->invalidate_results(); // Clean list of courses.
348 $courses->set_include_currentcourse();
349 }
d62c5561 350 if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search())) {
c866fe44 351 // Existing course.
33d34b4b 352 $hasrestoreoption = true;
39bc4c6f 353 $html .= $form;
c866fe44
SH
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'));
3a076721 356 if ($wholecourse) {
c866fe44
SH
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);
165b7bd9 361 } else {
c866fe44 362 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'target', 'value' => backup::TARGET_EXISTING_ADDING));
c5f0d9f5
MN
363 }
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');
371 } else {
372 $html .= $selectacoursehtml;
165b7bd9 373 }
fe3db0ca
DW
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));
39bc4c6f
SH
376 $html .= html_writer::end_tag('div');
377 $html .= html_writer::end_tag('form');
378 }
785d6603 379
33d34b4b 380 if (!$hasrestoreoption) {
c866fe44 381 echo $this->output->notification(get_string('norestoreoptions', 'backup'));
33d34b4b
SH
382 }
383
785d6603
SH
384 $html .= html_writer::end_tag('div');
385 return $html;
386 }
387
ad93ddd5
SH
388 /**
389 * Displays the import course selector
390 *
391 * @param moodle_url $nextstageurl
392 * @param import_course_search $courses
393 * @return string
394 */
c866fe44
SH
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));
ad93ddd5 400 }
dc2abd40 401 // We only allow import adding for now. Enforce it here.
c866fe44
SH
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'));
ad93ddd5 405 $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
fe3db0ca 406 $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
5b48fefa 407 $html .= html_writer::start_tag('div', array('class' => 'mt-3'));
fe3db0ca 408 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', $attrs));
ad93ddd5 409 $html .= html_writer::end_tag('div');
5b48fefa 410 $html .= html_writer::end_tag('div');
ad93ddd5
SH
411 $html .= html_writer::end_tag('form');
412 $html .= html_writer::end_tag('div');
413 return $html;
414 }
415
416 /**
417 * Creates a detailed pairing (key + value)
418 *
419 * @staticvar int $count
420 * @param string $label
421 * @param string $value
422 * @return string
423 */
785d6603 424 protected function backup_detail_pair($label, $value) {
c866fe44
SH
425 static $count = 0;
426 $count ++;
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));
3ec69c2e 429 $html .= html_writer::tag('div', $value, array('class' => 'detail-pair-value pl-2', 'name' => 'detail-pair-value-'.$count));
785d6603
SH
430 $html .= html_writer::end_tag('div');
431 return $html;
432 }
433
ad93ddd5
SH
434 /**
435 * Created a detailed pairing with an input
436 *
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
443 * @return string
444 */
c866fe44
SH
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'));
3acc9b81
SH
448 } else {
449 $description = '';
450 }
c866fe44
SH
451 return $this->backup_detail_pair(
452 $label,
453 html_writer::empty_tag('input', $attributes + array('name' => $name, 'type' => $type, 'value' => $value)) . $description
454 );
785d6603
SH
455 }
456
ad93ddd5
SH
457 /**
458 * Creates a detailed pairing with a select
459 *
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
467 * @return string
468 */
c866fe44 469 protected function backup_detail_select($label, $name, $options, $selected = '', $nothing = false, array $attributes = array(), $description = null) {
3acc9b81 470 if (!empty ($description)) {
c866fe44 471 $description = html_writer::tag('span', $description, array('class' => 'description'));
3acc9b81
SH
472 } else {
473 $description = '';
474 }
475 return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
785d6603
SH
476 }
477
ad93ddd5
SH
478 /**
479 * Displays precheck notices
480 *
481 * @param array $results
482 * @return string
483 */
785d6603 484 public function precheck_notices($results) {
c866fe44 485 $output = html_writer::start_tag('div', array('class' => 'restore-precheck-notices'));
785d6603
SH
486 if (array_key_exists('errors', $results)) {
487 foreach ($results['errors'] as $error) {
488 $output .= $this->output->notification($error);
489 }
490 }
491 if (array_key_exists('warnings', $results)) {
492 foreach ($results['warnings'] as $warning) {
54ae34ce 493 $output .= $this->output->notification($warning, 'notifyproblem');
785d6603
SH
494 }
495 }
496 return $output.html_writer::end_tag('div');
497 }
498
ad93ddd5
SH
499 /**
500 * Displays substage buttons
501 *
502 * @param bool $haserrors
503 * @return string
504 */
3acc9b81
SH
505 public function substage_buttons($haserrors) {
506 $output = html_writer::start_tag('div', array('continuebutton'));
507 if (!$haserrors) {
fe3db0ca
DW
508 $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
509 $output .= html_writer::empty_tag('input', $attrs);
3acc9b81 510 }
fe3db0ca
DW
511 $attrs = array('type' => 'submit', 'name' => 'cancel', 'value' => get_string('cancel'), 'class' => 'btn btn-secondary');
512 $output .= html_writer::empty_tag('input', $attrs);
3acc9b81
SH
513 $output .= html_writer::end_tag('div');
514 return $output;
515 }
516
ad93ddd5
SH
517 /**
518 * Displays a role mapping interface
519 *
520 * @param array $rolemappings
521 * @param array $roles
522 * @return string
523 */
3acc9b81
SH
524 public function role_mappings($rolemappings, $roles) {
525 $roles[0] = get_string('none');
c866fe44 526 $output = html_writer::start_tag('div', array('class' => 'restore-rolemappings'));
3acc9b81 527 $output .= $this->output->heading(get_string('restorerolemappings', 'backup'), 2);
c866fe44 528 foreach ($rolemappings as $id => $mapping) {
3acc9b81
SH
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);
533 }
534 $output .= html_writer::end_tag('div');
535 return $output;
536 }
537
ad93ddd5
SH
538 /**
539 * Displays a continue button
540 *
541 * @param string|moodle_url $url
542 * @param string $method
543 * @return string
544 */
c866fe44 545 public function continue_button($url, $method = 'post') {
785d6603
SH
546 if (!($url instanceof moodle_url)) {
547 $url = new moodle_url($url);
548 }
39bc4c6f
SH
549 if ($method != 'post') {
550 $method = 'get';
551 }
785d6603 552 $url->param('sesskey', sesskey());
5c3e1eed 553 $button = new single_button($url, get_string('continue'), $method, true);
785d6603
SH
554 $button->class = 'continuebutton';
555 return $this->render($button);
556 }
72f38ce7
DC
557 /**
558 * Print a backup files tree
72f38ce7
DC
559 * @param array $options
560 * @return string
561 */
30ad8cdd 562 public function backup_files_viewer(array $options = null) {
6aef525d
DC
563 $files = new backup_files_viewer($options);
564 return $this->render($files);
72f38ce7
DC
565 }
566
2cd901a4
MP
567 /**
568 * Generate the status indicator markup for display in the
569 * backup restore file area UI.
570 *
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.
574 */
01436f75
MP
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.
2cd901a4 579 $progresssetup = array(
01436f75
MP
580 'backupid' => $backupid,
581 'restoreid' => $restoreid,
582 'operation' => $operation,
583 'width' => '100'
2cd901a4
MP
584 );
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');
592 }
593
594 return $status;
595 }
596
ad93ddd5
SH
597 /**
598 * Displays a backup files viewer
599 *
600 * @global stdClass $USER
c866fe44 601 * @param backup_files_viewer $viewer
ad93ddd5
SH
602 * @return string
603 */
6aef525d
DC
604 public function render_backup_files_viewer(backup_files_viewer $viewer) {
605 global $CFG;
606 $files = $viewer->files;
72f38ce7 607
2cd901a4
MP
608 $async = async_helper::is_async_enabled();
609
610 $tablehead = array(
611 get_string('filename', 'backup'),
612 get_string('time'),
613 get_string('size'),
614 get_string('download'),
615 get_string('restore'));
616 if ($async) {
617 $tablehead[] = get_string('status', 'backup');
618 }
619
6aef525d 620 $table = new html_table();
bac233d3 621 $table->attributes['class'] = 'backup-files-table generaltable';
2cd901a4 622 $table->head = $tablehead;
6aef525d
DC
623 $table->width = '100%';
624 $table->data = array();
625
2cd901a4
MP
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).
629 if ($async
630 && (($viewer->filearea == 'course' && $viewer->currentcontext->contextlevel == CONTEXT_COURSE)
631 || ($viewer->filearea == 'activity' && $viewer->currentcontext->contextlevel == CONTEXT_MODULE))
632 ) {
633 $table->data = \async_helper::get_async_backups($this, $viewer->currentcontext->instanceid);
634 }
635
636 // Add completed backups.
6aef525d
DC
637 foreach ($files as $file) {
638 if ($file->is_directory()) {
639 continue;
72f38ce7 640 }
c866fe44
SH
641 $fileurl = moodle_url::make_pluginfile_url(
642 $file->get_contextid(),
643 $file->get_component(),
644 $file->get_filearea(),
645 null,
646 $file->get_filepath(),
647 $file->get_filename(),
648 true
649 );
6aef525d
DC
650 $params = array();
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);
5bbea733
JD
660 $restorelink = html_writer::link($restoreurl, get_string('restore'));
661 $downloadlink = html_writer::link($fileurl, get_string('download'));
662
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)) {
666 $restorelink = '';
667 }
668 if (!can_download_from_backup_filearea($params['filearea'], $viewer->currentcontext)) {
669 $downloadlink = '';
670 }
671 }
2cd901a4 672 $tabledata = array(
6aef525d 673 $file->get_filename(),
2cd901a4
MP
674 userdate ($file->get_timemodified()),
675 display_size ($file->get_filesize()),
5bbea733 676 $downloadlink,
2cd901a4
MP
677 $restorelink
678 );
679 if ($async) {
680 $tabledata[] = $this->get_status_display(backup::STATUS_FINISHED_OK, null);
681 }
682
683 $table->data[] = $tabledata;
72f38ce7 684 }
6aef525d
DC
685
686 $html = html_writer::table($table);
5bbea733
JD
687
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;
694 }
695 }
696
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())
705 ),
706 get_string('managefiles', 'backup'),
707 'post'
708 );
709 }
6aef525d 710
72f38ce7
DC
711 return $html;
712 }
3acc9b81 713
ad93ddd5
SH
714 /**
715 * Renders a restore course search object
716 *
717 * @param restore_course_search $component
718 * @return string
719 */
3acc9b81
SH
720 public function render_restore_course_search(restore_course_search $component) {
721 $url = $component->get_url();
722
eb9935c9 723 $output = html_writer::start_tag('div', array('class' => 'restore-course-search mb-1'));
40ea6753 724 $output .= html_writer::start_tag('div', array('class' => 'rcs-results table-sm w-75'));
3acc9b81
SH
725
726 $table = new html_table();
29cbe431 727 $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
3acc9b81 728 $table->data = array();
33d34b4b
SH
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';
735 }
736 $row->cells = array(
c866fe44 737 html_writer::empty_tag('input', array('type' => 'radio', 'name' => 'targetid', 'value' => $course->id)),
a689cd1d
AG
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)))
33d34b4b
SH
740 );
741 $table->data[] = $row;
3acc9b81 742 }
33d34b4b
SH
743 if ($component->has_more_results()) {
744 $cell = new html_table_cell(get_string('moreresults', 'backup'));
745 $cell->colspan = 3;
746 $cell->attributes['class'] = 'notifyproblem';
747 $row = new html_table_row(array($cell));
748 $row->attributes['class'] = 'rcs-course';
749 $table->data[] = $row;
750 }
751 } else {
752 $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
753 $cell->colspan = 3;
754 $cell->attributes['class'] = 'notifyproblem';
755 $row = new html_table_row(array($cell));
756 $row->attributes['class'] = 'rcs-course';
3acc9b81
SH
757 $table->data[] = $row;
758 }
759 $output .= html_writer::table($table);
3acc9b81
SH
760 $output .= html_writer::end_tag('div');
761
eb9935c9
BB
762 $data = [
763 'inform' => true,
764 'extraclasses' => 'rcs-search mb-3 w-25',
765 'inputname' => restore_course_search::$VAR_SEARCH,
766 'searchstring' => get_string('searchcourses'),
767 'query' => $component->get_search(),
768 ];
769 $output .= $this->output->render_from_template('core/search_input', $data);
30ad8cdd 770
3acc9b81
SH
771 $output .= html_writer::end_tag('div');
772 return $output;
773 }
774
ad93ddd5
SH
775 /**
776 * Renders an import course search object
777 *
778 * @param import_course_search $component
779 * @return string
780 */
781 public function render_import_course_search(import_course_search $component) {
782 $url = $component->get_url();
783
784 $output = html_writer::start_tag('div', array('class' => 'import-course-search'));
33d34b4b 785 if ($component->get_count() === 0) {
ad93ddd5 786 $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
a4df8560 787
5b48fefa 788 $output .= html_writer::start_tag('div', array('class' => 'ics-search form-inline'));
fe3db0ca
DW
789 $attrs = array(
790 'type' => 'text',
791 'name' => restore_course_search::$VAR_SEARCH,
792 'value' => $component->get_search(),
793 'class' => 'form-control'
794 );
795 $output .= html_writer::empty_tag('input', $attrs);
796 $attrs = array(
797 'type' => 'submit',
798 'name' => 'searchcourses',
799 'value' => get_string('search'),
5b48fefa 800 'class' => 'btn btn-secondary ml-1'
fe3db0ca
DW
801 );
802 $output .= html_writer::empty_tag('input', $attrs);
a4df8560
TL
803 $output .= html_writer::end_tag('div');
804
ad93ddd5
SH
805 $output .= html_writer::end_tag('div');
806 return $output;
807 }
808
534ee16e 809 $countstr = '';
c4dbfb2c 810 if ($component->has_more_results()) {
534ee16e 811 $countstr = get_string('morecoursesearchresults', 'backup', $component->get_count());
c4dbfb2c 812 } else {
534ee16e 813 $countstr = get_string('totalcoursesearchresults', 'backup', $component->get_count());
c4dbfb2c 814 }
ad93ddd5 815
c866fe44 816 $output .= html_writer::tag('div', $countstr, array('class' => 'ics-totalresults'));
ad93ddd5 817 $output .= html_writer::start_tag('div', array('class' => 'ics-results'));
ad93ddd5
SH
818
819 $table = new html_table();
29cbe431 820 $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
ad93ddd5
SH
821 $table->data = array();
822 foreach ($component->get_results() as $course) {
823 $row = new html_table_row();
824 $row->attributes['class'] = 'ics-course';
825 if (!$course->visible) {
826 $row->attributes['class'] .= ' dimmed';
827 }
828 $row->cells = array(
c866fe44 829 html_writer::empty_tag('input', array('type' => 'radio', 'name' => 'importid', 'value' => $course->id)),
a689cd1d
AG
830 format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
831 format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
ad93ddd5
SH
832 );
833 $table->data[] = $row;
834 }
c4dbfb2c
RL
835 if ($component->has_more_results()) {
836 $cell = new html_table_cell(get_string('moreresults', 'backup'));
837 $cell->colspan = 3;
838 $cell->attributes['class'] = 'notifyproblem';
839 $row = new html_table_row(array($cell));
840 $row->attributes['class'] = 'rcs-course';
841 $table->data[] = $row;
842 }
ad93ddd5 843 $output .= html_writer::table($table);
ad93ddd5
SH
844 $output .= html_writer::end_tag('div');
845
5b48fefa 846 $output .= html_writer::start_tag('div', array('class' => 'ics-search form-inline'));
fe3db0ca
DW
847 $attrs = array(
848 'type' => 'text',
849 'name' => restore_course_search::$VAR_SEARCH,
850 'value' => $component->get_search(),
851 'class' => 'form-control');
852 $output .= html_writer::empty_tag('input', $attrs);
853 $attrs = array(
854 'type' => 'submit',
855 'name' => 'searchcourses',
856 'value' => get_string('search'),
5b48fefa 857 'class' => 'btn btn-secondary ml-1'
fe3db0ca
DW
858 );
859 $output .= html_writer::empty_tag('input', $attrs);
ad93ddd5
SH
860 $output .= html_writer::end_tag('div');
861
862 $output .= html_writer::end_tag('div');
863 return $output;
864 }
865
866 /**
867 * Renders a restore category search object
6aef525d 868 *
ad93ddd5
SH
869 * @param restore_category_search $component
870 * @return string
871 */
3acc9b81
SH
872 public function render_restore_category_search(restore_category_search $component) {
873 $url = $component->get_url();
874
eb9935c9 875 $output = html_writer::start_tag('div', array('class' => 'restore-course-search mb-1'));
40ea6753 876 $output .= html_writer::start_tag('div', array('class' => 'rcs-results table-sm w-75'));
3acc9b81
SH
877
878 $table = new html_table();
879 $table->head = array('', get_string('name'), get_string('description'));
880 $table->data = array();
33d34b4b
SH
881
882 if ($component->get_count() !== 0) {
883 foreach ($component->get_results() as $category) {
884 $row = new html_table_row();
885 $row->attributes['class'] = 'rcs-course';
886 if (!$category->visible) {
887 $row->attributes['class'] .= ' dimmed';
888 }
56343109 889 $context = context_coursecat::instance($category->id);
33d34b4b 890 $row->cells = array(
c866fe44 891 html_writer::empty_tag('input', array('type' => 'radio', 'name' => 'targetid', 'value' => $category->id)),
a689cd1d 892 format_string($category->name, true, array('context' => context_coursecat::instance($category->id))),
56343109
DH
893 format_text(file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id,
894 'coursecat', 'description', null), $category->descriptionformat, array('overflowdiv' => true))
33d34b4b
SH
895 );
896 $table->data[] = $row;
3acc9b81 897 }
33d34b4b
SH
898 if ($component->has_more_results()) {
899 $cell = new html_table_cell(get_string('moreresults', 'backup'));
900 $cell->attributes['class'] = 'notifyproblem';
901 $cell->colspan = 3;
902 $row = new html_table_row(array($cell));
903 $row->attributes['class'] = 'rcs-course';
904 $table->data[] = $row;
905 }
906 } else {
907 $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
908 $cell->colspan = 3;
909 $cell->attributes['class'] = 'notifyproblem';
910 $row = new html_table_row(array($cell));
911 $row->attributes['class'] = 'rcs-course';
3acc9b81
SH
912 $table->data[] = $row;
913 }
914 $output .= html_writer::table($table);
3acc9b81
SH
915 $output .= html_writer::end_tag('div');
916
eb9935c9
BB
917 $data = [
918 'inform' => true,
919 'extraclasses' => 'rcs-search mb-3 w-25',
920 'inputname' => restore_category_search::$VAR_SEARCH,
921 'searchstring' => get_string('search'),
922 'query' => $component->get_search(),
923 ];
924 $output .= $this->output->render_from_template('core/search_input', $data);
3acc9b81
SH
925
926 $output .= html_writer::end_tag('div');
927 return $output;
928 }
2cd901a4
MP
929
930 /**
931 * Get markup to render table for all of a users async
932 * in progress restores.
933 *
934 * @param int $userid The Moodle user id.
935 * @param \context $context The Moodle context for these restores.
936 * @return string $html The table HTML.
937 */
938 public function restore_progress_viewer ($userid, $context) {
939 $tablehead = array(get_string('course'), get_string('time'), get_string('status', 'backup'));
940
941 $table = new html_table();
942 $table->attributes['class'] = 'backup-files-table generaltable';
943 $table->head = $tablehead;
944 $tabledata = array();
945
946 // Get all in progress async restores for this user.
947 $restores = \async_helper::get_async_restores($userid);
948
949 // For each backup get, new item name, time restore created and progress.
950 foreach ($restores as $restore) {
951
952 $restorename = \async_helper::get_restore_name($context);
953 $timecreated = $restore->timecreated;
01436f75 954 $status = $this->get_status_display($restore->status, $restore->backupid, $restore->backupid, null, 'restore');
2cd901a4
MP
955
956 $tablerow = array($restorename, userdate($timecreated), $status);
957 $tabledata[] = $tablerow;
958 }
959
960 $table->data = $tabledata;
961 $html = html_writer::table($table);
962
963 return $html;
964 }
01436f75
MP
965
966 /**
967 * Get markup to render table for all of a users course copies.
968 *
969 * @param int $userid The Moodle user id.
970 * @param int $courseid The id of the course to get the backups for.
971 * @return string $html The table HTML.
972 */
973 public function copy_progress_viewer(int $userid, int $courseid): string {
974 $tablehead = array(
975 get_string('copysource', 'backup'),
976 get_string('copydest', 'backup'),
977 get_string('time'),
978 get_string('copyop', 'backup'),
979 get_string('status', 'backup')
980 );
981
982 $table = new html_table();
983 $table->attributes['class'] = 'backup-files-table generaltable';
984 $table->head = $tablehead;
985
986 $tabledata = array();
987
988 // Get all in progress course copies for this user.
989 $copies = \core_backup\copy\copy::get_copies($userid, $courseid);
990
991 foreach ($copies as $copy) {
992 $sourceurl = new \moodle_url('/course/view.php', array('id' => $copy->sourceid));
993
994 $tablerow = array(
995 html_writer::link($sourceurl, $copy->source),
996 $copy->destination,
997 userdate($copy->time),
998 get_string($copy->operation),
999 $this->get_status_display($copy->status, $copy->backupid, $copy->restoreid, $copy->operation)
1000 );
1001 $tabledata[] = $tablerow;
1002 }
1003
1004 $table->data = $tabledata;
1005 $html = html_writer::table($table);
1006
1007 return $html;
1008 }
72f38ce7 1009}
6aef525d 1010
72f38ce7
DC
1011/**
1012 * Data structure representing backup files viewer
1013 *
1014 * @copyright 2010 Dongsheng Cai
1015 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1016 * @since Moodle 2.0
1017 */
1018class backup_files_viewer implements renderable {
c866fe44
SH
1019
1020 /**
1021 * @var array
1022 */
6aef525d 1023 public $files;
c866fe44
SH
1024
1025 /**
1026 * @var context
1027 */
6aef525d 1028 public $filecontext;
c866fe44
SH
1029
1030 /**
1031 * @var string
1032 */
6aef525d 1033 public $component;
c866fe44
SH
1034
1035 /**
1036 * @var string
1037 */
6aef525d 1038 public $filearea;
c866fe44
SH
1039
1040 /**
1041 * @var context
1042 */
6aef525d 1043 public $currentcontext;
72f38ce7
DC
1044
1045 /**
1046 * Constructor of backup_files_viewer class
72f38ce7
DC
1047 * @param array $options
1048 */
30ad8cdd
DC
1049 public function __construct(array $options = null) {
1050 global $CFG, $USER;
6aef525d
DC
1051 $fs = get_file_storage();
1052 $this->currentcontext = $options['currentcontext'];
1053 $this->filecontext = $options['filecontext'];
1054 $this->component = $options['component'];
1055 $this->filearea = $options['filearea'];
1056 $files = $fs->get_area_files($this->filecontext->id, $this->component, $this->filearea, false, 'timecreated');
1057 $this->files = array_reverse($files);
72f38ce7 1058 }
30ad8cdd 1059}