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 $restore_gradebook_history = optional_param('restore_gradebook_history', 0, PARAM_INT
);
17 //Check that we have all we need
19 $backup_unique_code = required_param( 'backup_unique_code' );
21 $file = required_param( 'file' );
28 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $id))) {
29 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
32 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM
))) {
33 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
38 if (!$site = get_site()) {
39 error("Site not found!");
42 //Checks for the required files/functions to restore every mod
44 if ($allmods = get_records("modules") ) {
45 foreach ($allmods as $mod) {
46 $modname = $mod->name
;
47 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
48 $modrestore = $modname."_restore_mods";
49 if (file_exists($modfile)) {
50 include_once($modfile);
51 if (function_exists($modrestore)) {
52 $var = "exists_".$modname;
59 $var = "restore_".$modname;
63 //Check include user info
64 $var = "restore_user_info_".$modname;
71 //Check other parameters
72 if (!isset($restore_metacourse)) {
73 $restore_metacourse = 1;
76 if (!isset($restore_users)) {
80 if (!isset($restore_groups)) {
81 if (empty($CFG->enablegroupings
)) {
82 $restore_groups = RESTORE_GROUPS_ONLY
;
84 $restore_groups = RESTORE_GROUPS_GROUPINGS
;
88 if (!isset($restore_logs)) {
92 if (!isset($restore_user_files)) {
93 $restore_user_files = 1;
96 if (!isset($restore_course_files)) {
97 $restore_course_files = 1;
100 if (!isset($restore_site_files)) {
101 $restore_site_files = 1;
104 if (!isset($restore_messages)) {
105 $restore_messages = 1;
108 if (!isset($restore_blogs)) {
112 if (!isset($restore_restoreto)) {
113 if (!user_can_create_courses()) {
114 $restore_restoreto = 1;
116 $restore_restoreto = 2;
120 if (!isset($course_header->category
->id
)) {
121 $course_header->category
->id
= 0;
124 if(!isset($form1->startdate
)) {
125 $form1->startdate
= $course_header->course_startdate
; //$course_header->course_startdate;
128 if (empty($form1->shortname
)) {
129 $form1->shortname
= $course_header->course_shortname
; //'_shortname'; //$course_header->course_shortname;
132 if (empty($form1->fullname
)) {
133 $form1->fullname
= $course_header->course_fullname
; // '_fullname'; //$course_header->course_fullname;
137 notice("No restorable modules are installed!");
142 <script type=
"text/javascript">
144 function selectItemInMenuByName(formId
, menuName
, selectIndex
) {
145 myForm
= document
.getElementById(formId
)
146 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
147 myLen
= menuName
.length
;
148 myName
= myForm
.elements
[i
].name
;
149 myType
= myForm
.elements
[i
].type
;
150 if (myName
.substring(0,myLen
) == menuName
&& myType
== "select-one") {
151 myForm
.elements
[i
].options
[selectIndex
].selected
= true;
156 function selectItemInRadioByName(formId
, radioName
, selectIndex
) {
157 myForm
= document
.getElementById(formId
)
158 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
159 myLen
= radioName
.length
;
160 myName
= myForm
.elements
[i
].name
;
161 myType
= myForm
.elements
[i
].type
;
162 if (myName
.substring(0,myLen
) == radioName
&& myType
== "radio") {
163 myRadioGroup
= myForm
.elements
[myName
];
164 myRadioGroup
[selectIndex
].checked
= true;
169 function selectItemInCheckboxByName(formId
, checkName
, checked
) {
170 myForm
= document
.getElementById(formId
)
171 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
172 myLen
= checkName
.length
;
173 myName
= myForm
.elements
[i
].name
;
174 myType
= myForm
.elements
[i
].type
;
175 if (myName
.substring(0,myLen
) == checkName
&& myType
== "checkbox") {
176 myForm
.elements
[i
].checked
= checked
;
183 <form id=
"form1" method=
"post" action=
"restore.php">
185 <table cellpadding=
"5" class=
"boxaligncenter">
188 //First, course destination
191 echo "<td align=\"right\"><b>";
192 echo '<label for="menurestore_restoreto">'.get_string ('restoreto').'</label>';
194 echo "</td><td colspan=\"3\">";
197 // permission should have been checked already
200 * if user has manageactivities in any course and we are not restoring from within SITEID
201 * existingcoursedeleting
202 * existingcourseadding
204 * currentcoursedeleting
206 * if user has course:create in any category, we show
210 // Non-cached - get accessinfo
211 if (isset($USER->access
)) {
212 $accessinfo = $USER->access
;
214 $accessinfo = get_user_access_sitewide($USER->id
);
217 $mycourses = get_user_courses_bycap($USER->id
, 'moodle/site:restore', $accessinfo, true);
219 // if the user can manage 2 or more courses and we are not restoring from within SITEID,
220 // we show options for existing courses
222 if (count($mycourses) > 1 && $id != SITEID
) {
223 $restore_restoreto_options[0] = get_string("existingcoursedeleting");
224 $restore_restoreto_options[1] = get_string("existingcourseadding");
225 // else if the user can write to current course
226 } else if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $id))){
227 $restore_restoreto_options[0] = get_string("currentcoursedeleting");
228 $restore_restoreto_options[1] = get_string("currentcourseadding");
231 // if user can create any course at all, give the option
232 if (user_can_create_courses()) {
233 $restore_restoreto_options[2] = get_string("newcourse");
236 /// Acummulator for hidden options and proper XHTML output
237 $hidden_options = '';
239 choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
241 if (user_can_create_courses()) { //display these fields conditionally
243 // find the list of cates user can edit
244 echo "<tr valign=\"top\" >";
245 echo "<td align=\"right\">";
246 echo '<label for="menurestore_restorecatto">'.get_string ('category').'</label>';
249 // Category selection isn't alowed if restoring from within SITEID course
251 make_categories_list($categories, $parents);
252 choose_from_menu($categories, "restore_restorecatto", $course_header->category
->id
, "");
254 print_string('notavailable');
255 echo '<input type="hidden" name="restore_restorecatto" id="menurestore_restorecatto" value="0" />';
260 echo "<tr valign=\"top\" >";
261 echo "<td align=\"right\">";
262 echo '<label for="shortnamefield">'.get_string ('shortname').'</label>';
264 echo "<td><input type=\"text\" id=\"shortnamefield\" name=\"shortname\" maxlength=\"100\" size=\"20\" value=\"".s($form1->shortname
)."\" alt=\"".get_string("shortname")."\" />" ;
265 helpbutton("courseshortname", get_string("shortname")) ;
266 if (isset($err["shortname"])) formerr($err["shortname"]);
269 echo "<tr valign=\"top\" >";
270 echo "<td align=\"right\">";
271 echo '<label for="fullnamefield">'.get_string ('fullname').'</label>';
273 echo "<td><input type=\"text\" id=\"fullnamefield\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"".s($form1->fullname
)."\" alt=\" ".get_string("fullname")."\" />" ;
274 helpbutton("coursefullname", get_string("fullname")) ;
275 if (isset($err["fullname"])) formerr($err["fullname"]);
277 echo "<tr valign=\"top\"> ";
278 echo "<td align=\"right\"> ";
279 print_string("startdate");
281 /// Show the roll dates option only if the backup course has a start date
282 /// (some formats like main page, social..., haven't it and rolling dates
283 /// from 0 produces crazy dates. MDL-10125
284 if ($form1->startdate
) {
285 print_date_selector("startday", "startmonth", "startyear", $form1->startdate
);
286 helpbutton("coursestartdate", get_string("startdate"));
288 print_string('notavailable');
289 echo '<input type="hidden" name="startyear" value="0" />';
290 echo '<input type="hidden" name="startmonth" value="0" />';
291 echo '<input type="hidden" name="startday" value="0" />';
296 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
297 //Now, check modules and info and show posibilities
298 if ($allmods = get_records("modules") ) {
299 //Print option to select/deselect everything with 1 click.
301 echo "<td align=\"right\">";
302 echo '<b>'.get_string("include")."</b>";
304 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
305 get_string("all")."</a>/";
306 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
307 get_string("none")."</a>";
309 echo "<td align=\"right\">";
310 echo '<b> </b>';
312 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
313 get_string("all")."</a>/";
314 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
315 get_string("none")."</a>";
318 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
321 foreach ($allmods as $mod) {
322 $modname = $mod->name
;
323 $modrestore = $modname."_restore_mods";
324 //If exists the lib & function
325 $exist = "exists_".$modname;
326 $restore_var = "restore_".$modname;
327 $user_info_var = "restore_user_info_".$modname;
328 if (isset($
$exist)) {
330 //Now check that we have that module info in the backup file
331 if (isset($info->mods
[$modname]) && $info->mods
[$modname]->backup
== "true") {
333 echo "<tr class=\"r".$currentrow."\">";
334 echo "<td align=\"right\"> ";
336 $restore_options[1] = get_string("yes");
337 $restore_options[0] = get_string("no");
338 //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
339 //choose_from_radio($restore_options, $restore_var, $$restore_var);
341 print_checkbox($restore_var, $
$restore_var, $
$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
342 //If backup contains user data, then show menu, else fix it to
344 echo "</td><td align=\"right\"> ";
346 if ($info->mods
[$modname]->userinfo
== "true") {
347 $restore_user_options[1] = get_string("yes");
348 $restore_user_options[0] = get_string("no");
349 //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
350 //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
351 print_checkbox($user_info_var, $
$user_info_var, $
$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
353 //Module haven't userdata
354 echo get_string("withoutuserdata");
355 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
358 if (isset($info->mods
[$modname]->instances
)) {
359 $instances = $info->mods
[$modname]->instances
;
361 if (!empty($instances) && is_array($instances)) {
362 echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
363 foreach ($instances as $instance) {
365 $var = 'restore_'.$modname.'_instance_'.$instance->id
;
366 $
$var = optional_param($var,1);
367 print_checkbox($var,$
$var,$
$var,$instance->name
,$instance->name
,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
368 echo '</td><td align="right"> ';
369 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id
;
370 $
$var = optional_param($var,1);
371 if (!empty($info->mods
[$modname]->instances
) && ($info->mods
[$modname]->instances
[$instance->id
]->userinfo
== 'true')) {
372 print_checkbox($var,$
$var,$
$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
374 echo '<input type="hidden" name="'.$var.'" value="0" />';
378 echo '</table></td></tr>';
381 //Module isn't restorable
382 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
383 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
386 //Module isn't restorable
387 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
388 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
391 //Module isn't restorable
392 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
393 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
395 $currentrow = ($currentrow +
1) %
2;
398 echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
400 //Now print the Metacourse tr
402 echo "<td align=\"right\" colspan=\"2\"><b>";
403 echo '<label for="menurestore_metacourse">'.get_string ("metacourse").'</label>';
404 echo "</b></td><td colspan=\"2\">";
405 //If metacourse are in the backup file, show menu, else fixed to no
406 if ($info->backup_metacourse
== "true") {
407 $metacourse_options = array();
408 $metacourse_options[0] = get_string("no");
409 $metacourse_options[1] = get_string("yes");
410 choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
412 echo get_string("no");
413 echo "<input type=\"hidden\" id=\"menurestore_metacourse\" name=\"restore_metacourse\" value=\"0\" />";
416 //Now print the Users tr
418 echo "<td align=\"right\" colspan=\"2\"><b>";
419 echo '<label for="menurestore_users">'.get_string ("users").'</label>';
420 echo "</b></td><td colspan=\"2\">";
421 //If some user is present in the backup file
422 if ($info->backup_users
== "all" or $info->backup_users
== "course") {
423 $user_options = array();
424 //If all users are in the backup file
425 if ($info->backup_users
== "all") {
426 $user_options[0] = get_string("all");
428 $user_options[1] = get_string("course");
429 $user_options[2] = get_string("none");
430 choose_from_menu($user_options, "restore_users", $restore_users, "");
432 echo get_string("none");
433 echo "<input type=\"hidden\" id=\"menurestore_users\" name=\"restore_users\" value=\"2\" />";
438 //Now print the Groups tr (assume there is no $info->backup_groups)
440 echo "<td align=\"right\" colspan=\"2\"><b>";
441 $helplink = helpbutton('grouprestore', get_string('groups'), '', true, false, '', true);
442 if (empty($CFG->enablegroupings
)) {
443 echo '<label for="menurestore_groups">'.get_string ("groups").'</label>'.$helplink;
444 echo "</b></td><td colspan=\"2\">";
445 $group_options[RESTORE_GROUPS_NONE
] = get_string('no');
446 $group_options[RESTORE_GROUPS_ONLY
] = get_string('yes');
448 echo '<label for="menurestore_groups">'.get_string ('groupsgroupings', 'group').'</label>'.$helplink;
449 echo "</b></td><td colspan=\"2\">";
450 $group_options[RESTORE_GROUPS_NONE
] = get_string('none');
451 $group_options[RESTORE_GROUPS_ONLY
] = get_string('groupsonly', 'group');
452 $group_options[RESTORE_GROUPINGS_ONLY
] = get_string('groupingsonly', 'group');
453 $group_options[RESTORE_GROUPS_GROUPINGS
] = get_string('groupsgroupings', 'group'); //all.
456 echo get_string('none');
457 echo "<input type=\"hidden\" name=\"restore_groups\" value=\"2\" />";
459 choose_from_menu($group_options, 'restore_groups', $restore_groups, '');
462 //Now print the Logs tr
464 echo "<td align=\"right\" colspan=\"2\"><b>";
465 echo '<label for="menurestore_logs">'.get_string ("logs").'</label>';
466 echo "</b></td><td colspan=\"2\">";
467 //If logs are in the backup file, show menu, else fixed to no
468 if ($info->backup_logs
== "true") {
469 $log_options = array();
470 $log_options[0] = get_string("no");
471 $log_options[1] = get_string("yes");
472 choose_from_menu($log_options, "restore_logs", $restore_logs, "");
474 echo get_string("no");
475 echo "<input type=\"hidden\" id=\"menurestore_logs\" name=\"restore_logs\" value=\"0\" />";
479 //Now print the User Files tr
481 echo "<td align=\"right\" colspan=\"2\"><b>";
482 echo '<label for="menurestore_user_files">'.get_string ("userfiles").'</label>';
483 echo "</b></td><td colspan=\"2\">";
484 //If user files are in the backup file, show menu, else fixed to no
485 if ($info->backup_user_files
== "true") {
486 $user_file_options = array();
487 $user_file_options[0] = get_string("no");
488 $user_file_options[1] = get_string("yes");
489 choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
491 echo get_string("no");
492 echo "<input type=\"hidden\" id=\"menurestore_user_files\" name=\"restore_user_files\" value=\"0\" />";
496 //Now print the Course Files tr
498 echo "<td align=\"right\" colspan=\"2\"><b>";
499 echo '<label for="menurestore_course_files">'.get_string ("coursefiles").'</label>';
500 echo "</b></td><td colspan=\"2\">";
501 echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
502 echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
503 //If course files are in the backup file, show menu, else fixed to no
504 if ($info->backup_course_files
== "true") {
505 $course_file_options = array();
506 $course_file_options[0] = get_string("no");
507 $course_file_options[1] = get_string("yes");
508 choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
510 echo get_string("no");
511 echo "<input type=\"hidden\" id=\"menurestore_course_files\" name=\"restore_course_files\" value=\"0\" />";
516 //Now print the Site Files tr
518 echo "<td align=\"right\" colspan=\"2\"><b>";
519 echo '<label for="menurestore_site_files">'.get_string ("sitefiles").'</label>';
520 echo "</b></td><td colspan=\"2\">";
521 //If site files are in the backup file, show menu, else fixed to no
522 if (isset($info->backup_site_files
) && $info->backup_site_files
== "true") {
523 $site_file_options = array();
524 $site_file_options[0] = get_string("no");
525 $site_file_options[1] = get_string("yes");
526 choose_from_menu($site_file_options, "restore_site_files", $restore_site_files, "");
528 echo get_string("no");
529 echo "<input type=\"hidden\" id=\"menurestore_site_files\" name=\"restore_site_files\" value=\"0\" />";
533 // do you want grade histories to be restored?
534 if (empty($CFG->disablegradehistory
)) {
536 echo "<td align=\"right\" colspan=\"2\"><b>";
537 echo '<label for="menurestore_gradebook_history">'.get_string ('gradebookhistories', 'grades').'</label>';
538 echo "</b></td><td colspan=\"2\">";
539 if (isset($info->gradebook_histories
) && $info->gradebook_histories
== "true") {
540 $gradebook_history_options = array();
541 $gradebook_history_options[0] = get_string("no");
542 $gradebook_history_options[1] = get_string("yes");
543 choose_from_menu($gradebook_history_options, "restore_gradebook_history", $restore_gradebook_history, "");
545 echo get_string("no");
546 echo "<input type=\"hidden\" id=\"menurestore_gradebook_history\" name=\"restore_gradebook_history\" value=\"0\" />";
550 $hidden_options .= '<input type="hidden" name="restore_gradebook_history" value="0" />';
553 //This tr is slighty different. Everything becomes hidden if
554 //we haven't messages is the backup, to avoid confusions to users.
555 //If messages are in the backup file, show menu, else fixed to no and show nothing
556 //Also, messaging must be enabled in the destination site
557 if (isset($info->backup_messages
) && $info->backup_messages
== "true" && !empty($CFG->messaging
)) {
559 echo "<td align=\"right\" colspan=\"2\"><b>";
560 echo '<label for="menurestore_messages">'.get_string ('messages', 'message').'</label>';
561 echo "</b></td><td colspan=\"2\">";
562 $message_options = array();
563 $message_options[0] = get_string("no");
564 $message_options[1] = get_string("yes");
565 choose_from_menu($message_options, "restore_messages", $restore_messages, "");
568 $hidden_options .= '<input type="hidden" name="restore_messages" value="0" />';
571 //This tr is slighty different. Everything becomes hidden if
572 //we haven't blogs is the backup, to avoid confusions to users.
573 //If blogs are in the backup file, show menu, else fixed to no and show nothing
574 //Also, blogs must be enabled in the destination site
575 if (isset($info->backup_blogs
) && $info->backup_blogs
== "true" && !empty($CFG->bloglevel
)) {
577 echo "<td align=\"right\" colspan=\"2\"><b>";
578 echo '<label for="menurestore_blogs">'.get_string ('blogs', 'blog').'</label>';
579 echo "</b></td><td colspan=\"2\">";
580 $blog_options = array();
581 $blog_options[0] = get_string("no");
582 $blog_options[1] = get_string("yes");
583 choose_from_menu($blog_options, "restore_blogs", $restore_blogs, "");
586 $hidden_options .= '<input type="hidden" name="restore_blogs" value="0" />';
596 print_heading(get_string('rolemappings'));
597 $xml_file = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/moodle.xml";
599 $info = restore_read_xml_info($xml_file);
601 // fix for MDL-9068, front page course is just a normal course
602 $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "shortname", ROLENAME_ORIGINAL
);
603 $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "name", ROLENAME_ORIGINAL
);
604 $allroles = get_records('role');
606 echo ('<table width="100%" class="restore-form-instances">');
607 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>');
609 if ($info->backup_moodle_version
< 2006092801) {
610 // 1.6 and below backup
613 echo ('<tr><td align="right">');
614 echo '<label for="menudefaultteacheredit">'.get_string ('defaultcourseteacher').'</label>';
615 echo ('</td><td algin="left">');
617 // get the first teacheredit legacy
618 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
));
620 $editteacher = reset($roles);
621 choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id
, 'new role', '', '0');
624 /// Non-editting teacher
625 echo ('<tr><td align="right">');
626 echo '<label for="menudefaultteacher">'.get_string ('noneditingteacher').'</label>';
627 print_string('noneditingteacher');
628 echo ('</td><td algin="left">');
630 // get the first teacheredit legacy
631 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
));
632 $teacher = reset($roles);
634 choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id
, 'new role', '', '0');
639 echo ('<tr><td align="right">');
640 echo '<label for="menudefaultstudent">'.get_string ('defaultcoursestudent').'</label>';
641 echo ('</td><td algin="left">');
643 // get the first teacheredit legacy
644 $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
));
645 $studentrole = array_shift($roles);
647 choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id
, 'new role', '', '0');
651 // 1.7 and above backup
652 $roles = restore_read_xml_roles($xml_file);
654 if (!empty($roles->roles
)) { // possible to have course with no roles
655 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
656 $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
659 foreach ($roles->roles
as $roleid=>$role) {
661 $mappableroles = $siteroleschoicearray;
663 echo ('<tr><td align="right">');
664 echo '<label for="menuroles_'.$roleid.'">'.$role->name
." (".($role->shortname
).")".'</label>';
665 echo ('</td><td align="left">');
667 /// first, we see if any exact role definition is found
668 /// if found, that is the only option of restoring to
670 if ($samerole = restore_samerole($roleid, $role)) {
671 $matchrole = $samerole->id
;
672 // if an exact role is found, it does not matter whether this user can assign this role or not,
673 // this will be presented as a valid option regardless
674 $mappableroles[$samerole->id
] = $allroles[$samerole->id
]->name
." (". $allroles[$samerole->id
]->shortname
.")";
676 // no exact role found, let's try to match shortname
677 // this is useful in situations where basic roles differ slightly in definition
679 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
680 if ($siteroleshortname == $role->shortname
) {
681 $matchrole = $siteroleid;
686 choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, 'new role', '', '0');
693 echo ('</table>'); // end of role mappings table
697 <div style=
"text-align:center">
699 /// Print captured hidden options, now that we have closed the table
700 echo $hidden_options;
702 <input type=
"hidden" name=
"id" value=
"<?php p($id) ?>" />
703 <input type
="hidden" name
="launch" value
="check" />
704 <input type
="hidden" name
="fromform" value
="1" />
705 <input type
="submit" value
="<?php print_string("continue") ?>" />
706 <input type
="submit" name
="cancel" value
="<?php print_string("cancel
") ?>" />
714 * Look for a role in the database with exactly the same definition as the one in the backup file.
716 * @param integer $roleid the id that the role in the backup files had on the old server.
717 * @param object $role the rest of the definition of the role from the backup file.
719 function restore_samerole($roleid, $rolefromxml) {
722 // First we try some intelligent guesses, then, if none of those work, we do a more extensive
725 // First guess, try let's use the id
726 if (restore_is_samerole($roleid, $rolefromxml)) {
727 return get_record('role', 'id', $roleid);
730 // Second guess, try the shortname
731 $testroleid = get_field('role', 'id', 'shortname', $rolefromxml->shortname
);
732 if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
733 return get_record('role', 'id', $testroleid);
736 // Finally, search all other roles. In orter to speed things up, we exclude the ones we have
737 // already tested, and we only search roles with the same number of capabilities set in their
739 $extracondition = '';
741 $extracondition = "AND roleid <> $testroleid";
743 $candidateroleids = get_records_sql("SELECT roleid
744 FROM {$CFG->prefix}role_capabilities
745 WHERE roleid <> $roleid $extracondition
747 HAVING COUNT(capability) = ".count($rolefromxml->capabilities
));
748 if (!empty($candidateroleids)) {
749 foreach ($candidateroleids as $testroleid => $notused) {
750 if (restore_is_samerole($testroleid, $rolefromxml)) {
751 return get_record('role', 'id', $testroleid);
760 * Compare a role in the database with one loaded from the backup file, and determine whether
761 * they have identical permissions for each capability.
762 * @param integer $testroleid the id of the role from the database to test against.
763 * @param object $rolefromxml the role definition loaded from the backup file.
764 * @return boolean true if the two roles are identical.
766 function restore_is_samerole($testroleid, $rolefromxml) {
767 // Load the role definition from the databse.
768 $rolefromdb = get_records('role_capabilities', 'roleid', $testroleid, '', 'capability,permission');
773 // Quick check, do they have the permissions on the same number of capabilities?
774 if (count($rolefromdb) != count($rolefromxml->capabilities
)) {
778 // If they do, check each one.
779 foreach ($rolefromdb as $capability => $permissions) {
780 if (!isset($rolefromxml->capabilities
[$capability]) ||
781 $permissions->permission
!= $rolefromxml->capabilities
[$capability]->permission
) {