MDL-35603 - Backup - Course import selector notice
[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 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 74 public function backup_details($details, $nextstageurl) {
c3847443
FM
75 $yestick = $this->output->pix_icon('i/valid', get_string('yes'));
76 $notick = $this->output->pix_icon('i/valid', get_string('no'));
785d6603
SH
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')));
67233725
DC
95 if (!empty($details->include_file_references_to_external_content)) {
96 $message = '';
97 if (backup_general_helper::backup_is_samesite($details)) {
98 $message = $yestick . ' ' . get_string('filereferencessamesite', 'backup');
99 } else {
100 $message = $notick . ' ' . get_string('filereferencesnotsamesite', 'backup');
101 }
102 $html .= $this->backup_detail_pair(get_string('includefilereferences', 'backup'), $message);
103 }
104
785d6603
SH
105 $html .= html_writer::end_tag('div');
106
107 $html .= html_writer::start_tag('div', array('class'=>'backup-section settings-section'));
108 $html .= $this->output->heading(get_string('backupsettings', 'backup'), 2, array('class'=>'header'));
109 foreach ($details->root_settings as $label=>$value) {
ac6dc09c
EL
110 if ($label == 'filename' or $label == 'user_files') {
111 continue;
112 }
573cdf85 113 $html .= $this->backup_detail_pair(get_string('rootsetting'.str_replace('_','',$label), 'backup'), $value?$yestick:$notick);
785d6603
SH
114 }
115 $html .= html_writer::end_tag('div');
116
1b42bc07
DM
117 if ($details->type === 'course') {
118 $html .= html_writer::start_tag('div', array('class'=>'backup-section'));
119 $html .= $this->output->heading(get_string('backupcoursedetails', 'backup'), 2, array('class'=>'header'));
120 $html .= $this->backup_detail_pair(get_string('coursetitle', 'backup'), $details->course->title);
121 $html .= $this->backup_detail_pair(get_string('courseid', 'backup'), $details->course->courseid);
785d6603 122
1b42bc07
DM
123 $html .= html_writer::start_tag('div', array('class'=>'backup-sub-section'));
124 $html .= $this->output->heading(get_string('backupcoursesections', 'backup'), 3, array('class'=>'subheader'));
125 foreach ($details->sections as $key=>$section) {
126 $included = $key.'_included';
127 $userinfo = $key.'_userinfo';
128 if ($section->settings[$included] && $section->settings[$userinfo]) {
129 $value = get_string('sectionincanduser','backup');
130 } else if ($section->settings[$included]) {
131 $value = get_string('sectioninc','backup');
132 } else {
785d6603
SH
133 continue;
134 }
1b42bc07
DM
135 $html .= $this->backup_detail_pair(get_string('backupcoursesection', 'backup', $section->title), $value);
136 $table = null;
137 foreach ($details->activities as $activitykey=>$activity) {
138 if ($activity->sectionid != $section->sectionid) {
139 continue;
140 }
141 if (empty($table)) {
142 $table = new html_table();
143 $table->head = array('Module', 'Title', 'Userinfo');
144 $table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
145 $table->align = array('left','left', 'center');
146 $table->attributes = array('class'=>'activitytable generaltable');
147 $table->data = array();
148 }
149 $name = get_string('pluginname', $activity->modulename);
d9f95015 150 $icon = new pix_icon('icon', $name, $activity->modulename, array('class' => 'iconlarge icon-pre'));
1b42bc07 151 $table->data[] = array(
d9f95015 152 $this->output->render($icon).$name,
1b42bc07
DM
153 $activity->title,
154 ($activity->settings[$activitykey.'_userinfo'])?$yestick:$notick,
155 );
156 }
157 if (!empty($table)) {
158 $html .= $this->backup_detail_pair(get_string('sectionactivities','backup'), html_writer::table($table));
785d6603 159 }
30ad8cdd 160
1b42bc07
DM
161 }
162 $html .= html_writer::end_tag('div');
163 $html .= html_writer::end_tag('div');
785d6603 164 }
785d6603
SH
165
166 $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
83a9c866 167 $html .= html_writer::end_tag('div');
785d6603
SH
168
169 return $html;
170 }
171
f666f149
DM
172 /**
173 * Displays the general information about a backup file with non-standard format
174 *
f666f149 175 * @param moodle_url $nextstageurl URL to send user to
71d1751c 176 * @param array $details basic info about the file (format, type)
f666f149
DM
177 * @return string HTML code to display
178 */
71d1751c 179 public function backup_details_nonstandard($nextstageurl, array $details) {
f666f149
DM
180
181 $html = html_writer::start_tag('div', array('class' => 'backup-restore nonstandardformat'));
182 $html .= html_writer::start_tag('div', array('class' => 'backup-section'));
183 $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, 'header');
71d1751c 184 $html .= $this->output->box(get_string('backupdetailsnonstandardinfo', 'backup'), 'noticebox');
f666f149
DM
185 $html .= $this->backup_detail_pair(
186 get_string('backupformat', 'backup'),
71d1751c
DM
187 get_string('backupformat'.$details['format'], 'backup'));
188 $html .= $this->backup_detail_pair(
189 get_string('backuptype', 'backup'),
190 get_string('backuptype'.$details['type'], 'backup'));
f666f149
DM
191 $html .= html_writer::end_tag('div');
192 $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
193 $html .= html_writer::end_tag('div');
194
195 return $html;
196 }
197
198 /**
199 * Displays the general information about a backup file with unknown format
200 *
201 * @param moodle_url $nextstageurl URL to send user to
202 * @return string HTML code to display
203 */
204 public function backup_details_unknown(moodle_url $nextstageurl) {
205
206 $html = html_writer::start_tag('div', array('class' => 'unknownformat'));
207 $html .= $this->output->heading(get_string('errorinvalidformat', 'backup'), 2, 'notifyproblem');
208 $html .= html_writer::tag('div', get_string('errorinvalidformatinfo', 'backup'), array('class' => 'notifyproblem'));
209 $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post');
210 $html .= html_writer::end_tag('div');
211
212 return $html;
213 }
214
ad93ddd5
SH
215 /**
216 * Displays a course selector for restore
217 *
218 * @param moodle_url $nextstageurl
3a076721 219 * @param bool $wholecourse true if we are restoring whole course (as with backup::TYPE_1COURSE), false otherwise
ad93ddd5
SH
220 * @param restore_category_search $categories
221 * @param restore_course_search $courses
222 * @param int $currentcourse
223 * @return string
224 */
3a076721 225 public function course_selector(moodle_url $nextstageurl, $wholecourse = true, restore_category_search $categories = null, restore_course_search $courses=null, $currentcourse = null) {
687f512d 226 global $CFG, $PAGE;
785d6603
SH
227 require_once($CFG->dirroot.'/course/lib.php');
228
229 $nextstageurl->param('sesskey', sesskey());
230
231 $form = html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
232 foreach ($nextstageurl->params() as $key=>$value) {
233 $form .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
234 }
235
33d34b4b 236 $hasrestoreoption = false;
785d6603 237
33d34b4b 238 $html = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore'));
3a076721 239 if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) {
39bc4c6f 240 // New course
33d34b4b 241 $hasrestoreoption = true;
39bc4c6f
SH
242 $html .= $form;
243 $html .= html_writer::start_tag('div', array('class'=>'bcs-new-course backup-section'));
244 $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class'=>'header'));
245 $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target', backup::TARGET_NEW_COURSE, array('checked'=>'checked'));
3acc9b81 246 $html .= $this->backup_detail_pair(get_string('selectacategory', 'backup'), $this->render($categories));
d6e23c14 247 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
39bc4c6f
SH
248 $html .= html_writer::end_tag('div');
249 $html .= html_writer::end_tag('form');
250 }
785d6603 251
3a076721 252 if ($wholecourse && !empty($currentcourse)) {
33d34b4b
SH
253 // Current course
254 $hasrestoreoption = true;
10618448
SH
255 $html .= $form;
256 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'targetid', 'value'=>$currentcourse));
257 $html .= html_writer::start_tag('div', array('class'=>'bcs-current-course backup-section'));
258 $html .= $this->output->heading(get_string('restoretocurrentcourse', 'backup'), 2, array('class'=>'header'));
259 $html .= $this->backup_detail_input(get_string('restoretocurrentcourseadding', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_ADDING, array('checked'=>'checked'));
260 $html .= $this->backup_detail_input(get_string('restoretocurrentcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_DELETING);
261 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
262 $html .= html_writer::end_tag('div');
263 $html .= html_writer::end_tag('form');
264 }
265
33d34b4b 266 if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search())) {
39bc4c6f 267 // Existing course
33d34b4b 268 $hasrestoreoption = true;
39bc4c6f
SH
269 $html .= $form;
270 $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section'));
271 $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header'));
3a076721 272 if ($wholecourse) {
165b7bd9
EL
273 $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_ADDING, array('checked'=>'checked'));
274 $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_DELETING);
275 $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
276 } else {
277 // We only allow restore adding to existing for now. Enforce it here.
278 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'target', 'value'=>backup::TARGET_EXISTING_ADDING));
279 $courses->invalidate_results(); // Clean list of courses
280 $courses->set_include_currentcourse(); // Show current course in the list
281 $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
282 }
39bc4c6f
SH
283 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
284 $html .= html_writer::end_tag('div');
285 $html .= html_writer::end_tag('form');
286 }
785d6603 287
33d34b4b
SH
288 if (!$hasrestoreoption) {
289 echo $this->output->notification(get_string('norestoreoptions','backup'));
290 }
291
785d6603
SH
292 $html .= html_writer::end_tag('div');
293 return $html;
294 }
295
ad93ddd5
SH
296 /**
297 * Displays the import course selector
298 *
299 * @param moodle_url $nextstageurl
300 * @param import_course_search $courses
301 * @return string
302 */
303 public function import_course_selector(moodle_url $nextstageurl, import_course_search $courses=null) {
304 $html = html_writer::start_tag('div', array('class'=>'import-course-selector backup-restore'));
305 $html .= html_writer::start_tag('form', array('method'=>'post', 'action'=>$nextstageurl->out_omit_querystring()));
306 foreach ($nextstageurl->params() as $key=>$value) {
307 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
308 }
dc2abd40
EL
309 // We only allow import adding for now. Enforce it here.
310 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'target', 'value'=>backup::TARGET_CURRENT_ADDING));
ad93ddd5
SH
311 $html .= html_writer::start_tag('div', array('class'=>'ics-existing-course backup-section'));
312 $html .= $this->output->heading(get_string('importdatafrom'), 2, array('class'=>'header'));
ad93ddd5
SH
313 $html .= $this->backup_detail_pair(get_string('selectacourse', 'backup'), $this->render($courses));
314 $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
315 $html .= html_writer::end_tag('div');
316 $html .= html_writer::end_tag('form');
317 $html .= html_writer::end_tag('div');
318 return $html;
319 }
320
321 /**
322 * Creates a detailed pairing (key + value)
323 *
324 * @staticvar int $count
325 * @param string $label
326 * @param string $value
327 * @return string
328 */
785d6603
SH
329 protected function backup_detail_pair($label, $value) {
330 static $count= 0;
331 $count++;
332 $html = html_writer::start_tag('div', array('class'=>'detail-pair'));
333 $html .= html_writer::tag('label', $label, array('class'=>'detail-pair-label', 'for'=>'detail-pair-value-'.$count));
334 $html .= html_writer::tag('div', $value, array('class'=>'detail-pair-value', 'name'=>'detail-pair-value-'.$count));
335 $html .= html_writer::end_tag('div');
336 return $html;
337 }
338
ad93ddd5
SH
339 /**
340 * Created a detailed pairing with an input
341 *
342 * @param string $label
343 * @param string $type
344 * @param string $name
345 * @param string $value
346 * @param array $attributes
347 * @param string|null $description
348 * @return string
349 */
3acc9b81
SH
350 protected function backup_detail_input($label, $type, $name, $value, array $attributes=array(), $description=null) {
351 if (!empty ($description)) {
352 $description = html_writer::tag('span', $description, array('class'=>'description'));
353 } else {
354 $description = '';
355 }
3211569a 356 return $this->backup_detail_pair($label, html_writer::empty_tag('input', $attributes+array('name'=>$name, 'type'=>$type, 'value'=>$value)).$description);
785d6603
SH
357 }
358
ad93ddd5
SH
359 /**
360 * Creates a detailed pairing with a select
361 *
362 * @param string $label
363 * @param string $name
364 * @param array $options
365 * @param string $selected
366 * @param bool $nothing
367 * @param array $attributes
368 * @param string|null $description
369 * @return string
370 */
3acc9b81
SH
371 protected function backup_detail_select($label, $name, $options, $selected='', $nothing=false, array $attributes=array(), $description=null) {
372 if (!empty ($description)) {
373 $description = html_writer::tag('span', $description, array('class'=>'description'));
374 } else {
375 $description = '';
376 }
377 return $this->backup_detail_pair($label, html_writer::select($options, $name, $selected, false, $attributes).$description);
785d6603
SH
378 }
379
ad93ddd5
SH
380 /**
381 * Displays precheck notices
382 *
383 * @param array $results
384 * @return string
385 */
785d6603
SH
386 public function precheck_notices($results) {
387 $output = html_writer::start_tag('div', array('class'=>'restore-precheck-notices'));
388 if (array_key_exists('errors', $results)) {
389 foreach ($results['errors'] as $error) {
390 $output .= $this->output->notification($error);
391 }
392 }
393 if (array_key_exists('warnings', $results)) {
394 foreach ($results['warnings'] as $warning) {
395 $output .= $this->output->notification($warning, 'notifywarning notifyproblem');
396 }
397 }
398 return $output.html_writer::end_tag('div');
399 }
400
ad93ddd5
SH
401 /**
402 * Displays substage buttons
403 *
404 * @param bool $haserrors
405 * @return string
406 */
3acc9b81
SH
407 public function substage_buttons($haserrors) {
408 $output = html_writer::start_tag('div', array('continuebutton'));
409 if (!$haserrors) {
410 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue')));
411 }
412 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
413 $output .= html_writer::end_tag('div');
414 return $output;
415 }
416
ad93ddd5
SH
417 /**
418 * Displays a role mapping interface
419 *
420 * @param array $rolemappings
421 * @param array $roles
422 * @return string
423 */
3acc9b81
SH
424 public function role_mappings($rolemappings, $roles) {
425 $roles[0] = get_string('none');
426 $output = html_writer::start_tag('div', array('class'=>'restore-rolemappings'));
427 $output .= $this->output->heading(get_string('restorerolemappings', 'backup'), 2);
428 foreach ($rolemappings as $id=>$mapping) {
429 $label = $mapping->name;
430 $name = 'mapping'.$id;
431 $selected = $mapping->targetroleid;
432 $output .= $this->backup_detail_select($label, $name, $roles, $mapping->targetroleid, false, array(), $mapping->description);
433 }
434 $output .= html_writer::end_tag('div');
435 return $output;
436 }
437
ad93ddd5
SH
438 /**
439 * Displays a continue button
440 *
441 * @param string|moodle_url $url
442 * @param string $method
443 * @return string
444 */
39bc4c6f 445 public function continue_button($url, $method='post') {
785d6603
SH
446 if (!($url instanceof moodle_url)) {
447 $url = new moodle_url($url);
448 }
39bc4c6f
SH
449 if ($method != 'post') {
450 $method = 'get';
451 }
785d6603 452 $url->param('sesskey', sesskey());
39bc4c6f 453 $button = new single_button($url, get_string('continue'), $method);
785d6603
SH
454 $button->class = 'continuebutton';
455 return $this->render($button);
456 }
72f38ce7
DC
457 /**
458 * Print a backup files tree
72f38ce7
DC
459 * @param array $options
460 * @return string
461 */
30ad8cdd 462 public function backup_files_viewer(array $options = null) {
6aef525d
DC
463 $files = new backup_files_viewer($options);
464 return $this->render($files);
72f38ce7
DC
465 }
466
ad93ddd5
SH
467 /**
468 * Displays a backup files viewer
469 *
470 * @global stdClass $USER
471 * @param backup_files_viewer $tree
472 * @return string
473 */
6aef525d
DC
474 public function render_backup_files_viewer(backup_files_viewer $viewer) {
475 global $CFG;
476 $files = $viewer->files;
72f38ce7 477
6aef525d 478 $table = new html_table();
bac233d3 479 $table->attributes['class'] = 'backup-files-table generaltable';
6aef525d 480 $table->head = array(get_string('filename', 'backup'), get_string('time'), get_string('size'), get_string('download'), get_string('restore'));
6aef525d
DC
481 $table->width = '100%';
482 $table->data = array();
483
484 foreach ($files as $file) {
485 if ($file->is_directory()) {
486 continue;
72f38ce7 487 }
6aef525d
DC
488 $fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename(), true);
489 $params = array();
490 $params['action'] = 'choosebackupfile';
491 $params['filename'] = $file->get_filename();
492 $params['filepath'] = $file->get_filepath();
493 $params['component'] = $file->get_component();
494 $params['filearea'] = $file->get_filearea();
495 $params['filecontextid'] = $file->get_contextid();
496 $params['contextid'] = $viewer->currentcontext->id;
497 $params['itemid'] = $file->get_itemid();
498 $restoreurl = new moodle_url('/backup/restorefile.php', $params);
499 $table->data[] = array(
500 $file->get_filename(),
501 userdate($file->get_timemodified()),
502 display_size($file->get_filesize()),
503 html_writer::link($fileurl, get_string('download')),
504 html_writer::link($restoreurl, get_string('restore')),
505 );
72f38ce7 506 }
6aef525d
DC
507
508 $html = html_writer::table($table);
1a22a422 509 $html .= $this->output->single_button(new moodle_url('/backup/backupfilesedit.php', array('currentcontext'=>$viewer->currentcontext->id, 'contextid'=>$viewer->filecontext->id, 'filearea'=>$viewer->filearea, 'component'=>$viewer->component, 'returnurl'=>$this->page->url->out())), get_string('managefiles', 'backup'), 'post');
6aef525d 510
72f38ce7
DC
511 return $html;
512 }
3acc9b81 513
ad93ddd5
SH
514 /**
515 * Renders a restore course search object
516 *
517 * @param restore_course_search $component
518 * @return string
519 */
3acc9b81
SH
520 public function render_restore_course_search(restore_course_search $component) {
521 $url = $component->get_url();
522
523 $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
3acc9b81 524 $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
3acc9b81
SH
525
526 $table = new html_table();
29cbe431 527 $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
3acc9b81 528 $table->data = array();
33d34b4b
SH
529 if ($component->get_count() !== 0) {
530 foreach ($component->get_results() as $course) {
531 $row = new html_table_row();
532 $row->attributes['class'] = 'rcs-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'=>'targetid', 'value'=>$course->id)),
a689cd1d
AG
538 format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
539 format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
33d34b4b
SH
540 );
541 $table->data[] = $row;
3acc9b81 542 }
33d34b4b
SH
543 if ($component->has_more_results()) {
544 $cell = new html_table_cell(get_string('moreresults', 'backup'));
545 $cell->colspan = 3;
546 $cell->attributes['class'] = 'notifyproblem';
547 $row = new html_table_row(array($cell));
548 $row->attributes['class'] = 'rcs-course';
549 $table->data[] = $row;
550 }
551 } else {
552 $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
553 $cell->colspan = 3;
554 $cell->attributes['class'] = 'notifyproblem';
555 $row = new html_table_row(array($cell));
556 $row->attributes['class'] = 'rcs-course';
3acc9b81
SH
557 $table->data[] = $row;
558 }
559 $output .= html_writer::table($table);
3acc9b81
SH
560 $output .= html_writer::end_tag('div');
561
562 $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
563 $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
564 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
565 $output .= html_writer::end_tag('div');
30ad8cdd 566
3acc9b81
SH
567 $output .= html_writer::end_tag('div');
568 return $output;
569 }
570
ad93ddd5
SH
571 /**
572 * Renders an import course search object
573 *
574 * @param import_course_search $component
575 * @return string
576 */
577 public function render_import_course_search(import_course_search $component) {
578 $url = $component->get_url();
579
580 $output = html_writer::start_tag('div', array('class' => 'import-course-search'));
33d34b4b 581 if ($component->get_count() === 0) {
ad93ddd5 582 $output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));
a4df8560
TL
583
584 $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
585 $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
586 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
587 $output .= html_writer::end_tag('div');
588
ad93ddd5
SH
589 $output .= html_writer::end_tag('div');
590 return $output;
591 }
592
c4dbfb2c
RL
593 $count_str = '';
594 if ($component->has_more_results()) {
595 $count_str = get_string('morecoursesearchresults', 'backup', $component->get_count());
596 } else {
597 $count_str = get_string('totalcoursesearchresults', 'backup', $component->get_count());
598 }
ad93ddd5 599
c4dbfb2c 600 $output .= html_writer::tag('div', $count_str, array('class'=>'ics-totalresults'));
ad93ddd5 601 $output .= html_writer::start_tag('div', array('class' => 'ics-results'));
ad93ddd5
SH
602
603 $table = new html_table();
29cbe431 604 $table->head = array('', get_string('shortnamecourse'), get_string('fullnamecourse'));
ad93ddd5
SH
605 $table->data = array();
606 foreach ($component->get_results() as $course) {
607 $row = new html_table_row();
608 $row->attributes['class'] = 'ics-course';
609 if (!$course->visible) {
610 $row->attributes['class'] .= ' dimmed';
611 }
612 $row->cells = array(
613 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'importid', 'value'=>$course->id)),
a689cd1d
AG
614 format_string($course->shortname, true, array('context' => context_course::instance($course->id))),
615 format_string($course->fullname, true, array('context' => context_course::instance($course->id)))
ad93ddd5
SH
616 );
617 $table->data[] = $row;
618 }
c4dbfb2c
RL
619 if ($component->has_more_results()) {
620 $cell = new html_table_cell(get_string('moreresults', 'backup'));
621 $cell->colspan = 3;
622 $cell->attributes['class'] = 'notifyproblem';
623 $row = new html_table_row(array($cell));
624 $row->attributes['class'] = 'rcs-course';
625 $table->data[] = $row;
626 }
ad93ddd5 627 $output .= html_writer::table($table);
ad93ddd5
SH
628 $output .= html_writer::end_tag('div');
629
630 $output .= html_writer::start_tag('div', array('class'=>'ics-search'));
631 $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
632 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
633 $output .= html_writer::end_tag('div');
634
635 $output .= html_writer::end_tag('div');
636 return $output;
637 }
638
639 /**
640 * Renders a restore category search object
6aef525d 641 *
ad93ddd5
SH
642 * @param restore_category_search $component
643 * @return string
644 */
3acc9b81
SH
645 public function render_restore_category_search(restore_category_search $component) {
646 $url = $component->get_url();
647
648 $output = html_writer::start_tag('div', array('class' => 'restore-course-search'));
3acc9b81 649 $output .= html_writer::start_tag('div', array('class' => 'rcs-results'));
3acc9b81
SH
650
651 $table = new html_table();
652 $table->head = array('', get_string('name'), get_string('description'));
653 $table->data = array();
33d34b4b
SH
654
655 if ($component->get_count() !== 0) {
656 foreach ($component->get_results() as $category) {
657 $row = new html_table_row();
658 $row->attributes['class'] = 'rcs-course';
659 if (!$category->visible) {
660 $row->attributes['class'] .= ' dimmed';
661 }
662 $row->cells = array(
663 html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)),
a689cd1d 664 format_string($category->name, true, array('context' => context_coursecat::instance($category->id))),
33d34b4b
SH
665 format_text($category->description, $category->descriptionformat, array('overflowdiv'=>true))
666 );
667 $table->data[] = $row;
3acc9b81 668 }
33d34b4b
SH
669 if ($component->has_more_results()) {
670 $cell = new html_table_cell(get_string('moreresults', 'backup'));
671 $cell->attributes['class'] = 'notifyproblem';
672 $cell->colspan = 3;
673 $row = new html_table_row(array($cell));
674 $row->attributes['class'] = 'rcs-course';
675 $table->data[] = $row;
676 }
677 } else {
678 $cell = new html_table_cell(get_string('nomatchingcourses', 'backup'));
679 $cell->colspan = 3;
680 $cell->attributes['class'] = 'notifyproblem';
681 $row = new html_table_row(array($cell));
682 $row->attributes['class'] = 'rcs-course';
3acc9b81
SH
683 $table->data[] = $row;
684 }
685 $output .= html_writer::table($table);
3acc9b81
SH
686 $output .= html_writer::end_tag('div');
687
688 $output .= html_writer::start_tag('div', array('class'=>'rcs-search'));
689 $output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_category_search::$VAR_SEARCH, 'value'=>$component->get_search()));
690 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
691 $output .= html_writer::end_tag('div');
692
693 $output .= html_writer::end_tag('div');
694 return $output;
695 }
72f38ce7 696}
6aef525d 697
72f38ce7
DC
698/**
699 * Data structure representing backup files viewer
700 *
701 * @copyright 2010 Dongsheng Cai
702 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
703 * @since Moodle 2.0
704 */
705class backup_files_viewer implements renderable {
6aef525d
DC
706 public $files;
707 public $filecontext;
708 public $component;
709 public $filearea;
710 public $currentcontext;
72f38ce7
DC
711
712 /**
713 * Constructor of backup_files_viewer class
72f38ce7
DC
714 * @param array $options
715 */
30ad8cdd
DC
716 public function __construct(array $options = null) {
717 global $CFG, $USER;
6aef525d
DC
718 $fs = get_file_storage();
719 $this->currentcontext = $options['currentcontext'];
720 $this->filecontext = $options['filecontext'];
721 $this->component = $options['component'];
722 $this->filearea = $options['filearea'];
723 $files = $fs->get_area_files($this->filecontext->id, $this->component, $this->filearea, false, 'timecreated');
724 $this->files = array_reverse($files);
72f38ce7 725 }
30ad8cdd 726}