be46e84668e415d6bead77b99a68d3eb5e1b85bf
[moodle.git] / backup / backup_form.html
1 <?php //$Id$
2     //This page prints the backup form to select everything
4     //Check login   
5     require_login();
7     if (!empty($course->id)) {
8         if (!isteacheredit($course->id)) {
9             if (empty($to)) {
10                 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
11             } else {
12                 if (!isteacheredit($to)) {
13                     error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
14                 }
15             }
16         }
17     } else {
18         if (!isadmin()) {
19             error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
20         }
21     }
23     //Check site
24     if (!$site = get_site()) {
25         error("Site not found!");
26     }
28    //Checks for the required files/functions to backup every mod
29     //And check if there is data about it 
30     $count = 0;
31     if ($allmods = get_records("modules") ) {
32         foreach ($allmods as $mod) {
33             $modname = $mod->name;
34             $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
35             $modbackup = $modname."_backup_mods";
36             $modbackupone = $modname.'_backup_one_mod';
37             $modcheckbackup = $modname."_check_backup_mods";
38             if (file_exists($modfile)) {
39                include_once($modfile);
40                if (function_exists($modbackup) and function_exists($modcheckbackup)) {
41                    $var = "exists_".$modname;
42                    $$var = true;
43                    $count++;
44                    // check that there are instances and we can back them up individually
45                    if (count_records('course_modules','course',$course->id,'module',$mod->id) && function_exists($modbackupone)) {
46                        $var = 'exists_one_'.$modname;
47                        $$var = true;
48                    }
49                }
50             }
51             //Check data
52             //Check module info
53             $var = "backup_".$modname;
54             $$var = optional_param( $var,1);
55             //Check include user info
56             $var = "backup_user_info_".$modname;
57             $$var = optional_param( $var,1);
58         }
59     }
61     //Check other parameters
62     $backup_metacourse = optional_param('backup_metacourse',1);
63     $backup_users = optional_param('backup_users',1);
64     $backup_logs = optional_param('backup_logs',0);
65     $backup_user_files = optional_param('backup_user_files',1);
66     $backup_course_files = optional_param('backup_course_files',1);
67     $backup_messages = optional_param('backup_messages',1);
69     if ($count == 0) {
70         notice("No backupable modules are installed!");
71     }
73 ?>
75 <script language="JavaScript" type="text/javascript">
76 <!--
77 function selectItemInMenuByName(formId, menuName, selectIndex ) {
78     myForm = document.getElementById(formId)
79     for (i=0,n=myForm.elements.length;i<n;i++) {
80         myLen = menuName.length;
81         myName = myForm.elements[i].name;
82         myType = myForm.elements[i].type;
83         if (myName.substring(0,myLen) == menuName && myType == "select-one") {
84             myForm.elements[i].options[selectIndex].selected = true;
85         }
86     }
87 }
89 function selectItemInRadioByName(formId, radioName, selectIndex ) {
90     myForm = document.getElementById(formId)
91     for (i=0,n=myForm.elements.length;i<n;i++) {
92         myLen = radioName.length;
93         myName = myForm.elements[i].name;
94         myType = myForm.elements[i].type;
95         if (myName.substring(0,myLen) == radioName && myType == "radio") {
96             myRadioGroup = myForm.elements[myName];
97             myRadioGroup[selectIndex].checked = true;
98         }
99     }
102 function selectItemInCheckboxByName(formId, checkName, checked ) {
103     myForm = document.getElementById(formId);
104     for (i=0,n=myForm.elements.length;i<n;i++) {
105         myLen = checkName.length;
106         myName = myForm.elements[i].name;
107         myType = myForm.elements[i].type;
108         if (myName.substring(0,myLen) == checkName && myType == "checkbox") {
109             myForm.elements[i].checked = checked;
110         }
111     }
113 -->
114 </script>
116 <form name="form1" id="form1" method="post" action="backup.php">
117 <table cellpadding="5">
118 <?php
119     //Now, check modules and info and show posibilities
120     if ($allmods = get_records("modules") ) {
121         //Print option to select/deselect everything with 1 click.
122         echo "<tr>";
123         echo "<td align=\"right\">";
124         echo '<b>'.get_string("include").":</b>";
125         echo "</td><td>";
126         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_', true);\">". get_string("all")."</a>/";
127         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_', false);\">".  get_string("none")."</a>";
128         echo "</td>";
129         echo "<td align=\"right\">";
130         echo '<b>&nbsp;</b>';
131         echo "</td><td>";
132         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_user_info_', true);\">". get_string("all")."</a>/";
133         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_user_info_', false);\">". get_string("none")."</a>";
134         echo "</td>";
135         echo "</tr>";
136         echo "<tr><td colspan=\"4\"><hr /></td></tr>";
137         $currentrow = 0;
138         foreach ($allmods as $mod) {
139             $modname = $mod->name;
140             $modbackup = $modname."_backup_mods";
141             //If exists the lib & function
142             $var = "exists_".$modname;
143             if (isset($$var) && $$var) {
144                 //Print the full tr
145                 echo "<tr class=\"r".$currentrow."\">";
146                 echo "  <td align=\"right\">&nbsp;";
147                 echo "</td><td>\n";
148                 $backup_options[0] = get_string("no"); 
149                 $backup_options[1] = get_string("yes");
150                 $var = "backup_".$modname;
151                 //choose_from_menu($backup_options, $var, $$var, "");
152                 //Print the checkbox
153                 print_checkbox($var, $$var, $$var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'backup_'.$modname.'\',this.checked)');
154                 echo "</td><td align=\"right\">&nbsp;";
155                 echo "</td><td>";
156                 $var = "backup_user_info_".$modname;
157                 if (empty($to)) {
158                     $backup_user_options[0] = get_string("withoutuserdata"); 
159                     $backup_user_options[1] = get_string("withuserdata");
160                     //choose_from_menu($backup_user_options, $var, $$var, "");
161                     print_checkbox($var, $$var, $$var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'backup_user_info_'.$modname.'\',this.checked)');
162                 } else {
163                     echo '<input type="hidden" name="'.$var.'" value="0" />';
164                 }
165                 echo "</td></tr>\n"; 
166                 // if we have the backup_one_mod function, loop here.
167                 $var = 'exists_one_'.$modname;
168                 if (!empty($$var)) {
169                     echo '<tr><td></td><td colspan="3"><table class="backup-form-instances">';
170                     $instances = get_all_instances_in_course($modname,$course);
171                     foreach ($instances as $instance) {
172                         echo '<tr><td>';
173                         $var = 'backup_'.$modname.'_instance_'.$instance->id;
174                         $$var = optional_param($var,1);
175                         print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'backup_'.$modname.'\'].checked=1;');
176                         echo '</td><td align="right">&nbsp;';
177                         $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
178                         $$var = optional_param($var,1);
179                         if (empty($to)) {
180                             print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'backup_user_info_'.$modname.'\'].checked=1;');
181                         } else {
182                             echo '<input type="hidden" name="'.$var.'" value="0" />';
183                         }
184                         echo '</td></tr>';
185                     }
186                     echo '</table></td></tr>';
187                 }
188               
189             }
190         }
191         //Line
192         echo "<tr><td colspan=\"4\"><hr /></td></tr>\n";
193         
194         if (empty($to)) {
195             //Now print the Metacourse tr
196             echo "<tr>";
197             echo "<td align=\"right\" colspan=\"2\"><b>";
198             echo get_string ("metacourse").":";
199             echo "</b></td><td colspan=\"2\">";
200             $meta_options[0] = get_string("no"); 
201             $meta_options[1] = get_string("yes"); 
202             choose_from_menu($meta_options, "backup_metacourse", $backup_metacourse, "");
203             echo "</td></tr>";
204         }
205         else {
206             echo '<input type="hidden" name="backup_metacourse" value="0" />';
207         }
209         if (empty($to)) {
210             //Now print the Users tr
211             echo "<tr>";
212             echo "<td align=\"right\" colspan=\"2\"><b>";
213             echo get_string("users").":";
214             echo "</b></td><td colspan=\"2\">";
215             $user_options[0] = get_string("all");
216             $user_options[1] = get_string("course");
217             $user_options[2] = get_string("none");
218             choose_from_menu($user_options, "backup_users", $backup_users, "");
219             echo "</td></tr>";
220         }
221         else {
222             echo '<input type="hidden" name="backup_users" value="0" />';
223         }
224         
225         if (empty($to)) {
226             //Now print the Logs tr
227             echo "<tr>";
228             echo "<td align=\"right\" colspan=\"2\"><b>";
229             echo get_string("logs").":";                                               
230             echo "</b></td><td colspan=\"2\">";
231             $log_options[0] = get_string("no");
232             $log_options[1] = get_string("yes"); 
233             choose_from_menu($log_options, "backup_logs", $backup_logs, ""); 
234             echo "</td></tr>";
235         }
236         else {
237             echo '<input type="hidden" name="backup_logs" value="0" />';
238         }
239  
240         if (empty($to)) {
241             //Now print the User Files tr
242             echo "<tr>";
243             echo "<td align=\"right\" colspan=\"2\"><b>";
244             echo get_string ("userfiles").":";
245             echo "</b></td><td colspan=\"2\">";
246             $user_file_options[0] = get_string("no"); 
247             $user_file_options[1] = get_string("yes"); 
248             choose_from_menu($user_file_options, "backup_user_files", $backup_user_files, "");
249             echo "</td></tr>";
250         }
251         else {
252             echo '<input type="hidden" name="backup_user_files" value="0" />';
253         }
254         //Now print the Course Files tr
255         echo "<tr>";
256         echo "<td align=\"right\" colspan=\"2\"><b>";
257         echo get_string ("coursefiles").":";
258         echo "</b></td><td colspan=\"2\">";
259         $course_file_options[0] = get_string("no");
260         $course_file_options[1] = get_string("yes");
261         choose_from_menu($course_file_options, "backup_course_files", $backup_course_files, "");
262         echo "</td></tr>";
264         if (empty($to) && $course->id == SITEID) {
265             //If we are in a SITEID backup print the Messages tr
266             echo "<tr>";
267             echo "<td align=\"right\" colspan=\"2\"><b>";
268             echo get_string ('messages','message').":";
269             echo "</b></td><td colspan=\"2\">";
270             $mess_options[0] = get_string("no"); 
271             $mess_options[1] = get_string("yes"); 
272             choose_from_menu($mess_options, "backup_messages", $backup_messages, "");
273             echo "</td></tr>";
274         }
275         else {
276             echo '<input type="hidden" name="backup_messages" value="0" />';
277         }
278     }
280     $backup_unique_code = time();
282     //Calculate the backup string
284     //Calculate the backup word
285     //Take off some characters in the filename !!
286     $takeoff = array(" ", ":", "/", "\\", "|");
287     $backup_word = str_replace($takeoff,"_",strtolower(get_string("backupfilename")));
288     //If non-translated, use "backup"
289     if (substr($backup_word,0,1) == "[") {
290         $backup_word= "backup";
291     }
293     //Calculate the date format string
294     $backup_date_format = str_replace(" ","_",get_string("backupnameformat"));
295     //If non-translated, use "%Y%m%d-%H%M"
296     if (substr($backup_date_format,0,1) == "[") {
297         $backup_date_format = "%%Y%%m%%d-%%H%%M";
298     }
300     //Calculate the shortname
301     $backup_shortname = clean_filename($course->shortname);
302     if (empty($backup_shortname) or $backup_shortname == '_' ) {
303         $backup_shortname = $course->id;
304     }
306     //Calculate the final backup filename
307     //The backup word
308     $backup_name = $backup_word."-";
309     //The shortname
310     $backup_name .= strtolower($backup_shortname)."-";
311     //The date format
312     $backup_name .= userdate(time(),$backup_date_format,99,false);
313     //The extension
314     $backup_name .= ".zip";
315     //And finally, clean everything
316     $backup_name = clean_filename($backup_name);
318     //Calculate the backup unique code to allow simultaneus backups (to define
319     //the temp-directory name and records in backup temp tables
320 ?>
321 </table>
322 <br />
323 <center>
324 <input type="hidden" name="id"     value="<?php  p($id) ?>" />
325 <input type="hidden" name="to"     value="<?php p($to) ?>" />
326 <input type="hidden" name="backup_unique_code" value="<?php p($backup_unique_code); ?>" />
327 <input type="hidden" name="backup_name" value="<?php p($backup_name); ?>" />
328 <input type="hidden" name="launch" value="check" />
329 <input type="submit" value="<?php  print_string("continue") ?>" />
330 <input type="submit" name="cancel" value="<?php  print_string("cancel") ?>" />
331 </center>
332 </form>