f83190867cb352ecd197ac1a53b11a2dfe3e3ac3
[moodle.git] / backup / util / ui / renderer.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * This file contains backup and restore output renderers
20  *
21  * @package   moodlecore
22  * @copyright 2010 Sam Hemelryk
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 /**
27  * The primary renderer for the backup.
28  *
29  * Can be retrieved with the following code:
30  * <?php
31  * $renderer = $PAGE->get_renderer('core','backup');
32  * ?>
33  *
34  * @copyright 2010 Sam Hemelryk
35  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class core_backup_renderer extends plugin_renderer_base {
38     /**
39      * Renderers a progress bar for the backup or restore given the items that
40      * make it up.
41      * @param array $items An array of items
42      * @return string
43      */
44     public function progress_bar(array $items) {
45         foreach ($items as &$item) {
46             $text = $item['text'];
47             unset($item['text']);
48             if (array_key_exists('link', $item)) {
49                 $link = $item['link'];
50                 unset($item['link']);
51                 $item = html_writer::link($link, $text, $item);
52             } else {
53                 $item = html_writer::tag('span', $text, $item);
54             }
55         }
56         return html_writer::tag('div', join(get_separator(), $items), array('class'=>'backup_progress clearfix'));
57     }
59     /**
60      * The backup and restore pages may display a log (if any) in a scrolling box.
61      *
62      * @param string $loghtml Log content in HTML format
63      * @return string HTML content that shows the log
64      */
65     public function log_display($loghtml) {
66         global $OUTPUT;
67         $out = html_writer::start_div('backup_log');
68         $out .= $OUTPUT->heading(get_string('backuplog', 'backup'));
69         $out .= html_writer::start_div('backup_log_contents');
70         $out .= $loghtml;
71         $out .= html_writer::end_div();
72         $out .= html_writer::end_div();
73         return $out;
74     }
76     /**
77      * Prints a dependency notification
78      * @param string $message
79      * @return string
80      */
81     public function dependency_notification($message) {
82         return html_writer::tag('div', $message, array('class'=>'notification dependencies_enforced'));
83     }
85     /**
86      * Displays the details of a backup file
87      *
88      * @param stdClass $details
89      * @param moodle_url $nextstageurl
90      * @return string
91      */
92     public function backup_details($details, $nextstageurl) {
93         $yestick = $this->output->pix_icon('i/valid', get_string('yes'));
94         $notick = $this->output->pix_icon('i/invalid', get_string('no'));
96         $html  = html_writer::start_tag('div', array('class'=>'backup-restore'));
98         $html .= html_writer::start_tag('div', array('class'=>'backup-section'));
99         $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, array('class'=>'header'));
100         $html .= $this->backup_detail_pair(get_string('backuptype', 'backup'), get_string('backuptype'.$details->type, 'backup'));
101         $html .= $this->backup_detail_pair(get_string('backupformat', 'backup'), get_string('backupformat'.$details->format, 'backup'));
102         $html .= $this->backup_detail_pair(get_string('backupmode', 'backup'), get_string('backupmode'.$details->mode, 'backup'));
103         $html .= $this->backup_detail_pair(get_string('backupdate', 'backup'), userdate($details->backup_date));
104         $html .= $this->backup_detail_pair(get_string('moodleversion', 'backup'),
105                 html_writer::tag('span', $details->moodle_release, array('class'=>'moodle_release')).
106                 html_writer::tag('span', '['.$details->moodle_version.']', array('class'=>'moodle_version sub-detail')));
107         $html .= $this->backup_detail_pair(get_string('backupversion', 'backup'),
108                 html_writer::tag('span', $details->backup_release, array('class'=>'moodle_release')).
109                 html_writer::tag('span', '['.$details->backup_version.']', array('class'=>'moodle_version sub-detail')));
110         $html .= $this->backup_detail_pair(get_string('originalwwwroot', 'backup'),
111                 html_writer::tag('span', $details->original_wwwroot, array('class'=>'originalwwwroot')).
112                 html_writer::tag('span', '['.$details->original_site_identifier_hash.']', array('class'=>'sitehash sub-detail')));
113         if (!empty($details->include_file_references_to_external_content)) {
114             $message = '';
115             if (backup_general_helper::backup_is_samesite($details)) {
116                 $message = $yestick . ' ' . get_string('filereferencessamesite', 'backup');
117             } else {
118                 $message = $notick . ' ' . get_string('filereferencesnotsamesite', 'backup');
119             }
120             $html .= $this->backup_detail_pair(get_string('includefilereferences', 'backup'), $message);
121         }
123         $html .= html_writer::end_tag('div');
125         $html .= html_writer::start_tag('div', array('class'=>'backup-section settings-section'));
126         $html .= $this->output->heading(get_string('backupsettings', 'backup'), 2, array('class'=>'header'));
127         foreach ($details->root_settings as $label=>$value) {
128             if ($label == 'filename' or $label == 'user_files') {
129                 continue;
130             }
131             $html .= $this->backup_detail_pair(get_string('rootsetting'.str_replace('_','',$label), 'backup'), $value?$yestick:$notick);
132         }
133         $html .= html_writer::end_tag('div');
135         if ($details->type === 'course') {
136             $html .= html_writer::start_tag('div', array('class'=>'backup-section'));
137             $html .= $this->output->heading(get_string('backupcoursedetails', 'backup'), 2, array('class'=>'header'));
138             $html .= $this->backup_detail_pair(get_string('coursetitle', 'backup'), $details->course->title);
139             $html .= $this->backup_detail_pair(get_string('courseid', 'backup'), $details->course->courseid);
141             $html .= html_writer::start_tag('div', array('class'=>'backup-sub-section'));
142             $html .= $this->output->heading(get_string('backupcoursesections', 'backup'), 3, array('class'=>'subheader'));
143             foreach ($details->sections as $key=>$section) {
144                 $included = $key.'_included';
145                 $userinfo = $key.'_userinfo';
146                 if ($section->settings[$included] && $section->settings[$userinfo]) {
147                     $value = get_string('sectionincanduser','backup');
148                 } else if ($section->settings[$included]) {
149                     $value = get_string('sectioninc','backup');
150                 } else {
151                     continue;
152                 }
153                 $html .= $this->backup_detail_pair(get_string('backupcoursesection', 'backup', $section->title), $value);
154                 $table = null;
155                 foreach ($details->activities as $activitykey=>$activity) {
156                     if ($activity->sectionid != $section->sectionid) {
157                         continue;
158                     }
159                     if (empty($table)) {
160                         $table = new html_table();
161                         $table->head = array(get_string('module','backup'), get_string('title','backup'), get_string('userinfo','backup'));
162                         $table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
163                         $table->align = array('left','left', 'center');
164                         $table->attributes = array('class'=>'activitytable generaltable');
165                         $table->data = array();
166                     }
167                     $name = get_string('pluginname', $activity->modulename);
168                     $icon = new pix_icon('icon', $name, $activity->modulename, array('class' => 'iconlarge icon-pre'));
169                     $table->data[] = array(
170                         $this->output->render($icon).$name,
171                         $activity->title,
172                         ($activity->settings[$activitykey.'_userinfo'])?$yestick:$notick,
173                     );
174                 }
175                 if (!empty($table)) {
176                     $html .= $this->backup_detail_pair(get_string('sectionactivities','backup'), html_writer::table($table));
177                 }
179             }
180             $html .= html_writer::end_tag('div');
181             $html .= html_writer::end_tag('div');
182         }
184         $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
185         $html .= html_writer::end_tag('div');
187         return $html;
188     }
190     /**
191      * Displays the general information about a backup file with non-standard format
192      *
193      * @param moodle_url $nextstageurl URL to send user to
194      * @param array $details basic info about the file (format, type)
195      * @return string HTML code to display
196      */
197     public function backup_details_nonstandard($nextstageurl, array $details) {
199         $html  = html_writer::start_tag('div', array('class' => 'backup-restore nonstandardformat'));
200         $html .= html_writer::start_tag('div', array('class' => 'backup-section'));
201         $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, 'header');
202         $html .= $this->output->box(get_string('backupdetailsnonstandardinfo', 'backup'), 'noticebox');
203         $html .= $this->backup_detail_pair(
204             get_string('backupformat', 'backup'),
205             get_string('backupformat'.$details['format'], 'backup'));
206         $html .= $this->backup_detail_pair(
207             get_string('backuptype', 'backup'),
208             get_string('backuptype'.$details['type'], 'backup'));
209         $html .= html_writer::end_tag('div');
210         $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
211         $html .= html_writer::end_tag('div');
213         return $html;
214     }
216     /**
217      * Displays the general information about a backup file with unknown format
218      *
219      * @param moodle_url $nextstageurl URL to send user to
220      * @return string HTML code to display
221      */
222     public function backup_details_unknown(moodle_url $nextstageurl) {
224         $html  = html_writer::start_tag('div', array('class' => 'unknownformat'));
225         $html .= $this->output->heading(get_string('errorinvalidformat', 'backup'), 2, 'notifyproblem');
226         $html .= html_writer::tag('div', get_string('errorinvalidformatinfo', 'backup'), array('class' => 'notifyproblem'));
227         $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
228         $html .= html_writer::end_tag('div');
230         return $html;
231     }
233     /**
234      * Displays a course selector for restore
235      *
236      * @param moodle_url $nextstageurl
237      * @param bool $wholecourse true if we are restoring whole course (as with backup::TYPE_1COURSE), false otherwise
238      * @param restore_category_search $categories
239      * @param restore_course_search $courses
240      * @param int $currentcourse
241      * @return string
242      */
243     public function course_selector(moodle_url $nextstageurl, $wholecourse = true, restore_category_search $categories = null, restore_course_search $courses=null, $currentcourse = null) {
244         global $CFG, $PAGE;
245         require_once($CFG->dirroot.'/course/lib.php');
247         $nextstageurl->param('sesskey', sesskey());
249         $form = html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
250         foreach ($nextstageurl->params() as $key=>$value) {
251             $form .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
252         }
254         $hasrestoreoption = false;
256         $html  = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore'));
257         if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) {
258             // New course
259             $hasrestoreoption = true;
260             $html .= $form;
261             $html .= html_writer::start_tag('div', array('class'=>'bcs-new-course backup-section'));
262             $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class'=>'header'));
263             $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target', backup::TARGET_NEW_COURSE, array('checked'=>'checked'));
264             $html .= $this->backup_detail_pair(get_string('selectacategory', 'backup'), $this->render($categories));
265             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
266             $html .= html_writer::end_tag('div');
267             $html .= html_writer::end_tag('form');
268         }
270         if ($wholecourse && !empty($currentcourse)) {
271             // Current course
272             $hasrestoreoption = true;
273             $html .= $form;
274             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'targetid', 'value'=>$currentcourse));
275             $html .= html_writer::start_tag('div', array('class'=>'bcs-current-course backup-section'));
276             $html .= $this->output->heading(get_string('restoretocurrentcourse', 'backup'), 2, array('class'=>'header'));
277             $html .= $this->backup_detail_input(get_string('restoretocurrentcourseadding', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_ADDING, array('checked'=>'checked'));
278             $html .= $this->backup_detail_input(get_string('restoretocurrentcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_DELETING);
279             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
280             $html .= html_writer::end_tag('div');
281             $html .= html_writer::end_tag('form');
282         }
284         if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search())) {
285             // Existing course
286             $hasrestoreoption = true;
287             $html .= $form;
288             $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section'));
289             $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header'));
290             if ($wholecourse) {
291                 $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_ADDING, array('checked'=>'checked'));
292                 $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_DELETING);
293                 $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
294             } else {
295                 // We only allow restore adding to existing for now. Enforce it here.
296                 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'target', 'value'=>backup::TARGET_EXISTING_ADDING));
297                 $courses->invalidate_results(); // Clean list of courses
298                 $courses->set_include_currentcourse(); // Show current course in the list
299                 $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
300             }
301             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
302             $html .= html_writer::end_tag('div');
303             $html .= html_writer::end_tag('form');
304         }
306         if (!$hasrestoreoption) {
307             echo $this->output->notification(get_string('norestoreoptions','backup'));
308         }
310         $html .= html_writer::end_tag('div');
311         return $html;
312     }
314     /**
315      * Displays the import course selector
316      *
317      * @param moodle_url $nextstageurl
318      * @param import_course_search $courses
319      * @return string
320      */
321     public function import_course_selector(moodle_url $nextstageurl, import_course_search $courses=null) {
322         $html  = html_writer::start_tag('div', array('class'=>'import-course-selector backup-restore'));
323         $html .= html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
324         foreach ($nextstageurl->params() as $key=>$value) {
325             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
326         }
327         // We only allow import adding for now. Enforce it here.
328         $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'target', 'value'=>backup::TARGET_CURRENT_ADDING));
329         $html .= html_writer::start_tag('div', array('class'=>'ics-existing-course backup-section'));
330         $html .= $this->output->heading(get_string('importdatafrom'), 2, array('class'=>'header'));
331         $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
332         $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
333         $html .= html_writer::end_tag('div');
334         $html .= html_writer::end_tag('form');
335         $html .= html_writer::end_tag('div');
336         return $html;
337     }
339     /**
340      * Creates a detailed pairing (key + value)
341      *
342      * @staticvar int $count
343      * @param string $label
344      * @param string $value
345      * @return string
346      */
347     protected function backup_detail_pair($label, $value) {
348         static $count= 0;
349         $count++;
350         $html  = html_writer::start_tag('div', array('class'=>'detail-pair'));
351         $html .= html_writer::tag('label', $label, array('class'=>'detail-pair-label', 'for'=>'detail-pair-value-'.$count));
352         $html .= html_writer::tag('div', $value, array('class'=>'detail-pair-value', 'name'=>'detail-pair-value-'.$count));
353         $html .= html_writer::end_tag('div');
354         return $html;
355     }
357     /**
358      * Created a detailed pairing with an input
359      *
360      * @param string $label
361      * @param string $type
362      * @param string $name
363      * @param string $value
364      * @param array $attributes
365      * @param string|null $description
366      * @return string
367      */
368     protected function backup_detail_input($label, $type, $name, $value, array $attributes=array(), $description=null) {
369         if (!empty ($description)) {
370             $description = html_writer::tag('span', $description, array('class'=>'description'));
371         } else {
372             $description = '';
373         }
374         return $this->backup_detail_pair($label, html_writer::empty_tag('input', $attributes+array('name'=>$name, 'type'=>$type, 'value'=>$value)).$description);
375     }
377     /**
378      * Creates a detailed pairing with a select
379      *
380      * @param string $label
381      * @param string $name
382      * @param array $options
383      * @param string $selected
384      * @param bool $nothing
385      * @param array $attributes
386      * @param string|null $description
387      * @return string
388      */
389     protected function backup_detail_select($label, $name, $options, $selected='', $nothing=false, array $attributes=array(), $description=null) {
390         if (!empty ($description)) {
391             $description = html_writer::tag('span', $description, array('class'=>'description'));
392         } else {
393             $description = '';
394         }
395         return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
396     }
398     /**
399      * Displays precheck notices
400      *
401      * @param array $results
402      * @return string
403      */
404     public function precheck_notices($results) {
405         $output = html_writer::start_tag('div', array('class'=>'restore-precheck-notices'));
406         if (array_key_exists('errors', $results)) {
407             foreach ($results['errors'] as $error) {
408                 $output .= $this->output->notification($error);
409             }
410         }
411         if (array_key_exists('warnings', $results)) {
412             foreach ($results['warnings'] as $warning) {
413                 $output .= $this->output->notification($warning, 'notifywarning notifyproblem');
414             }
415         }
416         return $output.html_writer::end_tag('div');
417     }
419     /**
420      * Displays substage buttons
421      *
422      * @param bool $haserrors
423      * @return string
424      */
425     public function substage_buttons($haserrors) {
426         $output  = html_writer::start_tag('div', array('continuebutton'));
427         if (!$haserrors) {
428             $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue')));
429         }
430         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
431         $output .= html_writer::end_tag('div');
432         return $output;
433     }
435     /**
436      * Displays a role mapping interface
437      *
438      * @param array $rolemappings
439      * @param array $roles
440      * @return string
441      */
442     public function role_mappings($rolemappings, $roles) {
443         $roles[0] = get_string('none');
444         $output  = html_writer::start_tag('div', array('class'=>'restore-rolemappings'));
445         $output .= $this->output->heading(get_string('restorerolemappings', 'backup'), 2);
446         foreach ($rolemappings as $id=>$mapping) {
447             $label = $mapping->name;
448             $name = 'mapping'.$id;
449             $selected = $mapping->targetroleid;
450             $output .= $this->backup_detail_select($label, $name, $roles, $mapping->targetroleid, false, array(), $mapping->description);
451         }
452         $output .= html_writer::end_tag('div');
453         return $output;
454     }
456     /**
457      * Displays a continue button
458      *
459      * @param string|moodle_url $url
460      * @param string $method
461      * @return string
462      */
463     public function continue_button($url, $method='post') {
464         if (!($url instanceof moodle_url)) {
465             $url = new moodle_url($url);
466         }
467         if ($method != 'post') {
468             $method = 'get';
469         }
470         $url->param('sesskey', sesskey());
471         $button = new single_button($url, get_string('continue'), $method);
472         $button->class = 'continuebutton';
473         return $this->render($button);
474     }
475     /**
476      * Print a backup files tree
477      * @param array $options
478      * @return string
479      */
480     public function backup_files_viewer(array $options = null) {
481         $files = new backup_files_viewer($options);
482         return $this->render($files);
483     }
485     /**
486      * Displays a backup files viewer
487      *
488      * @global stdClass $USER
489      * @param backup_files_viewer $tree
490      * @return string
491      */
492     public function render_backup_files_viewer(backup_files_viewer $viewer) {
493         global $CFG;
494         $files = $viewer->files;
496         $table = new html_table();
497         $table->attributes['class'] = 'backup-files-table generaltable';
498         $table->head = array(get_string('filename', 'backup'), get_string('time'), get_string('size'), get_string('download'), get_string('restore'));
499         $table->width = '100%';
500         $table->data = array();
502         foreach ($files as $file) {
503             if ($file->is_directory()) {
504                 continue;
505             }
506             $fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename(), true);
507             $params = array();
508             $params['action'] = 'choosebackupfile';
509             $params['filename'] = $file->get_filename();
510             $params['filepath'] = $file->get_filepath();
511             $params['component'] = $file->get_component();
512             $params['filearea'] = $file->get_filearea();
513             $params['filecontextid'] = $file->get_contextid();
514             $params['contextid'] = $viewer->currentcontext->id;
515             $params['itemid'] = $file->get_itemid();
516             $restoreurl = new moodle_url('/backup/restorefile.php', $params);
517             $table->data[] = array(
518                 $file->get_filename(),
519                 userdate($file->get_timemodified()),
520                 display_size($file->get_filesize()),
521                 html_writer::link($fileurl, get_string('download')),
522                 html_writer::link($restoreurl, get_string('restore')),
523                 );
524         }
526         $html = html_writer::table($table);
527         $html .= $this->output->single_button(new moodle_url('/backup/backupfilesedit.php', array('currentcontext'=>$viewer->currentcontext->id, 'contextid'=>$viewer->filecontext->id, 'filearea'=>$viewer->filearea, 'component'=>$viewer->component, 'returnurl'=>$this->page->url->out())), get_string('managefiles', 'backup'), 'post');
529         return $html;
530     }
532     /**
533      * Renders a restore course search object
534      *
535      * @param restore_course_search $component
536      * @return string
537      */
538     public function render_restore_course_search(restore_course_search $component) {
539         $url = $component->get_url();
541         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
542         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
544         $table = new html_table();
545         $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
546         $table->data = array();
547         if ($component->get_count() !== 0) {
548             foreach ($component->get_results() as $course) {
549                 $row = new html_table_row();
550                 $row->attributes['class'] = 'rcs-course';
551                 if (!$course->visible) {
552                     $row->attributes['class'] .= ' dimmed';
553                 }
554                 $row->cells = array(
555                     html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)),
556                     format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
557                     format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
558                 );
559                 $table->data[] = $row;
560             }
561             if ($component->has_more_results()) {
562                 $cell = new html_table_cell(get_string('moreresults', 'backup'));
563                 $cell->colspan = 3;
564                 $cell->attributes['class'] = 'notifyproblem';
565                 $row = new html_table_row(array($cell));
566                 $row->attributes['class'] = 'rcs-course';
567                 $table->data[] = $row;
568             }
569         } else {
570             $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
571             $cell->colspan = 3;
572             $cell->attributes['class'] = 'notifyproblem';
573             $row = new html_table_row(array($cell));
574             $row->attributes['class'] = 'rcs-course';
575             $table->data[] = $row;
576         }
577         $output .= html_writer::table($table);
578         $output .= html_writer::end_tag('div');
580         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
581         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
582         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
583         $output .= html_writer::end_tag('div');
585         $output .= html_writer::end_tag('div');
586         return $output;
587     }
589     /**
590      * Renders an import course search object
591      *
592      * @param import_course_search $component
593      * @return string
594      */
595     public function render_import_course_search(import_course_search $component) {
596         $url = $component->get_url();
598         $output = html_writer::start_tag('div', array('class' => 'import-course-search'));
599         if ($component->get_count() === 0) {
600             $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
602             $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
603             $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
604             $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
605             $output .= html_writer::end_tag('div');
607             $output .= html_writer::end_tag('div');
608             return $output;
609         }
611         $countstr = '';
612         if ($component->has_more_results()) {
613             $countstr = get_string('morecoursesearchresults', 'backup', $component->get_count());
614         } else {
615             $countstr = get_string('totalcoursesearchresults', 'backup', $component->get_count());
616         }
618         $output .= html_writer::tag('div', $countstr, array('class'=>'ics-totalresults'));
619         $output .= html_writer::start_tag('div', array('class' => 'ics-results'));
621         $table = new html_table();
622         $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
623         $table->data = array();
624         foreach ($component->get_results() as $course) {
625             $row = new html_table_row();
626             $row->attributes['class'] = 'ics-course';
627             if (!$course->visible) {
628                 $row->attributes['class'] .= ' dimmed';
629             }
630             $row->cells = array(
631                 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'importid', 'value'=>$course->id)),
632                 format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
633                 format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
634             );
635             $table->data[] = $row;
636         }
637         if ($component->has_more_results()) {
638             $cell = new html_table_cell(get_string('moreresults', 'backup'));
639             $cell->colspan = 3;
640             $cell->attributes['class'] = 'notifyproblem';
641             $row = new html_table_row(array($cell));
642             $row->attributes['class'] = 'rcs-course';
643             $table->data[] = $row;
644         }
645         $output .= html_writer::table($table);
646         $output .= html_writer::end_tag('div');
648         $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
649         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
650         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
651         $output .= html_writer::end_tag('div');
653         $output .= html_writer::end_tag('div');
654         return $output;
655     }
657     /**
658      * Renders a restore category search object
659      *
660      * @param restore_category_search $component
661      * @return string
662      */
663     public function render_restore_category_search(restore_category_search $component) {
664         $url = $component->get_url();
666         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
667         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
669         $table = new html_table();
670         $table->head = array('', get_string('name'), get_string('description'));
671         $table->data = array();
673         if ($component->get_count() !== 0) {
674             foreach ($component->get_results() as $category) {
675                 $row = new html_table_row();
676                 $row->attributes['class'] = 'rcs-course';
677                 if (!$category->visible) {
678                     $row->attributes['class'] .= ' dimmed';
679                 }
680                 $row->cells = array(
681                     html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)),
682                     format_string($category->name, true, array('context' => context_coursecat::instance($category->id))),
683                     format_text($category->description, $category->descriptionformat, array('overflowdiv'=>true))
684                 );
685                 $table->data[] = $row;
686             }
687             if ($component->has_more_results()) {
688                 $cell = new html_table_cell(get_string('moreresults', 'backup'));
689                 $cell->attributes['class'] = 'notifyproblem';
690                 $cell->colspan = 3;
691                 $row = new html_table_row(array($cell));
692                 $row->attributes['class'] = 'rcs-course';
693                 $table->data[] = $row;
694             }
695         } else {
696             $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
697             $cell->colspan = 3;
698             $cell->attributes['class'] = 'notifyproblem';
699             $row = new html_table_row(array($cell));
700             $row->attributes['class'] = 'rcs-course';
701             $table->data[] = $row;
702         }
703         $output .= html_writer::table($table);
704         $output .= html_writer::end_tag('div');
706         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
707         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_category_search::$VAR_SEARCH, 'value'=>$component->get_search()));
708         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
709         $output .= html_writer::end_tag('div');
711         $output .= html_writer::end_tag('div');
712         return $output;
713     }
716 /**
717  * Data structure representing backup files viewer
718  *
719  * @copyright 2010 Dongsheng Cai
720  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
721  * @since     Moodle 2.0
722  */
723 class backup_files_viewer implements renderable {
724     public $files;
725     public $filecontext;
726     public $component;
727     public $filearea;
728     public $currentcontext;
730     /**
731      * Constructor of backup_files_viewer class
732      * @param array $options
733      */
734     public function __construct(array $options = null) {
735         global $CFG, $USER;
736         $fs = get_file_storage();
737         $this->currentcontext = $options['currentcontext'];
738         $this->filecontext    = $options['filecontext'];
739         $this->component      = $options['component'];
740         $this->filearea       = $options['filearea'];
741         $files = $fs->get_area_files($this->filecontext->id, $this->component, $this->filearea, false, 'timecreated');
742         $this->files = array_reverse($files);
743     }