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