65a89505defcf60b78a9daa847fcdba4055da7c7
[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     public function backup_details($details, $nextstageurl) {
68         $yestick = $this->output->pix_icon('i/tick_green_big', get_string('yes'));
69         $notick = $this->output->pix_icon('i/cross_red_big', get_string('no'));
71         $html  = html_writer::start_tag('div', array('class'=>'backup-restore'));
73         $html .= html_writer::start_tag('div', array('class'=>'backup-section'));
74         $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, array('class'=>'header'));
75         $html .= $this->backup_detail_pair(get_string('backuptype', 'backup'), get_string('backuptype'.$details->type, 'backup'));
76         $html .= $this->backup_detail_pair(get_string('backupformat', 'backup'), get_string('backupformat'.$details->format, 'backup'));
77         $html .= $this->backup_detail_pair(get_string('backupmode', 'backup'), get_string('backupmode'.$details->mode, 'backup'));
78         $html .= $this->backup_detail_pair(get_string('backupdate', 'backup'), userdate($details->backup_date));
79         $html .= $this->backup_detail_pair(get_string('moodleversion', 'backup'),
80                 html_writer::tag('span', $details->moodle_release, array('class'=>'moodle_release')).
81                 html_writer::tag('span', '['.$details->moodle_version.']', array('class'=>'moodle_version sub-detail')));
82         $html .= $this->backup_detail_pair(get_string('backupversion', 'backup'),
83                 html_writer::tag('span', $details->backup_release, array('class'=>'moodle_release')).
84                 html_writer::tag('span', '['.$details->backup_version.']', array('class'=>'moodle_version sub-detail')));
85         $html .= $this->backup_detail_pair(get_string('originalwwwroot', 'backup'),
86                 html_writer::tag('span', $details->original_wwwroot, array('class'=>'originalwwwroot')).
87                 html_writer::tag('span', '['.$details->original_site_identifier_hash.']', array('class'=>'sitehash sub-detail')));
88         $html .= html_writer::end_tag('div');
90         $html .= html_writer::start_tag('div', array('class'=>'backup-section settings-section'));
91         $html .= $this->output->heading(get_string('backupsettings', 'backup'), 2, array('class'=>'header'));
92         foreach ($details->root_settings as $label=>$value) {
93             if ($label == 'filename') continue;
94             $html .= $this->backup_detail_pair(get_string('rootsetting'.str_replace('_','',$label), 'backup'), $value?$yestick:$notick);
95         }
96         $html .= html_writer::end_tag('div');
98         $html .= html_writer::start_tag('div', array('class'=>'backup-section'));
99         $html .= $this->output->heading(get_string('backupcoursedetails', 'backup'), 2, array('class'=>'header'));
100         $html .= $this->backup_detail_pair(get_string('coursetitle', 'backup'), $details->course->title);
101         $html .= $this->backup_detail_pair(get_string('courseid', 'backup'), $details->course->courseid);
103         $html .= html_writer::start_tag('div', array('class'=>'backup-sub-section'));
104         $html .= $this->output->heading(get_string('backupcoursesections', 'backup'), 3, array('class'=>'subheader'));
105         foreach ($details->sections as $key=>$section) {
106             $included = $key.'_included';
107             $userinfo = $key.'_userinfo';
108             if ($section->settings[$included] && $section->settings[$userinfo]) {
109                 $value = get_string('sectionincanduser','backup');
110             } else if ($section->settings[$included]) {
111                 $value = get_string('sectioninc','backup');
112             } else {
113                 continue;
114             }
115             $html .= $this->backup_detail_pair(get_string('backupcoursesection', 'backup', $section->title), $value);
116             $table = null;
117             foreach ($details->activities as $activitykey=>$activity) {
118                 if ($activity->sectionid != $section->sectionid) {
119                     continue;
120                 }
121                 if (empty($table)) {
122                     $table = new html_table();
123                     $table->head = array('Module', 'Title', 'Userinfo');
124                     $table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
125                     $table->align = array('left','left', 'center');
126                     $table->attributes = array('class'=>'activitytable generaltable');
127                     $table->data = array();
128                 }
129                 $table->data[] = array(
130                     get_string('pluginname', $activity->modulename),
131                     $activity->title,
132                     ($activity->settings[$activitykey.'_userinfo'])?$yestick:$notick,
133                 );
134             }
135             if (!empty($table)) {
136                 $html .= $this->backup_detail_pair(get_string('sectionactivities','backup'), html_writer::table($table));
137             }
139         }
140         $html .= html_writer::end_tag('div');
141         $html .= html_writer::end_tag('div');
142         $html .= html_writer::end_tag('div');
144         $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
146         return $html;
147     }
149     public function course_selector(moodle_url $nextstageurl, $details, $categories, restore_course_search $courses=null, $currentcourse = null) {
150         global $CFG;
151         require_once($CFG->dirroot.'/course/lib.php');
153         $nextstageurl->param('sesskey', sesskey());
155         $form = html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
156         foreach ($nextstageurl->params() as $key=>$value) {
157             $form .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
158         }
160         $html  = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore'));
162         // Current course
163         if (!empty($currentcourse)) {
164             $html .= $form;
165             $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'targetid', 'value'=>$currentcourse));
166             $html .= html_writer::start_tag('div', array('class'=>'bcs-current-course backup-section'));
167             $html .= $this->output->heading(get_string('restoretocurrentcourse', 'backup'), 2, array('class'=>'header'));
168             $html .= $this->backup_detail_input(get_string('restoretocurrentcourseadding', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_ADDING, array('checked'=>'checked'));
169             $html .= $this->backup_detail_input(get_string('restoretocurrentcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_DELETING);
170             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
171             $html .= html_writer::end_tag('div');
172             $html .= html_writer::end_tag('form');
173         }
175         if ($categories->get_resultscount() > 0 || $categories->get_search() == '') {
176             // New course
177             $html .= $form;
178             $html .= html_writer::start_tag('div', array('class'=>'bcs-new-course backup-section'));
179             $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class'=>'header'));
180             $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target', backup::TARGET_NEW_COURSE, array('checked'=>'checked'));
181             //$html .= $this->backup_detail_select(get_string('coursecategory', 'backup'), 'targetid', $categories);
182             $html .= $this->backup_detail_pair(get_string('selectacategory', 'backup'), $this->render($categories));
183             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
184             $html .= html_writer::end_tag('div');
185             $html .= html_writer::end_tag('form');
186         }
188         if ($courses->get_resultscount() > 0 || $courses->get_search() == '') {
189             // Existing course
190             $html .= $form;
191             $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section'));
192             $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header'));
193             $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_ADDING, array('checked'=>'checked'));
194             $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_DELETING);
195             $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
196             $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
197             $html .= html_writer::end_tag('div');
198             $html .= html_writer::end_tag('form');
199         }
201         $html .= html_writer::end_tag('div');
202         return $html;
203     }
205     protected function backup_detail_pair($label, $value) {
206         static $count= 0;
207         $count++;
208         $html  = html_writer::start_tag('div', array('class'=>'detail-pair'));
209         $html .= html_writer::tag('label', $label, array('class'=>'detail-pair-label', 'for'=>'detail-pair-value-'.$count));
210         $html .= html_writer::tag('div', $value, array('class'=>'detail-pair-value', 'name'=>'detail-pair-value-'.$count));
211         $html .= html_writer::end_tag('div');
212         return $html;
213     }
215     protected function backup_detail_input($label, $type, $name, $value, array $attributes=array(), $description=null) {
216         if (!empty ($description)) {
217             $description = html_writer::tag('span', $description, array('class'=>'description'));
218         } else {
219             $description = '';
220         }
221         return $this->backup_detail_pair($label, html_writer::empty_tag('input', $attributes+array('name'=>$name, 'type'=>$type, 'value'=>$value)).$description);
222     }
224     protected function backup_detail_select($label, $name, $options, $selected='', $nothing=false, array $attributes=array(), $description=null) {
225         if (!empty ($description)) {
226             $description = html_writer::tag('span', $description, array('class'=>'description'));
227         } else {
228             $description = '';
229         }
230         return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
231     }
233     public function precheck_notices($results) {
234         $output = html_writer::start_tag('div', array('class'=>'restore-precheck-notices'));
235         if (array_key_exists('errors', $results)) {
236             foreach ($results['errors'] as $error) {
237                 $output .= $this->output->notification($error);
238             }
239         }
240         if (array_key_exists('warnings', $results)) {
241             foreach ($results['warnings'] as $warning) {
242                 $output .= $this->output->notification($warning, 'notifywarning notifyproblem');
243             }
244         }
245         return $output.html_writer::end_tag('div');
246     }
248     public function substage_buttons($haserrors) {
249         $output  = html_writer::start_tag('div', array('continuebutton'));
250         if (!$haserrors) {
251             $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue')));
252         }
253         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
254         $output .= html_writer::end_tag('div');
255         return $output;
256     }
258     public function role_mappings($rolemappings, $roles) {
259         $roles[0] = get_string('none');
260         $output  = html_writer::start_tag('div', array('class'=>'restore-rolemappings'));
261         $output .= $this->output->heading(get_string('restorerolemappings', 'backup'), 2);
262         foreach ($rolemappings as $id=>$mapping) {
263             $label = $mapping->name;
264             $name = 'mapping'.$id;
265             $selected = $mapping->targetroleid;
266             $output .= $this->backup_detail_select($label, $name, $roles, $mapping->targetroleid, false, array(), $mapping->description);
267         }
268         $output .= html_writer::end_tag('div');
269         return $output;
270     }
272     public function continue_button($url, $method='post') {
273         if (!($url instanceof moodle_url)) {
274             $url = new moodle_url($url);
275         }
276         if ($method != 'post') {
277             $method = 'get';
278         }
279         $url->param('sesskey', sesskey());
280         $button = new single_button($url, get_string('continue'), $method);
281         $button->class = 'continuebutton';
282         return $this->render($button);
283     }
284     /**
285      * Print a backup files tree
286      * @param array $options
287      * @return string
288      */
289     public function backup_files_viewer(array $options = null) {
290         $tree = new backup_files_viewer($options);
291         return $this->render($tree);
292     }
294     public function render_backup_files_viewer(backup_files_viewer $tree) {
295         global $USER;
296         $user_context = get_context_instance(CONTEXT_USER, $USER->id);
297         $options = new stdclass;
298         $module = array('name'=>'backup_files_tree', 'fullpath'=>'/backup/util/ui/module.js', 'requires'=>array('yui2-treeview', 'yui2-json'), 'strings'=>array(array('restore', 'moodle')));
299         $htmlid = 'backup-treeview-'.uniqid();
300         $options->htmlid = $htmlid;
301         $options->usercontextid = $user_context->id;
302         $options->currentcontextid = $tree->options['context']->id;
303         $this->page->requires->js_init_call('M.core_backup_files_tree.init', array($options), false, $module);
305         $html = '<div>';
306         foreach($tree->path as $path) {
307             $html .= $path;
308             $html .= ' / ';
309         }
310         $html .= '</div>';
312         $html .= '<div id="'.$htmlid.'" class="filemanager-container">';
313         if (empty($tree->tree)) {
314             $html .= get_string('nofilesavailable', 'repository');
315         } else {
316             $html .= '<ul>';
317             foreach($tree->tree as $node) {
318                 $link_attributes = array();
319                 if (!empty($node['isdir'])) {
320                     $class = ' class="file-tree-folder"';
321                     $restore_link = '';
322                 } else {
323                     $class = ' class="file-tree-file"';
324                     $link_attributes['target'] = '_blank';
325                     $restore_link = html_writer::link($node['restoreurl'], get_string('restore', 'moodle'), $link_attributes);
326                 }
327                 $html .= '<li '.$class.'>';
328                 $html .= html_writer::link($node['url'], $node['filename'], $link_attributes);
329                 // when js is off, use this restore link
330                 // otherwise, yui treeview will generate a restore link in js
331                 $html .= ' '.$restore_link;
332                 $html .= '</li>';
333             }
334             $html .= '</ul>';
335         }
336         $html .= '</div>';
337         return $html;
338     }
340     public function render_restore_course_search(restore_course_search $component) {
341         $url = $component->get_url();
343         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
344         if ($component->get_totalcount() === 0) {
345             $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
346             $output .= html_writer::end_tag('div');
347             return $output;
348         }
350         $output .= html_writer::tag('div', get_string('totalcoursesearchresults', 'backup', $component->get_totalcount()), array('class'=>'rcs-totalresults'));
352         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
353         if ($component->get_totalpages()>1) {
354             $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);
355             $output .= $this->output->render($pagingbar);
356         }
358         $table = new html_table();
359         $table->head = array('', get_string('shortname'), get_string('fullname'));
360         $table->data = array();
361         foreach ($component->get_results() as $course) {
362             $row = new html_table_row();
363             $row->attributes['class'] = 'rcs-course';
364             if (!$course->visible) {
365                 $row->attributes['class'] .= ' dimmed';
366             }
367             $row->cells = array(
368                 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)),
369                 $course->shortname,
370                 $course->fullname
371             );
372             $table->data[] = $row;
373         }
374         $output .= html_writer::table($table);
375         if (isset($pagingbar)) {
376             $output .= $this->output->render($pagingbar);
377         }
378         $output .= html_writer::end_tag('div');
380         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
381         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
382         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
383         $output .= html_writer::end_tag('div');
385         $output .= html_writer::end_tag('div');
386         return $output;
387     }
389     public function render_restore_category_search(restore_category_search $component) {
390         $url = $component->get_url();
392         $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
393         if ($component->get_totalcount() === 0) {
394             $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
395             $output .= html_writer::end_tag('div');
396             return $output;
397         }
399         $output .= html_writer::tag('div', get_string('totalcategorysearchresults', 'backup', $component->get_totalcount()), array('class'=>'rcs-totalresults'));
401         $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
402         if ($component->get_totalpages()>1) {
403             $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);
404             $output .= $this->output->render($pagingbar);
405         }
407         $table = new html_table();
408         $table->head = array('', get_string('name'), get_string('description'));
409         $table->data = array();
410         foreach ($component->get_results() as $category) {
411             $row = new html_table_row();
412             $row->attributes['class'] = 'rcs-course';
413             if (!$category->visible) {
414                 $row->attributes['class'] .= ' dimmed';
415             }
416             $row->cells = array(
417                 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)),
418                 $category->name,
419                 format_text($category->description, $category->descriptionformat)
420             );
421             $table->data[] = $row;
422         }
423         $output .= html_writer::table($table);
424         if (isset($pagingbar)) {
425             $output .= $this->output->render($pagingbar);
426         }
427         $output .= html_writer::end_tag('div');
429         $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
430         $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_category_search::$VAR_SEARCH, 'value'=>$component->get_search()));
431         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
432         $output .= html_writer::end_tag('div');
434         $output .= html_writer::end_tag('div');
435         return $output;
436     }
438 /**
439  * Data structure representing backup files viewer
440  *
441  * @copyright 2010 Dongsheng Cai
442  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
443  * @since     Moodle 2.0
444  */
445 class backup_files_viewer implements renderable {
446     public $tree;
447     public $path;
449     /**
450      * Constructor of backup_files_viewer class
451      * @param array $options
452      */
453     public function __construct(array $options = null) {
454         global $CFG, $USER;
455         $browser = get_file_browser();
456         $file_info = $browser->get_file_info($options['filecontext'], $options['component'], $options['filearea'], $options['itemid'], $options['filepath'], $options['filename']);
457         $this->options = (array)$options;
459         $this->tree = array();
460         if (!$file_info) {
461             $this->path = array();
462             $this->tree = array();
463             return;
464         }
465         $children = $file_info->get_children();
466         $parent_info = $file_info->get_parent();
468         $level = $parent_info;
469         $this->path = array();
470         while ($level) {
471             $params = $level->get_params();
472             $context = get_context_instance_by_id($params['contextid']);
473             // lock user in course level
474             if ($context->contextlevel == CONTEXT_COURSECAT or $context->contextlevel == CONTEXT_SYSTEM) {
475                 break;
476             }
477             $url = new moodle_url('/backup/restorefile.php', $params);
478             $this->path[] = html_writer::link($url->out(false), $level->get_visible_name());
479             $level = $level->get_parent();
480         }
481         $this->path = array_reverse($this->path);
482         $this->path[] = $file_info->get_visible_name();
484         $this->add_to_tree($children);
486         if (!empty($options['show_user_backup'])) {
487             $browser = get_file_browser();
488             $user_context = get_context_instance(CONTEXT_USER, $USER->id);
489             $fileinfo = $browser->get_file_info($user_context, null, null, null, null, null);
490             $children = $fileinfo->get_children();
491             $this->add_to_tree($children);
492         }
493     }
495     function add_to_tree($children) {
496         foreach ($children as $child) {
497             $filedate = $child->get_timemodified();
498             $filesize = $child->get_filesize();
499             $mimetype = $child->get_mimetype();
500             $params = $child->get_params();
501             $fileitem = array(
502                     'params'   => $params,
503                     'filename' => $child->get_visible_name(),
504                     'filedate' => $filedate ? userdate($filedate) : '',
505                     'filesize' => $filesize ? display_size($filesize) : ''
506                     );
507             $is_coursebackup = ($params['component'] == 'backup' && in_array($params['filearea'], array('course', 'section', 'activity', 'backup')));
508             $is_userbackup = ($params['component'] == 'user' && $params['filearea'] == 'backup');
509             if ($is_userbackup) {
510                 // XXX: hacky, current context
511                 $params['contextid'] = $this->options['context']->id;
512             }
513             if ($child->is_directory()) {
514                 // ignore all other fileares except backup_course backup_section and backup_activity
515                 if (!$is_coursebackup and !$is_userbackup) {
516                     continue;
517                 }
518                 $fileitem['isdir'] = true;
519                 // link to this folder
520                 $folderurl = new moodle_url('/backup/restorefile.php', $params);
521                 $fileitem['url'] = $folderurl->out(false);
522             } else {
523                 $restoreurl = new moodle_url('/backup/restorefile.php', array_merge($params, array('action'=>'choosebackupfile')));
524                 // link to this file
525                 $fileitem['url'] = $child->get_url();
526                 $fileitem['restoreurl'] = $restoreurl->out(false);
527             }
528             $this->tree[] = $fileitem;
529         }
530     }