<?PHP //$Id$
    //This page receive all the restore_form data. Then, if existing course
    //has been selected, shows a list of courses to select one.
    //It cheks that the parammeter from restore_form are coherent.
    //It puts all the restore info in the session.
    //Finally, it calls restore_execute to do the hard work

    //Get objects from session
    if ($SESSION) {
        $info = $SESSION->info;
        $course_header = $SESSION->course_header;
        if (isset($SESSION->restore)) {
            $restore = $SESSION->restore;
        }
    }

    //If restore session info exists, but we are posting parameters
    //manually, this has prioriry
    if (isset($restore) and isset($restore_restoreto)) {
        unset($restore);
    }

    //Check required objects from session

    //info
    require_variable($info);

    //course_header
    require_variable($course_header);

    //If the restore object doesn't exist, we are going
    //to check every variable individually and create it

    if (!isset($restore)) {
        //Check that we have all we need
        //backup_unique_code
        require_variable($backup_unique_code);
        //file
        require_variable($file);
        //Checks for the required restoremod parameters
        if ($allmods = get_records("modules")) {
            foreach ($allmods as $mod) {
                $modname = $mod->name;
                $var = "restore_".$modname;
                require_variable($$var); 
                $var = "restore_user_info_".$modname;
                require_variable($$var);
            }
        }
        //restoreto
        require_variable($restore_restoreto);
        //restore_users
        require_variable($restore_users);
        //restore_logs
        require_variable($restore_logs);
        //restore_user_files
        require_variable($restore_user_files);
        //restore_course_files
        require_variable($restore_course_files);
        //restore_course_files
        require_variable($restore_course_files);

        //Check we've selected a course
        if (!isset($course_id)) {
            $course_id = 0;
        }
 
        //We are here, having all we need !!
        //Create the restore object and put it in the session
        $restore->backup_unique_code = $backup_unique_code;
        $restore->file = $file;
        if ($allmods = get_records("modules")) {
            foreach ($allmods as $mod) {
                $modname = $mod->name;
                $var = "restore_".$modname;
                $restore->mods[$modname]->restore=$$var;
                $var = "restore_user_info_".$modname;
                $restore->mods[$modname]->userinfo=$$var;
            }
        }
        $restore->restoreto=$restore_restoreto;
        $restore->users=$restore_users;
        $restore->logs=$restore_logs;
        $restore->user_files=$restore_user_files;
        $restore->course_files=$restore_course_files;
        $restore->course_id=$course_id;
    } else {
        //We have the object, so check if we have a new course_id
        //passed as parammeter
        if ($course_id) {
            $restore->course_id=$course_id;
        }
    }
    //We have the object with data, put it in the session
    $SESSION->restore = $restore;

    //From here to the wnd of the script, only use the $restore object

    //Check login
    require_login();

    //Check admin
    if (!empty($id)) {
        if (!isteacheredit($id)) {
            error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
        }
    } else {
        if (!isadmin()) {
            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!");
    }
 
    //Depending the selected restoreto:
    // If user is a teacher (and nor admin):
    //    0-Current course, deleting: Put $restore->course_id and $restore->deleting (true), create the restore object
    //    1-Current course, adding: Put $restore->course_id and $restore->deleting (false), create the restore object
    // If the uses is an admin:
    //    0-Existing course, deleting: Select the destination course and launch the check again, then
    //        put $restore->course_id and $restore->deleting (true), create the restore object.
    //    1-Existing course, adding: Select the destination course and launch the check again, then
    //        put $restore->course_id and $restore->deleting (false), create the restore object.
    //    2-New course: Create the restore object and launch the execute.

    //If the user is a teacher and not an admin
    if (isteacher($id) and !isadmin()) {
        $restore->course_id = $id;
        if ($restore->restoreto == 0) {
            $restore->deleting = true;
        } else {
            $restore->deleting = false;
        }
    }

    //If the user is an admin
    if (isadmin()) {
        //Set restore->deleting as needed
        if ($restore->restoreto == 0) {
            $restore->deleting = true;
        } else {
            $restore->deleting = false;
        }
    }

    //Now, select the course if needed
    if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) and (isadmin())) {
        if ($courses = get_courses("all","c.fullname")) {
            print_heading(get_string("choosecourse"));
            print_simple_box_start("CENTER");
            foreach ($courses as $course) {
                echo "<A HREF=\"restore.php?course_id=$course->id&launch=check&id=$id&file=$file\">$course->fullname ($course->shortname)</A><BR>";
            }
            print_simple_box_end();
        } else {
            print_heading(get_string("nocoursesyet"));
            print_continue("$CFG->wwwroot/$CFG->admin/index.php");
        }
    //Checks everything and execute restore
    } else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) {
        $show_continue_button = true;
        //Check if we've selected any mod's user info and restore->users 
        //is set to none. Change it to course and inform.
        if ($restore->users == 2) {
            $changed = false;
            $mods = $restore->mods;
            foreach ($mods as $mod) {
                if ($mod->userinfo) {
                    $changed = true;
                }
            }
            if ($changed) {
                echo get_string ("noteuserschangednonetocourse");
                echo "<hr noshade size=\"1\">";
                $restore->users = 1;
            }
        }
        //Save the restore session object
        $SESSION->restore = $restore;
        if ($show_continue_button) {
            //Print the continue button to execute the restore NOW !!!!
            //All is prepared !!!
            echo "<CENTER>";
            $hidden["launch"]             = "execute";
            $hidden["file"]               =  $file;
            $hidden["id"]                 =  $id;
            print_single_button("restore.php", $hidden, get_string("restorecoursenow"),"post");
            echo "</CENTER>";
        } else {
            //Show error
            error ("Something was wrong checking restore preferences");
        } 

    //If we are here. Something must be wrong. Debug !!!
    } else {
        print_object($restore);
        print_object($info);
        print_object($course_header);
        error ("Something must be wrong");
    }

?>