Merge branch 'MDL-38766/MOODLE_23_STABLE' of github.com:kemitix/moodle into MOODLE_23...
[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         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);
151                     $table->data[] = array(
152                         $this->output->render($icon).'&nbsp;'.$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'));
525         $countstr = '';
526         if ($component->has_more_results()) {
527             $countstr = get_string('morecoursesearchresults', 'backup', $component->get_count());
528         } else {
529             $countstr = get_string('totalcoursesearchresults', 'backup', $component->get_count());
530         }
532         $output .= html_writer::tag('div', $countstr, array('class'=>'rcs-totalresults'));
533         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
535         $table = new html_table();
536         $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
537         $table->data = array();
538         if ($component->get_count() !== 0) {
539             foreach ($component->get_results() as $course) {
540                 $row = new html_table_row();
541                 $row->attributes['class'] = 'rcs-course';
542                 if (!$course->visible) {
543                     $row->attributes['class'] .= ' dimmed';
544                 }
545                 $row->cells = array(
546                     html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)),
547                     format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))),
548                     format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)))
549                 );
550                 $table->data[] = $row;
551             }
552             if ($component->has_more_results()) {
553                 $cell = new html_table_cell(get_string('moreresults', 'backup'));
554                 $cell->colspan = 3;
555                 $cell->attributes['class'] = 'notifyproblem';
556                 $row = new html_table_row(array($cell));
557                 $row->attributes['class'] = 'rcs-course';
558                 $table->data[] = $row;
559             }
560         } else {
561             $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
562             $cell->colspan = 3;
563             $cell->attributes['class'] = 'notifyproblem';
564             $row = new html_table_row(array($cell));
565             $row->attributes['class'] = 'rcs-course';
566             $table->data[] = $row;
567         }
568         $output .= html_writer::table($table);
569         $output .= html_writer::end_tag('div');
571         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
572         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
573         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
574         $output .= html_writer::end_tag('div');
576         $output .= html_writer::end_tag('div');
577         return $output;
578     }
580     /**
581      * Renders an import course search object
582      *
583      * @param import_course_search $component
584      * @return string
585      */
586     public function render_import_course_search(import_course_search $component) {
587         $url = $component->get_url();
589         $output = html_writer::start_tag('div', array('class' => 'import-course-search'));
590         if ($component->get_count() === 0) {
591             $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
593             $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
594             $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
595             $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
596             $output .= html_writer::end_tag('div');
598             $output .= html_writer::end_tag('div');
599             return $output;
600         }
602         $countstr = '';
603         if ($component->has_more_results()) {
604             $countstr = get_string('morecoursesearchresults', 'backup', $component->get_count());
605         } else {
606             $countstr = get_string('totalcoursesearchresults', 'backup', $component->get_count());
607         }
609         $output .= html_writer::tag('div', $countstr, array('class'=>'ics-totalresults'));
610         $output .= html_writer::start_tag('div', array('class' => 'ics-results'));
612         $table = new html_table();
613         $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
614         $table->data = array();
615         foreach ($component->get_results() as $course) {
616             $row = new html_table_row();
617             $row->attributes['class'] = 'ics-course';
618             if (!$course->visible) {
619                 $row->attributes['class'] .= ' dimmed';
620             }
621             $row->cells = array(
622                 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'importid', 'value'=>$course->id)),
623                 format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))),
624                 format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)))
625             );
626             $table->data[] = $row;
627         }
628         if ($component->has_more_results()) {
629             $cell = new html_table_cell(get_string('moreresults', 'backup'));
630             $cell->colspan = 3;
631             $cell->attributes['class'] = 'notifyproblem';
632             $row = new html_table_row(array($cell));
633             $row->attributes['class'] = 'rcs-course';
634             $table->data[] = $row;
635         }
636         $output .= html_writer::table($table);
637         $output .= html_writer::end_tag('div');
639         $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
640         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
641         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
642         $output .= html_writer::end_tag('div');
644         $output .= html_writer::end_tag('div');
645         return $output;
646     }
648     /**
649      * Renders a restore category search object
650      *
651      * @param restore_category_search $component
652      * @return string
653      */
654     public function render_restore_category_search(restore_category_search $component) {
655         $url = $component->get_url();
657         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
658         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
660         $table = new html_table();
661         $table->head = array('', get_string('name'), get_string('description'));
662         $table->data = array();
664         if ($component->get_count() !== 0) {
665             foreach ($component->get_results() as $category) {
666                 $row = new html_table_row();
667                 $row->attributes['class'] = 'rcs-course';
668                 if (!$category->visible) {
669                     $row->attributes['class'] .= ' dimmed';
670                 }
671                 $row->cells = array(
672                     html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)),
673                     format_string($category->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $category->id))),
674                     format_text($category->description, $category->descriptionformat, array('overflowdiv'=>true))
675                 );
676                 $table->data[] = $row;
677             }
678             if ($component->has_more_results()) {
679                 $cell = new html_table_cell(get_string('moreresults', 'backup'));
680                 $cell->attributes['class'] = 'notifyproblem';
681                 $cell->colspan = 3;
682                 $row = new html_table_row(array($cell));
683                 $row->attributes['class'] = 'rcs-course';
684                 $table->data[] = $row;
685             }
686         } else {
687             $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
688             $cell->colspan = 3;
689             $cell->attributes['class'] = 'notifyproblem';
690             $row = new html_table_row(array($cell));
691             $row->attributes['class'] = 'rcs-course';
692             $table->data[] = $row;
693         }
694         $output .= html_writer::table($table);
695         $output .= html_writer::end_tag('div');
697         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
698         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_category_search::$VAR_SEARCH, 'value'=>$component->get_search()));
699         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
700         $output .= html_writer::end_tag('div');
702         $output .= html_writer::end_tag('div');
703         return $output;
704     }
707 /**
708  * Data structure representing backup files viewer
709  *
710  * @copyright 2010 Dongsheng Cai
711  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
712  * @since     Moodle 2.0
713  */
714 class backup_files_viewer implements renderable {
715     public $files;
716     public $filecontext;
717     public $component;
718     public $filearea;
719     public $currentcontext;
721     /**
722      * Constructor of backup_files_viewer class
723      * @param array $options
724      */
725     public function __construct(array $options = null) {
726         global $CFG, $USER;
727         $fs = get_file_storage();
728         $this->currentcontext = $options['currentcontext'];
729         $this->filecontext    = $options['filecontext'];
730         $this->component      = $options['component'];
731         $this->filearea       = $options['filearea'];
732         $files = $fs->get_area_files($this->filecontext->id, $this->component, $this->filearea, false, 'timecreated');
733         $this->files = array_reverse($files);
734     }