64583f866c9a314833d11c1087f958e43acf363d
[moodle.git] / backup / backup_form.html
1 <?php
2     //This page prints the backup form to select everything
3     global $DB;
5     //Check login
6     require_login();
8     $loginurl = get_login_url();
10     //Init backupuserinfo
11     $backupuserinfo = false;
13     if (!empty($course->id)) {
14         if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $course->id))) {
15             if (empty($to)) {
16                 print_error("cannotuseadminadminorteacher", '', $loginurl);
17             } else {
18                 if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $to))) {
19                     print_error("cannotuseadminadminorteacher", '', $loginurl);
20                 }
21             }
22         }
23         $backupuserinfo = has_capability('moodle/backup:userinfo', get_context_instance(CONTEXT_COURSE, $course->id));
24     } else {
25         if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_SYSTEM))) {
26             print_error("cannotuseadmin", '', $loginurl);
27         }
28         $backupuserinfo = has_capability('moodle/backup:userinfo', get_context_instance(CONTEXT_SYSTEM));
29     }
31     //Check site
32     $site = get_site();
34     //Checks for the required files/functions to backup every mod
35     //And check if there is data about it
36     $count = 0;
37     if ($allmods = $DB->get_records('modules', array('visible'=>1)) ) {
38         foreach ($allmods as $mod) {
39             $modname = $mod->name;
40             $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
41             $modbackup = $modname."_backup_mods";
42             $modbackupone = $modname.'_backup_one_mod';
43             $modcheckbackup = $modname."_check_backup_mods";
44             if (file_exists($modfile)) {
45                include_once($modfile);
46                if (function_exists($modbackup) and function_exists($modcheckbackup)) {
47                    $var = "exists_".$modname;
48                    $$var = true;
49                    $count++;
50                    // check that there are instances and we can back them up individually
51                    if ($DB->count_records('course_modules',array('course'=>$course->id,'module'=>$mod->id)) && function_exists($modbackupone)) {
52                        $var = 'exists_one_'.$modname;
53                        $$var = true;
54                    }
55                }
56             }
57             //Check data
58             //Check module info
59             $var = "backup_".$modname;
60             $$var = optional_param( $var,1, PARAM_CLEAN);
61             //Check include user info
62             $var = "backup_user_info_".$modname;
63             $$var = optional_param( $var,1, PARAM_CLEAN);
64         }
65     }
67     //Check other parameters
68     $backup_metacourse = optional_param('backup_metacourse',1, PARAM_BOOL);
69     $backup_users = optional_param('backup_users',1, PARAM_BOOL);
70     $backup_logs = optional_param('backup_logs',0, PARAM_BOOL);
71     $backup_user_files = optional_param('backup_user_files',1, PARAM_BOOL);
72     $backup_course_files = optional_param('backup_course_files',1, PARAM_BOOL);
73     $backup_site_files = optional_param('backup_site_files',1, PARAM_BOOL);
74     $backup_gradebook_history =  optional_param('backup_gradebook_history', 0, PARAM_INT);
75     $backup_messages = optional_param('backup_messages',1, PARAM_BOOL);
76     $backup_blogs = optional_param('backup_blogs',1, PARAM_BOOL);
78     if ($count == 0) {
79         notice("No backupable modules are installed!");
80     }
82 ?>
84 <script type="text/javascript">
85 <!--
86 function selectItemInMenuByName(formId, menuName, selectIndex ) {
87     myForm = document.getElementById(formId)
88     for (i=0,n=myForm.elements.length;i<n;i++) {
89         myLen = menuName.length;
90         myName = myForm.elements[i].name;
91         myType = myForm.elements[i].type;
92         if (myName.substring(0,myLen) == menuName && myType == "select-one") {
93             myForm.elements[i].options[selectIndex].selected = true;
94         }
95     }
96 }
98 function selectItemInRadioByName(formId, radioName, selectIndex ) {
99     myForm = document.getElementById(formId)
100     for (i=0,n=myForm.elements.length;i<n;i++) {
101         myLen = radioName.length;
102         myName = myForm.elements[i].name;
103         myType = myForm.elements[i].type;
104         if (myName.substring(0,myLen) == radioName && myType == "radio") {
105             myRadioGroup = myForm.elements[myName];
106             myRadioGroup[selectIndex].checked = true;
107         }
108     }
111 function selectItemInCheckboxByName(formId, checkName, checked ) {
112     myForm = document.getElementById(formId);
113     for (i=0,n=myForm.elements.length;i<n;i++) {
114         myLen = checkName.length;
115         myName = myForm.elements[i].name;
116         myType = myForm.elements[i].type;
117         if (myName.substring(0,myLen) == checkName && myType == "checkbox") {
118             myForm.elements[i].checked = checked;
119         }
120     }
122 -->
123 </script>
124 <form id="form1" method="post" action="backup.php">
125 <table cellpadding="5" style="margin-left:auto;margin-right:auto;">
126 <?php
127     echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
129 /// Acummulator for hidden options and proper XHTML output
130     $hidden_options = '';
131     //Now, check modules and info and show posibilities
132     if ($allmods = $DB->get_records('modules', array('visible'=>1)) ) {
133         //Print option to select/deselect everything with 1 click.
134         echo "<tr>";
135         echo "<td align=\"right\">";
136         echo '<b>'.get_string("include")."</b>";
137         echo "</td><td>";
138         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_', true);\">". get_string("all")."</a>/";
139         echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_', false);\">".  get_string("none")."</a>";
140         echo "</td>";
141         if (empty($to) and $backupuserinfo) {
142             echo "<td align=\"right\">";
143             echo '<b>&nbsp;</b>';
144             echo "</td><td>";
145             echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_user_info_', true);\">". get_string("all")."</a>/";
146             echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_user_info_', false);\">". get_string("none")."</a>";
147             echo "</td>";
148         } else {
149             echo "<td colspan=\"2\">&nbsp;</td>";
150         }
151         echo "</tr>";
152         echo "<tr><td colspan=\"4\"><hr /></td></tr>";
153         $currentrow = 0;
154         foreach ($allmods as $mod) {
155             $modname = $mod->name;
156             $modbackup = $modname."_backup_mods";
157             //If exists the lib & function
158             $var   = 'exists_'.$modname;
159             $exvar = 'exists_one_'.$modname;
160             if (!empty($$var) and !empty($$exvar)) {
161                 //Print the full tr
162                 echo "<tr class=\"r".$currentrow."\">";
163                 echo "  <td align=\"right\">&nbsp;";
164                 echo "</td><td>\n";
165                 $backup_options[0] = get_string("no");
166                 $backup_options[1] = get_string("yes");
167                 $var = "backup_".$modname;
168                 //Print the checkbox
169                 print_checkbox($var, $$var, $$var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'backup_'.$modname.'\',this.checked)');
170                 echo "</td><td align=\"right\">&nbsp;";
171                 echo "</td><td>";
172                 $var = "backup_user_info_".$modname;
173                 if (empty($to) and $backupuserinfo) {
174                     $backup_user_options[0] = get_string("withoutuserdata");
175                     $backup_user_options[1] = get_string("withuserdata");
176                     print_checkbox($var, $$var, $$var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'backup_user_info_'.$modname.'\',this.checked)');
177                 } else {
178                     echo '<input type="hidden" name="'.$var.'" value="0" />';
179                 }
180                 echo "</td></tr>\n";
181                 echo '<tr><td></td><td colspan="3"><table class="backup-form-instances">';
182                 $instances = get_all_instances_in_course($modname, $course, NULL, true);
183                 $var = $exvar;
184                 foreach ($instances as $instance) {
185                     echo '<tr><td>';
186                     $var = 'backup_'.$modname.'_instance_'.$instance->id;
187                     $$var = optional_param($var,1, PARAM_CLEAN);
188                     print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'backup_'.$modname.'\'].checked=1;');
189                     echo '</td><td align="right">&nbsp;';
190                     $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
191                     $$var = optional_param($var,1, PARAM_CLEAN);
192                     if (empty($to) and $backupuserinfo) {
193                         print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'backup_user_info_'.$modname.'\'].checked=1;');
194                     } else {
195                         echo '<input type="hidden" name="'.$var.'" value="0" />';
196                     }
197                     echo '</td></tr>';
198                 }
199                 echo '</table></td></tr>';
200             }
201         }
202         //Line
203         echo "<tr><td colspan=\"4\"><hr /></td></tr>\n";
205         if (empty($to) && !empty($course->metacourse)) {
206             //Now print the Metacourse tr
207             echo "<tr>";
208             echo "<td align=\"right\" colspan=\"2\"><b>";
209             echo '<label for="menubackup_metacourse">'.get_string ("metacourse").'</label>';
210             echo "</b></td><td colspan=\"2\">";
211             $meta_options[0] = get_string("no");
212             $meta_options[1] = get_string("yes");
213             echo $OUTPUT->select(html_select::make($meta_options, "backup_metacourse", $backup_metacourse, false));
214             echo "</td></tr>";
215         }
216         else {
217             $hidden_options .= '<input type="hidden" name="backup_metacourse" value="0" />';
218         }
220         if (empty($to) and $backupuserinfo) {
221             //Now print the Users tr
222             echo "<tr>";
223             echo "<td align=\"right\" colspan=\"2\"><b>";
224             echo '<label for="menubackup_users">'.get_string ("users").'</label>';
225             echo "</b></td><td colspan=\"2\">";
226             if (has_capability('moodle/site:backup', get_context_instance(CONTEXT_SYSTEM))) {
227                 $user_options[0] = get_string("all");
228             }
229             $user_options[1] = get_string("course");
230             $user_options[2] = get_string("none");
231             echo $OUTPUT->select(html_select::make($user_options, "backup_users", $backup_users, false));
232             echo "</td></tr>";
233         }
234         else {
235             $hidden_options .= '<input type="hidden" name="backup_users" value="2" />';
236         }
238         if (empty($to) and $backupuserinfo) {
239             //Now print the Logs tr
240             echo "<tr>";
241             echo "<td align=\"right\" colspan=\"2\"><b>";
242             echo '<label for="menubackup_logs">'.get_string ("logs").'</label>';
243             echo "</b></td><td colspan=\"2\">";
244             $log_options[0] = get_string("no");
245             $log_options[1] = get_string("yes");
246             echo $OUTPUT->select(html_select::make($log_options, "backup_logs", $backup_logs, false));
247             echo "</td></tr>";
248         }
249         else {
250             $hidden_options .= '<input type="hidden" name="backup_logs" value="0" />';
251         }
253         if (empty($to) and $backupuserinfo) {
254             //Now print the User Files tr
255             echo "<tr>";
256             echo "<td align=\"right\" colspan=\"2\"><b>";
257             echo '<label for="menubackup_user_files">'.get_string ("userfiles").'</label>';
258             echo "</b></td><td colspan=\"2\">";
259             $user_file_options[0] = get_string("no");
260             $user_file_options[1] = get_string("yes");
261             echo $OUTPUT->select(html_select::make($user_file_options, "backup_user_files", $backup_user_files, false));
262             echo "</td></tr>";
263         }
264         else {
265             $hidden_options .= '<input type="hidden" name="backup_user_files" value="0" />';
266         }
268         //Now print the Course Files tr
269         echo "<tr>";
270         echo "<td align=\"right\" colspan=\"2\"><b>";
271         echo '<label for="menubackup_course_files">'.get_string ("coursefiles").'</label>';
272         echo "</b></td><td colspan=\"2\">";
273         $course_file_options[0] = get_string("no");
274         $course_file_options[1] = get_string("yes");
275         echo $OUTPUT->select(html_select::make($course_file_options, "backup_course_files", $backup_course_files, false));
276         echo "</td></tr>";
279         if ($course->id != SITEID){
280             //Now print the site Files tr
281             echo "<tr>";
282             echo "<td align=\"right\" colspan=\"2\"><b>";
283             echo '<label for="menubackup_site_files">'.get_string ("sitefilesused").'</label>';
284             echo $OUTPUT->help_icon('sitefilesused', get_string('sitefilesused'));
285             echo "</b></td><td colspan=\"2\">";
286             $course_file_options[0] = get_string("no");
287             $course_file_options[1] = get_string("yes");
288             echo $OUTPUT->select(html_select::make($course_file_options, "backup_site_files", $backup_site_files, false));
289             echo "</td></tr>";
290         }
292         // do you want grade histories to be backed up?
293         if (empty($to) and $backupuserinfo and empty($CFG->disablegradehistory)) {
294             echo "<tr>";
295             echo "<td align=\"right\" colspan=\"2\"><b>";
296             echo '<label for="menubackup_gradebook_history">'.get_string ('gradebookhistories', 'grades').'</label>';
297             echo "</b></td><td colspan=\"2\">";
298             $gradebook_history_options[0] = get_string("no");
299             $gradebook_history_options[1] = get_string("yes");
300             echo $OUTPUT->select(html_select::make($gradebook_history_options, "backup_gradebook_history", $backup_gradebook_history, false));
301             echo "</td></tr>";
302         } else {
303             $hidden_options .= '<input type="hidden" name="backup_gradebook_history" value="0" />';
304         }
306         if (empty($to) and $backupuserinfo and $course->id == SITEID) {
307             //If we are in a SITEID backup print the Messages tr
308             echo "<tr>";
309             echo "<td align=\"right\" colspan=\"2\"><b>";
310             echo '<label for="menubackup_messages">'.get_string ('messages', 'message').'</label>';
311             echo "</b></td><td colspan=\"2\">";
312             $mess_options[0] = get_string("no");
313             $mess_options[1] = get_string("yes");
314             echo $OUTPUT->select(html_select::make($mess_options, "backup_messages", $backup_messages, false));
315             echo "</td></tr>";
316         }
317         else {
318             $hidden_options .= '<input type="hidden" name="backup_messages" value="0" />';
319         }
321         if (empty($to) and $backupuserinfo and $course->id == SITEID and !empty($CFG->bloglevel)) {
322             //If we are in a SITEID backup and blogs are enabled print the Blogs tr
323             echo "<tr>";
324             echo "<td align=\"right\" colspan=\"2\"><b>";
325             echo get_string ('blogs','blog').":";
326             echo "</b></td><td colspan=\"2\">";
327             $blog_options[0] = get_string("no");
328             $blog_options[1] = get_string("yes");
329             echo $OUTPUT->select(html_select::make($blog_options, "backup_blogs", $backup_blogs, false));
330             echo "</td></tr>";
331         }
332         else {
333             $hidden_options .= '<input type="hidden" name="backup_blogs" value="0" />';
334         }
335     }
337     //Now print a place to select which role assignments to back up.
338     if (empty($to) and $backupuserinfo) {
339         //Line
340         echo "<tr><td colspan=\"4\"><hr /></td></tr>\n";
342         echo "<tr>";
343         echo "<td align=\"right\" valign=\"top\" colspan=\"2\"><b>";
344         echo get_string("backuproleassignments");
345         echo "</b></td><td colspan=\"2\">";
346         $roles = get_all_roles();
347         foreach ($roles as $role) {
348             print_checkbox('backupassignments_' . $role->shortname, 1, true, strip_tags(format_string($role->name, true)));
349             echo "<br />";
350         }
351         echo "<a href=\"#\" onclick=\"selectItemInCheckboxByName('form1', 'backupassignments_', true); return false;\">". get_string("all")."</a>/";
352         echo "<a href=\"#\" onclick=\"selectItemInCheckboxByName('form1', 'backupassignments_', false); return false;\">". get_string("none")."</a>";
353         echo "</td></tr>";
354     }
356     $backup_unique_code = time();
358     //Calculate the backup string
359     //Calculate the backup unique code to allow simultaneus backups (to define
360     //the temp-directory name and records in backup temp tables
361     $backup_name = backup_get_zipfile_name($course, $backup_unique_code);
364 ?>
365 </table>
366 <div style="text-align:center;margin-left:auto;margin-right:auto">
367 <?php
368 /// Print captured hidden options, now that we have closed the table
369     echo $hidden_options;
370 ?>
371 <input type="hidden" name="id"     value="<?php  p($id) ?>" />
372 <input type="hidden" name="to"     value="<?php p($to) ?>" />
373 <input type="hidden" name="backup_unique_code" value="<?php p($backup_unique_code); ?>" />
374 <input type="hidden" name="backup_name" value="<?php p($backup_name); ?>" />
375 <input type="hidden" name="launch" value="check" />
376 <input type="submit" value="<?php  print_string("continue") ?>" />
377 <input type="submit" name="cancel" value="<?php  print_string("cancel") ?>" />
378 </div>
379 </form>