182
207
* if user has course:create in any category, we show
186
$mycourses = get_capability_courses('moodle/site:restore');
187
// if the user can manage 2 or more courses,
188
// or if the only course the user can manage is not the current course
211
// Non-cached - get accessinfo
212
if (isset($USER->access)) {
213
$accessinfo = $USER->access;
215
$accessinfo = get_user_access_sitewide($USER->id);
218
$mycourses = get_user_courses_bycap($USER->id, 'moodle/site:restore', $accessinfo, true);
220
// if the user can manage 2 or more courses and we are not restoring from within SITEID,
189
221
// we show options for existing courses
190
if (count($mycourses) > 1 || !in_array($id, $mycourses)) {
191
// if user can manage more than 1 course, or if user can restore to a single different course
223
if (count($mycourses) > 1 && $id != SITEID) {
192
224
$restore_restoreto_options[0] = get_string("existingcoursedeleting");
193
225
$restore_restoreto_options[1] = get_string("existingcourseadding");
194
226
// else if the user can write to current course
195
227
} else if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))){
196
228
$restore_restoreto_options[0] = get_string("currentcoursedeleting");
197
$restore_restoreto_options[1] = get_string("currentcourseadding");
229
$restore_restoreto_options[1] = get_string("currentcourseadding");
200
232
// if user can create any course at all, give the option
201
if (user_can_create_courses()) {
202
$restore_restoreto_options[2] = get_string("newcourse");
233
if ($cancreatecourses) {
234
$restore_restoreto_options[2] = get_string("newcourse");
237
/// Acummulator for hidden options and proper XHTML output
238
$hidden_options = '';
205
240
choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
206
241
echo "</td></tr>";
207
if (user_can_create_courses()) { //display these fields conditionally
242
if ($cancreatecourses) { //display these fields conditionally
209
244
// find the list of cates user can edit
210
245
echo "<tr valign=\"top\" >";
211
246
echo "<td align=\"right\">";
212
print_string('category');
247
echo '<label for="menurestore_restorecatto">'.get_string ('category').'</label>';
215
choose_from_menu(get_creatable_categories(), "restore_restorecatto", $course_header->category->id, "");
250
// Category selection isn't alowed if restoring from within SITEID course
252
make_categories_list($categories, $parents);
253
choose_from_menu($categories, "restore_restorecatto", $course_header->category->id, "");
255
print_string('notavailable');
256
echo '<input type="hidden" name="restore_restorecatto" id="menurestore_restorecatto" value="0" />';
219
261
echo "<tr valign=\"top\" >";
220
262
echo "<td align=\"right\">";
221
print_string("shortname");
223
echo "<td><input type=\"text\" name=\"shortname\" maxlength=\"15\" size=\"10\" value=\"$form1->shortname\" alt=\"".get_string("shortname")."\" />" ;
263
echo '<label for="shortnamefield">'.get_string ('shortname').'</label>';
265
echo "<td><input type=\"text\" id=\"shortnamefield\" name=\"shortname\" maxlength=\"100\" size=\"20\" value=\"".s($form1->shortname)."\" alt=\"".get_string("shortname")."\" />" ;
224
266
helpbutton("courseshortname", get_string("shortname")) ;
225
if (isset($err["shortname"])) formerr($err["shortname"]);
267
if (isset($err["shortname"])) formerr($err["shortname"]);
226
270
echo "<tr valign=\"top\" >";
227
271
echo "<td align=\"right\">";
228
print_string("fullname");
230
echo "<td><input type=\"text\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"$form1->fullname\" alt=\" ".get_string("fullname")."\" />" ;
272
echo '<label for="fullnamefield">'.get_string ('fullname').'</label>';
274
echo "<td><input type=\"text\" id=\"fullnamefield\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"".s($form1->fullname)."\" alt=\" ".get_string("fullname")."\" />" ;
231
275
helpbutton("coursefullname", get_string("fullname")) ;
232
if (isset($err["fullname"])) formerr($err["fullname"]);
276
if (isset($err["fullname"])) formerr($err["fullname"]);
233
277
echo"</td></tr>";
234
278
echo "<tr valign=\"top\"> ";
235
279
echo "<td align=\"right\"> ";
236
print_string("startdate");
238
print_date_selector("startday", "startmonth", "startyear", $form1->startdate);
239
helpbutton("coursestartdate", get_string("startdate"));
280
print_string("startdate");
282
/// Show the roll dates option only if the backup course has a start date
283
/// (some formats like main page, social..., haven't it and rolling dates
284
/// from 0 produces crazy dates. MDL-10125
285
if ($form1->startdate) {
286
print_date_selector("startday", "startmonth", "startyear", $form1->startdate);
287
helpbutton("coursestartdate", get_string("startdate"));
289
print_string('notavailable');
290
echo '<input type="hidden" name="startyear" value="0" />';
291
echo '<input type="hidden" name="startmonth" value="0" />';
292
echo '<input type="hidden" name="startday" value="0" />';
240
294
echo "</td></tr>";
296
/// If user cannot create course, prevent any change in startyear/month/day (so restore wont calculate any roll. MDL-12922
297
echo '<input type="hidden" name="startyear" value="0" />';
298
echo '<input type="hidden" name="startmonth" value="0" />';
299
echo '<input type="hidden" name="startday" value="0" />';
243
302
echo "<tr><td colspan=\"4\"><hr /></td></tr>";
346
406
//Now print the Metacourse tr
348
408
echo "<td align=\"right\" colspan=\"2\"><b>";
349
echo get_string("metacourse").":";
409
echo '<label for="menurestore_metacourse">'.get_string ("metacourse").'</label>';
350
410
echo "</b></td><td colspan=\"2\">";
351
411
//If metacourse are in the backup file, show menu, else fixed to no
352
412
if ($info->backup_metacourse == "true") {
413
$metacourse_options = array();
353
414
$metacourse_options[0] = get_string("no");
354
$metacourse_options[1] = get_string("yes");
355
choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
415
$metacourse_options[1] = get_string("yes");
416
choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
357
418
echo get_string("no");
358
echo "<input type=\"hidden\" name=\"restore_metacourse\" value=\"0\" />";
419
echo "<input type=\"hidden\" id=\"menurestore_metacourse\" name=\"restore_metacourse\" value=\"0\" />";
360
421
echo "</td></tr>";
361
422
//Now print the Users tr
363
424
echo "<td align=\"right\" colspan=\"2\"><b>";
364
echo get_string("users").":";
425
echo '<label for="menurestore_users">'.get_string ("users").'</label>';
365
426
echo "</b></td><td colspan=\"2\">";
366
427
//If some user is present in the backup file
367
428
if ($info->backup_users == "all" or $info->backup_users == "course") {
429
$user_options = array();
368
430
//If all users are in the backup file
369
431
if ($info->backup_users == "all") {
370
432
$user_options[0] = get_string("all");
374
436
choose_from_menu($user_options, "restore_users", $restore_users, "");
376
438
echo get_string("none");
377
echo "<input type=\"hidden\" name=\"restore_users\" value=\"2\" />";
439
echo "<input type=\"hidden\" id=\"menurestore_users\" name=\"restore_users\" value=\"2\" />";
380
442
echo "</td></tr>";
444
//Now print the Groups tr (assume there is no $info->backup_groups)
446
echo "<td align=\"right\" colspan=\"2\"><b>";
447
$helplink = helpbutton('grouprestore', get_string('groups'), '', true, false, '', true);
448
if (empty($CFG->enablegroupings)) {
449
echo '<label for="menurestore_groups">'.get_string ("groups").'</label>'.$helplink;
450
echo "</b></td><td colspan=\"2\">";
451
$group_options[RESTORE_GROUPS_NONE] = get_string('no');
452
$group_options[RESTORE_GROUPS_ONLY] = get_string('yes');
454
echo '<label for="menurestore_groups">'.get_string ('groupsgroupings', 'group').'</label>'.$helplink;
455
echo "</b></td><td colspan=\"2\">";
456
$group_options[RESTORE_GROUPS_NONE] = get_string('none');
457
$group_options[RESTORE_GROUPS_ONLY] = get_string('groupsonly', 'group');
458
$group_options[RESTORE_GROUPINGS_ONLY] = get_string('groupingsonly', 'group');
459
$group_options[RESTORE_GROUPS_GROUPINGS] = get_string('groupsgroupings', 'group'); //all.
462
echo get_string('none');
463
echo "<input type=\"hidden\" name=\"restore_groups\" value=\"2\" />";
465
choose_from_menu($group_options, 'restore_groups', $restore_groups, '');
382
468
//Now print the Logs tr
384
470
echo "<td align=\"right\" colspan=\"2\"><b>";
385
echo get_string("logs").":";
471
echo '<label for="menurestore_logs">'.get_string ("logs").'</label>';
386
472
echo "</b></td><td colspan=\"2\">";
387
473
//If logs are in the backup file, show menu, else fixed to no
388
474
if ($info->backup_logs == "true") {
475
$log_options = array();
389
476
$log_options[0] = get_string("no");
390
$log_options[1] = get_string("yes");
391
choose_from_menu($log_options, "restore_logs", $restore_logs, "");
477
$log_options[1] = get_string("yes");
478
choose_from_menu($log_options, "restore_logs", $restore_logs, "");
393
480
echo get_string("no");
394
echo "<input type=\"hidden\" name=\"restore_logs\" value=\"0\" />";
481
echo "<input type=\"hidden\" id=\"menurestore_logs\" name=\"restore_logs\" value=\"0\" />";
396
483
echo "</td></tr>";
398
485
//Now print the User Files tr
400
487
echo "<td align=\"right\" colspan=\"2\"><b>";
401
echo get_string ("userfiles").":";
488
echo '<label for="menurestore_user_files">'.get_string ("userfiles").'</label>';
402
489
echo "</b></td><td colspan=\"2\">";
403
490
//If user files are in the backup file, show menu, else fixed to no
404
491
if ($info->backup_user_files == "true") {
405
$user_file_options[0] = get_string("no");
406
$user_file_options[1] = get_string("yes");
492
$user_file_options = array();
493
$user_file_options[0] = get_string("no");
494
$user_file_options[1] = get_string("yes");
407
495
choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
409
497
echo get_string("no");
410
echo "<input type=\"hidden\" name=\"restore_user_files\" value=\"0\" />";
498
echo "<input type=\"hidden\" id=\"menurestore_user_files\" name=\"restore_user_files\" value=\"0\" />";
412
500
echo "</td></tr>";
414
502
//Now print the Course Files tr
416
504
echo "<td align=\"right\" colspan=\"2\"><b>";
417
echo get_string ("coursefiles").":";
505
echo '<label for="menurestore_course_files">'.get_string ("coursefiles").'</label>';
418
506
echo "</b></td><td colspan=\"2\">";
419
507
echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
420
508
echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
421
509
//If course files are in the backup file, show menu, else fixed to no
422
510
if ($info->backup_course_files == "true") {
511
$course_file_options = array();
423
512
$course_file_options[0] = get_string("no");
424
513
$course_file_options[1] = get_string("yes");
425
514
choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
427
516
echo get_string("no");
428
echo "<input type=\"hidden\" name=\"restore_course_files\" value=\"0\" />";
517
echo "<input type=\"hidden\" id=\"menurestore_course_files\" name=\"restore_course_files\" value=\"0\" />";
430
519
echo "</td></tr>";
432
//Now print the Messages tr
522
//Now print the Site Files tr
434
524
echo "<td align=\"right\" colspan=\"2\"><b>";
525
echo '<label for="menurestore_site_files">'.get_string ("sitefiles").'</label>';
526
echo "</b></td><td colspan=\"2\">";
527
//If site files are in the backup file, show menu, else fixed to no
528
if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
529
$site_file_options = array();
530
$site_file_options[0] = get_string("no");
531
$site_file_options[1] = get_string("yes");
532
choose_from_menu($site_file_options, "restore_site_files", $restore_site_files, "");
534
echo get_string("no");
535
echo "<input type=\"hidden\" id=\"menurestore_site_files\" name=\"restore_site_files\" value=\"0\" />";
539
// do you want grade histories to be restored?
540
if (empty($CFG->disablegradehistory)) {
542
echo "<td align=\"right\" colspan=\"2\"><b>";
543
echo '<label for="menurestore_gradebook_history">'.get_string ('gradebookhistories', 'grades').'</label>';
544
echo "</b></td><td colspan=\"2\">";
545
if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
546
$gradebook_history_options = array();
547
$gradebook_history_options[0] = get_string("no");
548
$gradebook_history_options[1] = get_string("yes");
549
choose_from_menu($gradebook_history_options, "restore_gradebook_history", $restore_gradebook_history, "");
551
echo get_string("no");
552
echo "<input type=\"hidden\" id=\"menurestore_gradebook_history\" name=\"restore_gradebook_history\" value=\"0\" />";
556
$hidden_options .= '<input type="hidden" name="restore_gradebook_history" value="0" />';
436
559
//This tr is slighty different. Everything becomes hidden if
437
560
//we haven't messages is the backup, to avoid confusions to users.
438
561
//If messages are in the backup file, show menu, else fixed to no and show nothing
439
if ($info->backup_messages == "true") {
440
echo get_string ('messages','message').":";
562
//Also, messaging must be enabled in the destination site
563
if (isset($info->backup_messages) && $info->backup_messages == "true" && !empty($CFG->messaging)) {
565
echo "<td align=\"right\" colspan=\"2\"><b>";
566
echo '<label for="menurestore_messages">'.get_string ('messages', 'message').'</label>';
441
567
echo "</b></td><td colspan=\"2\">";
568
$message_options = array();
442
569
$message_options[0] = get_string("no");
443
570
$message_options[1] = get_string("yes");
444
571
choose_from_menu($message_options, "restore_messages", $restore_messages, "");
446
echo " </b></td><td colspan=\"2\">";
447
echo "<input type=\"hidden\" name=\"restore_messages\" value=\"0\" />";
574
$hidden_options .= '<input type="hidden" name="restore_messages" value="0" />';
577
//This tr is slighty different. Everything becomes hidden if
578
//we haven't blogs is the backup, to avoid confusions to users.
579
//If blogs are in the backup file, show menu, else fixed to no and show nothing
580
//Also, blogs must be enabled in the destination site
581
if (isset($info->backup_blogs) && $info->backup_blogs == "true" && !empty($CFG->bloglevel)) {
583
echo "<td align=\"right\" colspan=\"2\"><b>";
584
echo '<label for="menurestore_blogs">'.get_string ('blogs', 'blog').'</label>';
585
echo "</b></td><td colspan=\"2\">";
586
$blog_options = array();
587
$blog_options[0] = get_string("no");
588
$blog_options[1] = get_string("yes");
589
choose_from_menu($blog_options, "restore_blogs", $restore_blogs, "");
592
$hidden_options .= '<input type="hidden" name="restore_blogs" value="0" />';
457
602
print_heading(get_string('rolemappings'));
460
605
$info = restore_read_xml_info($xml_file);
462
607
// fix for MDL-9068, front page course is just a normal course
463
$siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname");
464
$siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name");
608
$siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname", ROLENAME_ORIGINAL);
609
$siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name", ROLENAME_ORIGINAL);
610
$allroles = get_records('role');
466
612
echo ('<table width="100%" class="restore-form-instances">');
467
613
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>');
469
615
if ($info->backup_moodle_version < 2006092801) {
470
616
// 1.6 and below backup
472
618
/// Editting teacher
473
619
echo ('<tr><td align="right">');
474
print_string('defaultcourseteacher');
620
echo '<label for="menudefaultteacheredit">'.get_string ('defaultcourseteacher').'</label>';
475
621
echo ('</td><td algin="left">');
477
623
// get the first teacheredit legacy
478
$roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
624
$roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
480
$editteacher = array_shift($roles);
626
$editteacher = reset($roles);
481
627
choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id, 'new role', '', '0');
482
628
echo ('</td></tr>');
484
630
/// Non-editting teacher
485
631
echo ('<tr><td align="right">');
632
echo '<label for="menudefaultteacher">'.get_string ('noneditingteacher').'</label>';
486
633
print_string('noneditingteacher');
487
634
echo ('</td><td algin="left">');
489
636
// get the first teacheredit legacy
490
$roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
491
$teacher = array_shift($roles);
637
$roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
638
$teacher = reset($roles);
493
640
choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id, 'new role', '', '0');
494
641
echo ('</td></tr>');
498
645
echo ('<tr><td align="right">');
499
print_string('defaultcoursestudent');
646
echo '<label for="menudefaultstudent">'.get_string ('defaultcoursestudent').'</label>';
500
647
echo ('</td><td algin="left">');
502
649
// get the first teacheredit legacy
503
$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
650
$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
504
651
$studentrole = array_shift($roles);
506
653
choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id, 'new role', '', '0');
507
654
echo ('</td></tr>');
510
657
// 1.7 and above backup
511
658
$roles = restore_read_xml_roles($xml_file);
513
660
if (!empty($roles->roles)) { // possible to have course with no roles
514
661
foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
515
662
$siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
518
665
foreach ($roles->roles as $roleid=>$role) {
667
$mappableroles = $siteroleschoicearray;
519
669
echo ('<tr><td align="right">');
520
echo $role->name." (".($role->shortname).")";
670
echo '<label for="menuroles_'.$roleid.'">'.format_string($role->name)." (".($role->shortname).")".'</label>';
521
671
echo ('</td><td align="left">');
523
// see if any short name match
525
foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
526
if ($siteroleshortname == $role->shortname) {
527
$matchrole = $siteroleid;
673
/// first, we see if any exact role definition is found
674
/// if found, that is the only option of restoring to
676
if ($samerole = restore_samerole($roleid, $role)) {
677
$matchrole = $samerole->id;
678
// if an exact role is found, it does not matter whether this user can assign this role or not,
679
// this will be presented as a valid option regardless
680
$mappableroles[$samerole->id] = format_string($allroles[$samerole->id]->name)." (". $allroles[$samerole->id]->shortname.")";
682
// no exact role found, let's try to match shortname
683
// this is useful in situations where basic roles differ slightly in definition
685
foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
686
if ($siteroleshortname == $role->shortname) {
687
$matchrole = $siteroleid;
532
choose_from_menu ($siteroleschoicearray, "roles_".$roleid, $matchrole, 'new role', '', '0');
692
choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, 'new role', '', '0');
539
699
echo ('</table>'); // end of role mappings table
720
* Look for a role in the database with exactly the same definition as the one in the backup file.
722
* @param integer $roleid the id that the role in the backup files had on the old server.
723
* @param object $role the rest of the definition of the role from the backup file.
725
function restore_samerole($roleid, $rolefromxml) {
728
// First we try some intelligent guesses, then, if none of those work, we do a more extensive
731
// First guess, try let's use the id
732
if (restore_is_samerole($roleid, $rolefromxml)) {
733
return get_record('role', 'id', $roleid);
736
// Second guess, try the shortname
737
$testroleid = get_field('role', 'id', 'shortname', $rolefromxml->shortname);
738
if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
739
return get_record('role', 'id', $testroleid);
742
// Finally, search all other roles. In orter to speed things up, we exclude the ones we have
743
// already tested, and we only search roles with the same number of capabilities set in their
745
$extracondition = '';
747
$extracondition = "AND roleid <> $testroleid";
749
$candidateroleids = get_records_sql("SELECT roleid
750
FROM {$CFG->prefix}role_capabilities
751
WHERE roleid <> $roleid $extracondition
753
HAVING COUNT(capability) = ".count($rolefromxml->capabilities));
754
if (!empty($candidateroleids)) {
755
foreach ($candidateroleids as $testroleid => $notused) {
756
if (restore_is_samerole($testroleid, $rolefromxml)) {
757
return get_record('role', 'id', $testroleid);
766
* Compare a role in the database with one loaded from the backup file, and determine whether
767
* they have identical permissions for each capability.
768
* @param integer $testroleid the id of the role from the database to test against.
769
* @param object $rolefromxml the role definition loaded from the backup file.
770
* @return boolean true if the two roles are identical.
772
function restore_is_samerole($testroleid, $rolefromxml) {
773
// Load the role definition from the databse.
774
$rolefromdb = get_records('role_capabilities', 'roleid', $testroleid, '', 'capability,permission');
779
// Quick check, do they have the permissions on the same number of capabilities?
780
if (count($rolefromdb) != count($rolefromxml->capabilities)) {
784
// If they do, check each one.
785
foreach ($rolefromdb as $capability => $permissions) {
786
if (!isset($rolefromxml->capabilities[$capability]) ||
787
$permissions->permission != $rolefromxml->capabilities[$capability]->permission) {