<?php //$Id$
    //This page prints the backup todo list to see everything

    //Check login   
    require_login();

    if (!empty($course->id)) {
        if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $course->id))) {
            if (empty($to)) {
                error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
            } else {
                if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $to))) {
                    error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
                }
            }
        }
    } else {
        if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_SYSTEM))) {
            error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
        }
    }


    //Check site
    if (!$site = get_site()) {
        error("Site not found!");
    }

    $preferences = new StdClass;
    backup_fetch_prefs_from_request($preferences,$count,$course);
    //Another Info
    backup_add_static_preferences($preferences);

    if ($count == 0) {
        notice("No backupable modules are installed!");
    }


    if (empty($to)) {
        //Start the main table
        echo "<table cellpadding=\"5\">";
        
        //Now print the Backup Name tr
        echo "<tr>";
        echo "<td align=\"right\"><b>";
        echo get_string("name").":";
        echo "</b></td><td>";
        echo $preferences->backup_name;
        echo "</td></tr>";
        
        //Start the main tr, where all the backup progress is done
        echo "<tr>";
        echo "<td colspan=\"2\">";
        
        //Start the main ul
        echo "<ul>";
    }

    $errorstr = '';
    $status = backup_execute($preferences, $errorstr);

    //Ends th main ul
    echo "</ul>";

    //End the main tr, where all the backup is done
    echo "</td></tr>";

    //End the main table
    echo "</table>";
    
    if (!$status) {
        error ("The backup did not complete successfully", 
               "$CFG->wwwroot/course/view.php?id=$course->id");
    }

    if (empty($to)) {
        //Print final message
        print_simple_box(get_string("backupfinished"),"center");
        print_continue("$CFG->wwwroot/files/index.php?id=".$preferences->backup_course."&amp;wdir=/backupdata");    
    } else {
        print_simple_box(get_string('importdataexported'),"CENTER");
        if (!empty($preferences->backup_destination)) {
            $filename = $preferences->backup_destination."/".$preferences->backup_name;
        } else {
            $filename = $preferences->backup_course."/backupdata/".$preferences->backup_name;
        }
        error_log($filename);
        $SESSION->import_preferences = $preferences;
        print_continue($CFG->wwwroot.'/course/import/activities/index.php?id='.$to.'&fromcourse='.$id.'&filename='.$filename);
    }

    $SESSION->backupprefs[$course->id] = null; // unset it so we're clear next time.
?>
