796c7d041730d91f94aa957a33e07eba35116b0a
[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     }
58     /**
59      * Prints a dependency notification
60      * @param string $message
61      * @return string
62      */
63     public function dependency_notification($message) {
64         return html_writer::tag('div', $message, array('class'=>'notification dependencies_enforced'));
65     }
67     /**
68      * Displays the details of a backup file
69      *
70      * @param stdClass $details
71      * @param moodle_url $nextstageurl
72      * @return string
73      */
74     public function backup_details($details, $nextstageurl) {
75         $yestick = $this->output->pix_icon('i/valid', get_string('yes'));
76         $notick = $this->output->pix_icon('i/valid', get_string('no'));
78         $html  = html_writer::start_tag('div', array('class'=>'backup-restore'));
80         $html .= html_writer::start_tag('div', array('class'=>'backup-section'));
81         $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, array('class'=>'header'));
82         $html .= $this->backup_detail_pair(get_string('backuptype', 'backup'), get_string('backuptype'.$details->type, 'backup'));
83         $html .= $this->backup_detail_pair(get_string('backupformat', 'backup'), get_string('backupformat'.$details->format, 'backup'));
84         $html .= $this->backup_detail_pair(get_string('backupmode', 'backup'), get_string('backupmode'.$details->mode, 'backup'));
85         $html .= $this->backup_detail_pair(get_string('backupdate', 'backup'), userdate($details->backup_date));
86         $html .= $this->backup_detail_pair(get_string('moodleversion', 'backup'),
87                 html_writer::tag('span', $details->moodle_release, array('class'=>'moodle_release')).
88                 html_writer::tag('span', '['.$details->moodle_version.']', array('class'=>'moodle_version sub-detail')));
89         $html .= $this->backup_detail_pair(get_string('backupversion', 'backup'),
90                 html_writer::tag('span', $details->backup_release, array('class'=>'moodle_release')).
91                 html_writer::tag('span', '['.$details->backup_version.']', array('class'=>'moodle_version sub-detail')));
92         $html .= $this->backup_detail_pair(get_string('originalwwwroot', 'backup'),
93                 html_writer::tag('span', $details->original_wwwroot, array('class'=>'originalwwwroot')).
94                 html_writer::tag('span', '['.$details->original_site_identifier_hash.']', array('class'=>'sitehash sub-detail')));
95         if (!empty($details->include_file_references_to_external_content)) {
96             $message = '';
97             if (backup_general_helper::backup_is_samesite($details)) {
98                 $message = $yestick . ' ' . get_string('filereferencessamesite', 'backup');
99             } else {
100                 $message = $notick . ' ' . get_string('filereferencesnotsamesite', 'backup');
101             }
102             $html .= $this->backup_detail_pair(get_string('includefilereferences', 'backup'), $message);
103         }
105         $html .= html_writer::end_tag('div');
107         $html .= html_writer::start_tag('div', array('class'=>'backup-section settings-section'));
108         $html .= $this->output->heading(get_string('backupsettings', 'backup'), 2, array('class'=>'header'));
109         foreach ($details->root_settings as $label=>$value) {
110             if ($label == 'filename' or $label == 'user_files') {
111                 continue;
112             }
113             $html .= $this->backup_detail_pair(get_string('rootsetting'.str_replace('_','',$label), 'backup'), $value?$yestick:$notick);
114         }
115         $html .= html_writer::end_tag('div');
117         if ($details->type === 'course') {
118             $html .= html_writer::start_tag('div', array('class'=>'backup-section'));
119             $html .= $this->output->heading(get_string('backupcoursedetails', 'backup'), 2, array('class'=>'header'));
120             $html .= $this->backup_detail_pair(get_string('coursetitle', 'backup'), $details->course->title);
121             $html .= $this->backup_detail_pair(get_string('courseid', 'backup'), $details->course->courseid);
123             $html .= html_writer::start_tag('div', array('class'=>'backup-sub-section'));
124             $html .= $this->output->heading(get_string('backupcoursesections', 'backup'), 3, array('class'=>'subheader'));
125             foreach ($details->sections as $key=>$section) {
126                 $included = $key.'_included';
127                 $userinfo = $key.'_userinfo';
128                 if ($section->settings[$included] && $section->settings[$userinfo]) {
129                     $value = get_string('sectionincanduser','backup');
130                 } else if ($section->settings[$included]) {
131                     $value = get_string('sectioninc','backup');
132                 } else {
133                     continue;
134                 }
135                 $html .= $this->backup_detail_pair(get_string('backupcoursesection', 'backup', $section->title), $value);
136                 $table = null;
137                 foreach ($details->activities as $activitykey=>$activity) {
138                     if ($activity->sectionid != $section->sectionid) {
139                         continue;
140                     }
141                     if (empty($table)) {
142                         $table = new html_table();
143                         $table->head = array('Module', 'Title', 'Userinfo');
144                         $table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
145                         $table->align = array('left','left', 'center');
146                         $table->attributes = array('class'=>'activitytable generaltable');
147                         $table->data = array();
148                     }
149                     $name = get_string('pluginname', $activity->modulename);
150                     $icon = new pix_icon('icon', $name, $activity->modulename, array('class' => 'iconlarge icon-pre'));
151                     $table->data[] = array(
152                         $this->output->render($icon).$name,
153                         $activity->title,
154                         ($activity->settings[$activitykey.'_userinfo'])?$yestick:$notick,
155                     );
156                 }
157                 if (!empty($table)) {
158                     $html .= $this->backup_detail_pair(get_string('sectionactivities','backup'), html_writer::table($table));
159                 }
161             }
162             $html .= html_writer::end_tag('div');
163             $html .= html_writer::end_tag('div');
164         }
166         $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
167         $html .= html_writer::end_tag('div');
169         return $html;
170     }
172     /**
173      * Displays the general information about a backup file with non-standard format
174      *
175      * @param moodle_url $nextstageurl URL to send user to
176      * @param array $details basic info about the file (format, type)
177      * @return string HTML code to display
178      */
179     public function backup_details_nonstandard($nextstageurl, array $details) {
181         $html  = html_writer::start_tag('div', array('class' => 'backup-restore nonstandardformat'));
182         $html .= html_writer::start_tag('div', array('class' => 'backup-section'));
183         $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, 'header');
184         $html .= $this->output->box(get_string('backupdetailsnonstandardinfo', 'backup'), 'noticebox');
185         $html .= $this->backup_detail_pair(
186             get_string('backupformat', 'backup'),
187             get_string('backupformat'.$details['format'], 'backup'));
188         $html .= $this->backup_detail_pair(
189             get_string('backuptype', 'backup'),
190             get_string('backuptype'.$details['type'], 'backup'));
191         $html .= html_writer::end_tag('div');
192         $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
193         $html .= html_writer::end_tag('div');
195         return $html;
196     }
198     /**
199      * Displays the general information about a backup file with unknown format
200      *
201      * @param moodle_url $nextstageurl URL to send user to
202      * @return string HTML code to display
203      */
204     public function backup_details_unknown(moodle_url $nextstageurl) {
206         $html  = html_writer::start_tag('div', array('class' => 'unknownformat'));
207         $html .= $this->output->heading(get_string('errorinvalidformat', 'backup'), 2, 'notifyproblem');
208         $html .= html_writer::tag('div', get_string('errorinvalidformatinfo', 'backup'), array('class' => 'notifyproblem'));
209         $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
210         $html .= html_writer::end_tag('div');
212         return $html;
213     }
215     /**
216      * Displays a course selector for restore
217      *
218      * @param moodle_url $nextstageurl
219      * @param bool $wholecourse true if we are restoring whole course (as with backup::TYPE_1COURSE), false otherwise
220      * @param restore_category_search $categories
221      * @param restore_course_search $courses
222      * @param int $currentcourse
223      * @return string
224      */
225     public function course_selector(moodle_url $nextstageurl, $wholecourse = true, restore_category_search $categories = null, restore_course_search $courses=null, $currentcourse = null) {
226         global $CFG, $PAGE;
227         require_once($CFG->dirroot.'/course/lib.php');
229         $nextstageurl->param('sesskey', sesskey());
231         $form = html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
232         foreach ($nextstageurl->params() as $key=>$value) {
233             $form .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
234         }
236         $hasrestoreoption = false;
238         $html  = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore'));
239         if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) {
240             // New course
241             $hasrestoreoption = true;
242             $html .= $form;
243             $html .= html_writer::start_tag('div', array('class'=>'bcs-new-course backup-section'));
244             $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class'=>'header'));
245             $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target', backup::TARGET_NEW_COURSE, array('checked'=>'checked'));
246             $html .= $this->backup_detail_pair(get_string('selectacategory', 'backup'), $this->render($categories));
247             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
248             $html .= html_writer::end_tag('div');
249             $html .= html_writer::end_tag('form');
250         }
252         if ($wholecourse && !empty($currentcourse)) {
253             // Current course
254             $hasrestoreoption = true;
255             $html .= $form;
256             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'targetid', 'value'=>$currentcourse));
257             $html .= html_writer::start_tag('div', array('class'=>'bcs-current-course backup-section'));
258             $html .= $this->output->heading(get_string('restoretocurrentcourse', 'backup'), 2, array('class'=>'header'));
259             $html .= $this->backup_detail_input(get_string('restoretocurrentcourseadding', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_ADDING, array('checked'=>'checked'));
260             $html .= $this->backup_detail_input(get_string('restoretocurrentcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_DELETING);
261             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
262             $html .= html_writer::end_tag('div');
263             $html .= html_writer::end_tag('form');
264         }
266         if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search())) {
267             // Existing course
268             $hasrestoreoption = true;
269             $html .= $form;
270             $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section'));
271             $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header'));
272             if ($wholecourse) {
273                 $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_ADDING, array('checked'=>'checked'));
274                 $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_DELETING);
275                 $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
276             } else {
277                 // We only allow restore adding to existing for now. Enforce it here.
278                 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'target', 'value'=>backup::TARGET_EXISTING_ADDING));
279                 $courses->invalidate_results(); // Clean list of courses
280                 $courses->set_include_currentcourse(); // Show current course in the list
281                 $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
282             }
283             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
284             $html .= html_writer::end_tag('div');
285             $html .= html_writer::end_tag('form');
286         }
288         if (!$hasrestoreoption) {
289             echo $this->output->notification(get_string('norestoreoptions','backup'));
290         }
292         $html .= html_writer::end_tag('div');
293         return $html;
294     }
296     /**
297      * Displays the import course selector
298      *
299      * @param moodle_url $nextstageurl
300      * @param import_course_search $courses
301      * @return string
302      */
303     public function import_course_selector(moodle_url $nextstageurl, import_course_search $courses=null) {
304         $html  = html_writer::start_tag('div', array('class'=>'import-course-selector backup-restore'));
305         $html .= html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
306         foreach ($nextstageurl->params() as $key=>$value) {
307             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
308         }
309         // We only allow import adding for now. Enforce it here.
310         $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'target', 'value'=>backup::TARGET_CURRENT_ADDING));
311         $html .= html_writer::start_tag('div', array('class'=>'ics-existing-course backup-section'));
312         $html .= $this->output->heading(get_string('importdatafrom'), 2, array('class'=>'header'));
313         $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
314         $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
315         $html .= html_writer::end_tag('div');
316         $html .= html_writer::end_tag('form');
317         $html .= html_writer::end_tag('div');
318         return $html;
319     }
321     /**
322      * Creates a detailed pairing (key + value)
323      *
324      * @staticvar int $count
325      * @param string $label
326      * @param string $value
327      * @return string
328      */
329     protected function backup_detail_pair($label, $value) {
330         static $count= 0;
331         $count++;
332         $html  = html_writer::start_tag('div', array('class'=>'detail-pair'));
333         $html .= html_writer::tag('label', $label, array('class'=>'detail-pair-label', 'for'=>'detail-pair-value-'.$count));
334         $html .= html_writer::tag('div', $value, array('class'=>'detail-pair-value', 'name'=>'detail-pair-value-'.$count));
335         $html .= html_writer::end_tag('div');
336         return $html;
337     }
339     /**
340      * Created a detailed pairing with an input
341      *
342      * @param string $label
343      * @param string $type
344      * @param string $name
345      * @param string $value
346      * @param array $attributes
347      * @param string|null $description
348      * @return string
349      */
350     protected function backup_detail_input($label, $type, $name, $value, array $attributes=array(), $description=null) {
351         if (!empty ($description)) {
352             $description = html_writer::tag('span', $description, array('class'=>'description'));
353         } else {
354             $description = '';
355         }
356         return $this->backup_detail_pair($label, html_writer::empty_tag('input', $attributes+array('name'=>$name, 'type'=>$type, 'value'=>$value)).$description);
357     }
359     /**
360      * Creates a detailed pairing with a select
361      *
362      * @param string $label
363      * @param string $name
364      * @param array $options
365      * @param string $selected
366      * @param bool $nothing
367      * @param array $attributes
368      * @param string|null $description
369      * @return string
370      */
371     protected function backup_detail_select($label, $name, $options, $selected='', $nothing=false, array $attributes=array(), $description=null) {
372         if (!empty ($description)) {
373             $description = html_writer::tag('span', $description, array('class'=>'description'));
374         } else {
375             $description = '';
376         }
377         return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
378     }
380     /**
381      * Displays precheck notices
382      *
383      * @param array $results
384      * @return string
385      */
386     public function precheck_notices($results) {
387         $output = html_writer::start_tag('div', array('class'=>'restore-precheck-notices'));
388         if (array_key_exists('errors', $results)) {
389             foreach ($results['errors'] as $error) {
390                 $output .= $this->output->notification($error);
391             }
392         }
393         if (array_key_exists('warnings', $results)) {
394             foreach ($results['warnings'] as $warning) {
395                 $output .= $this->output->notification($warning, 'notifywarning notifyproblem');
396             }
397         }
398         return $output.html_writer::end_tag('div');
399     }
401     /**
402      * Displays substage buttons
403      *
404      * @param bool $haserrors
405      * @return string
406      */
407     public function substage_buttons($haserrors) {
408         $output  = html_writer::start_tag('div', array('continuebutton'));
409         if (!$haserrors) {
410             $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue')));
411         }
412         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
413         $output .= html_writer::end_tag('div');
414         return $output;
415     }
417     /**
418      * Displays a role mapping interface
419      *
420      * @param array $rolemappings
421      * @param array $roles
422      * @return string
423      */
424     public function role_mappings($rolemappings, $roles) {
425         $roles[0] = get_string('none');
426         $output  = html_writer::start_tag('div', array('class'=>'restore-rolemappings'));
427         $output .= $this->output->heading(get_string('restorerolemappings', 'backup'), 2);
428         foreach ($rolemappings as $id=>$mapping) {
429             $label = $mapping->name;
430             $name = 'mapping'.$id;
431             $selected = $mapping->targetroleid;
432             $output .= $this->backup_detail_select($label, $name, $roles, $mapping->targetroleid, false, array(), $mapping->description);
433         }
434         $output .= html_writer::end_tag('div');
435         return $output;
436     }
438     /**
439      * Displays a continue button
440      *
441      * @param string|moodle_url $url
442      * @param string $method
443      * @return string
444      */
445     public function continue_button($url, $method='post') {
446         if (!($url instanceof moodle_url)) {
447             $url = new moodle_url($url);
448         }
449         if ($method != 'post') {
450             $method = 'get';
451         }
452         $url->param('sesskey', sesskey());
453         $button = new single_button($url, get_string('continue'), $method);
454         $button->class = 'continuebutton';
455         return $this->render($button);
456     }
457     /**
458      * Print a backup files tree
459      * @param array $options
460      * @return string
461      */
462     public function backup_files_viewer(array $options = null) {
463         $files = new backup_files_viewer($options);
464         return $this->render($files);
465     }
467     /**
468      * Displays a backup files viewer
469      *
470      * @global stdClass $USER
471      * @param backup_files_viewer $tree
472      * @return string
473      */
474     public function render_backup_files_viewer(backup_files_viewer $viewer) {
475         global $CFG;
476         $files = $viewer->files;
478         $table = new html_table();
479         $table->attributes['class'] = 'backup-files-table generaltable';
480         $table->head = array(get_string('filename', 'backup'), get_string('time'), get_string('size'), get_string('download'), get_string('restore'));
481         $table->width = '100%';
482         $table->data = array();
484         foreach ($files as $file) {
485             if ($file->is_directory()) {
486                 continue;
487             }
488             $fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename(), true);
489             $params = array();
490             $params['action'] = 'choosebackupfile';
491             $params['filename'] = $file->get_filename();
492             $params['filepath'] = $file->get_filepath();
493             $params['component'] = $file->get_component();
494             $params['filearea'] = $file->get_filearea();
495             $params['filecontextid'] = $file->get_contextid();
496             $params['contextid'] = $viewer->currentcontext->id;
497             $params['itemid'] = $file->get_itemid();
498             $restoreurl = new moodle_url('/backup/restorefile.php', $params);
499             $table->data[] = array(
500                 $file->get_filename(),
501                 userdate($file->get_timemodified()),
502                 display_size($file->get_filesize()),
503                 html_writer::link($fileurl, get_string('download')),
504                 html_writer::link($restoreurl, get_string('restore')),
505                 );
506         }
508         $html = html_writer::table($table);
509         $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');
511         return $html;
512     }
514     /**
515      * Renders a restore course search object
516      *
517      * @param restore_course_search $component
518      * @return string
519      */
520     public function render_restore_course_search(restore_course_search $component) {
521         $url = $component->get_url();
523         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
524         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
526         $table = new html_table();
527         $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
528         $table->data = array();
529         if ($component->get_count() !== 0) {
530             foreach ($component->get_results() as $course) {
531                 $row = new html_table_row();
532                 $row->attributes['class'] = 'rcs-course';
533                 if (!$course->visible) {
534                     $row->attributes['class'] .= ' dimmed';
535                 }
536                 $row->cells = array(
537                     html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)),
538                     format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
539                     format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
540                 );
541                 $table->data[] = $row;
542             }
543             if ($component->has_more_results()) {
544                 $cell = new html_table_cell(get_string('moreresults', 'backup'));
545                 $cell->colspan = 3;
546                 $cell->attributes['class'] = 'notifyproblem';
547                 $row = new html_table_row(array($cell));
548                 $row->attributes['class'] = 'rcs-course';
549                 $table->data[] = $row;
550             }
551         } else {
552             $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
553             $cell->colspan = 3;
554             $cell->attributes['class'] = 'notifyproblem';
555             $row = new html_table_row(array($cell));
556             $row->attributes['class'] = 'rcs-course';
557             $table->data[] = $row;
558         }
559         $output .= html_writer::table($table);
560         $output .= html_writer::end_tag('div');
562         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
563         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
564         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
565         $output .= html_writer::end_tag('div');
567         $output .= html_writer::end_tag('div');
568         return $output;
569     }
571     /**
572      * Renders an import course search object
573      *
574      * @param import_course_search $component
575      * @return string
576      */
577     public function render_import_course_search(import_course_search $component) {
578         $url = $component->get_url();
580         $output = html_writer::start_tag('div', array('class' => 'import-course-search'));
581         if ($component->get_count() === 0) {
582             $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
584             $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
585             $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
586             $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
587             $output .= html_writer::end_tag('div');
589             $output .= html_writer::end_tag('div');
590             return $output;
591         }
593         $count_str = '';
594         if ($component->has_more_results()) {
595             $count_str = get_string('morecoursesearchresults', 'backup', $component->get_count());
596         } else {
597             $count_str = get_string('totalcoursesearchresults', 'backup', $component->get_count());
598         }
600         $output .= html_writer::tag('div', $count_str, array('class'=>'ics-totalresults'));
601         $output .= html_writer::start_tag('div', array('class' => 'ics-results'));
603         $table = new html_table();
604         $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
605         $table->data = array();
606         foreach ($component->get_results() as $course) {
607             $row = new html_table_row();
608             $row->attributes['class'] = 'ics-course';
609             if (!$course->visible) {
610                 $row->attributes['class'] .= ' dimmed';
611             }
612             $row->cells = array(
613                 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'importid', 'value'=>$course->id)),
614                 format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
615                 format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
616             );
617             $table->data[] = $row;
618         }
619         if ($component->has_more_results()) {
620             $cell = new html_table_cell(get_string('moreresults', 'backup'));
621             $cell->colspan = 3;
622             $cell->attributes['class'] = 'notifyproblem';
623             $row = new html_table_row(array($cell));
624             $row->attributes['class'] = 'rcs-course';
625             $table->data[] = $row;
626         }
627         $output .= html_writer::table($table);
628         $output .= html_writer::end_tag('div');
630         $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
631         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
632         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
633         $output .= html_writer::end_tag('div');
635         $output .= html_writer::end_tag('div');
636         return $output;
637     }
639     /**
640      * Renders a restore category search object
641      *
642      * @param restore_category_search $component
643      * @return string
644      */
645     public function render_restore_category_search(restore_category_search $component) {
646         $url = $component->get_url();
648         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
649         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
651         $table = new html_table();
652         $table->head = array('', get_string('name'), get_string('description'));
653         $table->data = array();
655         if ($component->get_count() !== 0) {
656             foreach ($component->get_results() as $category) {
657                 $row = new html_table_row();
658                 $row->attributes['class'] = 'rcs-course';
659                 if (!$category->visible) {
660                     $row->attributes['class'] .= ' dimmed';
661                 }
662                 $row->cells = array(
663                     html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)),
664                     format_string($category->name, true, array('context' => context_coursecat::instance($category->id))),
665                     format_text($category->description, $category->descriptionformat, array('overflowdiv'=>true))
666                 );
667                 $table->data[] = $row;
668             }
669             if ($component->has_more_results()) {
670                 $cell = new html_table_cell(get_string('moreresults', 'backup'));
671                 $cell->attributes['class'] = 'notifyproblem';
672                 $cell->colspan = 3;
673                 $row = new html_table_row(array($cell));
674                 $row->attributes['class'] = 'rcs-course';
675                 $table->data[] = $row;
676             }
677         } else {
678             $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
679             $cell->colspan = 3;
680             $cell->attributes['class'] = 'notifyproblem';
681             $row = new html_table_row(array($cell));
682             $row->attributes['class'] = 'rcs-course';
683             $table->data[] = $row;
684         }
685         $output .= html_writer::table($table);
686         $output .= html_writer::end_tag('div');
688         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
689         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_category_search::$VAR_SEARCH, 'value'=>$component->get_search()));
690         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
691         $output .= html_writer::end_tag('div');
693         $output .= html_writer::end_tag('div');
694         return $output;
695     }
698 /**
699  * Data structure representing backup files viewer
700  *
701  * @copyright 2010 Dongsheng Cai
702  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
703  * @since     Moodle 2.0
704  */
705 class backup_files_viewer implements renderable {
706     public $files;
707     public $filecontext;
708     public $component;
709     public $filearea;
710     public $currentcontext;
712     /**
713      * Constructor of backup_files_viewer class
714      * @param array $options
715      */
716     public function __construct(array $options = null) {
717         global $CFG, $USER;
718         $fs = get_file_storage();
719         $this->currentcontext = $options['currentcontext'];
720         $this->filecontext    = $options['filecontext'];
721         $this->component      = $options['component'];
722         $this->filearea       = $options['filearea'];
723         $files = $fs->get_area_files($this->filecontext->id, $this->component, $this->filearea, false, 'timecreated');
724         $this->files = array_reverse($files);
725     }