Fixes for Bug MDL-8617 "Implement groupings & course modules..."
[moodle-pu.git] / backup / restore_form.html
bloba6a334fa050d0b5744f4629a431baee9f4b31c54
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' );
11 if (!($course_header = $SESSION->course_header)) {
12 error( 'course_header object missing from session' );
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_COURSE, $id))) {
27 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
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");
35 //Check site
36 if (!$site = get_site()) {
37 error("Site not found!");
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++;
55 //Check data
56 //Check module info
57 $var = "restore_".$modname;
58 if (!isset($$var)) {
59 $$var = 1;
61 //Check include user info
62 $var = "restore_user_info_".$modname;
63 if (!isset($$var)) {
64 $$var = 1;
69 //Check other parameters
70 if (!isset($restore_metacourse)) {
71 $restore_metacourse = 1;
74 if (!isset($restore_users)) {
75 $restore_users = 1;
78 if (!isset($restore_logs)) {
79 $restore_logs = 1;
82 if (!isset($restore_user_files)) {
83 $restore_user_files = 1;
86 if (!isset($restore_course_files)) {
87 $restore_course_files = 1;
90 if (!isset($restore_messages)) {
91 $restore_messages = 1;
94 if (!isset($restore_restoreto)) {
95 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
96 $restore_restoreto = 1;
98 if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
99 $restore_restoreto = 2;
103 if(!isset($form1->startdate)) {
104 $form1->startdate = $course_header->course_startdate; //$course_header->course_startdate;
107 if (empty($form1->shortname)) {
108 $form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname;
111 if (empty($form1->fullname)) {
112 $form1->fullname = $course_header->course_fullname; // '_fullname'; //$course_header->course_fullname;
115 if ($count == 0) {
116 notice("No restorable modules are installed!");
121 <script type="text/javascript">
122 <!--
123 function selectItemInMenuByName(formId, menuName, selectIndex ) {
124 myForm = document.getElementById(formId)
125 for (i=0,n=myForm.elements.length;i<n;i++) {
126 myLen = menuName.length;
127 myName = myForm.elements[i].name;
128 myType = myForm.elements[i].type;
129 if (myName.substring(0,myLen) == menuName && myType == "select-one") {
130 myForm.elements[i].options[selectIndex].selected = true;
135 function selectItemInRadioByName(formId, radioName, selectIndex ) {
136 myForm = document.getElementById(formId)
137 for (i=0,n=myForm.elements.length;i<n;i++) {
138 myLen = radioName.length;
139 myName = myForm.elements[i].name;
140 myType = myForm.elements[i].type;
141 if (myName.substring(0,myLen) == radioName && myType == "radio") {
142 myRadioGroup = myForm.elements[myName];
143 myRadioGroup[selectIndex].checked = true;
148 function selectItemInCheckboxByName(formId, checkName, checked ) {
149 myForm = document.getElementById(formId)
150 for (i=0,n=myForm.elements.length;i<n;i++) {
151 myLen = checkName.length;
152 myName = myForm.elements[i].name;
153 myType = myForm.elements[i].type;
154 if (myName.substring(0,myLen) == checkName && myType == "checkbox") {
155 myForm.elements[i].checked = checked;
160 </script>
162 <form id="form1" method="post" action="restore.php">
163 <table cellpadding="5">
164 <?php
166 //First, course destination
167 //Print the full tr
168 echo "<tr>";
169 echo "<td align=\"right\"><b>";
170 echo get_string("restoreto").":</b>";
171 echo "</td><td colspan=\"3\">";
173 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
174 $restore_restoreto_options[0] = get_string("existingcoursedeleting");
175 $restore_restoreto_options[1] = get_string("existingcourseadding");
176 $restore_restoreto_options[2] = get_string("newcourse");
177 } else { // fix for MDL-7773
178 $restore_restoreto_options[0] = get_string("currentcoursedeleting");
179 $restore_restoreto_options[1] = get_string("currentcourseadding");
182 choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
183 echo "</td></tr>";
184 if (iscreator()) { //display these fields conditionally
185 echo "<tr valign=\"top\" >";
186 echo "<td align=\"right\">";
187 print_string("shortname");
188 echo " :</td>";
189 echo "<td><input type=\"text\" name=\"shortname\" maxlength=\"15\" size=\"10\" value=\"$form1->shortname\" alt=\"".get_string("shortname")."\" />" ;
190 helpbutton("courseshortname", get_string("shortname")) ;
191 if (isset($err["shortname"])) formerr($err["shortname"]);
192 echo "<tr valign=\"top\" >";
193 echo "<td align=\"right\">";
194 print_string("fullname");
195 echo " :</td>";
196 echo "<td><input type=\"text\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"$form1->fullname\" alt=\" ".get_string("fullname")."\" />" ;
197 helpbutton("coursefullname", get_string("fullname")) ;
198 if (isset($err["fullname"])) formerr($err["fullname"]);
199 echo"</td></tr>";
200 echo "<tr valign=\"top\"> ";
201 echo "<td align=\"right\"> ";
202 print_string("startdate");
203 echo ":</td><td>";
204 print_date_selector("startday", "startmonth", "startyear", $form1->startdate);
205 helpbutton("coursestartdate", get_string("startdate"));
206 echo "</td></tr>";
208 //Line
209 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
210 //Now, check modules and info and show posibilities
211 if ($allmods = get_records("modules") ) {
212 //Print option to select/deselect everything with 1 click.
213 echo "<tr>";
214 echo "<td align=\"right\">";
215 echo '<b>'.get_string("include").":</b>";
216 echo "</td><td>";
217 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
218 get_string("all")."</a>/";
219 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
220 get_string("none")."</a>";
221 echo "</td>";
222 echo "<td align=\"right\">";
223 echo '<b>&nbsp;</b>';
224 echo "</td><td>";
225 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
226 get_string("all")."</a>/";
227 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
228 get_string("none")."</a>";
229 echo "</td>";
230 echo "</tr>";
231 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
232 $currentrow = 0;
233 foreach ($allmods as $mod) {
234 $modname = $mod->name;
235 $modrestore = $modname."_restore_mods";
236 //If exists the lib & function
237 $exist = "exists_".$modname;
238 $restore_var = "restore_".$modname;
239 $user_info_var = "restore_user_info_".$modname;
240 if (isset($$exist)) {
241 if ($$exist) {
242 //Now check that we have that module info in the backup file
243 if (isset($info->mods[$modname]) && $info->mods[$modname]->backup == "true") {
244 //Print the full tr
245 echo "<tr class=\"r".$currentrow."\">";
246 echo "<td align=\"right\">&nbsp;";
247 echo "</td><td>";
248 $restore_options[1] = get_string("yes");
249 $restore_options[0] = get_string("no");
250 //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
251 //choose_from_radio($restore_options, $restore_var, $$restore_var);
252 //Print the checkbox
253 print_checkbox($restore_var, $$restore_var, $$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
254 //If backup contains user data, then show menu, else fix it to
255 //without user data
256 echo "</td><td align=\"right\">&nbsp;";
257 echo "</td><td>";
258 if ($info->mods[$modname]->userinfo == "true") {
259 $restore_user_options[1] = get_string("yes");
260 $restore_user_options[0] = get_string("no");
261 //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
262 //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
263 print_checkbox($user_info_var, $$user_info_var, $$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
264 } else {
265 //Module haven't userdata
266 echo get_string("withoutuserdata");
267 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
269 echo "</td></tr>";
270 if (isset($info->mods[$modname]->instances)) {
271 $instances = $info->mods[$modname]->instances;
273 if (!empty($instances) && is_array($instances)) {
274 echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
275 foreach ($instances as $instance) {
276 echo '<tr><td>';
277 $var = 'restore_'.$modname.'_instance_'.$instance->id;
278 $$var = optional_param($var,1);
279 print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
280 echo '</td><td align="right">&nbsp;';
281 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
282 $$var = optional_param($var,1);
283 if ($info->mods[$modname]->instances[$instance->id]->userinfo == 'true') {
284 print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
285 } else {
286 echo '<input type="hidden" name="'.$var.'" value="0" />';
288 echo '</td></tr>';
290 echo '</table></td></tr>';
292 } else {
293 //Module isn't restorable
294 echo "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
295 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
297 } else {
298 //Module isn't restorable
299 echo "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
300 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
302 } else {
303 //Module isn't restorable
304 echo "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
305 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
307 $currentrow = ($currentrow + 1) % 2;
309 //Line
310 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
312 //Now print the Metacourse tr
313 echo "<tr>";
314 echo "<td align=\"right\" colspan=\"2\"><b>";
315 echo get_string("metacourse").":";
316 echo "</b></td><td colspan=\"2\">";
317 //If metacourse are in the backup file, show menu, else fixed to no
318 if ($info->backup_metacourse == "true") {
319 $metacourse_options[0] = get_string("no");
320 $metacourse_options[1] = get_string("yes");
321 choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
322 } else {
323 echo get_string("no");
324 echo "<input type=\"hidden\" name=\"restore_metacourse\" value=\"0\" />";
326 echo "</td></tr>";
327 //Now print the Users tr
328 echo "<tr>";
329 echo "<td align=\"right\" colspan=\"2\"><b>";
330 echo get_string("users").":";
331 echo "</b></td><td colspan=\"2\">";
332 //If some user is present in the backup file
333 if ($info->backup_users == "all" or $info->backup_users == "course") {
334 //If all users are in the backup file
335 if ($info->backup_users == "all") {
336 $user_options[0] = get_string("all");
338 $user_options[1] = get_string("course");
339 $user_options[2] = get_string("none");
340 choose_from_menu($user_options, "restore_users", $restore_users, "");
341 } else {
342 echo get_string("none");
343 echo "<input type=\"hidden\" name=\"restore_users\" value=\"2\" />";
346 echo "</td></tr>";
348 //Now print the Logs tr
349 echo "<tr>";
350 echo "<td align=\"right\" colspan=\"2\"><b>";
351 echo get_string("logs").":";
352 echo "</b></td><td colspan=\"2\">";
353 //If logs are in the backup file, show menu, else fixed to no
354 if ($info->backup_logs == "true") {
355 $log_options[0] = get_string("no");
356 $log_options[1] = get_string("yes");
357 choose_from_menu($log_options, "restore_logs", $restore_logs, "");
358 } else {
359 echo get_string("no");
360 echo "<input type=\"hidden\" name=\"restore_logs\" value=\"0\" />";
362 echo "</td></tr>";
364 //Now print the User Files tr
365 echo "<tr>";
366 echo "<td align=\"right\" colspan=\"2\"><b>";
367 echo get_string ("userfiles").":";
368 echo "</b></td><td colspan=\"2\">";
369 //If user files are in the backup file, show menu, else fixed to no
370 if ($info->backup_user_files == "true") {
371 $user_file_options[0] = get_string("no");
372 $user_file_options[1] = get_string("yes");
373 choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
374 } else {
375 echo get_string("no");
376 echo "<input type=\"hidden\" name=\"restore_user_files\" value=\"0\" />";
378 echo "</td></tr>";
380 //Now print the Course Files tr
381 echo "<tr>";
382 echo "<td align=\"right\" colspan=\"2\"><b>";
383 echo get_string ("coursefiles").":";
384 echo "</b></td><td colspan=\"2\">";
385 echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
386 echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
387 //If course files are in the backup file, show menu, else fixed to no
388 if ($info->backup_course_files == "true") {
389 $course_file_options[0] = get_string("no");
390 $course_file_options[1] = get_string("yes");
391 choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
392 } else {
393 echo get_string("no");
394 echo "<input type=\"hidden\" name=\"restore_course_files\" value=\"0\" />";
396 echo "</td></tr>";
398 //Now print the Messages tr
399 echo "<tr>";
400 echo "<td align=\"right\" colspan=\"2\"><b>";
402 //This tr is slighty different. Everything becomes hidden if
403 //we haven't messages is the backup, to avoid confusions to users.
404 //If messages are in the backup file, show menu, else fixed to no and show nothing
405 if ($info->backup_messages == "true") {
406 echo get_string ('messages','message').":";
407 echo "</b></td><td colspan=\"2\">";
408 $message_options[0] = get_string("no");
409 $message_options[1] = get_string("yes");
410 choose_from_menu($message_options, "restore_messages", $restore_messages, "");
411 } else {
412 echo "&nbsp;</b></td><td colspan=\"2\">";
413 echo "<input type=\"hidden\" name=\"restore_messages\" value=\"0\" />";
415 echo "</td></tr>";
418 </td></tr></table>
420 <hr/>
421 <?php
423 print_heading(get_string('rolemappings'));
424 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
426 $info = restore_read_xml_info($xml_file);
428 if ($course->id == SITEID) {
429 $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_SYSTEM, SITEID), "shortname");
430 $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_SYSTEM, SITEID), "name");
431 } else {
432 $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname");
433 $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name");
436 echo ('<table width="100%" class="restore-form-instances">');
437 echo ('<tr><td align="right"><b>'.get_string('sourcerole').'</b></td><td align="left"><b>'.get_string('targetrole').'</b></td></tr>');
439 if ($info->backup_moodle_version < 2006092801) {
440 // 1.6 and below backup
442 /// Editting teacher
443 echo ('<tr><td align="right">');
444 print_string('defaultcourseteacher');
445 echo ('</td><td algin="left">');
447 // get the first teacheredit legacy
448 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
450 $editteacher = array_shift($roles);
451 choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id, 'new role', '', '0');
452 echo ('</td></tr>');
454 /// Non-editting teacher
455 echo ('<tr><td align="right">');
456 print_string('noneditingteacher');
457 echo ('</td><td algin="left">');
459 // get the first teacheredit legacy
460 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
461 $teacher = array_shift($roles);
463 choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id, 'new role', '', '0');
464 echo ('</td></tr>');
467 /// Student
468 echo ('<tr><td align="right">');
469 print_string('defaultcoursestudent');
470 echo ('</td><td algin="left">');
472 // get the first teacheredit legacy
473 $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
474 $studentrole = array_shift($roles);
476 choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id, 'new role', '', '0');
477 echo ('</td></tr>');
479 } else {
480 // 1.7 and above backup
481 $roles = restore_read_xml_roles($xml_file);
483 if (!empty($roles->roles)) { // possible to have course with no roles
484 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
485 $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
488 foreach ($roles->roles as $roleid=>$role) {
489 echo ('<tr><td align="right">');
490 echo $role->name." (".($role->shortname).")";
491 echo ('</td><td align="left">');
493 // see if any short name match
494 $matchrole = 0;
495 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
496 if ($siteroleshortname == $role->shortname) {
497 $matchrole = $siteroleid;
498 break;
502 choose_from_menu ($siteroleschoicearray, "roles_".$roleid, $matchrole, 'new role', '', '0');
503 echo ('</td></tr>');
507 } // end else
509 echo ('</table>'); // end of role mappings table
512 <br />
513 <center>
514 <input type="hidden" name="id" value="<?php p($id) ?>" />
515 <input type="hidden" name="launch" value="check" />
516 <input type="hidden" name="fromform" value="1" />
517 <input type="submit" value="<?php print_string("continue") ?>" />
518 <input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
519 </center>
520 </form>