adding capabilities
[moodle.git] / backup / restore_form.html
1 <?php //$Id$
2     //This page prints the restore form to select everything yo want
3     //to restore. Form is dinamically buid, depending of "info" object
4     //that contains the backup contents and depending of every mod
5     //capabilities.
7     //Get objects from session
8     if (!($info = $SESSION->info)) {
9       error( 'info object missing from session' );
10     }
11     if (!($course_header = $SESSION->course_header)) {
12       error( 'course_header object missing from session' );
13     }
15     //Check that we have all we need
16     //backup_unique_code
17     $backup_unique_code = required_param( 'backup_unique_code' );
18     //file
19     $file = required_param( 'file' );
21     //Check login
22     require_login();
24     //Check admin
25     if (!empty($id)) {
26         if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COUESE, $id))) {
27             error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
28         }
29     } else {
30         if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
31             error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");   
32         }
33     }
35     //Check site
36     if (!$site = get_site()) {
37         error("Site not found!");
38     } 
40     //Checks for the required files/functions to restore every mod
41     $count = 0;
42     if ($allmods = get_records("modules") ) {
43         foreach ($allmods as $mod) {
44             $modname = $mod->name;
45             $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
46             $modrestore = $modname."_restore_mods";
47             if (file_exists($modfile)) {
48                include_once($modfile);
49                if (function_exists($modrestore)) {
50                    $var = "exists_".$modname;
51                    $$var = true;
52                    $count++;
53                }
54             }
55             //Check data
56             //Check module info
57             $var = "restore_".$modname;
58             if (!isset($$var)) {
59                 $$var = 1;
60             }
61             //Check include user info
62             $var = "restore_user_info_".$modname;
63             if (!isset($$var)) {
64                 $$var = 1;
65             }
66         }
67     }
69     //Check other parameters
70     if (!isset($restore_metacourse)) {
71         $restore_metacourse = 1;
72     }
73    
74     if (!isset($restore_users)) {
75         $restore_users = 1;
76     }
77    
78     if (!isset($restore_logs)) {
79         $restore_logs = 1;
80     }
82     if (!isset($restore_user_files)) {
83         $restore_user_files = 1;
84     }
86     if (!isset($restore_course_files)) {
87         $restore_course_files = 1;
88     }
90     if (!isset($restore_messages)) {
91         $restore_messages = 1;
92         }
94     if (!isset($restore_restoreto)) {
95         if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
96             $restore_restoreto = 1;
97         }
98         if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
99             $restore_restoreto = 2;
100         }
101     }
103     if ($count == 0) {
104         notice("No restorable modules are installed!");
105     }
107 ?>
109 <script language="JavaScript" type="text/javascript">
110 <!--
111 function selectItemInMenuByName(formId, menuName, selectIndex ) {
112     myForm = document.getElementById(formId)
113     for (i=0,n=myForm.elements.length;i<n;i++) {
114         myLen = menuName.length;
115         myName = myForm.elements[i].name;
116         myType = myForm.elements[i].type;
117         if (myName.substring(0,myLen) == menuName && myType == "select-one") {
118             myForm.elements[i].options[selectIndex].selected = true;
119         }
120     }
123 function selectItemInRadioByName(formId, radioName, selectIndex ) {
124     myForm = document.getElementById(formId)
125     for (i=0,n=myForm.elements.length;i<n;i++) {
126         myLen = radioName.length;
127         myName = myForm.elements[i].name;
128         myType = myForm.elements[i].type;
129         if (myName.substring(0,myLen) == radioName && myType == "radio") {
130             myRadioGroup = myForm.elements[myName];
131             myRadioGroup[selectIndex].checked = true;
132         }
133     }
136 function selectItemInCheckboxByName(formId, checkName, checked ) {
137     myForm = document.getElementById(formId)
138     for (i=0,n=myForm.elements.length;i<n;i++) {
139         myLen = checkName.length;
140         myName = myForm.elements[i].name;
141         myType = myForm.elements[i].type;
142         if (myName.substring(0,myLen) == checkName && myType == "checkbox") {
143             myForm.elements[i].checked = checked;
144         }
145     }
147 -->
148 </script>
150 <form name="form1" id="form1" method="post" action="restore.php">
151 <table cellpadding="5">
152 <?php
154     //First, course destination
155     //Print the full tr
156     echo "<tr>";
157     echo "<td align=\"right\"><b>";
158     echo get_string("restoreto").":</b>";
159     echo "</td><td colspan=\"3\">";
160     if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COUESE, $id))) {
161         $restore_restoreto_options[0] = get_string("currentcoursedeleting");
162         $restore_restoreto_options[1] = get_string("currentcourseadding");
163     } 
164     if (iscreator()) {
165         $restore_restoreto_options[0] = get_string("existingcoursedeleting");
166         $restore_restoreto_options[1] = get_string("existingcourseadding");
167         $restore_restoreto_options[2] = get_string("newcourse"); 
168     }
169     choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
170     echo "</td></tr>";
171     //Line
172     echo "<tr><td colspan=\"4\"><hr /></td></tr>";
173     //Now, check modules and info and show posibilities
174     if ($allmods = get_records("modules") ) {
175         //Print option to select/deselect everything with 1 click.
176         echo "<tr>";
177         echo "<td align=\"right\">";
178         echo '<b>'.get_string("include").":</b>";
179         echo "</td><td>";
180         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
181              get_string("all")."</a>/";
182         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
183              get_string("none")."</a>";
184         echo "</td>";
185         echo "<td align=\"right\">";
186         echo '<b>&nbsp;</b>';
187         echo "</td><td>";
188         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
189              get_string("all")."</a>/";
190         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
191              get_string("none")."</a>";
192         echo "</td>";
193         echo "</tr>";
194         echo "<tr><td colspan=\"4\"><hr /></td></tr>";
195         $currentrow = 0;
196         foreach ($allmods as $mod) {
197             $modname = $mod->name;
198             $modrestore = $modname."_restore_mods";
199             //If exists the lib & function
200             $exist = "exists_".$modname;
201             $restore_var = "restore_".$modname;
202             $user_info_var = "restore_user_info_".$modname;
203             if (isset($$exist)) {
204                 if ($$exist) {
205                     //Now check that we have that module info in the backup file
206                     if (isset($info->mods[$modname]) && $info->mods[$modname]->backup == "true") {
207                         //Print the full tr
208                         echo "<tr class=\"r".$currentrow."\">";
209                         echo "<td align=\"right\">&nbsp;";
210                         echo "</td><td>";
211                         $restore_options[1] = get_string("yes");
212                         $restore_options[0] = get_string("no"); 
213                         //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
214                         //choose_from_radio($restore_options, $restore_var, $$restore_var);
215                         //Print the checkbox
216                         print_checkbox($restore_var, $$restore_var, $$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
217                         //If backup contains user data, then show menu, else fix it to
218                         //without user data
219                         echo "</td><td align=\"right\">&nbsp;";
220                         echo "</td><td>";
221                         if ($info->mods[$modname]->userinfo == "true") {
222                             $restore_user_options[1] = get_string("yes");
223                             $restore_user_options[0] = get_string("no"); 
224                             //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
225                             //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
226                             print_checkbox($user_info_var, $$user_info_var, $$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
227                         } else {
228                             //Module haven't userdata
229                             echo get_string("withoutuserdata");
230                             echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
231                         }
232                         echo "</td></tr>";
233                         $instances = $info->mods[$modname]->instances;
234                         if (!empty($instances) && is_array($instances)) {
235                             echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
236                             foreach ($instances as $instance) {
237                                 echo '<tr><td>';
238                                 $var = 'restore_'.$modname.'_instance_'.$instance->id;
239                                 $$var = optional_param($var,1);
240                                 print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
241                                 echo '</td><td align="right">&nbsp;';
242                                 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
243                                 $$var = optional_param($var,1);
244                                 if ($info->mods[$modname]->instances[$instance->id]->userinfo == 'true') {
245                                     print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
246                                 } else {
247                                     echo '<input type="hidden" name="'.$var.'" value="0" />';
248                                 }
249                                 echo '</td></tr>';
250                             }
251                             echo '</table></td></tr>';
252                         }
253                     } else {
254                         //Module isn't restorable
255                         echo "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
256                         echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
257                     }
258                 } else {
259                     //Module isn't restorable
260                     echo "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
261                     echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
262                 }
263             } else {
264                 //Module isn't restorable
265                 echo "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
266                 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
267             }
268             $currentrow = ($currentrow + 1) % 2;
269         }
270         //Line
271         echo "<tr><td colspan=\"4\"><hr /></td></tr>";
273         //Now print the Metacourse tr
274         echo "<tr>";
275         echo "<td align=\"right\" colspan=\"2\"><b>";
276         echo get_string("metacourse").":";                                               
277         echo "</b></td><td colspan=\"2\">";
278         //If metacourse are in the backup file, show menu, else fixed to no
279         if ($info->backup_metacourse == "true") {
280             $metacourse_options[0] = get_string("no");
281             $metacourse_options[1] = get_string("yes"); 
282             choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, ""); 
283         } else {
284             echo get_string("no");
285             echo "<input type=\"hidden\" name=\"restore_metacourse\" value=\"0\" />";
286         }
287         echo "</td></tr>";
288         //Now print the Users tr
289         echo "<tr>";
290         echo "<td align=\"right\" colspan=\"2\"><b>";
291         echo get_string("users").":";
292         echo "</b></td><td colspan=\"2\">";
293         //If some user is present in the backup file
294         if ($info->backup_users == "all" or $info->backup_users == "course") {
295             //If all users are in the backup file
296             if ($info->backup_users == "all") {
297                 $user_options[0] = get_string("all");
298             }
299             $user_options[1] = get_string("course");
300             $user_options[2] = get_string("none");
301             choose_from_menu($user_options, "restore_users", $restore_users, "");
302         } else {
303             echo get_string("none");
304             echo "<input type=\"hidden\" name=\"restore_users\" value=\"2\" />";
306         }
307         echo "</td></tr>";
309         //Now print the Logs tr
310         echo "<tr>";
311         echo "<td align=\"right\" colspan=\"2\"><b>";
312         echo get_string("logs").":";                                               
313         echo "</b></td><td colspan=\"2\">";
314         //If logs are in the backup file, show menu, else fixed to no
315         if ($info->backup_logs == "true") {
316             $log_options[0] = get_string("no");
317             $log_options[1] = get_string("yes"); 
318             choose_from_menu($log_options, "restore_logs", $restore_logs, ""); 
319         } else {
320             echo get_string("no");
321             echo "<input type=\"hidden\" name=\"restore_logs\" value=\"0\" />";
322         }
323         echo "</td></tr>";
325         //Now print the User Files tr
326         echo "<tr>";
327         echo "<td align=\"right\" colspan=\"2\"><b>";
328         echo get_string ("userfiles").":";
329         echo "</b></td><td colspan=\"2\">";
330         //If user files are in the backup file, show menu, else fixed to no
331         if ($info->backup_user_files == "true") {
332             $user_file_options[0] = get_string("no"); 
333             $user_file_options[1] = get_string("yes"); 
334             choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
335         } else {
336             echo get_string("no");
337             echo "<input type=\"hidden\" name=\"restore_user_files\" value=\"0\" />";
338         }
339         echo "</td></tr>";
341         //Now print the Course Files tr
342         echo "<tr>";
343         echo "<td align=\"right\" colspan=\"2\"><b>";
344         echo get_string ("coursefiles").":";
345         echo "</b></td><td colspan=\"2\">";
346         echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
347         echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
348         //If course files are in the backup file, show menu, else fixed to no
349         if ($info->backup_course_files == "true") {
350             $course_file_options[0] = get_string("no");
351             $course_file_options[1] = get_string("yes");
352             choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
353         } else {
354             echo get_string("no");
355             echo "<input type=\"hidden\" name=\"restore_course_files\" value=\"0\" />";
356         }
357         echo "</td></tr>";
359         //Now print the Messages tr
360         echo "<tr>";
361         echo "<td align=\"right\" colspan=\"2\"><b>";
362         
363         //This tr is slighty different. Everything becomes hidden if
364         //we haven't messages is the backup, to avoid confusions to users.
365         //If messages are in the backup file, show menu, else fixed to no and show nothing
366         if ($info->backup_messages == "true") {
367             echo get_string ('messages','message').":";
368             echo "</b></td><td colspan=\"2\">";
369             $message_options[0] = get_string("no");
370             $message_options[1] = get_string("yes");
371             choose_from_menu($message_options, "restore_messages", $restore_messages, "");
372         } else {
373             echo "&nbsp;</b></td><td colspan=\"2\">";
374             echo "<input type=\"hidden\" name=\"restore_messages\" value=\"0\" />";
375         }
376         echo "</td></tr>";
377     }
378 ?>
379 </table>
380 <br />
381 <center>
382 <input type="hidden" name="id"     value="<?php  p($id) ?>" />
383 <input type="hidden" name="launch" value="check" />
384 <input type="hidden" name="fromform" value="1" />
385 <input type="submit" value="<?php  print_string("continue") ?>" />
386 <input type="submit" name="cancel" value="<?php  print_string("cancel") ?>" />
387 </center>
388 </form>