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