publication MDL-24608 display a waiting page when uploading a backup
[moodle.git] / course / publish / backup.php
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 //                                                                       //
5 // This file is part of Moodle - http://moodle.org/                      //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
7 //                                                                       //
8 // Moodle is free software: you can redistribute it and/or modify        //
9 // it under the terms of the GNU General Public License as published by  //
10 // the Free Software Foundation, either version 3 of the License, or     //
11 // (at your option) any later version.                                   //
12 //                                                                       //
13 // Moodle is distributed in the hope that it will be useful,             //
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
16 // GNU General Public License for more details.                          //
17 //                                                                       //
18 // You should have received a copy of the GNU General Public License     //
19 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.       //
20 //                                                                       //
21 ///////////////////////////////////////////////////////////////////////////
23 /*
24  * @package    course
25  * @subpackage publish
26  * @author     Jerome Mouneyrac <jerome@mouneyrac.com>
27  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
28  * @copyright  (C) 1999 onwards Martin Dougiamas  http://dougiamas.com
29  *
30  * This page display the publication backup form
31  */
33 require_once('../../config.php');
34 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
35 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
36 require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
37 require_once($CFG->dirroot . '/course/publish/lib.php');
38 require_once($CFG->libdir . '/filelib.php');
41 //retrieve initial page parameters
42 $id = optional_param('id', 0, PARAM_INT);
43 $hubcourseid = optional_param('hubcourseid', 0, PARAM_INT);
44 $huburl = optional_param('huburl', '', PARAM_URL);
45 $hubname = optional_param('hubname', '', PARAM_TEXT);
47 //some permissions and parameters checking
48 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
49 require_login($course);
50 if (!has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, $id))
51         or !confirm_sesskey()) {
52     throw new moodle_exception('nopermission');
53 }
54 if (empty($huburl) or empty($hubcourseid)) {
55         throw new moodle_exception('missingparameter');
56 }
58 //page settings
59 $PAGE->set_url('/course/publish/backup.php');
60 $PAGE->set_pagelayout('course');
61 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
62 $PAGE->set_heading($course->fullname);
64 //BEGIN backup processing
65 $backupid = optional_param('backup', false, PARAM_ALPHANUM);
66 if (!($bc = backup_ui::load_controller($backupid))) {
67     $bc = new backup_controller(backup::TYPE_1COURSE, $id, backup::FORMAT_MOODLE,
68                     backup::INTERACTIVE_YES, backup::MODE_HUB, $USER->id);
69 }
70 $backup = new backup_ui($bc,
71         array('id' => $id, 'hubcourseid' => $hubcourseid, 'huburl' => $huburl, 'hubname' => $hubname));
72 $backup->process();
73 if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
74     $backup->execute();
75 } else {
76     $backup->save_controller();
77 }
79 if ($backup->get_stage() !== backup_ui::STAGE_COMPLETE) {
80     $renderer = $PAGE->get_renderer('core', 'backup');
81     echo $OUTPUT->header();
82     echo $OUTPUT->heading(get_string('publishcourseon', 'hub', !empty($hubname)?$hubname:$huburl), 3, 'main');
83     if ($backup->enforce_changed_dependencies()) {
84         echo $renderer->dependency_notification(get_string('dependenciesenforced', 'backup'));
85     }
86     echo $renderer->progress_bar($backup->get_progress_bar());
87     echo $backup->display();
88     echo $OUTPUT->footer();
89     die();
90 }
92 //$backupfile = $backup->get_stage_results();
93 $backupfile = $bc->get_results();
94 $backupfile = $backupfile['backup_destination'];
95 //END backup processing
97 //retrieve the token to call the hub
98 $registrationmanager = new registration_manager();
99 $registeredhub = $registrationmanager->get_registeredhub($huburl);
101 //display the sending file page
102 echo $OUTPUT->header();
103 echo $OUTPUT->heading(get_string('sendingcourse', 'hub'), 3, 'main');
104 $renderer = $PAGE->get_renderer('core', 'publish');
105 echo $renderer->sendingbackupinfo($backupfile);
106 flush();
108 //send backup file to the hub
109 $curl = new curl();
110 $params = array();
111 $params['filetype'] = HUB_BACKUP_FILE_TYPE;
112 $params['courseid'] = $hubcourseid;
113 $params['file'] = $backupfile;
114 $params['token'] = $registeredhub->token;
115 $curl->post($huburl . "/local/hub/webservice/upload.php", $params);
117 //delete the temp backup file from user_tohub aera
118 $backupfile->delete();
120 //Output sending success
121 echo $renderer->sentbackupinfo($id, $huburl, $hubname);
123 echo $OUTPUT->footer();