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
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
17 $backup_unique_code = required_param( 'backup_unique_code' );
19 $file = required_param( 'file' );
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");
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");
36 if (!$site = get_site()) {
37 error("Site not found!");
40 //Checks for the required files/functions to restore every mod
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;
57 $var = "restore_".$modname;
61 //Check include user info
62 $var = "restore_user_info_".$modname;
69 //Check other parameters
70 if (!isset($restore_metacourse)) {
71 $restore_metacourse = 1;
74 if (!isset($restore_users)) {
78 if (!isset($restore_logs)) {
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_site_files)) {
91 $restore_site_files = 1;
94 if (!isset($restore_messages)) {
95 $restore_messages = 1;
98 if (!isset($restore_restoreto)) {
99 if (!user_can_create_courses()) {
100 $restore_restoreto = 1;
102 $restore_restoreto = 2;
106 if(!isset($form1->startdate
)) {
107 $form1->startdate
= $course_header->course_startdate
; //$course_header->course_startdate;
110 if (empty($form1->shortname
)) {
111 $form1->shortname
= $course_header->course_shortname
; //'_shortname'; //$course_header->course_shortname;
114 if (empty($form1->fullname
)) {
115 $form1->fullname
= $course_header->course_fullname
; // '_fullname'; //$course_header->course_fullname;
119 notice("No restorable modules are installed!");
124 <script type=
"text/javascript">
126 function selectItemInMenuByName(formId
, menuName
, selectIndex
) {
127 myForm
= document
.getElementById(formId
)
128 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
129 myLen
= menuName
.length
;
130 myName
= myForm
.elements
[i
].name
;
131 myType
= myForm
.elements
[i
].type
;
132 if (myName
.substring(0,myLen
) == menuName
&& myType
== "select-one") {
133 myForm
.elements
[i
].options
[selectIndex
].selected
= true;
138 function selectItemInRadioByName(formId
, radioName
, selectIndex
) {
139 myForm
= document
.getElementById(formId
)
140 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
141 myLen
= radioName
.length
;
142 myName
= myForm
.elements
[i
].name
;
143 myType
= myForm
.elements
[i
].type
;
144 if (myName
.substring(0,myLen
) == radioName
&& myType
== "radio") {
145 myRadioGroup
= myForm
.elements
[myName
];
146 myRadioGroup
[selectIndex
].checked
= true;
151 function selectItemInCheckboxByName(formId
, checkName
, checked
) {
152 myForm
= document
.getElementById(formId
)
153 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
154 myLen
= checkName
.length
;
155 myName
= myForm
.elements
[i
].name
;
156 myType
= myForm
.elements
[i
].type
;
157 if (myName
.substring(0,myLen
) == checkName
&& myType
== "checkbox") {
158 myForm
.elements
[i
].checked
= checked
;
165 <form id=
"form1" method=
"post" action=
"restore.php">
167 <table cellpadding=
"5" class=
"boxaligncenter">
170 //First, course destination
173 echo "<td align=\"right\"><b>";
174 echo get_string("restoreto").":</b>";
175 echo "</td><td colspan=\"3\">";
178 // permission should have been checked already
181 * if user has manageactivities in any course, we show
182 * existingcoursedeleting
183 * existingcourseadding
185 * currentcoursedeleting
187 * if user has course:create in any category, we show
191 $mycourses = get_capability_courses('moodle/site:restore');
193 // if the user can manage 2 or more courses,
194 // or if the only course the user can manage is not the current course
195 // we show options for existing courses
196 if (count($mycourses) > 1 ||
!in_array($id, $mycourses)) {
197 // if user can manage more than 1 course, or if user can restore to a single different course
198 $restore_restoreto_options[0] = get_string("existingcoursedeleting");
199 $restore_restoreto_options[1] = get_string("existingcourseadding");
200 // else if the user can write to current course
201 } else if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $id))){
202 $restore_restoreto_options[0] = get_string("currentcoursedeleting");
203 $restore_restoreto_options[1] = get_string("currentcourseadding");
206 // if user can create any course at all, give the option
207 if (user_can_create_courses()) {
208 $restore_restoreto_options[2] = get_string("newcourse");
211 choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
213 if (user_can_create_courses()) { //display these fields conditionally
215 // find the list of cates user can edit
216 echo "<tr valign=\"top\" >";
217 echo "<td align=\"right\">";
218 print_string('category');
221 choose_from_menu(get_creatable_categories(), "restore_restorecatto", $course_header->category
->id
, "");
225 echo "<tr valign=\"top\" >";
226 echo "<td align=\"right\">";
227 print_string("shortname");
229 echo "<td><input type=\"text\" name=\"shortname\" maxlength=\"100\" size=\"20\" value=\"$form1->shortname\" alt=\"".get_string("shortname")."\" />" ;
230 helpbutton("courseshortname", get_string("shortname")) ;
231 if (isset($err["shortname"])) formerr($err["shortname"]);
232 echo "<tr valign=\"top\" >";
233 echo "<td align=\"right\">";
234 print_string("fullname");
236 echo "<td><input type=\"text\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"$form1->fullname\" alt=\" ".get_string("fullname")."\" />" ;
237 helpbutton("coursefullname", get_string("fullname")) ;
238 if (isset($err["fullname"])) formerr($err["fullname"]);
240 echo "<tr valign=\"top\"> ";
241 echo "<td align=\"right\"> ";
242 print_string("startdate");
244 print_date_selector("startday", "startmonth", "startyear", $form1->startdate
);
245 helpbutton("coursestartdate", get_string("startdate"));
249 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
250 //Now, check modules and info and show posibilities
251 if ($allmods = get_records("modules") ) {
252 //Print option to select/deselect everything with 1 click.
254 echo "<td align=\"right\">";
255 echo '<b>'.get_string("include").":</b>";
257 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
258 get_string("all")."</a>/";
259 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
260 get_string("none")."</a>";
262 echo "<td align=\"right\">";
263 echo '<b> </b>';
265 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
266 get_string("all")."</a>/";
267 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
268 get_string("none")."</a>";
271 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
273 foreach ($allmods as $mod) {
274 $modname = $mod->name
;
275 $modrestore = $modname."_restore_mods";
276 //If exists the lib & function
277 $exist = "exists_".$modname;
278 $restore_var = "restore_".$modname;
279 $user_info_var = "restore_user_info_".$modname;
280 if (isset($
$exist)) {
282 //Now check that we have that module info in the backup file
283 if (isset($info->mods
[$modname]) && $info->mods
[$modname]->backup
== "true") {
285 echo "<tr class=\"r".$currentrow."\">";
286 echo "<td align=\"right\"> ";
288 $restore_options[1] = get_string("yes");
289 $restore_options[0] = get_string("no");
290 //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
291 //choose_from_radio($restore_options, $restore_var, $$restore_var);
293 print_checkbox($restore_var, $
$restore_var, $
$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
294 //If backup contains user data, then show menu, else fix it to
296 echo "</td><td align=\"right\"> ";
298 if ($info->mods
[$modname]->userinfo
== "true") {
299 $restore_user_options[1] = get_string("yes");
300 $restore_user_options[0] = get_string("no");
301 //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
302 //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
303 print_checkbox($user_info_var, $
$user_info_var, $
$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
305 //Module haven't userdata
306 echo get_string("withoutuserdata");
307 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
310 if (isset($info->mods
[$modname]->instances
)) {
311 $instances = $info->mods
[$modname]->instances
;
313 if (!empty($instances) && is_array($instances)) {
314 echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
315 foreach ($instances as $instance) {
317 $var = 'restore_'.$modname.'_instance_'.$instance->id
;
318 $
$var = optional_param($var,1);
319 print_checkbox($var,$
$var,$
$var,$instance->name
,$instance->name
,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
320 echo '</td><td align="right"> ';
321 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id
;
322 $
$var = optional_param($var,1);
323 if ($info->mods
[$modname]->instances
[$instance->id
]->userinfo
== 'true') {
324 print_checkbox($var,$
$var,$
$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
326 echo '<input type="hidden" name="'.$var.'" value="0" />';
330 echo '</table></td></tr>';
333 //Module isn't restorable
334 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
335 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
338 //Module isn't restorable
339 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
340 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
343 //Module isn't restorable
344 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
345 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
347 $currentrow = ($currentrow +
1) %
2;
350 echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
352 //Now print the Metacourse tr
354 echo "<td align=\"right\" colspan=\"2\"><b>";
355 echo get_string("metacourse").":";
356 echo "</b></td><td colspan=\"2\">";
357 //If metacourse are in the backup file, show menu, else fixed to no
358 if ($info->backup_metacourse
== "true") {
359 $metacourse_options[0] = get_string("no");
360 $metacourse_options[1] = get_string("yes");
361 choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
363 echo get_string("no");
364 echo "<input type=\"hidden\" name=\"restore_metacourse\" value=\"0\" />";
367 //Now print the Users tr
369 echo "<td align=\"right\" colspan=\"2\"><b>";
370 echo get_string("users").":";
371 echo "</b></td><td colspan=\"2\">";
372 //If some user is present in the backup file
373 if ($info->backup_users
== "all" or $info->backup_users
== "course") {
374 //If all users are in the backup file
375 if ($info->backup_users
== "all") {
376 $user_options[0] = get_string("all");
378 $user_options[1] = get_string("course");
379 $user_options[2] = get_string("none");
380 choose_from_menu($user_options, "restore_users", $restore_users, "");
382 echo get_string("none");
383 echo "<input type=\"hidden\" name=\"restore_users\" value=\"2\" />";
388 //Now print the Logs tr
390 echo "<td align=\"right\" colspan=\"2\"><b>";
391 echo get_string("logs").":";
392 echo "</b></td><td colspan=\"2\">";
393 //If logs are in the backup file, show menu, else fixed to no
394 if ($info->backup_logs
== "true") {
395 $log_options[0] = get_string("no");
396 $log_options[1] = get_string("yes");
397 choose_from_menu($log_options, "restore_logs", $restore_logs, "");
399 echo get_string("no");
400 echo "<input type=\"hidden\" name=\"restore_logs\" value=\"0\" />";
404 //Now print the User Files tr
406 echo "<td align=\"right\" colspan=\"2\"><b>";
407 echo get_string ("userfiles").":";
408 echo "</b></td><td colspan=\"2\">";
409 //If user files are in the backup file, show menu, else fixed to no
410 if ($info->backup_user_files
== "true") {
411 $user_file_options[0] = get_string("no");
412 $user_file_options[1] = get_string("yes");
413 choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
415 echo get_string("no");
416 echo "<input type=\"hidden\" name=\"restore_user_files\" value=\"0\" />";
420 //Now print the Course Files tr
422 echo "<td align=\"right\" colspan=\"2\"><b>";
423 echo get_string ("coursefiles").":";
424 echo "</b></td><td colspan=\"2\">";
425 echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
426 echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
427 //If course files are in the backup file, show menu, else fixed to no
428 if ($info->backup_course_files
== "true") {
429 $course_file_options[0] = get_string("no");
430 $course_file_options[1] = get_string("yes");
431 choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
433 echo get_string("no");
434 echo "<input type=\"hidden\" name=\"restore_course_files\" value=\"0\" />";
439 //Now print the Site Files tr
441 echo "<td align=\"right\" colspan=\"2\"><b>";
442 echo get_string ("sitefiles").":";
443 echo "</b></td><td colspan=\"2\">";
444 //If site files are in the backup file, show menu, else fixed to no
445 if ($info->backup_site_files
== "true") {
446 $site_file_options[0] = get_string("no");
447 $site_file_options[1] = get_string("yes");
448 choose_from_menu($site_file_options, "restore_site_files", $restore_site_files, "");
450 echo get_string("no");
451 echo "<input type=\"hidden\" name=\"restore_site_files\" value=\"0\" />";
455 //Now print the Messages tr
457 echo "<td align=\"right\" colspan=\"2\"><b>";
459 //This tr is slighty different. Everything becomes hidden if
460 //we haven't messages is the backup, to avoid confusions to users.
461 //If messages are in the backup file, show menu, else fixed to no and show nothing
462 if ($info->backup_messages
== "true") {
463 echo get_string ('messages','message').":";
464 echo "</b></td><td colspan=\"2\">";
465 $message_options[0] = get_string("no");
466 $message_options[1] = get_string("yes");
467 choose_from_menu($message_options, "restore_messages", $restore_messages, "");
469 echo " </b></td><td colspan=\"2\">";
470 echo "<input type=\"hidden\" name=\"restore_messages\" value=\"0\" />";
480 print_heading(get_string('rolemappings'));
481 $xml_file = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/moodle.xml";
483 $info = restore_read_xml_info($xml_file);
485 // fix for MDL-9068, front page course is just a normal course
486 $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "shortname");
487 $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "name");
489 echo ('<table width="100%" class="restore-form-instances">');
490 echo ('<tr><td align="right" style="width:50%"><b>'.get_string('sourcerole').'</b></td><td align="left" style="width:50%"><b>'.get_string('targetrole').'</b></td></tr>');
492 if ($info->backup_moodle_version
< 2006092801) {
493 // 1.6 and below backup
496 echo ('<tr><td align="right">');
497 print_string('defaultcourseteacher');
498 echo ('</td><td algin="left">');
500 // get the first teacheredit legacy
501 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
, SITEID
));
503 $editteacher = array_shift($roles);
504 choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id
, 'new role', '', '0');
507 /// Non-editting teacher
508 echo ('<tr><td align="right">');
509 print_string('noneditingteacher');
510 echo ('</td><td algin="left">');
512 // get the first teacheredit legacy
513 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
, SITEID
));
514 $teacher = array_shift($roles);
516 choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id
, 'new role', '', '0');
521 echo ('<tr><td align="right">');
522 print_string('defaultcoursestudent');
523 echo ('</td><td algin="left">');
525 // get the first teacheredit legacy
526 $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
, SITEID
));
527 $studentrole = array_shift($roles);
529 choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id
, 'new role', '', '0');
533 // 1.7 and above backup
534 $roles = restore_read_xml_roles($xml_file);
536 if (!empty($roles->roles
)) { // possible to have course with no roles
537 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
538 $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
541 foreach ($roles->roles
as $roleid=>$role) {
542 echo ('<tr><td align="right">');
543 echo $role->name
." (".($role->shortname
).")";
544 echo ('</td><td align="left">');
546 // see if any short name match
548 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
549 if ($siteroleshortname == $role->shortname
) {
550 $matchrole = $siteroleid;
555 choose_from_menu ($siteroleschoicearray, "roles_".$roleid, $matchrole, 'new role', '', '0');
562 echo ('</table>'); // end of role mappings table
566 <div style=
"text-align:center">
567 <input type=
"hidden" name=
"id" value=
"<?php p($id) ?>" />
568 <input type
="hidden" name
="launch" value
="check" />
569 <input type
="hidden" name
="fromform" value
="1" />
570 <input type
="submit" value
="<?php print_string("continue") ?>" />
571 <input type
="submit" name
="cancel" value
="<?php print_string("cancel
") ?>" />