course-import MDL-23752 Implemented course import functionality back into Moodle 2.0
[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         $html .= html_writer::start_tag('div', array('class'=>'backup-section'));
106         $html .= $this->output->heading(get_string('backupcoursedetails', 'backup'), 2, array('class'=>'header'));
107         $html .= $this->backup_detail_pair(get_string('coursetitle', 'backup'), $details->course->title);
108         $html .= $this->backup_detail_pair(get_string('courseid', 'backup'), $details->course->courseid);
110         $html .= html_writer::start_tag('div', array('class'=>'backup-sub-section'));
111         $html .= $this->output->heading(get_string('backupcoursesections', 'backup'), 3, array('class'=>'subheader'));
112         foreach ($details->sections as $key=>$section) {
113             $included = $key.'_included';
114             $userinfo = $key.'_userinfo';
115             if ($section->settings[$included] && $section->settings[$userinfo]) {
116                 $value = get_string('sectionincanduser','backup');
117             } else if ($section->settings[$included]) {
118                 $value = get_string('sectioninc','backup');
119             } else {
120                 continue;
121             }
122             $html .= $this->backup_detail_pair(get_string('backupcoursesection', 'backup', $section->title), $value);
123             $table = null;
124             foreach ($details->activities as $activitykey=>$activity) {
125                 if ($activity->sectionid != $section->sectionid) {
126                     continue;
127                 }
128                 if (empty($table)) {
129                     $table = new html_table();
130                     $table->head = array('Module', 'Title', 'Userinfo');
131                     $table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
132                     $table->align = array('left','left', 'center');
133                     $table->attributes = array('class'=>'activitytable generaltable');
134                     $table->data = array();
135                 }
136                 $table->data[] = array(
137                     get_string('pluginname', $activity->modulename),
138                     $activity->title,
139                     ($activity->settings[$activitykey.'_userinfo'])?$yestick:$notick,
140                 );
141             }
142             if (!empty($table)) {
143                 $html .= $this->backup_detail_pair(get_string('sectionactivities','backup'), html_writer::table($table));
144             }
146         }
147         $html .= html_writer::end_tag('div');
148         $html .= html_writer::end_tag('div');
149         $html .= html_writer::end_tag('div');
151         $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
153         return $html;
154     }
156     /**
157      * Displays a course selector for restore
158      *
159      * @param moodle_url $nextstageurl
160      * @param stdClass $details
161      * @param restore_category_search $categories
162      * @param restore_course_search $courses
163      * @param int $currentcourse
164      * @return string
165      */
166     public function course_selector(moodle_url $nextstageurl, $details, restore_category_search $categories = null, restore_course_search $courses=null, $currentcourse = null) {
167         global $CFG;
168         require_once($CFG->dirroot.'/course/lib.php');
170         $nextstageurl->param('sesskey', sesskey());
172         $form = html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
173         foreach ($nextstageurl->params() as $key=>$value) {
174             $form .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
175         }
177         $html  = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore'));
179         // Current course
180         if (!empty($currentcourse)) {
181             $html .= $form;
182             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'targetid', 'value'=>$currentcourse));
183             $html .= html_writer::start_tag('div', array('class'=>'bcs-current-course backup-section'));
184             $html .= $this->output->heading(get_string('restoretocurrentcourse', 'backup'), 2, array('class'=>'header'));
185             $html .= $this->backup_detail_input(get_string('restoretocurrentcourseadding', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_ADDING, array('checked'=>'checked'));
186             $html .= $this->backup_detail_input(get_string('restoretocurrentcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_DELETING);
187             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
188             $html .= html_writer::end_tag('div');
189             $html .= html_writer::end_tag('form');
190         }
192         if (!empty($categories) && ($categories->get_resultscount() > 0 || $categories->get_search() == '')) {
193             // New course
194             $html .= $form;
195             $html .= html_writer::start_tag('div', array('class'=>'bcs-new-course backup-section'));
196             $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class'=>'header'));
197             $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target', backup::TARGET_NEW_COURSE, array('checked'=>'checked'));
198             //$html .= $this->backup_detail_select(get_string('coursecategory', 'backup'), 'targetid', $categories);
199             $html .= $this->backup_detail_pair(get_string('selectacategory', 'backup'), $this->render($categories));
200             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
201             $html .= html_writer::end_tag('div');
202             $html .= html_writer::end_tag('form');
203         }
205         if (!empty($courses) && ($courses->get_resultscount() > 0 || $courses->get_search() == '')) {
206             // Existing course
207             $html .= $form;
208             $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section'));
209             $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header'));
210             $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_ADDING, array('checked'=>'checked'));
211             $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_DELETING);
212             $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
213             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
214             $html .= html_writer::end_tag('div');
215             $html .= html_writer::end_tag('form');
216         }
218         $html .= html_writer::end_tag('div');
219         return $html;
220     }
222     /**
223      * Displays the import course selector
224      *
225      * @param moodle_url $nextstageurl
226      * @param import_course_search $courses
227      * @return string
228      */
229     public function import_course_selector(moodle_url $nextstageurl, import_course_search $courses=null) {
230         $html  = html_writer::start_tag('div', array('class'=>'import-course-selector backup-restore'));
231         $html .= html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
232         foreach ($nextstageurl->params() as $key=>$value) {
233             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
234         }
235         $html .= html_writer::start_tag('div', array('class'=>'ics-existing-course backup-section'));
236         $html .= $this->output->heading(get_string('importdatafrom'), 2, array('class'=>'header'));
237         $html .= $this->backup_detail_input(get_string('importadding', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_ADDING, array('checked'=>'checked'), get_string('importaddingdesc', 'backup'));
238         $html .= $this->backup_detail_input(get_string('importdeleting', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_DELETING, array(), get_string('importdeletingdesc', 'backup'));
239         $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
240         $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
241         $html .= html_writer::end_tag('div');
242         $html .= html_writer::end_tag('form');
243         $html .= html_writer::end_tag('div');
244         return $html;
245     }
247     /**
248      * Creates a detailed pairing (key + value)
249      *
250      * @staticvar int $count
251      * @param string $label
252      * @param string $value
253      * @return string
254      */
255     protected function backup_detail_pair($label, $value) {
256         static $count= 0;
257         $count++;
258         $html  = html_writer::start_tag('div', array('class'=>'detail-pair'));
259         $html .= html_writer::tag('label', $label, array('class'=>'detail-pair-label', 'for'=>'detail-pair-value-'.$count));
260         $html .= html_writer::tag('div', $value, array('class'=>'detail-pair-value', 'name'=>'detail-pair-value-'.$count));
261         $html .= html_writer::end_tag('div');
262         return $html;
263     }
265     /**
266      * Created a detailed pairing with an input
267      *
268      * @param string $label
269      * @param string $type
270      * @param string $name
271      * @param string $value
272      * @param array $attributes
273      * @param string|null $description
274      * @return string
275      */
276     protected function backup_detail_input($label, $type, $name, $value, array $attributes=array(), $description=null) {
277         if (!empty ($description)) {
278             $description = html_writer::tag('span', $description, array('class'=>'description'));
279         } else {
280             $description = '';
281         }
282         return $this->backup_detail_pair($label, html_writer::empty_tag('input', $attributes+array('name'=>$name, 'type'=>$type, 'value'=>$value)).$description);
283     }
285     /**
286      * Creates a detailed pairing with a select
287      *
288      * @param string $label
289      * @param string $name
290      * @param array $options
291      * @param string $selected
292      * @param bool $nothing
293      * @param array $attributes
294      * @param string|null $description
295      * @return string
296      */
297     protected function backup_detail_select($label, $name, $options, $selected='', $nothing=false, array $attributes=array(), $description=null) {
298         if (!empty ($description)) {
299             $description = html_writer::tag('span', $description, array('class'=>'description'));
300         } else {
301             $description = '';
302         }
303         return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
304     }
306     /**
307      * Displays precheck notices
308      *
309      * @param array $results
310      * @return string
311      */
312     public function precheck_notices($results) {
313         $output = html_writer::start_tag('div', array('class'=>'restore-precheck-notices'));
314         if (array_key_exists('errors', $results)) {
315             foreach ($results['errors'] as $error) {
316                 $output .= $this->output->notification($error);
317             }
318         }
319         if (array_key_exists('warnings', $results)) {
320             foreach ($results['warnings'] as $warning) {
321                 $output .= $this->output->notification($warning, 'notifywarning notifyproblem');
322             }
323         }
324         return $output.html_writer::end_tag('div');
325     }
327     /**
328      * Displays substage buttons
329      *
330      * @param bool $haserrors
331      * @return string
332      */
333     public function substage_buttons($haserrors) {
334         $output  = html_writer::start_tag('div', array('continuebutton'));
335         if (!$haserrors) {
336             $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue')));
337         }
338         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
339         $output .= html_writer::end_tag('div');
340         return $output;
341     }
343     /**
344      * Displays a role mapping interface
345      *
346      * @param array $rolemappings
347      * @param array $roles
348      * @return string
349      */
350     public function role_mappings($rolemappings, $roles) {
351         $roles[0] = get_string('none');
352         $output  = html_writer::start_tag('div', array('class'=>'restore-rolemappings'));
353         $output .= $this->output->heading(get_string('restorerolemappings', 'backup'), 2);
354         foreach ($rolemappings as $id=>$mapping) {
355             $label = $mapping->name;
356             $name = 'mapping'.$id;
357             $selected = $mapping->targetroleid;
358             $output .= $this->backup_detail_select($label, $name, $roles, $mapping->targetroleid, false, array(), $mapping->description);
359         }
360         $output .= html_writer::end_tag('div');
361         return $output;
362     }
364     /**
365      * Displays a continue button
366      *
367      * @param string|moodle_url $url
368      * @param string $method
369      * @return string
370      */
371     public function continue_button($url, $method='post') {
372         if (!($url instanceof moodle_url)) {
373             $url = new moodle_url($url);
374         }
375         if ($method != 'post') {
376             $method = 'get';
377         }
378         $url->param('sesskey', sesskey());
379         $button = new single_button($url, get_string('continue'), $method);
380         $button->class = 'continuebutton';
381         return $this->render($button);
382     }
383     /**
384      * Print a backup files tree
385      * @param array $options
386      * @return string
387      */
388     public function backup_files_viewer(array $options = null) {
389         $tree = new backup_files_viewer($options);
390         return $this->render($tree);
391     }
393     /**
394      * Displays a backup files viewer
395      *
396      * @global stdClass $USER
397      * @param backup_files_viewer $tree
398      * @return string
399      */
400     public function render_backup_files_viewer(backup_files_viewer $tree) {
401         global $USER;
402         $user_context = get_context_instance(CONTEXT_USER, $USER->id);
403         $options = new stdclass;
404         $module = array('name'=>'backup_files_tree', 'fullpath'=>'/backup/util/ui/module.js', 'requires'=>array('yui2-treeview', 'yui2-json'), 'strings'=>array(array('restore', 'moodle')));
405         $htmlid = 'backup-treeview-'.uniqid();
406         $options->htmlid = $htmlid;
407         $options->usercontextid = $user_context->id;
408         $options->currentcontextid = $tree->options['context']->id;
409         $this->page->requires->js_init_call('M.core_backup_files_tree.init', array($options), false, $module);
411         $html = '<div>';
412         foreach($tree->path as $path) {
413             $html .= $path;
414             $html .= ' / ';
415         }
416         $html .= '</div>';
418         $html .= '<div id="'.$htmlid.'" class="filemanager-container">';
419         if (empty($tree->tree)) {
420             $html .= get_string('nofilesavailable', 'repository');
421         } else {
422             $html .= '<ul>';
423             foreach($tree->tree as $node) {
424                 $link_attributes = array();
425                 if (!empty($node['isdir'])) {
426                     $class = ' class="file-tree-folder"';
427                     $restore_link = '';
428                 } else {
429                     $class = ' class="file-tree-file"';
430                     $link_attributes['target'] = '_blank';
431                     $restore_link = html_writer::link($node['restoreurl'], get_string('restore', 'moodle'), $link_attributes);
432                 }
433                 $html .= '<li '.$class.'>';
434                 $html .= html_writer::link($node['url'], $node['filename'], $link_attributes);
435                 // when js is off, use this restore link
436                 // otherwise, yui treeview will generate a restore link in js
437                 $html .= ' '.$restore_link;
438                 $html .= '</li>';
439             }
440             $html .= '</ul>';
441         }
442         $html .= '</div>';
443         return $html;
444     }
446     /**
447      * Renders a restore course search object
448      *
449      * @param restore_course_search $component
450      * @return string
451      */
452     public function render_restore_course_search(restore_course_search $component) {
453         $url = $component->get_url();
455         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
456         if ($component->get_totalcount() === 0) {
457             $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
458             $output .= html_writer::end_tag('div');
459             return $output;
460         }
462         $output .= html_writer::tag('div', get_string('totalcoursesearchresults', 'backup', $component->get_totalcount()), array('class'=>'rcs-totalresults'));
464         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
465         if ($component->get_totalpages()>1) {
466             $pagingbar = new paging_bar($component->get_totalcount(), $component->get_page(), $component->get_pagelimit(), new moodle_url($url, array('searchcourses'=>1)), restore_course_search::$VAR_PAGE);
467             $output .= $this->output->render($pagingbar);
468         }
470         $table = new html_table();
471         $table->head = array('', get_string('shortname'), get_string('fullname'));
472         $table->data = array();
473         foreach ($component->get_results() as $course) {
474             $row = new html_table_row();
475             $row->attributes['class'] = 'rcs-course';
476             if (!$course->visible) {
477                 $row->attributes['class'] .= ' dimmed';
478             }
479             $row->cells = array(
480                 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)),
481                 $course->shortname,
482                 $course->fullname
483             );
484             $table->data[] = $row;
485         }
486         $output .= html_writer::table($table);
487         if (isset($pagingbar)) {
488             $output .= $this->output->render($pagingbar);
489         }
490         $output .= html_writer::end_tag('div');
492         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
493         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
494         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
495         $output .= html_writer::end_tag('div');
497         $output .= html_writer::end_tag('div');
498         return $output;
499     }
501     /**
502      * Renders an import course search object
503      *
504      * @param import_course_search $component
505      * @return string
506      */
507     public function render_import_course_search(import_course_search $component) {
508         $url = $component->get_url();
510         $output = html_writer::start_tag('div', array('class' => 'import-course-search'));
511         if ($component->get_totalcount() === 0) {
512             $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
513             $output .= html_writer::end_tag('div');
514             return $output;
515         }
517         $output .= html_writer::tag('div', get_string('totalcoursesearchresults', 'backup', $component->get_totalcount()), array('class'=>'ics-totalresults'));
519         $output .= html_writer::start_tag('div', array('class' => 'ics-results'));
520         if ($component->get_totalpages()>1) {
521             $pagingbar = new paging_bar($component->get_totalcount(), $component->get_page(), $component->get_pagelimit(), new moodle_url($url, array('searchcourses'=>1)), restore_course_search::$VAR_PAGE);
522             $output .= $this->output->render($pagingbar);
523         }
525         $table = new html_table();
526         $table->head = array('', get_string('shortname'), get_string('fullname'));
527         $table->data = array();
528         foreach ($component->get_results() as $course) {
529             $row = new html_table_row();
530             $row->attributes['class'] = 'ics-course';
531             if (!$course->visible) {
532                 $row->attributes['class'] .= ' dimmed';
533             }
534             $row->cells = array(
535                 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'importid', 'value'=>$course->id)),
536                 $course->shortname,
537                 $course->fullname
538             );
539             $table->data[] = $row;
540         }
541         $output .= html_writer::table($table);
542         if (isset($pagingbar)) {
543             $output .= $this->output->render($pagingbar);
544         }
545         $output .= html_writer::end_tag('div');
547         $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
548         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
549         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
550         $output .= html_writer::end_tag('div');
552         $output .= html_writer::end_tag('div');
553         return $output;
554     }
556     /**
557      * Renders a restore category search object
558      * 
559      * @param restore_category_search $component
560      * @return string
561      */
562     public function render_restore_category_search(restore_category_search $component) {
563         $url = $component->get_url();
565         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
566         if ($component->get_totalcount() === 0) {
567             $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
568             $output .= html_writer::end_tag('div');
569             return $output;
570         }
572         $output .= html_writer::tag('div', get_string('totalcategorysearchresults', 'backup', $component->get_totalcount()), array('class'=>'rcs-totalresults'));
574         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
575         if ($component->get_totalpages()>1) {
576             $pagingbar = new paging_bar($component->get_totalcount(), $component->get_page(), $component->get_pagelimit(), new moodle_url($url, array('searchcourses'=>1)), restore_category_search::$VAR_PAGE);
577             $output .= $this->output->render($pagingbar);
578         }
580         $table = new html_table();
581         $table->head = array('', get_string('name'), get_string('description'));
582         $table->data = array();
583         foreach ($component->get_results() as $category) {
584             $row = new html_table_row();
585             $row->attributes['class'] = 'rcs-course';
586             if (!$category->visible) {
587                 $row->attributes['class'] .= ' dimmed';
588             }
589             $row->cells = array(
590                 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)),
591                 $category->name,
592                 format_text($category->description, $category->descriptionformat)
593             );
594             $table->data[] = $row;
595         }
596         $output .= html_writer::table($table);
597         if (isset($pagingbar)) {
598             $output .= $this->output->render($pagingbar);
599         }
600         $output .= html_writer::end_tag('div');
602         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
603         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_category_search::$VAR_SEARCH, 'value'=>$component->get_search()));
604         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
605         $output .= html_writer::end_tag('div');
607         $output .= html_writer::end_tag('div');
608         return $output;
609     }
611 /**
612  * Data structure representing backup files viewer
613  *
614  * @copyright 2010 Dongsheng Cai
615  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
616  * @since     Moodle 2.0
617  */
618 class backup_files_viewer implements renderable {
619     public $tree;
620     public $path;
622     /**
623      * Constructor of backup_files_viewer class
624      * @param array $options
625      */
626     public function __construct(array $options = null) {
627         global $CFG, $USER;
628         $browser = get_file_browser();
629         $file_info = $browser->get_file_info($options['filecontext'], $options['component'], $options['filearea'], $options['itemid'], $options['filepath'], $options['filename']);
630         $this->options = (array)$options;
632         $this->tree = array();
633         if (!$file_info) {
634             $this->path = array();
635             $this->tree = array();
636             return;
637         }
638         $children = $file_info->get_children();
639         $parent_info = $file_info->get_parent();
641         $level = $parent_info;
642         $this->path = array();
643         while ($level) {
644             $params = $level->get_params();
645             $context = get_context_instance_by_id($params['contextid']);
646             // lock user in course level
647             if ($context->contextlevel == CONTEXT_COURSECAT or $context->contextlevel == CONTEXT_SYSTEM) {
648                 break;
649             }
650             $url = new moodle_url('/backup/restorefile.php', $params);
651             $this->path[] = html_writer::link($url->out(false), $level->get_visible_name());
652             $level = $level->get_parent();
653         }
654         $this->path = array_reverse($this->path);
655         $this->path[] = $file_info->get_visible_name();
657         $this->add_to_tree($children);
659         if (!empty($options['show_user_backup'])) {
660             $browser = get_file_browser();
661             $user_context = get_context_instance(CONTEXT_USER, $USER->id);
662             $fileinfo = $browser->get_file_info($user_context, null, null, null, null, null);
663             $children = $fileinfo->get_children();
664             $this->add_to_tree($children);
665         }
666     }
668     function add_to_tree($children) {
669         foreach ($children as $child) {
670             $filedate = $child->get_timemodified();
671             $filesize = $child->get_filesize();
672             $mimetype = $child->get_mimetype();
673             $params = $child->get_params();
674             $fileitem = array(
675                     'params'   => $params,
676                     'filename' => $child->get_visible_name(),
677                     'filedate' => $filedate ? userdate($filedate) : '',
678                     'filesize' => $filesize ? display_size($filesize) : ''
679                     );
680             $is_coursebackup = ($params['component'] == 'backup' && in_array($params['filearea'], array('course', 'section', 'activity', 'backup')));
681             $is_userbackup = ($params['component'] == 'user' && $params['filearea'] == 'backup');
682             if ($is_userbackup) {
683                 // XXX: hacky, current context
684                 $params['contextid'] = $this->options['context']->id;
685             }
686             if ($child->is_directory()) {
687                 // ignore all other fileares except backup_course backup_section and backup_activity
688                 if (!$is_coursebackup and !$is_userbackup) {
689                     continue;
690                 }
691                 $fileitem['isdir'] = true;
692                 // link to this folder
693                 $folderurl = new moodle_url('/backup/restorefile.php', $params);
694                 $fileitem['url'] = $folderurl->out(false);
695             } else {
696                 $restoreurl = new moodle_url('/backup/restorefile.php', array_merge($params, array('action'=>'choosebackupfile')));
697                 // link to this file
698                 $fileitem['url'] = $child->get_url();
699                 $fileitem['restoreurl'] = $restoreurl->out(false);
700             }
701             $this->tree[] = $fileitem;
702         }
703     }