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_restoreto)) {
109 if (!user_can_create_courses()) {
110 $restore_restoreto = 1;
112 $restore_restoreto = 2;
116 if (!isset($course_header->category
->id
)) {
117 $course_header->category
->id
= 0;
120 if(!isset($form1->startdate
)) {
121 $form1->startdate
= $course_header->course_startdate
; //$course_header->course_startdate;
124 if (empty($form1->shortname
)) {
125 $form1->shortname
= $course_header->course_shortname
; //'_shortname'; //$course_header->course_shortname;
128 if (empty($form1->fullname
)) {
129 $form1->fullname
= $course_header->course_fullname
; // '_fullname'; //$course_header->course_fullname;
133 notice("No restorable modules are installed!");
138 <script type=
"text/javascript">
140 function selectItemInMenuByName(formId
, menuName
, selectIndex
) {
141 myForm
= document
.getElementById(formId
)
142 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
143 myLen
= menuName
.length
;
144 myName
= myForm
.elements
[i
].name
;
145 myType
= myForm
.elements
[i
].type
;
146 if (myName
.substring(0,myLen
) == menuName
&& myType
== "select-one") {
147 myForm
.elements
[i
].options
[selectIndex
].selected
= true;
152 function selectItemInRadioByName(formId
, radioName
, selectIndex
) {
153 myForm
= document
.getElementById(formId
)
154 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
155 myLen
= radioName
.length
;
156 myName
= myForm
.elements
[i
].name
;
157 myType
= myForm
.elements
[i
].type
;
158 if (myName
.substring(0,myLen
) == radioName
&& myType
== "radio") {
159 myRadioGroup
= myForm
.elements
[myName
];
160 myRadioGroup
[selectIndex
].checked
= true;
165 function selectItemInCheckboxByName(formId
, checkName
, checked
) {
166 myForm
= document
.getElementById(formId
)
167 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
168 myLen
= checkName
.length
;
169 myName
= myForm
.elements
[i
].name
;
170 myType
= myForm
.elements
[i
].type
;
171 if (myName
.substring(0,myLen
) == checkName
&& myType
== "checkbox") {
172 myForm
.elements
[i
].checked
= checked
;
179 <form id=
"form1" method=
"post" action=
"restore.php">
181 <table cellpadding=
"5" class=
"boxaligncenter">
184 //First, course destination
187 echo "<td align=\"right\"><b>";
188 echo '<label for="menurestore_restoreto">'.get_string ('restoreto').'</label>';
190 echo "</td><td colspan=\"3\">";
193 // permission should have been checked already
196 * if user has manageactivities in any course, we show
197 * existingcoursedeleting
198 * existingcourseadding
200 * currentcoursedeleting
202 * if user has course:create in any category, we show
206 // Non-cached - get accessinfo
207 if (isset($USER->access
)) {
208 $accessinfo = $USER->access
;
210 $accessinfo = get_user_access_sitewide($USER->id
);
213 $mycourses = get_user_courses_bycap($USER->id
, 'moodle/site:restore', $accessinfo, true);
214 // if the user can manage 2 or more courses,
215 // or if the only course the user can manage is not the current course
216 // we show options for existing courses
218 if (count($mycourses) > 1) {
219 // if user can manage more than 1 course, or if user can restore to a single different course
220 $restore_restoreto_options[0] = get_string("existingcoursedeleting");
221 $restore_restoreto_options[1] = get_string("existingcourseadding");
222 // else if the user can write to current course
223 } else if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $id))){
224 $restore_restoreto_options[0] = get_string("currentcoursedeleting");
225 $restore_restoreto_options[1] = get_string("currentcourseadding");
228 // if user can create any course at all, give the option
229 if (user_can_create_courses()) {
230 $restore_restoreto_options[2] = get_string("newcourse");
233 /// Acummulator for hidden options and proper XHTML output
234 $hidden_options = '';
236 choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
238 if (user_can_create_courses()) { //display these fields conditionally
240 // find the list of cates user can edit
241 echo "<tr valign=\"top\" >";
242 echo "<td align=\"right\">";
243 echo '<label for="menurestore_restorecatto">'.get_string ('category').'</label>';
246 make_categories_list($categories, $parents);
247 choose_from_menu($categories, "restore_restorecatto", $course_header->category
->id
, "");
251 echo "<tr valign=\"top\" >";
252 echo "<td align=\"right\">";
253 echo '<label for="shortnamefield">'.get_string ('shortname').'</label>';
255 echo "<td><input type=\"text\" id=\"shortnamefield\" name=\"shortname\" maxlength=\"100\" size=\"20\" value=\"$form1->shortname\" alt=\"".get_string("shortname")."\" />" ;
256 helpbutton("courseshortname", get_string("shortname")) ;
257 if (isset($err["shortname"])) formerr($err["shortname"]);
260 echo "<tr valign=\"top\" >";
261 echo "<td align=\"right\">";
262 echo '<label for="fullnamefield">'.get_string ('fullname').'</label>';
264 echo "<td><input type=\"text\" id=\"fullnamefield\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"$form1->fullname\" alt=\" ".get_string("fullname")."\" />" ;
265 helpbutton("coursefullname", get_string("fullname")) ;
266 if (isset($err["fullname"])) formerr($err["fullname"]);
268 echo "<tr valign=\"top\"> ";
269 echo "<td align=\"right\"> ";
270 print_string("startdate");
272 /// Show the roll dates option only if the backup course has a start date
273 /// (some formats like main page, social..., haven't it and rolling dates
274 /// from 0 produces crazy dates. MDL-10125
275 if ($form1->startdate
) {
276 print_date_selector("startday", "startmonth", "startyear", $form1->startdate
);
278 print_string('notavailable');
279 echo '<input type="hidden" name="startyear" value="0" />';
280 echo '<input type="hidden" name="startmonth" value="0" />';
281 echo '<input type="hidden" name="startday" value="0" />';
283 helpbutton("coursestartdate", get_string("startdate"));
287 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
288 //Now, check modules and info and show posibilities
289 if ($allmods = get_records("modules") ) {
290 //Print option to select/deselect everything with 1 click.
292 echo "<td align=\"right\">";
293 echo '<b>'.get_string("include")."</b>";
295 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
296 get_string("all")."</a>/";
297 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
298 get_string("none")."</a>";
300 echo "<td align=\"right\">";
301 echo '<b> </b>';
303 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
304 get_string("all")."</a>/";
305 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
306 get_string("none")."</a>";
309 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
312 foreach ($allmods as $mod) {
313 $modname = $mod->name
;
314 $modrestore = $modname."_restore_mods";
315 //If exists the lib & function
316 $exist = "exists_".$modname;
317 $restore_var = "restore_".$modname;
318 $user_info_var = "restore_user_info_".$modname;
319 if (isset($
$exist)) {
321 //Now check that we have that module info in the backup file
322 if (isset($info->mods
[$modname]) && $info->mods
[$modname]->backup
== "true") {
324 echo "<tr class=\"r".$currentrow."\">";
325 echo "<td align=\"right\"> ";
327 $restore_options[1] = get_string("yes");
328 $restore_options[0] = get_string("no");
329 //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
330 //choose_from_radio($restore_options, $restore_var, $$restore_var);
332 print_checkbox($restore_var, $
$restore_var, $
$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
333 //If backup contains user data, then show menu, else fix it to
335 echo "</td><td align=\"right\"> ";
337 if ($info->mods
[$modname]->userinfo
== "true") {
338 $restore_user_options[1] = get_string("yes");
339 $restore_user_options[0] = get_string("no");
340 //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
341 //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
342 print_checkbox($user_info_var, $
$user_info_var, $
$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
344 //Module haven't userdata
345 echo get_string("withoutuserdata");
346 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
349 if (isset($info->mods
[$modname]->instances
)) {
350 $instances = $info->mods
[$modname]->instances
;
352 if (!empty($instances) && is_array($instances)) {
353 echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
354 foreach ($instances as $instance) {
356 $var = 'restore_'.$modname.'_instance_'.$instance->id
;
357 $
$var = optional_param($var,1);
358 print_checkbox($var,$
$var,$
$var,$instance->name
,$instance->name
,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
359 echo '</td><td align="right"> ';
360 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id
;
361 $
$var = optional_param($var,1);
362 if ($info->mods
[$modname]->instances
[$instance->id
]->userinfo
== 'true') {
363 print_checkbox($var,$
$var,$
$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
365 echo '<input type="hidden" name="'.$var.'" value="0" />';
369 echo '</table></td></tr>';
372 //Module isn't restorable
373 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
374 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
377 //Module isn't restorable
378 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
379 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
382 //Module isn't restorable
383 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
384 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
386 $currentrow = ($currentrow +
1) %
2;
389 echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
391 //Now print the Metacourse tr
393 echo "<td align=\"right\" colspan=\"2\"><b>";
394 echo '<label for="menurestore_metacourse">'.get_string ("metacourse").'</label>';
395 echo "</b></td><td colspan=\"2\">";
396 //If metacourse are in the backup file, show menu, else fixed to no
397 if ($info->backup_metacourse
== "true") {
398 $metacourse_options = array();
399 $metacourse_options[0] = get_string("no");
400 $metacourse_options[1] = get_string("yes");
401 choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
403 echo get_string("no");
404 echo "<input type=\"hidden\" id=\"menurestore_metacourse\" name=\"restore_metacourse\" value=\"0\" />";
407 //Now print the Users tr
409 echo "<td align=\"right\" colspan=\"2\"><b>";
410 echo '<label for="menurestore_users">'.get_string ("users").'</label>';
411 echo "</b></td><td colspan=\"2\">";
412 //If some user is present in the backup file
413 if ($info->backup_users
== "all" or $info->backup_users
== "course") {
414 $user_options = array();
415 //If all users are in the backup file
416 if ($info->backup_users
== "all") {
417 $user_options[0] = get_string("all");
419 $user_options[1] = get_string("course");
420 $user_options[2] = get_string("none");
421 choose_from_menu($user_options, "restore_users", $restore_users, "");
423 echo get_string("none");
424 echo "<input type=\"hidden\" id=\"menurestore_users\" name=\"restore_users\" value=\"2\" />";
429 //Now print the Groups tr (assume there is no $info->backup_groups)
431 echo "<td align=\"right\" colspan=\"2\"><b>";
432 $helplink = helpbutton('grouprestore', get_string('groups'), '', true, false, '', true);
433 if (empty($CFG->enablegroupings
)) {
434 echo '<label for="menurestore_groups">'.get_string ("groups").'</label>'.$helplink;
435 echo "</b></td><td colspan=\"2\">";
436 $group_options[RESTORE_GROUPS_NONE
] = get_string('no');
437 $group_options[RESTORE_GROUPS_ONLY
] = get_string('yes');
439 echo '<label for="menurestore_groups">'.get_string ('groupsgroupings', 'group').'</label>'.$helplink;
440 echo "</b></td><td colspan=\"2\">";
441 $group_options[RESTORE_GROUPS_NONE
] = get_string('none');
442 $group_options[RESTORE_GROUPS_ONLY
] = get_string('groupsonly', 'group');
443 $group_options[RESTORE_GROUPINGS_ONLY
] = get_string('groupingsonly', 'group');
444 $group_options[RESTORE_GROUPS_GROUPINGS
] = get_string('groupsgroupings', 'group'); //all.
447 echo get_string('none');
448 echo "<input type=\"hidden\" name=\"restore_groups\" value=\"2\" />";
450 choose_from_menu($group_options, 'restore_groups', $restore_groups, '');
453 //Now print the Logs tr
455 echo "<td align=\"right\" colspan=\"2\"><b>";
456 echo '<label for="menurestore_logs">'.get_string ("logs").'</label>';
457 echo "</b></td><td colspan=\"2\">";
458 //If logs are in the backup file, show menu, else fixed to no
459 if ($info->backup_logs
== "true") {
460 $log_options = array();
461 $log_options[0] = get_string("no");
462 $log_options[1] = get_string("yes");
463 choose_from_menu($log_options, "restore_logs", $restore_logs, "");
465 echo get_string("no");
466 echo "<input type=\"hidden\" id=\"menurestore_logs\" name=\"restore_logs\" value=\"0\" />";
470 //Now print the User Files tr
472 echo "<td align=\"right\" colspan=\"2\"><b>";
473 echo '<label for="menurestore_user_files">'.get_string ("userfiles").'</label>';
474 echo "</b></td><td colspan=\"2\">";
475 //If user files are in the backup file, show menu, else fixed to no
476 if ($info->backup_user_files
== "true") {
477 $user_file_options = array();
478 $user_file_options[0] = get_string("no");
479 $user_file_options[1] = get_string("yes");
480 choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
482 echo get_string("no");
483 echo "<input type=\"hidden\" id=\"menurestore_user_files\" name=\"restore_user_files\" value=\"0\" />";
487 //Now print the Course Files tr
489 echo "<td align=\"right\" colspan=\"2\"><b>";
490 echo '<label for="menurestore_course_files">'.get_string ("coursefiles").'</label>';
491 echo "</b></td><td colspan=\"2\">";
492 echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
493 echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
494 //If course files are in the backup file, show menu, else fixed to no
495 if ($info->backup_course_files
== "true") {
496 $course_file_options = array();
497 $course_file_options[0] = get_string("no");
498 $course_file_options[1] = get_string("yes");
499 choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
501 echo get_string("no");
502 echo "<input type=\"hidden\" id=\"menurestore_course_files\" name=\"restore_course_files\" value=\"0\" />";
507 //Now print the Site Files tr
509 echo "<td align=\"right\" colspan=\"2\"><b>";
510 echo '<label for="menurestore_site_files">'.get_string ("sitefiles").'</label>';
511 echo "</b></td><td colspan=\"2\">";
512 //If site files are in the backup file, show menu, else fixed to no
513 if (isset($info->backup_site_files
) && $info->backup_site_files
== "true") {
514 $site_file_options = array();
515 $site_file_options[0] = get_string("no");
516 $site_file_options[1] = get_string("yes");
517 choose_from_menu($site_file_options, "restore_site_files", $restore_site_files, "");
519 echo get_string("no");
520 echo "<input type=\"hidden\" id=\"menurestore_site_files\" name=\"restore_site_files\" value=\"0\" />";
524 // do you want grade histories to be restored?
525 if (empty($CFG->disablegradehistory
)) {
527 echo "<td align=\"right\" colspan=\"2\"><b>";
528 echo '<label for="menurestore_gradebook_history">'.get_string ('gradebookhistories', 'grades').'</label>';
529 echo "</b></td><td colspan=\"2\">";
530 if (isset($info->gradebook_histories
) && $info->gradebook_histories
== "true") {
531 $gradebook_history_options = array();
532 $gradebook_history_options[0] = get_string("no");
533 $gradebook_history_options[1] = get_string("yes");
534 choose_from_menu($gradebook_history_options, "restore_gradebook_history", $restore_gradebook_history, "");
536 echo get_string("no");
537 echo "<input type=\"hidden\" id=\"menurestore_gradebook_history\" name=\"restore_gradebook_history\" value=\"0\" />";
541 $hidden_options .= '<input type="hidden" name="restore_gradebook_history" value="0" />';
545 //This tr is slighty different. Everything becomes hidden if
546 //we haven't messages is the backup, to avoid confusions to users.
547 //If messages are in the backup file, show menu, else fixed to no and show nothing
548 if (isset($info->backup_messages
) && $info->backup_messages
== "true") {
550 echo "<td align=\"right\" colspan=\"2\"><b>";
551 echo '<label for="menurestore_messages">'.get_string ('messages', 'messages').'</label>';
552 echo "</b></td><td colspan=\"2\">";
553 $message_options = array();
554 $message_options[0] = get_string("no");
555 $message_options[1] = get_string("yes");
556 choose_from_menu($message_options, "restore_messages", $restore_messages, "");
559 $hidden_options .= '<input type="hidden" name="restore_messages" value="0" />';
569 print_heading(get_string('rolemappings'));
570 $xml_file = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/moodle.xml";
572 $info = restore_read_xml_info($xml_file);
574 // fix for MDL-9068, front page course is just a normal course
575 $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "shortname");
576 $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "name");
577 $allroles = get_records('role');
579 echo ('<table width="100%" class="restore-form-instances">');
580 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>');
582 if ($info->backup_moodle_version
< 2006092801) {
583 // 1.6 and below backup
586 echo ('<tr><td align="right">');
587 echo '<label for="menudefaultteacheredit">'.get_string ('defaultcourseteacher').'</label>';
588 echo ('</td><td algin="left">');
590 // get the first teacheredit legacy
591 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
));
593 $editteacher = reset($roles);
594 choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id
, 'new role', '', '0');
597 /// Non-editting teacher
598 echo ('<tr><td align="right">');
599 echo '<label for="menudefaultteacher">'.get_string ('noneditingteacher').'</label>';
600 print_string('noneditingteacher');
601 echo ('</td><td algin="left">');
603 // get the first teacheredit legacy
604 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
));
605 $teacher = reset($roles);
607 choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id
, 'new role', '', '0');
612 echo ('<tr><td align="right">');
613 echo '<label for="menudefaultstudent">'.get_string ('defaultcoursestudent').'</label>';
614 echo ('</td><td algin="left">');
616 // get the first teacheredit legacy
617 $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
));
618 $studentrole = array_shift($roles);
620 choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id
, 'new role', '', '0');
624 // 1.7 and above backup
625 $roles = restore_read_xml_roles($xml_file);
627 if (!empty($roles->roles
)) { // possible to have course with no roles
628 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
629 $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
632 foreach ($roles->roles
as $roleid=>$role) {
634 $mappableroles = $siteroleschoicearray;
636 echo ('<tr><td align="right">');
637 echo '<label for="menuroles_'.$roleid.'">'.$role->name
." (".($role->shortname
).")".'</label>';
638 echo ('</td><td align="left">');
640 /// first, we see if any exact role definition is found
641 /// if found, that is the only option of restoring to
643 if ($samerole = restore_samerole($roleid, $role)) {
644 $matchrole = $samerole->id
;
645 // if an exact role is found, it does not matter whether this user can assign this role or not,
646 // this will be presented as a valid option regardless
647 $mappableroles[$samerole->id
] = $allroles[$samerole->id
]->name
." (". $allroles[$samerole->id
]->shortname
.")";
649 // no exact role found, let's try to match shortname
650 // this is useful in situations where basic roles differ slightly in definition
652 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
653 if ($siteroleshortname == $role->shortname
) {
654 $matchrole = $siteroleid;
659 choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, 'new role', '', '0');
666 echo ('</table>'); // end of role mappings table
670 <div style=
"text-align:center">
672 /// Print captured hidden options, now that we have closed the table
673 echo $hidden_options;
675 <input type=
"hidden" name=
"id" value=
"<?php p($id) ?>" />
676 <input type
="hidden" name
="launch" value
="check" />
677 <input type
="hidden" name
="fromform" value
="1" />
678 <input type
="submit" value
="<?php print_string("continue") ?>" />
679 <input type
="submit" name
="cancel" value
="<?php print_string("cancel
") ?>" />
687 * Look for a role in the database with exactly the same definition as the one in the backup file.
689 * @param integer $roleid the id that the role in the backup files had on the old server.
690 * @param object $role the rest of the definition of the role from the backup file.
692 function restore_samerole($roleid, $rolefromxml) {
695 // First we try some intelligent guesses, then, if none of those work, we do a more extensive
698 // First guess, try let's use the id
699 if (restore_is_samerole($roleid, $rolefromxml)) {
700 return get_record('role', 'id', $roleid);
703 // Second guess, try the shortname
704 $testroleid = get_field('role', 'id', 'shortname', $rolefromxml->shortname
);
705 if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
706 return get_record('role', 'id', $testroleid);
709 // Finally, search all other roles. In orter to speed things up, we exclude the ones we have
710 // already tested, and we only search roles with the same number of capabilities set in their
712 $extracondition = '';
714 $extracondition = "AND roleid <> $testroleid";
716 $candidateroleids = get_records_sql("SELECT roleid
717 FROM {$CFG->prefix}role_capabilities
718 WHERE roleid <> $roleid $extracondition
720 HAVING COUNT(capability) = ".count($rolefromxml->capabilities
));
721 if (!empty($candidateroleids)) {
722 foreach ($candidateroleids as $testroleid => $notused) {
723 if (restore_is_samerole($testroleid, $rolefromxml)) {
724 return get_record('role', 'id', $testroleid);
733 * Compare a role in the database with one loaded from the backup file, and determine whether
734 * they have identical permissions for each capability.
735 * @param integer $testroleid the id of the role from the database to test against.
736 * @param object $rolefromxml the role definition loaded from the backup file.
737 * @return boolean true if the two roles are identical.
739 function restore_is_samerole($testroleid, $rolefromxml) {
740 // Load the role definition from the databse.
741 $rolefromdb = get_records('role_capabilities', 'roleid', $testroleid, '', 'capability,permission');
746 // Quick check, do they have the permissions on the same number of capabilities?
747 if (count($rolefromdb) != count($rolefromxml->capabilities
)) {
751 // If they do, check each one.
752 foreach ($rolefromdb as $capability => $permissions) {
753 if (!isset($rolefromxml->capabilities
[$capability]) ||
754 $permissions->permission
!= $rolefromxml->capabilities
[$capability]->permission
) {