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