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' );
26 //Init restoreuserinfo
27 $restoreuserinfo = false;
31 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $id))) {
32 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
34 $restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_COURSE
, $id));
36 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM
))) {
37 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
39 $restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_SYSTEM
));
43 if (!$site = get_site()) {
44 error("Site not found!");
47 //Checks for the required files/functions to restore every mod
49 if ($allmods = get_records("modules") ) {
50 foreach ($allmods as $mod) {
51 $modname = $mod->name
;
52 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
53 $modrestore = $modname."_restore_mods";
54 if (file_exists($modfile)) {
55 include_once($modfile);
56 if (function_exists($modrestore)) {
57 $var = "exists_".$modname;
64 $var = "restore_".$modname;
68 //Check include user info
69 $var = "restore_user_info_".$modname;
70 if (!isset($
$var) && $restoreuserinfo) {
78 //Check other parameters
79 if (!isset($restore_metacourse)) {
80 $restore_metacourse = 1;
83 if (!isset($restore_users)) {
87 if (!isset($restore_groups)) {
88 if (empty($CFG->enablegroupings
)) {
89 $restore_groups = RESTORE_GROUPS_ONLY
;
91 $restore_groups = RESTORE_GROUPS_GROUPINGS
;
95 if (!isset($restore_logs)) {
99 if (!isset($restore_user_files)) {
100 $restore_user_files = 1;
103 if (!isset($restore_course_files)) {
104 $restore_course_files = 1;
107 if (!isset($restore_site_files)) {
108 $restore_site_files = 1;
111 if (!isset($restore_messages)) {
112 $restore_messages = 1;
115 if (!isset($restore_blogs)) {
119 $cancreatecourses = user_can_create_courses();
120 if (!isset($restore_restoreto)) {
121 if (!$cancreatecourses) {
122 $restore_restoreto = RESTORETO_CURRENT_ADDING
;
124 $restore_restoreto = RESTORETO_NEW_COURSE
;
128 if (!isset($course_header->category
->id
)) {
129 $course_header->category
->id
= 0;
132 if(!isset($form1->startdate
)) {
133 $form1->startdate
= $course_header->course_startdate
; //$course_header->course_startdate;
136 if (empty($form1->shortname
)) {
137 $form1->shortname
= $course_header->course_shortname
; //'_shortname'; //$course_header->course_shortname;
140 if (empty($form1->fullname
)) {
141 $form1->fullname
= $course_header->course_fullname
; // '_fullname'; //$course_header->course_fullname;
145 notice("No restorable modules are installed!");
150 <script type=
"text/javascript">
152 function selectItemInMenuByName(formId
, menuName
, selectIndex
) {
153 myForm
= document
.getElementById(formId
)
154 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
155 myLen
= menuName
.length
;
156 myName
= myForm
.elements
[i
].name
;
157 myType
= myForm
.elements
[i
].type
;
158 if (myName
.substring(0,myLen
) == menuName
&& myType
== "select-one") {
159 myForm
.elements
[i
].options
[selectIndex
].selected
= true;
164 function selectItemInRadioByName(formId
, radioName
, selectIndex
) {
165 myForm
= document
.getElementById(formId
)
166 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
167 myLen
= radioName
.length
;
168 myName
= myForm
.elements
[i
].name
;
169 myType
= myForm
.elements
[i
].type
;
170 if (myName
.substring(0,myLen
) == radioName
&& myType
== "radio") {
171 myRadioGroup
= myForm
.elements
[myName
];
172 myRadioGroup
[selectIndex
].checked
= true;
177 function selectItemInCheckboxByName(formId
, checkName
, checked
) {
178 myForm
= document
.getElementById(formId
)
179 for (i
=0,n
=myForm
.elements
.length
;i
<n
;i
++) {
180 myLen
= checkName
.length
;
181 myName
= myForm
.elements
[i
].name
;
182 myType
= myForm
.elements
[i
].type
;
183 if (myName
.substring(0,myLen
) == checkName
&& myType
== "checkbox") {
184 myForm
.elements
[i
].checked
= checked
;
191 <form id=
"form1" method=
"post" action=
"restore.php">
193 <table cellpadding=
"5" class=
"boxaligncenter">
196 //First, course destination
199 echo "<td align=\"right\"><b>";
200 echo '<label for="menurestore_restoreto">'.get_string ('restoreto').'</label>';
202 echo "</td><td colspan=\"3\">";
204 // permission should have been checked already
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);
215 // if the user can restore to current course, grant the "current" options
216 if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $id))){
217 $restore_restoreto_options[RESTORETO_CURRENT_DELETING
] = get_string("currentcoursedeleting");
218 $restore_restoreto_options[RESTORETO_CURRENT_ADDING
] = get_string("currentcourseadding");
221 // if user can create any course, grant the "new course" option
222 if ($cancreatecourses) {
223 $restore_restoreto_options[RESTORETO_NEW_COURSE
] = get_string("newcourse");
226 // if the user can restore to 2 or more courses (or to just 1 but different from current), grant the "existing" options
227 if (count($mycourses) > 1 ||
(count($mycourses) == 1 && $mycourses[0]->id
!= $id)) {
228 $restore_restoreto_options[RESTORETO_EXISTING_DELETING
] = get_string("existingcoursedeleting");
229 $restore_restoreto_options[RESTORETO_EXISTING_ADDING
] = get_string("existingcourseadding");
232 /// Acummulator for hidden options and proper XHTML output
233 $hidden_options = '';
235 choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
237 if ($cancreatecourses) { //display these fields conditionally
239 // find the list of cates user can edit
240 echo "<tr valign=\"top\" >";
241 echo "<td align=\"right\">";
242 echo '<label for="menurestore_restorecatto">'.get_string ('category').'</label>';
245 // Get the list of categories where the user can create courses
246 make_categories_list($categories, $parents, 'moodle/course:create');
247 $category_default = null;
248 // Best default 1: restoring in same site, use category of original course if available
249 if (backup_is_same_site($info) &&
250 !empty($course_header->category
->id
) &&
251 array_key_exists($course_header->category
->id
, $categories)) {
252 $category_default = $course_header->category
->id
;
253 // Best default 2: restore to the category of the course we are restoring from if available
255 $fromcat = get_field('course', 'category', 'id', $id);
256 if (!empty($fromcat) &&
257 array_key_exists($fromcat, $categories)) {
258 $category_default = $fromcat;
261 choose_from_menu($categories, 'restore_restorecatto', $category_default, "");
265 echo "<tr valign=\"top\" >";
266 echo "<td align=\"right\">";
267 echo '<label for="shortnamefield">'.get_string ('shortname').'</label>';
269 echo "<td><input type=\"text\" id=\"shortnamefield\" name=\"shortname\" maxlength=\"100\" size=\"20\" value=\"".s($form1->shortname
)."\" alt=\"".get_string("shortname")."\" />" ;
270 helpbutton("courseshortname", get_string("shortname")) ;
271 if (isset($err["shortname"])) formerr($err["shortname"]);
274 echo "<tr valign=\"top\" >";
275 echo "<td align=\"right\">";
276 echo '<label for="fullnamefield">'.get_string ('fullname').'</label>';
278 echo "<td><input type=\"text\" id=\"fullnamefield\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"".s($form1->fullname
)."\" alt=\" ".get_string("fullname")."\" />" ;
279 helpbutton("coursefullname", get_string("fullname")) ;
280 if (isset($err["fullname"])) formerr($err["fullname"]);
283 // nothing to show here if the user cannot create users
286 // If user can roll dates (in any course is enough for now) show the roll dates option.
287 // Important: The final restore check step will validate, once we know the final category/course
288 // where the restore is happening, if the user is really capable of rolling dates there, noticing
289 // and disabling roll if not allowed.
290 if (restore_user_can_roll_dates()) {
291 echo "<tr valign=\"top\"> ";
292 echo "<td align=\"right\"> ";
293 print_string("startdate");
295 /// Show the roll dates option only if the backup course has a start date
296 /// (some formats like main page, social..., haven't it and rolling dates
297 /// from 0 produces crazy dates. MDL-10125
298 if ($form1->startdate
) {
299 print_date_selector("startday", "startmonth", "startyear", $form1->startdate
);
300 helpbutton("coursestartdate", get_string("startdate"));
302 print_string('notavailable');
303 echo '<input type="hidden" name="startyear" value="0" />';
304 echo '<input type="hidden" name="startmonth" value="0" />';
305 echo '<input type="hidden" name="startday" value="0" />';
309 /// If user lacks the moodle/restore:rolldates completely, prevent any change in startyear/month/day (so restore won't perform any roll)
310 echo '<input type="hidden" name="startyear" value="0" />';
311 echo '<input type="hidden" name="startmonth" value="0" />';
312 echo '<input type="hidden" name="startday" value="0" />';
315 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
316 //Now, check modules and info and show posibilities
317 if ($allmods = get_records("modules") ) {
318 //Print option to select/deselect everything with 1 click.
320 echo "<td align=\"right\">";
321 echo '<b>'.get_string("include")."</b>";
323 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
324 get_string("all")."</a>/";
325 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
326 get_string("none")."</a>";
328 if ($restoreuserinfo) {
329 echo "<td align=\"right\">";
330 echo '<b> </b>';
332 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
333 get_string("all")."</a>/";
334 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
335 get_string("none")."</a>";
338 echo "<td colspan=\"2\"> </td>";
341 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
344 foreach ($allmods as $mod) {
345 $modname = $mod->name
;
346 $modrestore = $modname."_restore_mods";
347 //If exists the lib & function
348 $exist = "exists_".$modname;
349 $restore_var = "restore_".$modname;
350 $user_info_var = "restore_user_info_".$modname;
351 if (isset($
$exist)) {
353 //Now check that we have that module info in the backup file
354 if (isset($info->mods
[$modname]) && $info->mods
[$modname]->backup
== "true") {
356 echo "<tr class=\"r".$currentrow."\">";
357 echo "<td align=\"right\"> ";
359 $restore_options[1] = get_string("yes");
360 $restore_options[0] = get_string("no");
361 //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
362 //choose_from_radio($restore_options, $restore_var, $$restore_var);
364 print_checkbox($restore_var, $
$restore_var, $
$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
365 //If backup contains user data, then show menu, else fix it to
367 echo "</td><td align=\"right\"> ";
369 if ($info->mods
[$modname]->userinfo
== "true" && $restoreuserinfo) {
370 $restore_user_options[1] = get_string("yes");
371 $restore_user_options[0] = get_string("no");
372 //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
373 //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
374 print_checkbox($user_info_var, $
$user_info_var, $
$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
376 //Module haven't userdata
377 echo get_string("withoutuserdata");
378 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
381 if (isset($info->mods
[$modname]->instances
)) {
382 $instances = $info->mods
[$modname]->instances
;
384 if (!empty($instances) && is_array($instances)) {
385 echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
386 foreach ($instances as $instance) {
388 $var = 'restore_'.$modname.'_instance_'.$instance->id
;
389 $
$var = optional_param($var,1);
390 print_checkbox($var,$
$var,$
$var,$instance->name
,$instance->name
,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
391 echo '</td><td align="right"> ';
392 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id
;
393 $
$var = optional_param($var,1);
394 if (!empty($info->mods
[$modname]->instances
) && ($info->mods
[$modname]->instances
[$instance->id
]->userinfo
== 'true') && $restoreuserinfo) {
395 print_checkbox($var,$
$var,$
$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
397 echo '<input type="hidden" name="'.$var.'" value="0" />';
401 echo '</table></td></tr>';
404 //Module isn't restorable
405 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
406 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
409 //Module isn't restorable
410 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
411 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
414 //Module isn't restorable
415 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
416 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
418 $currentrow = ($currentrow +
1) %
2;
421 echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
423 //Now print the Metacourse tr
425 echo "<td align=\"right\" colspan=\"2\"><b>";
426 echo '<label for="menurestore_metacourse">'.get_string ("metacourse").'</label>';
427 echo "</b></td><td colspan=\"2\">";
428 //If metacourse are in the backup file, show menu, else fixed to no
429 if ($info->backup_metacourse
== "true") {
430 $metacourse_options = array();
431 $metacourse_options[0] = get_string("no");
432 $metacourse_options[1] = get_string("yes");
433 choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
435 echo get_string("no");
436 echo "<input type=\"hidden\" id=\"menurestore_metacourse\" name=\"restore_metacourse\" value=\"0\" />";
439 //Now print the Users tr
441 echo "<td align=\"right\" colspan=\"2\"><b>";
442 echo '<label for="menurestore_users">'.get_string ("users").'</label>';
443 echo "</b></td><td colspan=\"2\">";
444 //If some user is present in the backup file
445 if (($info->backup_users
== "all" or $info->backup_users
== "course") and $restoreuserinfo) {
446 $user_options = array();
447 //If all users are in the backup file and user has 'moodle/site:restore' at system level
448 if ($info->backup_users
== "all" and has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM
))) {
449 $user_options[0] = get_string("all");
451 $user_options[1] = get_string("course");
452 $user_options[2] = get_string("none");
453 choose_from_menu($user_options, "restore_users", $restore_users, "");
455 echo get_string("none");
456 echo "<input type=\"hidden\" id=\"menurestore_users\" name=\"restore_users\" value=\"2\" />";
461 //Now print the Groups tr (assume there is no $info->backup_groups)
463 echo "<td align=\"right\" colspan=\"2\"><b>";
464 $helplink = helpbutton('grouprestore', get_string('groups'), '', true, false, '', true);
465 echo '<label for="menurestore_groups">'.get_string ("groups").'</label>'.$helplink;
466 echo "</b></td><td colspan=\"2\">";
467 if (empty($CFG->enablegroupings
)) {
468 $group_options[RESTORE_GROUPS_NONE
] = get_string('no');
469 $group_options[RESTORE_GROUPS_ONLY
] = get_string('yes');
471 $group_options[RESTORE_GROUPS_NONE
] = get_string('none');
472 $group_options[RESTORE_GROUPS_ONLY
] = get_string('groupsonly', 'group');
473 $group_options[RESTORE_GROUPINGS_ONLY
] = get_string('groupingsonly', 'group');
474 $group_options[RESTORE_GROUPS_GROUPINGS
] = get_string('groupsgroupings', 'group'); //all.
476 choose_from_menu($group_options, 'restore_groups', $restore_groups, '');
479 //Now print the Logs tr
481 echo "<td align=\"right\" colspan=\"2\"><b>";
482 echo '<label for="menurestore_logs">'.get_string ("logs").'</label>';
483 echo "</b></td><td colspan=\"2\">";
484 //If logs are in the backup file, show menu, else fixed to no
485 if ($info->backup_logs
== "true" and $restoreuserinfo) {
486 $log_options = array();
487 $log_options[0] = get_string("no");
488 $log_options[1] = get_string("yes");
489 choose_from_menu($log_options, "restore_logs", $restore_logs, "");
491 echo get_string("no");
492 echo "<input type=\"hidden\" id=\"menurestore_logs\" name=\"restore_logs\" value=\"0\" />";
496 //Now print the User Files tr
498 echo "<td align=\"right\" colspan=\"2\"><b>";
499 echo '<label for="menurestore_user_files">'.get_string ("userfiles").'</label>';
500 echo "</b></td><td colspan=\"2\">";
501 //If user files are in the backup file, show menu, else fixed to no
502 if ($info->backup_user_files
== "true" and $restoreuserinfo) {
503 $user_file_options = array();
504 $user_file_options[0] = get_string("no");
505 $user_file_options[1] = get_string("yes");
506 choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
508 echo get_string("no");
509 echo "<input type=\"hidden\" id=\"menurestore_user_files\" name=\"restore_user_files\" value=\"0\" />";
513 //Now print the Course Files tr
515 echo "<td align=\"right\" colspan=\"2\"><b>";
516 echo '<label for="menurestore_course_files">'.get_string ("coursefiles").'</label>';
517 echo "</b></td><td colspan=\"2\">";
518 echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
519 echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
520 //If course files are in the backup file, show menu, else fixed to no
521 if ($info->backup_course_files
== "true") {
522 $course_file_options = array();
523 $course_file_options[0] = get_string("no");
524 $course_file_options[1] = get_string("yes");
525 choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
527 echo get_string("no");
528 echo "<input type=\"hidden\" id=\"menurestore_course_files\" name=\"restore_course_files\" value=\"0\" />";
533 //Now print the Site Files tr
535 echo "<td align=\"right\" colspan=\"2\"><b>";
536 echo '<label for="menurestore_site_files">'.get_string ("sitefiles").'</label>';
537 echo "</b></td><td colspan=\"2\">";
538 //If site files are in the backup file, show menu, else fixed to no
539 if (isset($info->backup_site_files
) && $info->backup_site_files
== "true") {
540 $site_file_options = array();
541 $site_file_options[0] = get_string("no");
542 $site_file_options[1] = get_string("yes");
543 choose_from_menu($site_file_options, "restore_site_files", $restore_site_files, "");
545 echo get_string("no");
546 echo "<input type=\"hidden\" id=\"menurestore_site_files\" name=\"restore_site_files\" value=\"0\" />";
550 // do you want grade histories to be restored?
551 if (empty($CFG->disablegradehistory
) and $restoreuserinfo) {
553 echo "<td align=\"right\" colspan=\"2\"><b>";
554 echo '<label for="menurestore_gradebook_history">'.get_string ('gradebookhistories', 'grades').'</label>';
555 echo "</b></td><td colspan=\"2\">";
556 if (isset($info->gradebook_histories
) && $info->gradebook_histories
== "true") {
557 $gradebook_history_options = array();
558 $gradebook_history_options[0] = get_string("no");
559 $gradebook_history_options[1] = get_string("yes");
560 choose_from_menu($gradebook_history_options, "restore_gradebook_history", $restore_gradebook_history, "");
562 echo get_string("no");
563 echo "<input type=\"hidden\" id=\"menurestore_gradebook_history\" name=\"restore_gradebook_history\" value=\"0\" />";
567 $hidden_options .= '<input type="hidden" name="restore_gradebook_history" value="0" />';
570 //This tr is slighty different. Everything becomes hidden if
571 //we haven't messages is the backup, to avoid confusions to users.
572 //If messages are in the backup file, show menu, else fixed to no and show nothing
573 //Also, messaging must be enabled in the destination site
574 if (isset($info->backup_messages
) && $info->backup_messages
== "true" && !empty($CFG->messaging
) and $restoreuserinfo) {
576 echo "<td align=\"right\" colspan=\"2\"><b>";
577 echo '<label for="menurestore_messages">'.get_string ('messages', 'message').'</label>';
578 echo "</b></td><td colspan=\"2\">";
579 $message_options = array();
580 $message_options[0] = get_string("no");
581 $message_options[1] = get_string("yes");
582 choose_from_menu($message_options, "restore_messages", $restore_messages, "");
585 $hidden_options .= '<input type="hidden" name="restore_messages" value="0" />';
588 //This tr is slighty different. Everything becomes hidden if
589 //we haven't blogs is the backup, to avoid confusions to users.
590 //If blogs are in the backup file, show menu, else fixed to no and show nothing
591 //Also, blogs must be enabled in the destination site
592 if (isset($info->backup_blogs
) && $info->backup_blogs
== "true" && !empty($CFG->bloglevel
) and $restoreuserinfo) {
594 echo "<td align=\"right\" colspan=\"2\"><b>";
595 echo '<label for="menurestore_blogs">'.get_string ('blogs', 'blog').'</label>';
596 echo "</b></td><td colspan=\"2\">";
597 $blog_options = array();
598 $blog_options[0] = get_string("no");
599 $blog_options[1] = get_string("yes");
600 choose_from_menu($blog_options, "restore_blogs", $restore_blogs, "");
603 $hidden_options .= '<input type="hidden" name="restore_blogs" value="0" />';
612 // Only show the roles mapping if restore of user info is allowed
613 if ($restoreuserinfo) {
614 print_heading(get_string('rolemappings'));
615 $xml_file = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/moodle.xml";
617 $info = restore_read_xml_info($xml_file);
619 // fix for MDL-9068, front page course is just a normal course
620 $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "shortname", ROLENAME_ORIGINAL
);
621 $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE
, $course->id
), "name", ROLENAME_ORIGINAL
);
622 $allroles = get_records('role');
624 echo ('<table width="100%" class="restore-form-instances">');
626 if ($info->backup_moodle_version
< 2006092801) {
627 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>');
629 // 1.6 and below backup
632 echo ('<tr><td align="right">');
633 echo '<label for="menudefaultteacheredit">'.get_string ('defaultcourseteacher').'</label>';
634 echo ('</td><td algin="left">');
636 // get the first teacheredit legacy
637 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
));
639 $editteacher = reset($roles);
640 choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id
, 'new role', '', '0');
643 /// Non-editting teacher
644 echo ('<tr><td align="right">');
645 echo '<label for="menudefaultteacher">'.get_string ('noneditingteacher').'</label>';
646 print_string('noneditingteacher');
647 echo ('</td><td algin="left">');
649 // get the first teacheredit legacy
650 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
));
651 $teacher = reset($roles);
653 choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id
, 'new role', '', '0');
658 echo ('<tr><td align="right">');
659 echo '<label for="menudefaultstudent">'.get_string ('defaultcoursestudent').'</label>';
660 echo ('</td><td algin="left">');
662 // get the first teacheredit legacy
663 $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
));
664 $studentrole = array_shift($roles);
666 choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id
, 'new role', '', '0');
670 // 1.7 and above backup
671 $roles = restore_read_xml_roles($xml_file);
673 // Calculate if any role can be mapped
675 $canmaproles = false;
676 if (has_capability('moodle/role:manage', get_context_instance(CONTEXT_SYSTEM
))) {
677 $str_new = 'new role';
679 } else if (!empty($siterolesarray)) {
684 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>');
687 if (!empty($roles->roles
)) { // possible to have course with no roles
688 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
689 $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
692 foreach ($roles->roles
as $roleid=>$role) {
694 if (!$canmaproles) { // User cannot map roles at all, add hidden element and continue
695 echo "<tr><td colspan=\"2\"><input type=\"hidden\" name=\"roles_{$roleid}\" value=\"0\" /></td></tr>";
699 $mappableroles = !empty($siteroleschoicearray) ?
$siteroleschoicearray : array();
701 echo ('<tr><td align="right">');
702 echo '<label for="menuroles_'.$roleid.'">'.format_string($role->name
)." (".($role->shortname
).")".'</label>';
703 echo ('</td><td align="left">');
705 /// first, we see if any exact role definition is found
706 /// if found, that is the only option of restoring to
708 if ($samerole = restore_samerole($roleid, $role)) {
709 $matchrole = $samerole->id
;
710 // if an exact role is found, it does not matter whether this user can assign this role or not,
711 // this will be presented as a valid option regardless
712 $mappableroles[$samerole->id
] = format_string($allroles[$samerole->id
]->name
)." (". $allroles[$samerole->id
]->shortname
.")";
714 // no exact role found, let's try to match shortname
715 // this is useful in situations where basic roles differ slightly in definition
717 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
718 if ($siteroleshortname == $role->shortname
) {
719 $matchrole = $siteroleid;
723 // Hack a bit, so, if role continues unmatched, and was "editingteacher", and "teacher" is available
724 // preselect it as fallback matching (can happen a lot)
725 if ($matchrole == 0 && $role->shortname
== 'editingteacher' && in_array('teacher', $siterolesarray)) {
726 $matchrole = array_search('teacher', $siterolesarray);
729 // If arrived here, role continues unmatched and the user lacks the 'moodle/role:manage' capability
730 // perform automatic matching to site defaultcourseroleid (only if available in the list)
731 if ($matchrole == 0 && !has_capability('moodle/role:manage', get_context_instance(CONTEXT_SYSTEM
))) {
732 if (array_key_exists($CFG->defaultcourseroleid
, $siterolesarray)) {
733 $matchrole = $CFG->defaultcourseroleid
;
737 choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, $str_new, '', '0');
743 echo ('</table>'); // end of role mappings table
748 <div style=
"text-align:center">
750 /// Print captured hidden options, now that we have closed the table
751 echo $hidden_options;
753 <input type=
"hidden" name=
"id" value=
"<?php p($id) ?>" />
754 <input type
="hidden" name
="launch" value
="check" />
755 <input type
="hidden" name
="fromform" value
="1" />
756 <input type
="submit" value
="<?php print_string("continue") ?>" />
757 <input type
="submit" name
="cancel" value
="<?php print_string("cancel
") ?>" />
766 * @param integer $roleid the id that the role in the backup files had on the old server.
767 * @param object $role the rest of the definition of the role from the backup file.
769 function restore_samerole($roleid, $rolefromxml) {
772 // First we try some intelligent guesses, then, if none of those work, we do a more extensive
775 // First guess, try let's use the id
776 if (restore_is_samerole($roleid, $rolefromxml)) {
777 return get_record('role', 'id', $roleid);
780 // Second guess, try the shortname
781 $testroleid = get_field('role', 'id', 'shortname', $rolefromxml->shortname
);
782 if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
783 return get_record('role', 'id', $testroleid);
786 // Finally, search all other roles. In orter to speed things up, we exclude the ones we have
787 // already tested, and we only search roles with the same number of capabilities set in their
789 $extracondition = '';
791 $extracondition = "AND roleid <> $testroleid";
793 $candidateroleids = get_records_sql("SELECT roleid
794 FROM {$CFG->prefix}role_capabilities
795 WHERE roleid <> $roleid $extracondition
797 HAVING COUNT(capability) = ".count($rolefromxml->capabilities
));
798 if (!empty($candidateroleids)) {
799 foreach ($candidateroleids as $testroleid => $notused) {
800 if (restore_is_samerole($testroleid, $rolefromxml)) {
801 return get_record('role', 'id', $testroleid);
810 * Compare a role in the database with one loaded from the backup file, and determine whether
811 * they have identical permissions for each capability.
812 * @param integer $testroleid the id of the role from the database to test against.
813 * @param object $rolefromxml the role definition loaded from the backup file.
814 * @return boolean true if the two roles are identical.
816 function restore_is_samerole($testroleid, $rolefromxml) {
817 // Load the role definition from the databse.
818 $rolefromdb = get_records('role_capabilities', 'roleid', $testroleid, '', 'capability,permission');
823 // Quick check, do they have the permissions on the same number of capabilities?
824 if (count($rolefromdb) != count($rolefromxml->capabilities
)) {
828 // If they do, check each one.
829 foreach ($rolefromdb as $capability => $permissions) {
830 if (!isset($rolefromxml->capabilities
[$capability]) ||
831 $permissions->permission
!= $rolefromxml->capabilities
[$capability]->permission
) {