File: restore_check.html

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (246 lines) | stat: -rw-r--r-- 10,220 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php //$Id: restore_check.html,v 1.26.2.1 2006/06/03 20:00:10 stronk7 Exp $
    //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;
        }
    }

    //Detect if we are coming from the restore form
    $fromform = optional_param ('fromform', 0, PARAM_INT);

    //If restore session info exists, but we are coming from the form
    //it has prioriry
    if (isset($restore) and !empty($fromform)) {
        unset($restore);
    }

    // check for session objects
    if (empty($info) or empty($course_header)) {
      error( 'important information missing from SESSION' );
    }

    //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
        $backup_unique_code = required_param( 'backup_unique_code' );
        //file
        $file = required_param( 'file');
        //Checks for the required restoremod parameters
        if ($allmods = get_records("modules")) {
            foreach ($allmods as $mod) {
                $modname = $mod->name;
                $var = "restore_".$modname;
                $$var = optional_param( $var,0); 
                $var = "restore_user_info_".$modname;
                $$var = optional_param( $var,0);
                $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
                if ($instances === NULL) {
                    continue;
                }
                foreach ($instances as $instance) {
                    $var = 'restore_'.$modname.'_instance_'.$instance->id;
                    $$var = optional_param($var,0);
                    $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
                    $$var = optional_param($var,0);
                }
            }
        }
        //restoreto
        $restore_restoreto = required_param('restore_restoreto');
        //restore_metacourse
        $restore_metacourse = required_param('restore_metacourse');
        //restore_users
        $restore_users = required_param('restore_users');
        //restore_logs
        $restore_logs = required_param('restore_logs');
        //restore_user_files
        $restore_user_files = required_param('restore_user_files');
        //restore_course_files
        $restore_course_files = required_param('restore_course_files');
        //restore_messages
        $restore_messages = required_param('restore_messages');

        //Check we've selected a course to restore to
        $course_id = optional_param('course_id', 0, PARAM_INT);
 
        //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;
                $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
                if ($instances === NULL) {
                    continue;
                }
                foreach ($instances as $instance) {
                    $var = 'restore_'.$modname.'_instance_'.$instance->id;
                    $restore->mods[$modname]->instances[$instance->id]->restore = $$var;
                    $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
                    $restore->mods[$modname]->instances[$instance->id]->userinfo = $$var;
                }
            }
        }
        $restore->restoreto=$restore_restoreto;
        $restore->metacourse=$restore_metacourse;
        $restore->users=$restore_users;
        $restore->logs=$restore_logs;
        $restore->user_files=$restore_user_files;
        $restore->course_files=$restore_course_files;
        $restore->messages=$restore_messages;
        $restore->course_id=$course_id;
    } else {
        //We have the object, so check if we have a new course_id
        //passed as parammeter
        $course_id = optional_param('course_id', 0, PARAM_INT);
        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 end 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 not creator):
    //    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 a creator:
    //    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 a creator
    if (isteacheredit($id) and !iscreator()) {
        $restore->course_id = $id;
        if ($restore->restoreto == 0) {
            $restore->deleting = true;
        } else {
            $restore->deleting = false;
        }
    }

    //If the user is a creator (or admin)
    if (iscreator()) {
        //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 (iscreator())) {
        if ($courses = get_courses("all","c.fullname","c.id,c.fullname,c.shortname,c.visible")) {
            print_heading(get_string("choosecourse"));
            print_simple_box_start("center");
            foreach ($courses as $course) {
                if (!isteacheredit($course->id)) {
                    continue;
                }
                if (empty($course->visible)) {
                    $optdimmed = ' class="dimmed" ';
                } else {
                    $optdimmed = '';
                }
                echo "<a $optdimmed 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)) {
        //Final access control check
        if ($restore->course_id == 0 and !iscreator()) {
            error("You need to be a creator or admin to restore into new course!");
        } else if ($restore->course_id != 0 and !isteacheredit($restore->course_id)) {
            error("You need to be an edit teacher or admin to restore into selected course!");
        }
        $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 we have selected user files or messages, then users must be restored too
            if ($restore->user_files || $restore->messages) {
                $changed = 1;
            }
            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");
    }

?>