MDL-23407, the advlink plugin of tinymce should use external repositories only
[moodle.git] / backup / util / ui / renderer.php
CommitLineData
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 */
37class 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
SH
66
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'));
70
71 $html = html_writer::start_tag('div', array('class'=>'backup-restore'));
72
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');
89
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('general'.str_replace('_','',$label), 'backup'), $value?$yestick:$notick);
95 }
96 $html .= html_writer::end_tag('div');
97
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);
102
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 }
138
139 }
140 $html .= html_writer::end_tag('div');
141 $html .= html_writer::end_tag('div');
142 $html .= html_writer::end_tag('div');
143
144 $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
145
146 return $html;
147 }
148
149 public function course_selector(moodle_url $nextstageurl, $details, $categories, $courses, $currentcourse = null) {
150 global $CFG;
151 require_once($CFG->dirroot.'/course/lib.php');
152
153 $nextstageurl->param('sesskey', sesskey());
154
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 }
159
160 $html = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore'));
161
162 // Current course
163 if (!empty($currentcourse)) {
164 $html .= $form;
165 $form .= 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);
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 }
174
39bc4c6f
SH
175 if (count($categories) > 0) {
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('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
183 $html .= html_writer::end_tag('div');
184 $html .= html_writer::end_tag('form');
185 }
785d6603 186
39bc4c6f
SH
187 if (count($courses) > 0) {
188 // Existing course
189 $html .= $form;
190 $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section'));
191 $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header'));
192 $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_ADDING);
193 $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_DELETING);
194 $html .= $this->backup_detail_select(get_string('restoretocourse', 'backup'), 'targetid', $courses);
195 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
196 $html .= html_writer::end_tag('div');
197 $html .= html_writer::end_tag('form');
198 }
785d6603
SH
199
200 $html .= html_writer::end_tag('div');
201 return $html;
202 }
203
204 protected function backup_detail_pair($label, $value) {
205 static $count= 0;
206 $count++;
207 $html = html_writer::start_tag('div', array('class'=>'detail-pair'));
208 $html .= html_writer::tag('label', $label, array('class'=>'detail-pair-label', 'for'=>'detail-pair-value-'.$count));
209 $html .= html_writer::tag('div', $value, array('class'=>'detail-pair-value', 'name'=>'detail-pair-value-'.$count));
210 $html .= html_writer::end_tag('div');
211 return $html;
212 }
213
214 protected function backup_detail_input($label, $type, $name, $value, array $attributes=array()) {
215 return $this->backup_detail_pair($label, html_writer::empty_tag('input', $attributes+array('name'=>$name, 'type'=>$type, 'value'=>$value)));
216 }
217
218 protected function backup_detail_select($label, $name, $options, $selected='', $nothing=false, array $attributes=array()) {
219 return $this->backup_detail_pair($label, html_writer::select($options, 'targetid', '', false, $attributes));
220 }
221
222 public function precheck_notices($results) {
223 $output = html_writer::start_tag('div', array('class'=>'restore-precheck-notices'));
224 if (array_key_exists('errors', $results)) {
225 foreach ($results['errors'] as $error) {
226 $output .= $this->output->notification($error);
227 }
228 }
229 if (array_key_exists('warnings', $results)) {
230 foreach ($results['warnings'] as $warning) {
231 $output .= $this->output->notification($warning, 'notifywarning notifyproblem');
232 }
233 }
234 return $output.html_writer::end_tag('div');
235 }
236
39bc4c6f 237 public function continue_button($url, $method='post') {
785d6603
SH
238 if (!($url instanceof moodle_url)) {
239 $url = new moodle_url($url);
240 }
39bc4c6f
SH
241 if ($method != 'post') {
242 $method = 'get';
243 }
785d6603 244 $url->param('sesskey', sesskey());
39bc4c6f 245 $button = new single_button($url, get_string('continue'), $method);
785d6603
SH
246 $button->class = 'continuebutton';
247 return $this->render($button);
248 }
72f38ce7
DC
249 /**
250 * Print a backup files tree
251 * @param file_info $fileinfo
252 * @param array $options
253 * @return string
254 */
255 public function backup_files_viewer(file_info $fileinfo, array $options = null) {
256 $tree = new backup_files_viewer($fileinfo, $options);
257 return $this->render($tree);
258 }
259
260 public function render_backup_files_viewer(backup_files_viewer $tree) {
261 $module = array('name'=>'backup_files_tree', 'fullpath'=>'/backup/util/ui/module.js', 'requires'=>array('yui2-treeview', 'yui2-json'), 'strings'=>array(array('restore', 'moodle')));
262 $htmlid = 'backup-treeview-'.uniqid();
263 $this->page->requires->js_init_call('M.core_backup_files_tree.init', array($htmlid), false, $module);
264
265 $html = '<div>';
266 foreach($tree->path as $path) {
267 $html .= $path;
268 $html .= ' / ';
269 }
270 $html .= '</div>';
271
272 $html .= '<div id="'.$htmlid.'" class="filemanager-container">';
273 if (empty($tree->tree)) {
274 $html .= get_string('nofilesavailable', 'repository');
275 } else {
276 $html .= '<ul>';
277 foreach($tree->tree as $node) {
278 $link_attributes = array();
279 if (!empty($node['isdir'])) {
280 $class = ' class="file-tree-folder"';
281 $restore_link = '';
282 } else {
283 $class = ' class="file-tree-file"';
284 $link_attributes['target'] = '_blank';
285 $restore_link = html_writer::link($node['restoreurl'], get_string('restore', 'moodle'), $link_attributes);
286 }
287 $html .= '<li '.$class.'>';
288 $html .= html_writer::link($node['url'], $node['filename'], $link_attributes);
289 // when js is off, use this restore link
290 // otherwise, yui treeview will generate a restore link in js
291 $html .= ' '.$restore_link;
292 $html .= '</li>';
293 }
294 $html .= '</ul>';
295 }
296 $html .= '</div>';
297 return $html;
298 }
785d6603 299
72f38ce7
DC
300}
301/**
302 * Data structure representing backup files viewer
303 *
304 * @copyright 2010 Dongsheng Cai
305 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
306 * @since Moodle 2.0
307 */
308class backup_files_viewer implements renderable {
309 public $tree;
310 public $path;
311
312 /**
313 * Constructor of backup_files_viewer class
314 * @param file_info $file_info
315 * @param array $options
316 */
317 public function __construct(file_info $file_info, array $options = null) {
318 global $CFG;
319 $this->options = (array)$options;
320
321 $this->tree = array();
322 $children = $file_info->get_children();
323 $parent_info = $file_info->get_parent();
324
325 $level = $parent_info;
326 $this->path = array();
327 while ($level) {
328 $params = $level->get_params();
329 $context = get_context_instance_by_id($params['contextid']);
330 // lock user in course level
331 if ($context->contextlevel == CONTEXT_COURSECAT or $context->contextlevel == CONTEXT_SYSTEM) {
332 break;
333 }
334 $url = new moodle_url('/backup/restorefile.php', $params);
335 $this->path[] = html_writer::link($url->out(false), $level->get_visible_name());
336 $level = $level->get_parent();
337 }
338 $this->path = array_reverse($this->path);
339 $this->path[] = $file_info->get_visible_name();
340
341 foreach ($children as $child) {
342 $filedate = $child->get_timemodified();
343 $filesize = $child->get_filesize();
344 $mimetype = $child->get_mimetype();
345 $params = $child->get_params();
346 $fileitem = array(
347 'params' => $params,
348 'filename' => $child->get_visible_name(),
349 'filedate' => $filedate ? userdate($filedate) : '',
350 'filesize' => $filesize ? display_size($filesize) : ''
351 );
352 if ($child->is_directory()) {
353 // ignore all other fileares except backup_course backup_section and backup_activity
354 if ($params['component'] != 'backup' or !in_array($params['filearea'], array('course', 'section', 'activity'))) {
355 continue;
356 }
357 $fileitem['isdir'] = true;
358 // link to this folder
359 $folderurl = new moodle_url('/backup/restorefile.php', $params);
360 $fileitem['url'] = $folderurl->out(false);
361 } else {
362 $restoreurl = new moodle_url('/backup/restorefile.php', array_merge($params, array('action'=>'choosebackupfile')));
363 // link to this file
364 $fileitem['url'] = $child->get_url();
365 $fileitem['restoreurl'] = $restoreurl->out(false);
366 }
367 $this->tree[] = $fileitem;
368 }
369 }
785d6603 370}