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_messages)) {
91 $restore_messages = 1;
94 if (!isset($restore_restoreto)) {
95 if (!user_can_create_courses()) {
96 $restore_restoreto = 1;
98 $restore_restoreto = 2;
102 if(!isset($form1->startdate
)) {
103 $form1->startdate
= $course_header->course_startdate
; //$course_header->course_startdate;
106 if (empty($form1->shortname
)) {
107 $form1->shortname
= $course_header->course_shortname
; //'_shortname'; //$course_header->course_shortname;
110 if (empty($form1->fullname
)) {
111 $form1->fullname
= $course_header->course_fullname
; // '_fullname'; //$course_header->course_fullname;
115 notice("No restorable modules are installed!");
120 <script type=
"text/javascript">
122 function selectItemInMenuByName(formId
, menuName
, selectIndex
) {
123 myForm
= document
.getElementById(formId
)
124 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
125 myLen
= menuName
.length
;
126 myName
= myForm
.elements
[i
].name
;
127 myType
= myForm
.elements
[i
].type
;
128 if (myName
.substring(0,myLen
) == menuName
&& myType
== "select-one") {
129 myForm
.elements
[i
].options
[selectIndex
].selected
= true;
134 function selectItemInRadioByName(formId
, radioName
, selectIndex
) {
135 myForm
= document
.getElementById(formId
)
136 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
137 myLen
= radioName
.length
;
138 myName
= myForm
.elements
[i
].name
;
139 myType
= myForm
.elements
[i
].type
;
140 if (myName
.substring(0,myLen
) == radioName
&& myType
== "radio") {
141 myRadioGroup
= myForm
.elements
[myName
];
142 myRadioGroup
[selectIndex
].checked
= true;
147 function selectItemInCheckboxByName(formId
, checkName
, checked
) {
148 myForm
= document
.getElementById(formId
)
149 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
150 myLen
= checkName
.length
;
151 myName
= myForm
.elements
[i
].name
;
152 myType
= myForm
.elements
[i
].type
;
153 if (myName
.substring(0,myLen
) == checkName
&& myType
== "checkbox") {
154 myForm
.elements
[i
].checked
= checked
;
161 <form id=
"form1" method=
"post" action=
"restore.php">
163 <table cellpadding=
"5" class=
"boxaligncenter">
166 //First, course destination
169 echo "<td align=\"right\"><b>";
170 echo get_string("restoreto").":</b>";
171 echo "</td><td colspan=\"3\">";
174 // permission should have been checked already
177 * if user has manageactivities in any course, we show
178 * existingcoursedeleting
179 * existingcourseadding
181 * currentcoursedeleting
183 * if user has course:create in any category, we show
187 $mycourses = get_capability_courses('moodle/course:restore');
189 // if the user can manage 2 or more courses,
190 // or if the only course the user can manage is not the current course
191 // we show options for existing courses
192 if (count($mycourses) > 1 ||
!in_array($id, $mycourses)) {
193 // if user can manage more than 1 course, or if user can restore to a single different course
194 $restore_restoreto_options[0] = get_string("existingcoursedeleting");
195 $restore_restoreto_options[1] = get_string("existingcourseadding");
196 // else if the user can write to current course
197 } else if (has_capability('moodle/course:restore', get_context_instance(CONTEXT_COURSE
, $id))){
198 $restore_restoreto_options[0] = get_string("currentcoursedeleting");
199 $restore_restoreto_options[1] = get_string("currentcourseadding");
202 // if user can create any course at all, give the option
203 if (user_can_create_courses()) {
204 $restore_restoreto_options[2] = get_string("newcourse");
207 choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
209 if (user_can_create_courses()) { //display these fields conditionally
211 // find the list of cates user can edit
212 echo "<tr valign=\"top\" >";
213 echo "<td align=\"right\">";
214 print_string('category');
217 choose_from_menu(get_creatable_categories(), "restore_restorecatto", $course_header->category
->id
, "");
221 echo "<tr valign=\"top\" >";
222 echo "<td align=\"right\">";
223 print_string("shortname");
225 echo "<td><input type=\"text\" name=\"shortname\" maxlength=\"15\" size=\"10\" value=\"$form1->shortname\" alt=\"".get_string("shortname")."\" />" ;
226 helpbutton("courseshortname", get_string("shortname")) ;
227 if (isset($err["shortname"])) formerr($err["shortname"]);
228 echo "<tr valign=\"top\" >";
229 echo "<td align=\"right\">";
230 print_string("fullname");
232 echo "<td><input type=\"text\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"$form1->fullname\" alt=\" ".get_string("fullname")."\" />" ;
233 helpbutton("coursefullname", get_string("fullname")) ;
234 if (isset($err["fullname"])) formerr($err["fullname"]);
236 echo "<tr valign=\"top\"> ";
237 echo "<td align=\"right\"> ";
238 print_string("startdate");
240 print_date_selector("startday", "startmonth", "startyear", $form1->startdate
);
241 helpbutton("coursestartdate", get_string("startdate"));
245 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
246 //Now, check modules and info and show posibilities
247 if ($allmods = get_records("modules") ) {
248 //Print option to select/deselect everything with 1 click.
250 echo "<td align=\"right\">";
251 echo '<b>'.get_string("include").":</b>";
253 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
254 get_string("all")."</a>/";
255 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
256 get_string("none")."</a>";
258 echo "<td align=\"right\">";
259 echo '<b> </b>';
261 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
262 get_string("all")."</a>/";
263 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
264 get_string("none")."</a>";
267 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
269 foreach ($allmods as $mod) {
270 $modname = $mod->name
;
271 $modrestore = $modname."_restore_mods";
272 //If exists the lib & function
273 $exist = "exists_".$modname;
274 $restore_var = "restore_".$modname;
275 $user_info_var = "restore_user_info_".$modname;
276 if (isset($
$exist)) {
278 //Now check that we have that module info in the backup file
279 if (isset($info->mods
[$modname]) && $info->mods
[$modname]->backup
== "true") {
281 echo "<tr class=\"r".$currentrow."\">";
282 echo "<td align=\"right\"> ";
284 $restore_options[1] = get_string("yes");
285 $restore_options[0] = get_string("no");
286 //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
287 //choose_from_radio($restore_options, $restore_var, $$restore_var);
289 print_checkbox($restore_var, $
$restore_var, $
$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
290 //If backup contains user data, then show menu, else fix it to
292 echo "</td><td align=\"right\"> ";
294 if ($info->mods
[$modname]->userinfo
== "true") {
295 $restore_user_options[1] = get_string("yes");
296 $restore_user_options[0] = get_string("no");
297 //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
298 //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
299 print_checkbox($user_info_var, $
$user_info_var, $
$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
301 //Module haven't userdata
302 echo get_string("withoutuserdata");
303 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
306 if (isset($info->mods
[$modname]->instances
)) {
307 $instances = $info->mods
[$modname]->instances
;
309 if (!empty($instances) && is_array($instances)) {
310 echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
311 foreach ($instances as $instance) {
313 $var = 'restore_'.$modname.'_instance_'.$instance->id
;
314 $
$var = optional_param($var,1);
315 print_checkbox($var,$
$var,$
$var,$instance->name
,$instance->name
,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
316 echo '</td><td align="right"> ';
317 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id
;
318 $
$var = optional_param($var,1);
319 if ($info->mods
[$modname]->instances
[$instance->id
]->userinfo
== 'true') {
320 print_checkbox($var,$
$var,$
$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
322 echo '<input type="hidden" name="'.$var.'" value="0" />';
326 echo '</table></td></tr>';
329 //Module isn't restorable
330 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
331 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
334 //Module isn't restorable
335 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
336 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
339 //Module isn't restorable
340 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
341 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
343 $currentrow = ($currentrow +
1) %
2;
346 echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
348 //Now print the Metacourse tr
350 echo "<td align=\"right\" colspan=\"2\"><b>";
351 echo get_string("metacourse").":";
352 echo "</b></td><td colspan=\"2\">";
353 //If metacourse are in the backup file, show menu, else fixed to no
354 if ($info->backup_metacourse
== "true") {
355 $metacourse_options[0] = get_string("no");
356 $metacourse_options[1] = get_string("yes");
357 choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
359 echo get_string("no");
360 echo "<input type=\"hidden\" name=\"restore_metacourse\" value=\"0\" />";
363 //Now print the Users tr
365 echo "<td align=\"right\" colspan=\"2\"><b>";
366 echo get_string("users").":";
367 echo "</b></td><td colspan=\"2\">";
368 //If some user is present in the backup file
369 if ($info->backup_users
== "all" or $info->backup_users
== "course") {
370 //If all users are in the backup file
371 if ($info->backup_users
== "all") {
372 $user_options[0] = get_string("all");
374 $user_options[1] = get_string("course");
375 $user_options[2] = get_string("none");
376 choose_from_menu($user_options, "restore_users", $restore_users, "");
378 echo get_string("none");
379 echo "<input type=\"hidden\" name=\"restore_users\" value=\"2\" />";
384 //Now print the Logs tr
386 echo "<td align=\"right\" colspan=\"2\"><b>";
387 echo get_string("logs").":";
388 echo "</b></td><td colspan=\"2\">";
389 //If logs are in the backup file, show menu, else fixed to no
390 if ($info->backup_logs
== "true") {
391 $log_options[0] = get_string("no");
392 $log_options[1] = get_string("yes");
393 choose_from_menu($log_options, "restore_logs", $restore_logs, "");
395 echo get_string("no");
396 echo "<input type=\"hidden\" name=\"restore_logs\" value=\"0\" />";
400 //Now print the User Files tr
402 echo "<td align=\"right\" colspan=\"2\"><b>";
403 echo get_string ("userfiles").":";
404 echo "</b></td><td colspan=\"2\">";
405 //If user files are in the backup file, show menu, else fixed to no
406 if ($info->backup_user_files
== "true") {
407 $user_file_options[0] = get_string("no");
408 $user_file_options[1] = get_string("yes");
409 choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
411 echo get_string("no");
412 echo "<input type=\"hidden\" name=\"restore_user_files\" value=\"0\" />";
416 //Now print the Course Files tr
418 echo "<td align=\"right\" colspan=\"2\"><b>";
419 echo get_string ("coursefiles").":";
420 echo "</b></td><td colspan=\"2\">";
421 echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
422 echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
423 //If course files are in the backup file, show menu, else fixed to no
424 if ($info->backup_course_files
== "true") {
425 $course_file_options[0] = get_string("no");
426 $course_file_options[1] = get_string("yes");
427 choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
429 echo get_string("no");
430 echo "<input type=\"hidden\" name=\"restore_course_files\" value=\"0\" />";
434 //Now print the Messages tr
436 echo "<td align=\"right\" colspan=\"2\"><b>";
438 //This tr is slighty different. Everything becomes hidden if
439 //we haven't messages is the backup, to avoid confusions to users.
440 //If messages are in the backup file, show menu, else fixed to no and show nothing
441 if ($info->backup_messages
== "true") {
442 echo get_string ('messages','message').":";
443 echo "</b></td><td colspan=\"2\">";
444 $message_options[0] = get_string("no");
445 $message_options[1] = get_string("yes");
446 choose_from_menu($message_options, "restore_messages", $restore_messages, "");
448 echo " </b></td><td colspan=\"2\">";
449 echo "<input type=\"hidden\" name=\"restore_messages\" value=\"0\" />";
459 print_heading(get_string('rolemappings'));
460 $xml_file = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/moodle.xml";
462 $info = restore_read_xml_info($xml_file);
464 // fix for MDL-9068, front page course is just a normal course
465 $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "shortname");
466 $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "name");
468 echo ('<table width="100%" class="restore-form-instances">');
469 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>');
471 if ($info->backup_moodle_version
< 2006092801) {
472 // 1.6 and below backup
475 echo ('<tr><td align="right">');
476 print_string('defaultcourseteacher');
477 echo ('</td><td algin="left">');
479 // get the first teacheredit legacy
480 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
, SITEID
));
482 $editteacher = array_shift($roles);
483 choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id
, 'new role', '', '0');
486 /// Non-editting teacher
487 echo ('<tr><td align="right">');
488 print_string('noneditingteacher');
489 echo ('</td><td algin="left">');
491 // get the first teacheredit legacy
492 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
, SITEID
));
493 $teacher = array_shift($roles);
495 choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id
, 'new role', '', '0');
500 echo ('<tr><td align="right">');
501 print_string('defaultcoursestudent');
502 echo ('</td><td algin="left">');
504 // get the first teacheredit legacy
505 $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
, SITEID
));
506 $studentrole = array_shift($roles);
508 choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id
, 'new role', '', '0');
512 // 1.7 and above backup
513 $roles = restore_read_xml_roles($xml_file);
515 if (!empty($roles->roles
)) { // possible to have course with no roles
516 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
517 $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
520 foreach ($roles->roles
as $roleid=>$role) {
521 echo ('<tr><td align="right">');
522 echo $role->name
." (".($role->shortname
).")";
523 echo ('</td><td align="left">');
525 // see if any short name match
527 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
528 if ($siteroleshortname == $role->shortname
) {
529 $matchrole = $siteroleid;
534 choose_from_menu ($siteroleschoicearray, "roles_".$roleid, $matchrole, 'new role', '', '0');
541 echo ('</table>'); // end of role mappings table
545 <div style=
"text-align:center">
546 <input type=
"hidden" name=
"id" value=
"<?php p($id) ?>" />
547 <input type
="hidden" name
="launch" value
="check" />
548 <input type
="hidden" name
="fromform" value
="1" />
549 <input type
="submit" value
="<?php print_string("continue") ?>" />
550 <input type
="submit" name
="cancel" value
="<?php print_string("cancel
") ?>" />