Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
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
 *
20
 * @package   core_backup
21
 * @copyright 2010 Sam Hemelryk
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die;
26
 
27
global $CFG;
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');
31
 
32
/**
33
 * The primary renderer for the backup.
34
 *
35
 * Can be retrieved with the following code:
36
 * <?php
37
 * $renderer = $PAGE->get_renderer('core', 'backup');
38
 * ?>
39
 *
40
 * @package   core_backup
41
 * @copyright 2010 Sam Hemelryk
42
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class core_backup_renderer extends plugin_renderer_base {
45
 
46
    /**
47
     * Same site notification display.
48
     *
49
     * @var string
50
     */
51
    private $samesitenotification = '';
52
 
53
    /**
54
     * Renderers a progress bar for the backup or restore given the items that make it up.
55
     *
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']);
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
            }
70
        }
71
        return html_writer::tag('div', join(get_separator(), $items), array('class' => 'backup_progress clearfix'));
72
    }
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) {
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');
84
        $out .= $loghtml;
85
        $out .= html_writer::end_div();
86
        $out .= html_writer::end_div();
87
        return $out;
88
    }
89
 
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
 
106
    /**
107
     * Prints a dependency notification
108
     *
109
     * @param string $message
110
     * @return string
111
     */
112
    public function dependency_notification($message) {
113
        return html_writer::tag('div', $message, array('class' => 'notification dependencies_enforced'));
114
    }
115
 
116
    /**
117
     * Displays the details of a backup file
118
     *
119
     * @param stdClass $details
120
     * @param moodle_url $nextstageurl
121
     * @return string
122
     */
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'));
126
 
127
        $html  = html_writer::start_tag('div', array('class' => 'backup-restore'));
128
 
129
        $html .= html_writer::start_tag('div', ['class' => 'backup-section',
130
            'role' => 'table', 'aria-labelledby' => 'backupdetailsheader']);
131
        $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, 'header', 'backupdetailsheader');
132
        $html .= $this->backup_detail_pair(get_string('backuptype', 'backup'), get_string('backuptype'.$details->type, 'backup'));
133
        $html .= $this->backup_detail_pair(get_string('backupformat', 'backup'), get_string('backupformat'.$details->format, 'backup'));
134
        $html .= $this->backup_detail_pair(get_string('backupmode', 'backup'), get_string('backupmode'.$details->mode, 'backup'));
135
        $html .= $this->backup_detail_pair(get_string('backupdate', 'backup'), userdate($details->backup_date));
136
        $html .= $this->backup_detail_pair(get_string('moodleversion', 'backup'),
11 efrain 137
                html_writer::tag('span', s($details->moodle_release), array('class' => 'moodle_release')).
138
                html_writer::tag('span', '[' . s($details->moodle_version) .']', array('class' => 'moodle_version sub-detail')));
1 efrain 139
        $html .= $this->backup_detail_pair(get_string('backupversion', 'backup'),
11 efrain 140
                html_writer::tag('span', s($details->backup_release), array('class' => 'moodle_release')).
141
                html_writer::tag('span', '[' . s($details->backup_version) . ']', array('class' => 'moodle_version sub-detail')));
1 efrain 142
        $html .= $this->backup_detail_pair(get_string('originalwwwroot', 'backup'),
11 efrain 143
                html_writer::tag('span', s($details->original_wwwroot), array('class' => 'originalwwwroot')).
144
                html_writer::tag('span', '[' . s($details->original_site_identifier_hash) . ']', array('class' => 'sitehash sub-detail')));
1 efrain 145
        if (!empty($details->include_file_references_to_external_content)) {
146
            $message = '';
147
            if (backup_general_helper::backup_is_samesite($details)) {
148
                $message = $yestick . ' ' . get_string('filereferencessamesite', 'backup');
149
            } else {
150
                $message = $notick . ' ' . get_string('filereferencesnotsamesite', 'backup');
151
            }
152
            $html .= $this->backup_detail_pair(get_string('includefilereferences', 'backup'), $message);
153
        }
154
 
155
        $html .= html_writer::end_tag('div');
156
 
157
        $html .= html_writer::start_tag('div', ['class' => 'backup-section settings-section',
158
            'role' => 'table', 'aria-labelledby' => 'backupsettingsheader']);
159
        $html .= $this->output->heading(get_string('backupsettings', 'backup'), 2, 'header', 'backupsettingsheader');
160
        foreach ($details->root_settings as $label => $value) {
161
            if ($label == 'filename' or $label == 'user_files') {
162
                continue;
163
            }
164
            $html .= $this->backup_detail_pair(get_string('rootsetting'.str_replace('_', '', $label), 'backup'), $value ? $yestick : $notick);
165
        }
166
        $html .= html_writer::end_tag('div');
167
 
168
        if ($details->type === 'course') {
169
            $html .= html_writer::start_tag('div', ['class' => 'backup-section',
170
                    'role' => 'table', 'aria-labelledby' => 'backupcoursedetailsheader']);
171
            $html .= $this->output->heading(get_string('backupcoursedetails', 'backup'), 2, 'header', 'backupcoursedetailsheader');
11 efrain 172
            $html .= $this->backup_detail_pair(get_string('coursetitle', 'backup'), format_string($details->course->title));
173
            $html .= $this->backup_detail_pair(get_string('courseid', 'backup'), clean_param($details->course->courseid, PARAM_INT));
1 efrain 174
 
175
            // Warning users about front page backups.
176
            if ($details->original_course_format === 'site') {
177
                $html .= $this->backup_detail_pair(get_string('type_format', 'plugin'), get_string('sitecourseformatwarning', 'backup'));
178
            }
179
            $html .= html_writer::start_tag('div', array('class' => 'backup-sub-section'));
180
            $html .= $this->output->heading(get_string('backupcoursesections', 'backup'), 3, array('class' => 'subheader'));
181
            foreach ($details->sections as $key => $section) {
182
                $included = $key.'_included';
183
                $userinfo = $key.'_userinfo';
184
                if ($section->settings[$included] && $section->settings[$userinfo]) {
185
                    $value = get_string('sectionincanduser', 'backup');
186
                } else if ($section->settings[$included]) {
187
                    $value = get_string('sectioninc', 'backup');
188
                } else {
189
                    continue;
190
                }
11 efrain 191
                $html .= $this->backup_detail_pair(get_string('backupcoursesection', 'backup', format_string($section->title)), $value);
1 efrain 192
                $table = null;
193
                foreach ($details->activities as $activitykey => $activity) {
194
                    if ($activity->sectionid != $section->sectionid) {
195
                        continue;
196
                    }
197
                    if (empty($table)) {
198
                        $table = new html_table();
199
                        $table->head = array(get_string('module', 'backup'), get_string('title', 'backup'), get_string('userinfo', 'backup'));
200
                        $table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
201
                        $table->align = array('left', 'left', 'center');
202
                        $table->attributes = array('class' => 'activitytable generaltable');
203
                        $table->data = array();
204
                    }
205
                    $name = get_string('pluginname', $activity->modulename);
206
                    $icon = new image_icon('monologo', '', $activity->modulename, ['class' => 'iconlarge icon-pre']);
207
                    $table->data[] = array(
208
                        $this->output->render($icon).$name,
11 efrain 209
                        format_string($activity->title),
1 efrain 210
                        ($activity->settings[$activitykey.'_userinfo']) ? $yestick : $notick,
211
                    );
212
                }
213
                if (!empty($table)) {
214
                    $html .= $this->backup_detail_pair(get_string('sectionactivities', 'backup'), html_writer::table($table));
215
                }
216
 
217
            }
218
            $html .= html_writer::end_tag('div');
219
            $html .= html_writer::end_tag('div');
220
        }
221
 
222
        $html .= $this->continue_button($nextstageurl, 'post');
223
        $html .= html_writer::end_tag('div');
224
 
225
        return $html;
226
    }
227
 
228
    /**
229
     * Displays the general information about a backup file with non-standard format
230
     *
231
     * @param moodle_url $nextstageurl URL to send user to
232
     * @param array $details basic info about the file (format, type)
233
     * @return string HTML code to display
234
     */
235
    public function backup_details_nonstandard($nextstageurl, array $details) {
236
 
237
        $html  = html_writer::start_tag('div', array('class' => 'backup-restore nonstandardformat'));
238
        $html .= html_writer::start_tag('div', array('class' => 'backup-section'));
239
        $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, 'header');
240
        $html .= $this->output->box(get_string('backupdetailsnonstandardinfo', 'backup'), 'noticebox');
241
        $html .= $this->backup_detail_pair(
242
            get_string('backupformat', 'backup'),
243
            get_string('backupformat'.$details['format'], 'backup'));
244
        $html .= $this->backup_detail_pair(
245
            get_string('backuptype', 'backup'),
246
            get_string('backuptype'.$details['type'], 'backup'));
247
        $html .= html_writer::end_tag('div');
248
        $html .= $this->continue_button($nextstageurl, 'post');
249
        $html .= html_writer::end_tag('div');
250
 
251
        return $html;
252
    }
253
 
254
    /**
255
     * Displays the general information about a backup file with unknown format
256
     *
257
     * @param moodle_url $nextstageurl URL to send user to
258
     * @return string HTML code to display
259
     */
260
    public function backup_details_unknown(moodle_url $nextstageurl) {
261
 
262
        $html  = html_writer::start_div('unknownformat');
263
        $html .= $this->output->heading(get_string('errorinvalidformat', 'backup'), 2);
264
        $html .= $this->output->notification(get_string('errorinvalidformatinfo', 'backup'), 'notifyproblem');
265
        $html .= $this->continue_button($nextstageurl, 'post');
266
        $html .= html_writer::end_div();
267
 
268
        return $html;
269
    }
270
 
271
    /**
272
     * Displays a course selector for restore
273
     *
274
     * @param moodle_url $nextstageurl
275
     * @param bool $wholecourse true if we are restoring whole course (as with backup::TYPE_1COURSE), false otherwise
276
     * @param restore_category_search $categories
277
     * @param restore_course_search $courses
278
     * @param int $currentcourse
279
     * @return string
280
     */
281
    public function course_selector(moodle_url $nextstageurl, $wholecourse = true, restore_category_search $categories = null,
282
                                    restore_course_search $courses = null, $currentcourse = null) {
283
        global $CFG;
284
        require_once($CFG->dirroot.'/course/lib.php');
285
 
286
        // These variables are used to check if the form using this function was submitted.
287
        $target = optional_param('target', false, PARAM_INT);
288
        $targetid = optional_param('targetid', null, PARAM_INT);
289
 
290
        // Check if they submitted the form but did not provide all the data we need.
291
        $missingdata = false;
292
        if ($target and is_null($targetid)) {
293
            $missingdata = true;
294
        }
295
 
296
        $nextstageurl->param('sesskey', sesskey());
297
 
298
        $form = html_writer::start_tag('form', array('method' => 'post', 'action' => $nextstageurl->out_omit_querystring(),
299
            'class' => 'mform'));
300
        foreach ($nextstageurl->params() as $key => $value) {
301
            $form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
302
        }
303
 
304
        $hasrestoreoption = false;
305
 
306
        $html  = html_writer::start_tag('div', array('class' => 'backup-course-selector backup-restore'));
307
        if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) {
308
            // New course.
309
            $hasrestoreoption = true;
310
            $html .= $form;
311
            $html .= html_writer::start_tag('div', array('class' => 'bcs-new-course backup-section'));
312
            $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class' => 'header'));
313
            $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target',
314
                backup::TARGET_NEW_COURSE, array('checked' => 'checked'));
315
            $selectacategoryhtml = $this->backup_detail_pair(get_string('selectacategory', 'backup'), $this->render($categories));
316
            // Display the category selection as required if the form was submitted but this data was not supplied.
317
            if ($missingdata && $target == backup::TARGET_NEW_COURSE) {
318
                $html .= html_writer::span(get_string('required'), 'error');
319
                $html .= html_writer::start_tag('fieldset', array('class' => 'error'));
320
                $html .= $selectacategoryhtml;
321
                $html .= html_writer::end_tag('fieldset');
322
            } else {
323
                $html .= $selectacategoryhtml;
324
            }
325
            $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
326
            $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', $attrs));
327
            $html .= html_writer::end_tag('div');
328
            $html .= html_writer::end_tag('form');
329
        }
330
 
331
        if ($wholecourse && !empty($currentcourse)) {
332
            // Current course.
333
            $hasrestoreoption = true;
334
            $html .= $form;
335
            $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'targetid', 'value' => $currentcourse));
336
            $html .= html_writer::start_tag('div', array('class' => 'bcs-current-course backup-section'));
337
            $html .= $this->output->heading(get_string('restoretocurrentcourse', 'backup'), 2, array('class' => 'header'));
338
            $html .= $this->backup_detail_input(get_string('restoretocurrentcourseadding', 'backup'), 'radio', 'target',
339
                backup::TARGET_CURRENT_ADDING, array('checked' => 'checked'));
340
            $html .= $this->backup_detail_input(get_string('restoretocurrentcoursedeleting', 'backup'), 'radio', 'target',
341
                backup::TARGET_CURRENT_DELETING);
342
            $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
343
            $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', $attrs));
344
            $html .= html_writer::end_tag('div');
345
            $html .= html_writer::end_tag('form');
346
        }
347
 
348
        // If we are restoring an activity, then include the current course.
349
        if (!$wholecourse) {
350
            $courses->invalidate_results(); // Clean list of courses.
351
            $courses->set_include_currentcourse();
352
        }
353
        if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search())) {
354
            // Existing course.
355
            $hasrestoreoption = true;
356
            $html .= $form;
357
            $html .= html_writer::start_tag('div', array('class' => 'bcs-existing-course backup-section'));
358
            $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class' => 'header'));
359
            if ($wholecourse) {
360
                $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target',
361
                    backup::TARGET_EXISTING_ADDING, array('checked' => 'checked'));
362
                $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target',
363
                    backup::TARGET_EXISTING_DELETING);
364
            } else {
365
                $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'target', 'value' => backup::TARGET_EXISTING_ADDING));
366
            }
367
            $selectacoursehtml = $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
368
            // Display the course selection as required if the form was submitted but this data was not supplied.
369
            if ($missingdata && $target == backup::TARGET_EXISTING_ADDING) {
370
                $html .= html_writer::span(get_string('required'), 'error');
371
                $html .= html_writer::start_tag('fieldset', array('class' => 'error'));
372
                $html .= $selectacoursehtml;
373
                $html .= html_writer::end_tag('fieldset');
374
            } else {
375
                $html .= $selectacoursehtml;
376
            }
377
            $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
378
            $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', $attrs));
379
            $html .= html_writer::end_tag('div');
380
            $html .= html_writer::end_tag('form');
381
        }
382
 
383
        if (!$hasrestoreoption) {
384
            echo $this->output->notification(get_string('norestoreoptions', 'backup'));
385
        }
386
 
387
        $html .= html_writer::end_tag('div');
388
        return $html;
389
    }
390
 
391
    /**
392
     * Displays the import course selector
393
     *
394
     * @param moodle_url $nextstageurl
395
     * @param import_course_search $courses
396
     * @return string
397
     */
398
    public function import_course_selector(moodle_url $nextstageurl, import_course_search $courses = null) {
399
        $html  = html_writer::start_tag('div', array('class' => 'import-course-selector backup-restore'));
400
        $html .= html_writer::start_tag('form', array('method' => 'post', 'action' => $nextstageurl->out_omit_querystring()));
401
        foreach ($nextstageurl->params() as $key => $value) {
402
            $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
403
        }
404
        // We only allow import adding for now. Enforce it here.
405
        $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'target', 'value' => backup::TARGET_CURRENT_ADDING));
406
        $html .= html_writer::start_tag('div', array('class' => 'ics-existing-course backup-section'));
407
        $html .= $this->output->heading(get_string('importdatafrom'), 2, array('class' => 'header'));
408
        $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
409
        $attrs = array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary');
410
        $html .= html_writer::start_tag('div', array('class' => 'mt-3'));
411
        $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', $attrs));
412
        $html .= html_writer::end_tag('div');
413
        $html .= html_writer::end_tag('div');
414
        $html .= html_writer::end_tag('form');
415
        $html .= html_writer::end_tag('div');
416
        return $html;
417
    }
418
 
419
    /**
420
     * Creates a detailed pairing (key + value)
421
     *
422
     * @staticvar int $count
423
     * @param string $label
424
     * @param string $value
425
     * @return string
426
     */
427
    protected function backup_detail_pair($label, $value) {
428
        static $count = 0;
429
        $count ++;
430
        $html  = html_writer::start_tag('div', ['class' => 'detail-pair', 'role' => 'row']);
431
        $html .= html_writer::tag('div', $label, ['class' => 'detail-pair-label mb-2', 'role' => 'cell']);
432
        $html .= html_writer::tag('div', $value, ['class' => 'detail-pair-value pl-2', 'role' => 'cell']);
433
        $html .= html_writer::end_tag('div');
434
        return $html;
435
    }
436
 
437
    /**
438
     * Creates a unique id string by appending an incremental number to the prefix.
439
     *
440
     * @param string $prefix To be used as the left part of the id string.
441
     * @return string
442
     */
443
    protected function make_unique_id(string $prefix): string {
444
        static $count = 0;
445
 
446
        return $prefix . '-' . $count++;
447
    }
448
 
449
    /**
450
     * Created a detailed pairing with an input
451
     *
452
     * @param string $label
453
     * @param string $type
454
     * @param string $name
455
     * @param string $value
456
     * @param array $attributes
457
     * @param string|null $description
458
     * @return string
459
     */
460
    protected function backup_detail_input($label, $type, $name, $value, array $attributes = array(), $description = null) {
461
        if (!empty($description)) {
462
            $description = html_writer::tag('span', $description, array('class' => 'description'));
463
        } else {
464
            $description = '';
465
        }
466
        $id = $this->make_unique_id('detail-pair-value');
467
        return $this->backup_detail_pair(
468
            html_writer::label($label, $id),
469
            html_writer::empty_tag('input', $attributes + ['id' => $id, 'name' => $name, 'type' => $type, 'value' => $value]) .
470
                $description
471
        );
472
    }
473
 
474
    /**
475
     * Creates a detailed pairing with a select
476
     *
477
     * @param string $label
478
     * @param string $name
479
     * @param array $options
480
     * @param string $selected
481
     * @param bool $nothing
482
     * @param array $attributes
483
     * @param string|null $description
484
     * @return string
485
     */
486
    protected function backup_detail_select($label, $name, $options, $selected = '', $nothing = false, array $attributes = array(), $description = null) {
487
        if (!empty ($description)) {
488
            $description = html_writer::tag('span', $description, array('class' => 'description'));
489
        } else {
490
            $description = '';
491
        }
492
        return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
493
    }
494
 
495
    /**
496
     * Displays precheck notices
497
     *
498
     * @param array $results
499
     * @return string
500
     */
501
    public function precheck_notices($results) {
502
        $output = html_writer::start_tag('div', array('class' => 'restore-precheck-notices'));
503
        if (array_key_exists('errors', $results)) {
504
            foreach ($results['errors'] as $error) {
505
                $output .= $this->output->notification($error);
506
            }
507
        }
508
        if (array_key_exists('warnings', $results)) {
509
            foreach ($results['warnings'] as $warning) {
510
                $output .= $this->output->notification($warning, 'notifyproblem');
511
            }
512
        }
513
        return $output.html_writer::end_tag('div');
514
    }
515
 
516
    /**
517
     * Displays substage buttons
518
     *
519
     * @param bool $haserrors
520
     * @return string
521
     */
522
    public function substage_buttons($haserrors) {
523
        $output  = html_writer::start_tag('div', array('continuebutton'));
524
        if (!$haserrors) {
11 efrain 525
            $attrs = ['type' => 'submit', 'value' => get_string('continue'), 'class' => 'btn btn-primary mr-1'];
1 efrain 526
            $output .= html_writer::empty_tag('input', $attrs);
527
        }
528
        $attrs = array('type' => 'submit', 'name' => 'cancel', 'value' => get_string('cancel'), 'class' => 'btn btn-secondary');
529
        $output .= html_writer::empty_tag('input', $attrs);
530
        $output .= html_writer::end_tag('div');
531
        return $output;
532
    }
533
 
534
    /**
535
     * Displays a role mapping interface
536
     *
537
     * @param array $rolemappings
538
     * @param array $roles
539
     * @return string
540
     */
541
    public function role_mappings($rolemappings, $roles) {
542
        $roles[0] = get_string('none');
543
        $output  = html_writer::start_tag('div', array('class' => 'restore-rolemappings'));
544
        $output .= $this->output->heading(get_string('restorerolemappings', 'backup'), 2);
545
        foreach ($rolemappings as $id => $mapping) {
546
            $label = $mapping->name;
547
            $name = 'mapping'.$id;
548
            $selected = $mapping->targetroleid;
549
            $output .= $this->backup_detail_select($label, $name, $roles, $mapping->targetroleid, false, array(), $mapping->description);
550
        }
551
        $output .= html_writer::end_tag('div');
552
        return $output;
553
    }
554
 
555
    /**
556
     * Displays a continue button, overriding core renderer method of the same in order
557
     * to override submission method of the button form
558
     *
559
     * @param string|moodle_url $url
560
     * @param string $method
561
     * @return string
562
     */
563
    public function continue_button($url, $method = 'post') {
564
        if (!($url instanceof moodle_url)) {
565
            $url = new moodle_url($url);
566
        }
567
        if ($method != 'post') {
568
            $method = 'get';
569
        }
570
        $button = new single_button($url, get_string('continue'), $method, single_button::BUTTON_PRIMARY);
571
        $button->class = 'continuebutton';
572
        return $this->render($button);
573
    }
574
    /**
575
     * Print a backup files tree
576
     * @param array $options
577
     * @return string
578
     */
579
    public function backup_files_viewer(array $options = null) {
580
        $files = new backup_files_viewer($options);
581
        return $this->render($files);
582
    }
583
 
584
    /**
585
     * Generate the status indicator markup for display in the
586
     * backup restore file area UI.
587
     *
588
     * @param int $statuscode The status code of the backup.
589
     * @param string $backupid The backup record id.
590
     * @return string|boolean $status The status indicator for the operation.
591
     */
592
    public function get_status_display($statuscode, $backupid, $restoreid=null, $operation='backup') {
593
        if ($statuscode == backup::STATUS_AWAITING
594
            || $statuscode == backup::STATUS_EXECUTING
595
            || $statuscode == backup::STATUS_REQUIRE_CONV) {  // In progress.
596
            $progresssetup = array(
597
                'backupid' => $backupid,
598
                'restoreid' => $restoreid,
599
                'operation' => $operation,
600
                'width' => '100'
601
            );
602
            $status = $this->render_from_template('core/async_backup_progress', $progresssetup);
603
        } else if ($statuscode == backup::STATUS_FINISHED_ERR) { // Error.
604
            $icon = $this->output->render(new \pix_icon('i/delete', get_string('failed', 'backup')));
605
            $status = \html_writer::span($icon, 'action-icon');
606
        } else if ($statuscode == backup::STATUS_FINISHED_OK) { // Complete.
607
            $icon = $this->output->render(new \pix_icon('i/checked', get_string('successful', 'backup')));
608
            $status = \html_writer::span($icon, 'action-icon');
609
        }
610
 
611
        return $status;
612
    }
613
 
614
    /**
615
     * Displays a backup files viewer
616
     *
617
     * @global stdClass $USER
618
     * @param backup_files_viewer $viewer
619
     * @return string
620
     */
621
    public function render_backup_files_viewer(backup_files_viewer $viewer) {
622
 
623
        $files = $viewer->files;
624
        $filestodisplay = false;
625
        foreach ($files as $file) {
626
            if (!$file->is_directory()) {
627
                $filestodisplay = true;
628
                break;
629
            }
630
        }
631
 
632
        $async = \async_helper::is_async_enabled();
633
 
634
        switch($viewer->filearea) {
635
            case 'activity':
636
                $title = get_string('choosefilefromactivitybackup', 'backup');
637
                $description = get_string('choosefilefromactivitybackup_help', 'backup');
638
                $button = get_string('managefiles_activity', 'backup');
639
                $nofilesstring = get_string('restorenofilesbackuparea_activity', 'backup');
640
                break;
641
            case 'course':
642
                $title = get_string('choosefilefromcoursebackup', 'backup');
643
                $description = get_string('choosefilefromcoursebackup_help', 'backup');
644
                $button = get_string('managefiles_course', 'backup');
645
                $nofilesstring = get_string('restorenofilesbackuparea_course', 'backup');
646
                break;
647
            case 'backup':
648
                $title = get_string('choosefilefromuserbackup', 'backup');
649
                $description = get_string('choosefilefromuserbackup_help', 'backup');
650
                $button = get_string('managefiles_backup', 'backup');
651
                $nofilesstring = get_string('restorenofilesbackuparea_backup', 'backup');
652
                break;
653
            case 'automated':
654
                $title = get_string('choosefilefromautomatedbackup', 'backup');
655
                $description = get_string('choosefilefromautomatedbackup_help', 'backup');
656
                $button = get_string('managefiles_automated', 'backup');
657
                $nofilesstring = get_string('restorenofilesbackuparea_automated', 'backup');
658
                break;
659
            default:
660
                $title = '';
661
                $description = '';
662
                $button = get_string('managefiles', 'backup');
663
                $nofilesstring = get_string('restorenofilesbackuparea', 'backup');
664
        }
665
 
666
        $html = html_writer::tag('h3', $title, ['class' => 'mt-6']);
667
        $html .= html_writer::tag('div', $description, ['class' => 'mb-3']);
668
 
669
        if ($filestodisplay || $async) {
670
            $tablehead = [
671
                get_string('filename', 'backup'),
672
                get_string('time'),
673
                get_string('size'),
674
                get_string('download'),
675
                get_string('restore'),
676
            ];
677
            if ($async) {
678
                $tablehead[] = get_string('status', 'backup');
679
            }
680
 
681
            $table = new html_table();
682
            $table->attributes['class'] = 'backup-files-table generaltable';
683
            $table->head = $tablehead;
684
            $table->width = '100%';
685
            $table->data = [];
686
 
687
            // First add in progress asynchronous backups.
688
            // Only if asynchronous backups are enabled.
689
            if ($async) {
690
                $tabledata = [];
691
                $backups = \async_helper::get_async_backups($viewer->filearea, $viewer->filecontext->instanceid);
692
                // For each backup get, new item name, time restore created and progress.
693
                foreach ($backups as $backup) {
694
                    $status = $this->get_status_display($backup->status, $backup->backupid);
695
                    $timecreated = $backup->timecreated;
696
                    $tablerow = [$backup->filename, userdate($timecreated), '-', '-', '-', $status];
697
                    $tabledata[] = $tablerow;
698
                }
699
                $table->data = $tabledata;
700
            }
701
 
702
            // Add completed backups.
703
            foreach ($files as $file) {
704
                if ($file->is_directory()) {
705
                    continue;
706
                }
707
                $fileurl = moodle_url::make_pluginfile_url(
708
                    $file->get_contextid(),
709
                    $file->get_component(),
710
                    $file->get_filearea(),
711
                    null,
712
                    $file->get_filepath(),
713
                    $file->get_filename(),
714
                    true
715
                );
716
                $params = [];
717
                $params['action'] = 'choosebackupfile';
718
                $params['filename'] = $file->get_filename();
719
                $params['filepath'] = $file->get_filepath();
720
                $params['component'] = $file->get_component();
721
                $params['filearea'] = $file->get_filearea();
722
                $params['filecontextid'] = $file->get_contextid();
723
                $params['contextid'] = $viewer->currentcontext->id;
724
                $params['itemid'] = $file->get_itemid();
725
                $restoreurl = new moodle_url('/backup/restorefile.php', $params);
726
                $restorelink = html_writer::link($restoreurl, get_string('restore'));
727
                $downloadlink = html_writer::link($fileurl, get_string('download'));
728
 
729
                // Conditional display of the restore and download links, initially only for the 'automated' filearea.
730
                if ($params['filearea'] == 'automated') {
731
                    if (!has_capability('moodle/restore:viewautomatedfilearea', $viewer->currentcontext)) {
732
                        $restorelink = '';
733
                    }
734
                    if (!can_download_from_backup_filearea($params['filearea'], $viewer->currentcontext)) {
735
                        $downloadlink = '';
736
                    }
737
                }
738
                $tabledata = [
739
                    $file->get_filename(),
740
                    userdate ($file->get_timemodified()),
741
                    display_size ($file->get_filesize()),
742
                    $downloadlink,
743
                    $restorelink,
744
                ];
745
                if ($async) {
746
                    $tabledata[] = $this->get_status_display(backup::STATUS_FINISHED_OK, null);
747
                }
748
 
749
                $table->data[] = $tabledata;
750
            }
751
 
752
            $html .= html_writer::table($table);
753
        } else {
754
            // There are no files to display.
755
            $html .= $this->notification($nofilesstring, 'notifymessage');
756
 
757
        }
758
 
759
        // For automated backups, the ability to manage backup files is controlled by the ability to download them.
760
        // All files must be from the same file area in a backup_files_viewer.
761
        $canmanagebackups = true;
762
        if ($viewer->filearea == 'automated') {
763
            if (!can_download_from_backup_filearea($viewer->filearea, $viewer->currentcontext)) {
764
                $canmanagebackups = false;
765
            }
766
        }
767
 
768
        if ($canmanagebackups) {
769
            $html .= $this->output->single_button(
770
                new moodle_url('/backup/backupfilesedit.php', array(
771
                        'currentcontext' => $viewer->currentcontext->id,
772
                        'contextid' => $viewer->filecontext->id,
773
                        'filearea' => $viewer->filearea,
774
                        'component' => $viewer->component,
775
                        'returnurl' => $this->page->url->out())
776
                ),
777
                $button,
778
                'post'
779
            );
780
        }
781
 
782
        return $html;
783
    }
784
 
785
    /**
786
     * Renders a restore course search object
787
     *
788
     * @param restore_course_search $component
789
     * @return string
790
     */
791
    public function render_restore_course_search(restore_course_search $component) {
792
        $output = html_writer::start_tag('div', array('class' => 'restore-course-search mb-1'));
793
        $output .= html_writer::start_tag('div', array('class' => 'rcs-results table-sm w-75'));
794
 
795
        $table = new html_table();
796
        $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
797
        $table->data = array();
798
        if ($component->get_count() !== 0) {
799
            foreach ($component->get_results() as $course) {
800
                $row = new html_table_row();
801
                $row->attributes['class'] = 'rcs-course';
802
                if (!$course->visible) {
803
                    $row->attributes['class'] .= ' dimmed';
804
                }
805
                $id = $this->make_unique_id('restore-course');
806
                $attrs = ['type' => 'radio', 'name' => 'targetid', 'value' => $course->id, 'id' => $id];
807
                if ($course->id == $component->get_current_course_id()) {
808
                    $attrs['checked'] = 'checked';
809
                }
810
                $row->cells = [
811
                    html_writer::empty_tag('input', $attrs),
812
                    html_writer::label(
813
                        format_string($course->shortname, true, ['context' => context_course::instance($course->id)]),
814
                        $id,
815
                        true,
816
                        ['class' => 'd-block']
817
                    ),
818
                    format_string($course->fullname, true, ['context' => context_course::instance($course->id)])
819
                ];
820
                $table->data[] = $row;
821
            }
822
            if ($component->has_more_results()) {
823
                $cell = new html_table_cell(get_string('moreresults', 'backup'));
824
                $cell->colspan = 3;
825
                $cell->attributes['class'] = 'notifyproblem';
826
                $row = new html_table_row(array($cell));
827
                $row->attributes['class'] = 'rcs-course';
828
                $table->data[] = $row;
829
            }
830
        } else {
831
            $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
832
            $cell->colspan = 3;
833
            $cell->attributes['class'] = 'notifyproblem';
834
            $row = new html_table_row(array($cell));
835
            $row->attributes['class'] = 'rcs-course';
836
            $table->data[] = $row;
837
        }
838
        $output .= html_writer::table($table);
839
        $output .= html_writer::end_tag('div');
840
 
841
        $data = [
842
            'inform' => true,
843
            'extraclasses' => 'rcs-search mb-3 w-25',
844
            'inputname' => restore_course_search::$VAR_SEARCH,
845
            'searchstring' => get_string('searchcourses'),
846
            'buttonattributes' => [
847
                (object) ['key' => 'name', 'value' => 'searchcourses'],
848
                (object) ['key' => 'value', 'value' => 1],
849
            ],
850
            'query' => $component->get_search(),
851
        ];
852
        $output .= $this->output->render_from_template('core/search_input', $data);
853
 
854
        $output .= html_writer::end_tag('div');
855
        return $output;
856
    }
857
 
858
    /**
859
     * Renders an import course search object
860
     *
861
     * @param import_course_search $component
862
     * @return string
863
     */
864
    public function render_import_course_search(import_course_search $component) {
865
        $output = html_writer::start_tag('div', array('class' => 'import-course-search'));
866
        if ($component->get_count() === 0) {
867
            $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
868
 
869
            $output .= html_writer::start_tag('div', ['class' => 'ics-search d-flex flex-wrap align-items-center']);
870
            $attrs = array(
871
                'type' => 'text',
872
                'name' => restore_course_search::$VAR_SEARCH,
873
                'value' => $component->get_search(),
874
                'aria-label' => get_string('searchcourses'),
875
                'placeholder' => get_string('searchcourses'),
876
                'class' => 'form-control'
877
            );
878
            $output .= html_writer::empty_tag('input', $attrs);
879
            $attrs = array(
880
                'type' => 'submit',
881
                'name' => 'searchcourses',
882
                'value' => get_string('search'),
883
                'class' => 'btn btn-secondary ml-1'
884
            );
885
            $output .= html_writer::empty_tag('input', $attrs);
886
            $output .= html_writer::end_tag('div');
887
 
888
            $output .= html_writer::end_tag('div');
889
            return $output;
890
        }
891
 
892
        $countstr = '';
893
        if ($component->has_more_results()) {
894
            $countstr = get_string('morecoursesearchresults', 'backup', $component->get_count());
895
        } else {
896
            $countstr = get_string('totalcoursesearchresults', 'backup', $component->get_count());
897
        }
898
 
899
        $output .= html_writer::tag('div', $countstr, array('class' => 'ics-totalresults'));
900
        $output .= html_writer::start_tag('div', array('class' => 'ics-results'));
901
 
902
        $table = new html_table();
903
        $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
904
        $table->data = array();
905
        foreach ($component->get_results() as $course) {
906
            $row = new html_table_row();
907
            $row->attributes['class'] = 'ics-course';
908
            if (!$course->visible) {
909
                $row->attributes['class'] .= ' dimmed';
910
            }
911
            $id = $this->make_unique_id('import-course');
912
            $row->cells = [
913
                html_writer::empty_tag('input', ['type' => 'radio', 'name' => 'importid', 'value' => $course->id,
914
                    'id' => $id]),
915
                html_writer::label(
916
                    format_string($course->shortname, true, ['context' => context_course::instance($course->id)]),
917
                    $id,
918
                    true,
919
                    ['class' => 'd-block']
920
                ),
921
                format_string($course->fullname, true, ['context' => context_course::instance($course->id)])
922
            ];
923
            $table->data[] = $row;
924
        }
925
        if ($component->has_more_results()) {
926
            $cell = new html_table_cell(get_string('moreresults', 'backup'));
927
            $cell->colspan = 3;
928
            $cell->attributes['class'] = 'notifyproblem';
929
            $row = new html_table_row(array($cell));
930
            $row->attributes['class'] = 'rcs-course';
931
            $table->data[] = $row;
932
        }
933
        $output .= html_writer::table($table);
934
        $output .= html_writer::end_tag('div');
935
 
936
        $output .= html_writer::start_tag('div', ['class' => 'ics-search d-flex flex-wrap align-items-center']);
937
        $attrs = array(
938
            'type' => 'text',
939
            'name' => restore_course_search::$VAR_SEARCH,
940
            'value' => $component->get_search(),
941
            'aria-label' => get_string('searchcourses'),
942
            'placeholder' => get_string('searchcourses'),
943
            'class' => 'form-control');
944
        $output .= html_writer::empty_tag('input', $attrs);
945
        $attrs = array(
946
            'type' => 'submit',
947
            'name' => 'searchcourses',
948
            'value' => get_string('search'),
949
            'class' => 'btn btn-secondary ml-1'
950
        );
951
        $output .= html_writer::empty_tag('input', $attrs);
952
        $output .= html_writer::end_tag('div');
953
 
954
        $output .= html_writer::end_tag('div');
955
        return $output;
956
    }
957
 
958
    /**
959
     * Renders a restore category search object
960
     *
961
     * @param restore_category_search $component
962
     * @return string
963
     */
964
    public function render_restore_category_search(restore_category_search $component) {
965
        $output = html_writer::start_tag('div', array('class' => 'restore-course-search mb-1'));
966
        $output .= html_writer::start_tag('div', array('class' => 'rcs-results table-sm w-75'));
967
 
968
        $table = new html_table();
969
        $table->head = array('', get_string('name'), get_string('description'));
970
        $table->data = array();
971
 
972
        if ($component->get_count() !== 0) {
973
            foreach ($component->get_results() as $category) {
974
                $row = new html_table_row();
975
                $row->attributes['class'] = 'rcs-course';
976
                if (!$category->visible) {
977
                    $row->attributes['class'] .= ' dimmed';
978
                }
979
                $context = context_coursecat::instance($category->id);
980
                $id = $this->make_unique_id('restore-category');
981
                $row->cells = [
982
                    html_writer::empty_tag('input', ['type' => 'radio', 'name' => 'targetid', 'value' => $category->id,
983
                        'id' => $id]),
984
                    html_writer::label(
985
                        format_string($category->name, true, ['context' => context_coursecat::instance($category->id)]),
986
                        $id,
987
                        true,
988
                        ['class' => 'd-block']
989
                    ),
990
                    format_text(file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id,
991
                        'coursecat', 'description', null), $category->descriptionformat, ['overflowdiv' => true])
992
                ];
993
                $table->data[] = $row;
994
            }
995
            if ($component->has_more_results()) {
996
                $cell = new html_table_cell(get_string('moreresults', 'backup'));
997
                $cell->attributes['class'] = 'notifyproblem';
998
                $cell->colspan = 3;
999
                $row = new html_table_row(array($cell));
1000
                $row->attributes['class'] = 'rcs-course';
1001
                $table->data[] = $row;
1002
            }
1003
        } else {
1004
            $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
1005
            $cell->colspan = 3;
1006
            $cell->attributes['class'] = 'notifyproblem';
1007
            $row = new html_table_row(array($cell));
1008
            $row->attributes['class'] = 'rcs-course';
1009
            $table->data[] = $row;
1010
        }
1011
        $output .= html_writer::table($table);
1012
        $output .= html_writer::end_tag('div');
1013
 
1014
        $data = [
1015
            'inform' => true,
1016
            'extraclasses' => 'rcs-search mb-3 w-25',
1017
            'inputname' => restore_category_search::$VAR_SEARCH,
1018
            'searchstring' => get_string('searchcoursecategories'),
1019
            'buttonattributes' => [
1020
                (object) ['key' => 'name', 'value' => 'searchcourses'],
1021
                (object) ['key' => 'value', 'value' => 1],
1022
            ],
1023
            'query' => $component->get_search(),
1024
        ];
1025
        $output .= $this->output->render_from_template('core/search_input', $data);
1026
 
1027
        $output .= html_writer::end_tag('div');
1028
        return $output;
1029
    }
1030
 
1031
    /**
1032
     * Get markup to render table for all of a users async
1033
     * in progress restores.
1034
     *
1035
     * @param int $userid The Moodle user id.
1036
     * @param \context $context The Moodle context for these restores.
1037
     * @return string $html The table HTML.
1038
     */
1039
    public function restore_progress_viewer($userid, $context) {
1040
        $tablehead = array(get_string('course'), get_string('time'), get_string('status', 'backup'));
1041
 
1042
        $table = new html_table();
1043
        $table->attributes['class'] = 'backup-files-table generaltable';
1044
        $table->head = $tablehead;
1045
        $tabledata = array();
1046
 
1047
        // Get all in progress async restores for this user.
1048
        $restores = \async_helper::get_async_restores($userid);
1049
 
1050
        // For each backup get, new item name, time restore created and progress.
1051
        foreach ($restores as $restore) {
1052
 
1053
            $restorename = \async_helper::get_restore_name($context);
1054
            $timecreated = $restore->timecreated;
1055
            $status = $this->get_status_display($restore->status, $restore->backupid, $restore->backupid, null, 'restore');
1056
 
1057
            $tablerow = array($restorename, userdate($timecreated), $status);
1058
            $tabledata[] = $tablerow;
1059
        }
1060
 
1061
        $table->data = $tabledata;
1062
        $html = html_writer::table($table);
1063
 
1064
        return $html;
1065
    }
1066
 
1067
    /**
1068
     * Get markup to render table for all of a users course copies.
1069
     *
1070
     * @param int $userid The Moodle user id.
1071
     * @param int $courseid The id of the course to get the backups for.
1072
     * @return string $html The table HTML.
1073
     */
1074
    public function copy_progress_viewer(int $userid, int $courseid): string {
1075
        $tablehead = array(
1076
            get_string('copysource', 'backup'),
1077
            get_string('copydest', 'backup'),
1078
            get_string('time'),
1079
            get_string('copyop', 'backup'),
1080
            get_string('status', 'backup')
1081
        );
1082
 
1083
        $table = new html_table();
1084
        $table->attributes['class'] = 'backup-files-table generaltable';
1085
        $table->head = $tablehead;
1086
 
1087
        $tabledata = array();
1088
 
1089
        // Get all in progress course copies for this user.
1090
        $copies = \copy_helper::get_copies($userid, $courseid);
1091
 
1092
        foreach ($copies as $copy) {
1093
            $sourceurl = new \moodle_url('/course/view.php', array('id' => $copy->sourceid));
1094
 
1095
            $tablerow = array(
1096
                html_writer::link($sourceurl, $copy->source),
1097
                $copy->destination,
1098
                userdate($copy->timecreated),
1099
                get_string($copy->operation),
1100
                $this->get_status_display($copy->status, $copy->backupid, $copy->restoreid, $copy->operation)
1101
            );
1102
            $tabledata[] = $tablerow;
1103
        }
1104
 
1105
        $table->data = $tabledata;
1106
        $html = html_writer::table($table);
1107
 
1108
        return $html;
1109
    }
1110
}
1111
 
1112
/**
1113
 * Data structure representing backup files viewer
1114
 *
1115
 * @copyright 2010 Dongsheng Cai
1116
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1117
 * @since     Moodle 2.0
1118
 */
1119
class backup_files_viewer implements renderable {
1120
 
1121
    /**
1122
     * @var array
1123
     */
1124
    public $files;
1125
 
1126
    /**
1127
     * @var context
1128
     */
1129
    public $filecontext;
1130
 
1131
    /**
1132
     * @var string
1133
     */
1134
    public $component;
1135
 
1136
    /**
1137
     * @var string
1138
     */
1139
    public $filearea;
1140
 
1141
    /**
1142
     * @var context
1143
     */
1144
    public $currentcontext;
1145
 
1146
    /**
1147
     * Constructor of backup_files_viewer class
1148
     * @param array $options
1149
     */
1150
    public function __construct(array $options = null) {
1151
        global $CFG, $USER;
1152
        $fs = get_file_storage();
1153
        $this->currentcontext = $options['currentcontext'];
1154
        $this->filecontext    = $options['filecontext'];
1155
        $this->component      = $options['component'];
1156
        $this->filearea       = $options['filearea'];
1157
        $files = $fs->get_area_files($this->filecontext->id, $this->component, $this->filearea, false, 'timecreated');
1158
        $this->files = array_reverse($files);
1159
    }
1160
}