Fixes for Bug MDL-8617 "Implement groupings & course modules..."
[moodle-pu.git] / backup / backup.php
blobf87a6d4aa7dac94189cbd026796045b2bfaa537c
1 <?php //$Id$
2 //This script is used to configure and execute the backup proccess.
4 //Define some globals for all the script
6 require_once ("../config.php");
7 require_once ("lib.php");
8 require_once ("backuplib.php");
9 require_once ("$CFG->libdir/blocklib.php");
10 require_once ("$CFG->libdir/adminlib.php");
12 $id = optional_param( 'id' ); // course id
13 $to = optional_param( 'to' ); // id of course to import into afterwards.
14 $cancel = optional_param( 'cancel' );
15 $launch = optional_param( 'launch' );
17 require_login();
19 if (!empty($id)) {
20 if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $id))) {
21 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
23 } else {
24 if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
25 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
29 if (!empty($to)) {
30 if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $to))) {
31 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
35 //Check site
36 if (!$site = get_site()) {
37 error("Site not found!");
40 //Check necessary functions exists. Thanks to gregb@crowncollege.edu
41 backup_required_functions();
43 //Check backup_version
44 if ($id) {
45 $linkto = "backup.php?id=".$id.((!empty($to)) ? '&to='.$to : '');
46 } else {
47 $linkto = "backup.php";
49 upgrade_backup_db($linkto);
51 //Get strings
52 if (empty($to)) {
53 $strcoursebackup = get_string("coursebackup");
55 else {
56 $strcoursebackup = get_string('importdata');
58 $stradministration = get_string("administration");
60 //If cancel has been selected, go back to course main page (bug 2817)
61 if ($cancel) {
62 if ($id) {
63 $redirecto = $CFG->wwwroot . '/course/view.php?id=' . $id; //Course page
64 } else {
65 $redirecto = $CFG->wwwroot.'/';
67 redirect ($redirecto, get_string('backupcancelled')); //Site page
68 exit;
71 //If no course has been selected, show a list of available courses
73 if (!$id) {
74 print_header("$site->shortname: $strcoursebackup", $site->fullname,
75 "<a href=\"$CFG->wwwroot/$CFG->admin/index.php\">$stradministration</a> -> $strcoursebackup");
77 if ($courses = get_courses('all','c.shortname','c.id,c.shortname,c.fullname')) {
78 print_heading(get_string("choosecourse"));
79 print_simple_box_start("center");
80 foreach ($courses as $course) {
81 echo '<a href="backup.php?id='.$course->id.'">'.s($course->fullname).' ('.s($course->shortname).')</a><br />'."\n";
83 print_simple_box_end();
84 } else {
85 print_heading(get_string("nocoursesyet"));
86 print_continue("$CFG->wwwroot/$CFG->admin/index.php");
88 print_footer();
89 exit;
92 //Get and check course
93 if (! $course = get_record("course", "id", $id)) {
94 error("Course ID was incorrect (can't find it)");
97 //Print header
98 if (has_capability('moodle/site:backup', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
99 print_header("$site->shortname: $strcoursebackup", $site->fullname,
100 "<a href=\"$CFG->wwwroot/$CFG->admin/index.php\">$stradministration</a> ->
101 <a href=\"backup.php\">$strcoursebackup</a> -> $course->fullname ($course->shortname)");
102 } else {
103 print_header("$course->shortname: $strcoursebackup", $course->fullname,
104 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->
105 $strcoursebackup");
108 //Print form
109 print_heading("$strcoursebackup: $course->fullname ($course->shortname)");
110 print_simple_box_start("center");
112 //Adjust some php variables to the execution of this script
113 @ini_set("max_execution_time","3000");
114 raise_memory_limit("192M");
116 //Call the form, depending the step we are
117 if (!$launch) {
118 // if we're at the start, clear the cache of prefs
119 if (isset($SESSION->backupprefs[$course->id])) {
120 unset($SESSION->backupprefs[$course->id]);
122 include_once("backup_form.html");
123 } else if ($launch == "check") {
124 include_once("backup_check.html");
125 } else if ($launch == "execute") {
126 include_once("backup_execute.html");
128 print_simple_box_end();
130 //Print footer
131 print_footer();