Commit | Line | Data |
---|---|---|
1904e9b3 SH |
1 | <?php |
2 | ||
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/>. | |
17 | ||
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 | */ | |
25 | ||
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']); | |
4886029c SH |
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 | } | |
1904e9b3 SH |
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 | } | |
72f38ce7 DC |
66 | /** |
67 | * Print a backup files tree | |
68 | * @param file_info $fileinfo | |
69 | * @param array $options | |
70 | * @return string | |
71 | */ | |
72 | public function backup_files_viewer(file_info $fileinfo, array $options = null) { | |
73 | $tree = new backup_files_viewer($fileinfo, $options); | |
74 | return $this->render($tree); | |
75 | } | |
76 | ||
77 | public function render_backup_files_viewer(backup_files_viewer $tree) { | |
78 | $module = array('name'=>'backup_files_tree', 'fullpath'=>'/backup/util/ui/module.js', 'requires'=>array('yui2-treeview', 'yui2-json'), 'strings'=>array(array('restore', 'moodle'))); | |
79 | $htmlid = 'backup-treeview-'.uniqid(); | |
80 | $this->page->requires->js_init_call('M.core_backup_files_tree.init', array($htmlid), false, $module); | |
81 | ||
82 | $html = '<div>'; | |
83 | foreach($tree->path as $path) { | |
84 | $html .= $path; | |
85 | $html .= ' / '; | |
86 | } | |
87 | $html .= '</div>'; | |
88 | ||
89 | $html .= '<div id="'.$htmlid.'" class="filemanager-container">'; | |
90 | if (empty($tree->tree)) { | |
91 | $html .= get_string('nofilesavailable', 'repository'); | |
92 | } else { | |
93 | $html .= '<ul>'; | |
94 | foreach($tree->tree as $node) { | |
95 | $link_attributes = array(); | |
96 | if (!empty($node['isdir'])) { | |
97 | $class = ' class="file-tree-folder"'; | |
98 | $restore_link = ''; | |
99 | } else { | |
100 | $class = ' class="file-tree-file"'; | |
101 | $link_attributes['target'] = '_blank'; | |
102 | $restore_link = html_writer::link($node['restoreurl'], get_string('restore', 'moodle'), $link_attributes); | |
103 | } | |
104 | $html .= '<li '.$class.'>'; | |
105 | $html .= html_writer::link($node['url'], $node['filename'], $link_attributes); | |
106 | // when js is off, use this restore link | |
107 | // otherwise, yui treeview will generate a restore link in js | |
108 | $html .= ' '.$restore_link; | |
109 | $html .= '</li>'; | |
110 | } | |
111 | $html .= '</ul>'; | |
112 | } | |
113 | $html .= '</div>'; | |
114 | return $html; | |
115 | } | |
116 | } | |
117 | /** | |
118 | * Data structure representing backup files viewer | |
119 | * | |
120 | * @copyright 2010 Dongsheng Cai | |
121 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
122 | * @since Moodle 2.0 | |
123 | */ | |
124 | class backup_files_viewer implements renderable { | |
125 | public $tree; | |
126 | public $path; | |
127 | ||
128 | /** | |
129 | * Constructor of backup_files_viewer class | |
130 | * @param file_info $file_info | |
131 | * @param array $options | |
132 | */ | |
133 | public function __construct(file_info $file_info, array $options = null) { | |
134 | global $CFG; | |
135 | $this->options = (array)$options; | |
136 | ||
137 | $this->tree = array(); | |
138 | $children = $file_info->get_children(); | |
139 | $parent_info = $file_info->get_parent(); | |
140 | ||
141 | $level = $parent_info; | |
142 | $this->path = array(); | |
143 | while ($level) { | |
144 | $params = $level->get_params(); | |
145 | $context = get_context_instance_by_id($params['contextid']); | |
146 | // lock user in course level | |
147 | if ($context->contextlevel == CONTEXT_COURSECAT or $context->contextlevel == CONTEXT_SYSTEM) { | |
148 | break; | |
149 | } | |
150 | $url = new moodle_url('/backup/restorefile.php', $params); | |
151 | $this->path[] = html_writer::link($url->out(false), $level->get_visible_name()); | |
152 | $level = $level->get_parent(); | |
153 | } | |
154 | $this->path = array_reverse($this->path); | |
155 | $this->path[] = $file_info->get_visible_name(); | |
156 | ||
157 | foreach ($children as $child) { | |
158 | $filedate = $child->get_timemodified(); | |
159 | $filesize = $child->get_filesize(); | |
160 | $mimetype = $child->get_mimetype(); | |
161 | $params = $child->get_params(); | |
162 | $fileitem = array( | |
163 | 'params' => $params, | |
164 | 'filename' => $child->get_visible_name(), | |
165 | 'filedate' => $filedate ? userdate($filedate) : '', | |
166 | 'filesize' => $filesize ? display_size($filesize) : '' | |
167 | ); | |
168 | if ($child->is_directory()) { | |
169 | // ignore all other fileares except backup_course backup_section and backup_activity | |
170 | if ($params['component'] != 'backup' or !in_array($params['filearea'], array('course', 'section', 'activity'))) { | |
171 | continue; | |
172 | } | |
173 | $fileitem['isdir'] = true; | |
174 | // link to this folder | |
175 | $folderurl = new moodle_url('/backup/restorefile.php', $params); | |
176 | $fileitem['url'] = $folderurl->out(false); | |
177 | } else { | |
178 | $restoreurl = new moodle_url('/backup/restorefile.php', array_merge($params, array('action'=>'choosebackupfile'))); | |
179 | // link to this file | |
180 | $fileitem['url'] = $child->get_url(); | |
181 | $fileitem['restoreurl'] = $restoreurl->out(false); | |
182 | } | |
183 | $this->tree[] = $fileitem; | |
184 | } | |
185 | } | |
186 | } |