MDL-31500 backup: Remove frontpage restore UI restrictions
[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_div('unknownformat');
225         $html .= $this->output->heading(get_string('errorinvalidformat', 'backup'), 2);
226         $html .= $this->output->notification(get_string('errorinvalidformatinfo', 'backup'), 'notifyproblem');
227         $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
228         $html .= html_writer::end_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         // These variables are used to check if the form using this function was submitted.
248         $target = optional_param('target', false, PARAM_INT);
249         $targetid = optional_param('targetid', null, PARAM_INT);
251         // Check if they submitted the form but did not provide all the data we need.
252         $missingdata = false;
253         if ($target and is_null($targetid)) {
254             $missingdata = true;
255         }
257         $nextstageurl->param('sesskey', sesskey());
259         $form = html_writer::start_tag('form', array('method' => 'post', 'action' => $nextstageurl->out_omit_querystring(),
260             'class' => 'mform'));
261         foreach ($nextstageurl->params() as $key=>$value) {
262             $form .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
263         }
265         $hasrestoreoption = false;
267         $html  = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore'));
268         if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) {
270             // New course
271             $hasrestoreoption = true;
272             $html .= $form;
273             $html .= html_writer::start_tag('div', array('class'=>'bcs-new-course backup-section'));
274             $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class'=>'header'));
275             $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target', backup::TARGET_NEW_COURSE, array('checked'=>'checked'));
276             $selectacategoryhtml = $this->backup_detail_pair(get_string('selectacategory', 'backup'), $this->render($categories));
277             // Display the category selection as required if the form was submitted but this data was not supplied.
278             if ($missingdata && $target == backup::TARGET_NEW_COURSE) {
279                 $html .= html_writer::span(get_string('required'), 'error');
280                 $html .= html_writer::start_tag('fieldset', array('class' => 'error'));
281                 $html .= $selectacategoryhtml;
282                 $html .= html_writer::end_tag('fieldset');
283             } else {
284                 $html .= $selectacategoryhtml;
285             }
286             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
287             $html .= html_writer::end_tag('div');
288             $html .= html_writer::end_tag('form');
289         }
291         if ($wholecourse && !empty($currentcourse)) {
292             // Current course
293             $hasrestoreoption = true;
294             $html .= $form;
295             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'targetid', 'value'=>$currentcourse));
296             $html .= html_writer::start_tag('div', array('class'=>'bcs-current-course backup-section'));
297             $html .= $this->output->heading(get_string('restoretocurrentcourse', 'backup'), 2, array('class'=>'header'));
298             $html .= $this->backup_detail_input(get_string('restoretocurrentcourseadding', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_ADDING, array('checked'=>'checked'));
299             $html .= $this->backup_detail_input(get_string('restoretocurrentcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_DELETING);
300             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
301             $html .= html_writer::end_tag('div');
302             $html .= html_writer::end_tag('form');
303         }
305         // If we are restoring an activity, then include the current course.
306         if (!$wholecourse) {
307             $courses->invalidate_results(); // Clean list of courses.
308             $courses->set_include_currentcourse();
309         }
310         if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search())) {
311             // Existing course
312             $hasrestoreoption = true;
313             $html .= $form;
314             $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section'));
315             $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header'));
316             if ($wholecourse) {
317                 $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_ADDING, array('checked'=>'checked'));
318                 $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_DELETING);
319             } else {
320                 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'target', 'value'=>backup::TARGET_EXISTING_ADDING));
321             }
322             $selectacoursehtml = $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
323             // Display the course selection as required if the form was submitted but this data was not supplied.
324             if ($missingdata && $target == backup::TARGET_EXISTING_ADDING) {
325                 $html .= html_writer::span(get_string('required'), 'error');
326                 $html .= html_writer::start_tag('fieldset', array('class' => 'error'));
327                 $html .= $selectacoursehtml;
328                 $html .= html_writer::end_tag('fieldset');
329             } else {
330                 $html .= $selectacoursehtml;
331             }
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         }
337         if (!$hasrestoreoption) {
338             echo $this->output->notification(get_string('norestoreoptions','backup'));
339         }
341         $html .= html_writer::end_tag('div');
342         return $html;
343     }
345     /**
346      * Displays the import course selector
347      *
348      * @param moodle_url $nextstageurl
349      * @param import_course_search $courses
350      * @return string
351      */
352     public function import_course_selector(moodle_url $nextstageurl, import_course_search $courses=null) {
353         $html  = html_writer::start_tag('div', array('class'=>'import-course-selector backup-restore'));
354         $html .= html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
355         foreach ($nextstageurl->params() as $key=>$value) {
356             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
357         }
358         // We only allow import adding for now. Enforce it here.
359         $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'target', 'value'=>backup::TARGET_CURRENT_ADDING));
360         $html .= html_writer::start_tag('div', array('class'=>'ics-existing-course backup-section'));
361         $html .= $this->output->heading(get_string('importdatafrom'), 2, array('class'=>'header'));
362         $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
363         $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
364         $html .= html_writer::end_tag('div');
365         $html .= html_writer::end_tag('form');
366         $html .= html_writer::end_tag('div');
367         return $html;
368     }
370     /**
371      * Creates a detailed pairing (key + value)
372      *
373      * @staticvar int $count
374      * @param string $label
375      * @param string $value
376      * @return string
377      */
378     protected function backup_detail_pair($label, $value) {
379         static $count= 0;
380         $count++;
381         $html  = html_writer::start_tag('div', array('class'=>'detail-pair'));
382         $html .= html_writer::tag('label', $label, array('class'=>'detail-pair-label', 'for'=>'detail-pair-value-'.$count));
383         $html .= html_writer::tag('div', $value, array('class'=>'detail-pair-value', 'name'=>'detail-pair-value-'.$count));
384         $html .= html_writer::end_tag('div');
385         return $html;
386     }
388     /**
389      * Created a detailed pairing with an input
390      *
391      * @param string $label
392      * @param string $type
393      * @param string $name
394      * @param string $value
395      * @param array $attributes
396      * @param string|null $description
397      * @return string
398      */
399     protected function backup_detail_input($label, $type, $name, $value, array $attributes=array(), $description=null) {
400         if (!empty ($description)) {
401             $description = html_writer::tag('span', $description, array('class'=>'description'));
402         } else {
403             $description = '';
404         }
405         return $this->backup_detail_pair($label, html_writer::empty_tag('input', $attributes+array('name'=>$name, 'type'=>$type, 'value'=>$value)).$description);
406     }
408     /**
409      * Creates a detailed pairing with a select
410      *
411      * @param string $label
412      * @param string $name
413      * @param array $options
414      * @param string $selected
415      * @param bool $nothing
416      * @param array $attributes
417      * @param string|null $description
418      * @return string
419      */
420     protected function backup_detail_select($label, $name, $options, $selected='', $nothing=false, array $attributes=array(), $description=null) {
421         if (!empty ($description)) {
422             $description = html_writer::tag('span', $description, array('class'=>'description'));
423         } else {
424             $description = '';
425         }
426         return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
427     }
429     /**
430      * Displays precheck notices
431      *
432      * @param array $results
433      * @return string
434      */
435     public function precheck_notices($results) {
436         $output = html_writer::start_tag('div', array('class'=>'restore-precheck-notices'));
437         if (array_key_exists('errors', $results)) {
438             foreach ($results['errors'] as $error) {
439                 $output .= $this->output->notification($error);
440             }
441         }
442         if (array_key_exists('warnings', $results)) {
443             foreach ($results['warnings'] as $warning) {
444                 $output .= $this->output->notification($warning, 'notifyproblem');
445             }
446         }
447         return $output.html_writer::end_tag('div');
448     }
450     /**
451      * Displays substage buttons
452      *
453      * @param bool $haserrors
454      * @return string
455      */
456     public function substage_buttons($haserrors) {
457         $output  = html_writer::start_tag('div', array('continuebutton'));
458         if (!$haserrors) {
459             $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue')));
460         }
461         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
462         $output .= html_writer::end_tag('div');
463         return $output;
464     }
466     /**
467      * Displays a role mapping interface
468      *
469      * @param array $rolemappings
470      * @param array $roles
471      * @return string
472      */
473     public function role_mappings($rolemappings, $roles) {
474         $roles[0] = get_string('none');
475         $output  = html_writer::start_tag('div', array('class'=>'restore-rolemappings'));
476         $output .= $this->output->heading(get_string('restorerolemappings', 'backup'), 2);
477         foreach ($rolemappings as $id=>$mapping) {
478             $label = $mapping->name;
479             $name = 'mapping'.$id;
480             $selected = $mapping->targetroleid;
481             $output .= $this->backup_detail_select($label, $name, $roles, $mapping->targetroleid, false, array(), $mapping->description);
482         }
483         $output .= html_writer::end_tag('div');
484         return $output;
485     }
487     /**
488      * Displays a continue button
489      *
490      * @param string|moodle_url $url
491      * @param string $method
492      * @return string
493      */
494     public function continue_button($url, $method='post') {
495         if (!($url instanceof moodle_url)) {
496             $url = new moodle_url($url);
497         }
498         if ($method != 'post') {
499             $method = 'get';
500         }
501         $url->param('sesskey', sesskey());
502         $button = new single_button($url, get_string('continue'), $method);
503         $button->class = 'continuebutton';
504         return $this->render($button);
505     }
506     /**
507      * Print a backup files tree
508      * @param array $options
509      * @return string
510      */
511     public function backup_files_viewer(array $options = null) {
512         $files = new backup_files_viewer($options);
513         return $this->render($files);
514     }
516     /**
517      * Displays a backup files viewer
518      *
519      * @global stdClass $USER
520      * @param backup_files_viewer $tree
521      * @return string
522      */
523     public function render_backup_files_viewer(backup_files_viewer $viewer) {
524         global $CFG;
525         $files = $viewer->files;
527         $table = new html_table();
528         $table->attributes['class'] = 'backup-files-table generaltable';
529         $table->head = array(get_string('filename', 'backup'), get_string('time'), get_string('size'), get_string('download'), get_string('restore'));
530         $table->width = '100%';
531         $table->data = array();
533         foreach ($files as $file) {
534             if ($file->is_directory()) {
535                 continue;
536             }
537             $fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename(), true);
538             $params = array();
539             $params['action'] = 'choosebackupfile';
540             $params['filename'] = $file->get_filename();
541             $params['filepath'] = $file->get_filepath();
542             $params['component'] = $file->get_component();
543             $params['filearea'] = $file->get_filearea();
544             $params['filecontextid'] = $file->get_contextid();
545             $params['contextid'] = $viewer->currentcontext->id;
546             $params['itemid'] = $file->get_itemid();
547             $restoreurl = new moodle_url('/backup/restorefile.php', $params);
548             $table->data[] = array(
549                 $file->get_filename(),
550                 userdate($file->get_timemodified()),
551                 display_size($file->get_filesize()),
552                 html_writer::link($fileurl, get_string('download')),
553                 html_writer::link($restoreurl, get_string('restore')),
554                 );
555         }
557         $html = html_writer::table($table);
558         $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');
560         return $html;
561     }
563     /**
564      * Renders a restore course search object
565      *
566      * @param restore_course_search $component
567      * @return string
568      */
569     public function render_restore_course_search(restore_course_search $component) {
570         $url = $component->get_url();
572         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
573         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
575         $table = new html_table();
576         $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
577         $table->data = array();
578         if ($component->get_count() !== 0) {
579             foreach ($component->get_results() as $course) {
580                 $row = new html_table_row();
581                 $row->attributes['class'] = 'rcs-course';
582                 if (!$course->visible) {
583                     $row->attributes['class'] .= ' dimmed';
584                 }
585                 $row->cells = array(
586                     html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)),
587                     format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
588                     format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
589                 );
590                 $table->data[] = $row;
591             }
592             if ($component->has_more_results()) {
593                 $cell = new html_table_cell(get_string('moreresults', 'backup'));
594                 $cell->colspan = 3;
595                 $cell->attributes['class'] = 'notifyproblem';
596                 $row = new html_table_row(array($cell));
597                 $row->attributes['class'] = 'rcs-course';
598                 $table->data[] = $row;
599             }
600         } else {
601             $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
602             $cell->colspan = 3;
603             $cell->attributes['class'] = 'notifyproblem';
604             $row = new html_table_row(array($cell));
605             $row->attributes['class'] = 'rcs-course';
606             $table->data[] = $row;
607         }
608         $output .= html_writer::table($table);
609         $output .= html_writer::end_tag('div');
611         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
612         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
613         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
614         $output .= html_writer::end_tag('div');
616         $output .= html_writer::end_tag('div');
617         return $output;
618     }
620     /**
621      * Renders an import course search object
622      *
623      * @param import_course_search $component
624      * @return string
625      */
626     public function render_import_course_search(import_course_search $component) {
627         $url = $component->get_url();
629         $output = html_writer::start_tag('div', array('class' => 'import-course-search'));
630         if ($component->get_count() === 0) {
631             $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
633             $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
634             $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
635             $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
636             $output .= html_writer::end_tag('div');
638             $output .= html_writer::end_tag('div');
639             return $output;
640         }
642         $countstr = '';
643         if ($component->has_more_results()) {
644             $countstr = get_string('morecoursesearchresults', 'backup', $component->get_count());
645         } else {
646             $countstr = get_string('totalcoursesearchresults', 'backup', $component->get_count());
647         }
649         $output .= html_writer::tag('div', $countstr, array('class'=>'ics-totalresults'));
650         $output .= html_writer::start_tag('div', array('class' => 'ics-results'));
652         $table = new html_table();
653         $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
654         $table->data = array();
655         foreach ($component->get_results() as $course) {
656             $row = new html_table_row();
657             $row->attributes['class'] = 'ics-course';
658             if (!$course->visible) {
659                 $row->attributes['class'] .= ' dimmed';
660             }
661             $row->cells = array(
662                 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'importid', 'value'=>$course->id)),
663                 format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
664                 format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
665             );
666             $table->data[] = $row;
667         }
668         if ($component->has_more_results()) {
669             $cell = new html_table_cell(get_string('moreresults', 'backup'));
670             $cell->colspan = 3;
671             $cell->attributes['class'] = 'notifyproblem';
672             $row = new html_table_row(array($cell));
673             $row->attributes['class'] = 'rcs-course';
674             $table->data[] = $row;
675         }
676         $output .= html_writer::table($table);
677         $output .= html_writer::end_tag('div');
679         $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
680         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
681         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
682         $output .= html_writer::end_tag('div');
684         $output .= html_writer::end_tag('div');
685         return $output;
686     }
688     /**
689      * Renders a restore category search object
690      *
691      * @param restore_category_search $component
692      * @return string
693      */
694     public function render_restore_category_search(restore_category_search $component) {
695         $url = $component->get_url();
697         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
698         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
700         $table = new html_table();
701         $table->head = array('', get_string('name'), get_string('description'));
702         $table->data = array();
704         if ($component->get_count() !== 0) {
705             foreach ($component->get_results() as $category) {
706                 $row = new html_table_row();
707                 $row->attributes['class'] = 'rcs-course';
708                 if (!$category->visible) {
709                     $row->attributes['class'] .= ' dimmed';
710                 }
711                 $row->cells = array(
712                     html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)),
713                     format_string($category->name, true, array('context' => context_coursecat::instance($category->id))),
714                     format_text($category->description, $category->descriptionformat, array('overflowdiv'=>true))
715                 );
716                 $table->data[] = $row;
717             }
718             if ($component->has_more_results()) {
719                 $cell = new html_table_cell(get_string('moreresults', 'backup'));
720                 $cell->attributes['class'] = 'notifyproblem';
721                 $cell->colspan = 3;
722                 $row = new html_table_row(array($cell));
723                 $row->attributes['class'] = 'rcs-course';
724                 $table->data[] = $row;
725             }
726         } else {
727             $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
728             $cell->colspan = 3;
729             $cell->attributes['class'] = 'notifyproblem';
730             $row = new html_table_row(array($cell));
731             $row->attributes['class'] = 'rcs-course';
732             $table->data[] = $row;
733         }
734         $output .= html_writer::table($table);
735         $output .= html_writer::end_tag('div');
737         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
738         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_category_search::$VAR_SEARCH, 'value'=>$component->get_search()));
739         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
740         $output .= html_writer::end_tag('div');
742         $output .= html_writer::end_tag('div');
743         return $output;
744     }
747 /**
748  * Data structure representing backup files viewer
749  *
750  * @copyright 2010 Dongsheng Cai
751  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
752  * @since     Moodle 2.0
753  */
754 class backup_files_viewer implements renderable {
755     public $files;
756     public $filecontext;
757     public $component;
758     public $filearea;
759     public $currentcontext;
761     /**
762      * Constructor of backup_files_viewer class
763      * @param array $options
764      */
765     public function __construct(array $options = null) {
766         global $CFG, $USER;
767         $fs = get_file_storage();
768         $this->currentcontext = $options['currentcontext'];
769         $this->filecontext    = $options['filecontext'];
770         $this->component      = $options['component'];
771         $this->filearea       = $options['filearea'];
772         $files = $fs->get_area_files($this->filecontext->id, $this->component, $this->filearea, false, 'timecreated');
773         $this->files = array_reverse($files);
774     }