3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This file contains backup and restore output renderers
22 * @copyright 2010 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * The primary renderer for the backup.
29 * Can be retrieved with the following code:
31 * $renderer = $PAGE->get_renderer('core','backup');
34 * @copyright 2010 Sam Hemelryk
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_backup_renderer extends plugin_renderer_base {
39 * Renderers a progress bar for the backup or restore given the items that
41 * @param array $items An array of items
44 public function progress_bar(array $items) {
45 foreach ($items as &$item) {
46 $text = $item['text'];
48 if (array_key_exists('link', $item)) {
49 $link = $item['link'];
51 $item = html_writer::link($link, $text, $item);
53 $item = html_writer::tag('span', $text, $item);
56 return html_writer::tag('div', join(get_separator(), $items), array('class'=>'backup_progress clearfix'));
59 * Prints a dependency notification
60 * @param string $message
63 public function dependency_notification($message) {
64 return html_writer::tag('div', $message, array('class'=>'notification dependencies_enforced'));
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);
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');
115 $html .= $this->backup_detail_pair(get_string('backupcoursesection', 'backup', $section->title), $value);
117 foreach ($details->activities as $activitykey=>$activity) {
118 if ($activity->sectionid != $section->sectionid) {
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();
129 $table->data[] = array(
130 get_string('pluginname', $activity->modulename),
132 ($activity->settings[$activitykey.'_userinfo'])?$yestick:$notick,
135 if (!empty($table)) {
136 $html .= $this->backup_detail_pair(get_string('sectionactivities','backup'), html_writer::table($table));
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');
149 public function course_selector(moodle_url $nextstageurl, $details, $categories, restore_course_search $courses=null, $currentcourse = null) {
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));
160 $html = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore'));
163 if (!empty($currentcourse)) {
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');
175 if ($categories->get_resultscount() > 0 || $categories->get_search() == '') {
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');
188 if ($courses->get_resultscount() > 0 || $courses->get_search() == '') {
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');
201 $html .= html_writer::end_tag('div');
205 protected function backup_detail_pair($label, $value) {
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');
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'));
221 return $this->backup_detail_pair($label, html_writer::empty_tag('input', $attributes+array('name'=>$name, 'type'=>$type, 'value'=>$value)).$description);
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'));
230 return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
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);
240 if (array_key_exists('warnings', $results)) {
241 foreach ($results['warnings'] as $warning) {
242 $output .= $this->output->notification($warning, 'notifywarning notifyproblem');
245 return $output.html_writer::end_tag('div');
248 public function substage_buttons($haserrors) {
249 $output = html_writer::start_tag('div', array('continuebutton'));
251 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue')));
253 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
254 $output .= html_writer::end_tag('div');
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);
268 $output .= html_writer::end_tag('div');
272 public function continue_button($url, $method='post') {
273 if (!($url instanceof moodle_url)) {
274 $url = new moodle_url($url);
276 if ($method != 'post') {
279 $url->param('sesskey', sesskey());
280 $button = new single_button($url, get_string('continue'), $method);
281 $button->class = 'continuebutton';
282 return $this->render($button);
285 * Print a backup files tree
286 * @param array $options
289 public function backup_files_viewer(array $options = null) {
290 $tree = new backup_files_viewer($options);
291 return $this->render($tree);
294 public function render_backup_files_viewer(backup_files_viewer $tree) {
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);
306 foreach($tree->path as $path) {
312 $html .= '<div id="'.$htmlid.'" class="filemanager-container">';
313 if (empty($tree->tree)) {
314 $html .= get_string('nofilesavailable', 'repository');
317 foreach($tree->tree as $node) {
318 $link_attributes = array();
319 if (!empty($node['isdir'])) {
320 $class = ' class="file-tree-folder"';
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);
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;
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');
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);
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';
368 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)),
372 $table->data[] = $row;
374 $output .= html_writer::table($table);
375 if (isset($pagingbar)) {
376 $output .= $this->output->render($pagingbar);
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');
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');
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);
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';
417 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)),
419 format_text($category->description, $category->descriptionformat)
421 $table->data[] = $row;
423 $output .= html_writer::table($table);
424 if (isset($pagingbar)) {
425 $output .= $this->output->render($pagingbar);
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');
439 * Data structure representing backup files viewer
441 * @copyright 2010 Dongsheng Cai
442 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
445 class backup_files_viewer implements renderable {
450 * Constructor of backup_files_viewer class
451 * @param array $options
453 public function __construct(array $options = null) {
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();
461 $this->path = array();
462 $this->tree = array();
465 $children = $file_info->get_children();
466 $parent_info = $file_info->get_parent();
468 $level = $parent_info;
469 $this->path = array();
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) {
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();
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);
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();
503 'filename' => $child->get_visible_name(),
504 'filedate' => $filedate ? userdate($filedate) : '',
505 'filesize' => $filesize ? display_size($filesize) : ''
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;
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) {
518 $fileitem['isdir'] = true;
519 // link to this folder
520 $folderurl = new moodle_url('/backup/restorefile.php', $params);
521 $fileitem['url'] = $folderurl->out(false);
523 $restoreurl = new moodle_url('/backup/restorefile.php', array_merge($params, array('action'=>'choosebackupfile')));
525 $fileitem['url'] = $child->get_url();
526 $fileitem['restoreurl'] = $restoreurl->out(false);
528 $this->tree[] = $fileitem;