forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore_form.html
792 lines (701 loc) · 33.3 KB
/
restore_form.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
<?php //$Id$
//This page prints the restore form to select everything yo want
//to restore. Form is dinamically buid, depending of "info" object
//that contains the backup contents and depending of every mod
//capabilities.
global $DB;
//Get objects from session
if (!($info = $SESSION->info)) {
print_error('sessionmissing' ,'debug', '', 'info');
}
if (!($course_header = $SESSION->course_header)) {
print_error('sessionmissing' ,'debug', '', 'course_header');
}
$restore_gradebook_history = optional_param('restore_gradebook_history', 0, PARAM_INT);
//Check that we have all we need
//backup_unique_code
$backup_unique_code = required_param( 'backup_unique_code' );
//file
$file = required_param( 'file' );
//Check login
require_login();
//Check admin
if (!empty($id)) {
if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
print_error('cannotuseadminadminorteacher', '', '$CFG->wwwroot/login/index.php');
}
} else {
if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM))) {
print_error('cannotuseadmin', '', '$CFG->wwwroot/login/index.php');
}
}
//Check site
if (!$site = get_site()) {
print_error("siteisnotdefined", 'debug');
}
//Checks for the required files/functions to restore every mod
$count = 0;
if ($allmods = $DB->get_records("modules") ) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
$modrestore = $modname."_restore_mods";
if (file_exists($modfile)) {
include_once($modfile);
if (function_exists($modrestore)) {
$var = "exists_".$modname;
$$var = true;
$count++;
}
}
//Check data
//Check module info
$var = "restore_".$modname;
if (!isset($$var)) {
$$var = 1;
}
//Check include user info
$var = "restore_user_info_".$modname;
if (!isset($$var)) {
$$var = 1;
}
}
}
//Check other parameters
if (!isset($restore_metacourse)) {
$restore_metacourse = 1;
}
if (!isset($restore_users)) {
$restore_users = 1;
}
if (!isset($restore_groups)) {
if (empty($CFG->enablegroupings)) {
$restore_groups = RESTORE_GROUPS_ONLY;
} else {
$restore_groups = RESTORE_GROUPS_GROUPINGS;
}
}
if (!isset($restore_logs)) {
$restore_logs = 1;
}
if (!isset($restore_user_files)) {
$restore_user_files = 1;
}
if (!isset($restore_course_files)) {
$restore_course_files = 1;
}
if (!isset($restore_site_files)) {
$restore_site_files = 1;
}
if (!isset($restore_messages)) {
$restore_messages = 1;
}
if (!isset($restore_blogs)) {
$restore_blogs = 1;
}
if (!isset($restore_restoreto)) {
if (!user_can_create_courses()) {
$restore_restoreto = 1;
} else {
$restore_restoreto = 2;
}
}
if (!isset($course_header->category->id)) {
$course_header->category->id = 0;
}
if(!isset($form1->startdate)) {
$form1->startdate = $course_header->course_startdate; //$course_header->course_startdate;
}
if (empty($form1->shortname)) {
$form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname;
}
if (empty($form1->fullname)) {
$form1->fullname = $course_header->course_fullname; // '_fullname'; //$course_header->course_fullname;
}
if ($count == 0) {
notice("No restorable modules are installed!");
}
?>
<script type="text/javascript">
<!--
function selectItemInMenuByName(formId, menuName, selectIndex ) {
myForm = document.getElementById(formId)
for (i=0,n=myForm.elements.length;i<n;i++) {
myLen = menuName.length;
myName = myForm.elements[i].name;
myType = myForm.elements[i].type;
if (myName.substring(0,myLen) == menuName && myType == "select-one") {
myForm.elements[i].options[selectIndex].selected = true;
}
}
}
function selectItemInRadioByName(formId, radioName, selectIndex ) {
myForm = document.getElementById(formId)
for (i=0,n=myForm.elements.length;i<n;i++) {
myLen = radioName.length;
myName = myForm.elements[i].name;
myType = myForm.elements[i].type;
if (myName.substring(0,myLen) == radioName && myType == "radio") {
myRadioGroup = myForm.elements[myName];
myRadioGroup[selectIndex].checked = true;
}
}
}
function selectItemInCheckboxByName(formId, checkName, checked ) {
myForm = document.getElementById(formId)
for (i=0,n=myForm.elements.length;i<n;i++) {
myLen = checkName.length;
myName = myForm.elements[i].name;
myType = myForm.elements[i].type;
if (myName.substring(0,myLen) == checkName && myType == "checkbox") {
myForm.elements[i].checked = checked;
}
}
}
-->
</script>
<form id="form1" method="post" action="restore.php">
<div>
<table cellpadding="5" class="boxaligncenter">
<?php
//First, course destination
//Print the full tr
echo "<tr>";
echo "<td align=\"right\"><b>";
echo '<label for="menurestore_restoreto">'.get_string ('restoreto').'</label>';
echo "</b>";
echo "</td><td colspan=\"3\">";
// permission should have been checked already
/**
* if user has manageactivities in any course and we are not restoring from within SITEID
* existingcoursedeleting
* existingcourseadding
* else we show
* currentcoursedeleting
* currentcourse
* if user has course:create in any category, we show
* newcourse
*/
// Non-cached - get accessinfo
if (isset($USER->access)) {
$accessinfo = $USER->access;
} else {
$accessinfo = get_user_access_sitewide($USER->id);
}
$mycourses = get_user_courses_bycap($USER->id, 'moodle/site:restore', $accessinfo, true);
// if the user can manage 2 or more courses and we are not restoring from within SITEID,
// we show options for existing courses
if (count($mycourses) > 1 && $id != SITEID) {
$restore_restoreto_options[0] = get_string("existingcoursedeleting");
$restore_restoreto_options[1] = get_string("existingcourseadding");
// else if the user can write to current course
} else if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))){
$restore_restoreto_options[0] = get_string("currentcoursedeleting");
$restore_restoreto_options[1] = get_string("currentcourseadding");
}
// if user can create any course at all, give the option
if (user_can_create_courses()) {
$restore_restoreto_options[2] = get_string("newcourse");
}
/// Acummulator for hidden options and proper XHTML output
$hidden_options = '';
choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
echo "</td></tr>";
if (user_can_create_courses()) { //display these fields conditionally
// find the list of cates user can edit
echo "<tr valign=\"top\" >";
echo "<td align=\"right\">";
echo '<label for="menurestore_restorecatto">'.get_string ('category').'</label>';
echo "</td>";
echo "<td>";
// Category selection isn't alowed if restoring from within SITEID course
if ($id != SITEID) {
make_categories_list($categories, $parents);
choose_from_menu($categories, "restore_restorecatto", $course_header->category->id, "");
} else {
print_string('notavailable');
echo '<input type="hidden" name="restore_restorecatto" id="menurestore_restorecatto" value="0" />';
}
echo "</td>";
echo "</tr>";
echo "<tr valign=\"top\" >";
echo "<td align=\"right\">";
echo '<label for="shortnamefield">'.get_string ('shortname').'</label>';
echo "</td>";
echo "<td><input type=\"text\" id=\"shortnamefield\" name=\"shortname\" maxlength=\"100\" size=\"20\" value=\"$form1->shortname\" alt=\"".get_string("shortname")."\" />" ;
helpbutton("courseshortname", get_string("shortname")) ;
if (isset($err["shortname"])) formerr($err["shortname"]);
echo "</td>";
echo "</tr>";
echo "<tr valign=\"top\" >";
echo "<td align=\"right\">";
echo '<label for="fullnamefield">'.get_string ('fullname').'</label>';
echo "</td>";
echo "<td><input type=\"text\" id=\"fullnamefield\" name=\"fullname\" maxlength=\"254\" size=\"50\" value=\"$form1->fullname\" alt=\" ".get_string("fullname")."\" />" ;
helpbutton("coursefullname", get_string("fullname")) ;
if (isset($err["fullname"])) formerr($err["fullname"]);
echo"</td></tr>";
echo "<tr valign=\"top\"> ";
echo "<td align=\"right\"> ";
print_string("startdate");
echo "</td><td>";
/// Show the roll dates option only if the backup course has a start date
/// (some formats like main page, social..., haven't it and rolling dates
/// from 0 produces crazy dates. MDL-10125
if ($form1->startdate) {
print_date_selector("startday", "startmonth", "startyear", $form1->startdate);
helpbutton("coursestartdate", get_string("startdate"));
} else {
print_string('notavailable');
echo '<input type="hidden" name="startyear" value="0" />';
echo '<input type="hidden" name="startmonth" value="0" />';
echo '<input type="hidden" name="startday" value="0" />';
}
echo "</td></tr>";
}
//Line
echo "<tr><td colspan=\"4\"><hr /></td></tr>";
//Now, check modules and info and show posibilities
if ($allmods = $DB->get_records("modules") ) {
//Print option to select/deselect everything with 1 click.
echo "<tr>";
echo "<td align=\"right\">";
echo '<b>'.get_string("include")."</b>";
echo "</td><td>";
echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
get_string("all")."</a>/";
echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
get_string("none")."</a>";
echo "</td>";
echo "<td align=\"right\">";
echo '<b> </b>';
echo "</td><td>";
echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', true);\">".
get_string("all")."</a>/";
echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_user_info_', false);\">".
get_string("none")."</a>";
echo "</td>";
echo "</tr>";
echo "<tr><td colspan=\"4\"><hr /></td></tr>";
$currentrow = 0;
$nonrestmod = '';
foreach ($allmods as $mod) {
$modname = $mod->name;
$modrestore = $modname."_restore_mods";
//If exists the lib & function
$exist = "exists_".$modname;
$restore_var = "restore_".$modname;
$user_info_var = "restore_user_info_".$modname;
if (isset($$exist)) {
if ($$exist) {
//Now check that we have that module info in the backup file
if (isset($info->mods[$modname]) && $info->mods[$modname]->backup == "true") {
//Print the full tr
echo "<tr class=\"r".$currentrow."\">";
echo "<td align=\"right\"> ";
echo "</td><td>";
$restore_options[1] = get_string("yes");
$restore_options[0] = get_string("no");
//choose_from_menu($restore_options, $restore_var, $$restore_var, "");
//choose_from_radio($restore_options, $restore_var, $$restore_var);
//Print the checkbox
print_checkbox($restore_var, $$restore_var, $$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
//If backup contains user data, then show menu, else fix it to
//without user data
echo "</td><td align=\"right\"> ";
echo "</td><td>";
if ($info->mods[$modname]->userinfo == "true") {
$restore_user_options[1] = get_string("yes");
$restore_user_options[0] = get_string("no");
//choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
//choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
print_checkbox($user_info_var, $$user_info_var, $$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
} else {
//Module haven't userdata
echo get_string("withoutuserdata");
echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
}
echo "</td></tr>";
if (isset($info->mods[$modname]->instances)) {
$instances = $info->mods[$modname]->instances;
}
if (!empty($instances) && is_array($instances)) {
echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
foreach ($instances as $instance) {
echo '<tr><td>';
$var = 'restore_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,1);
print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
echo '</td><td align="right"> ';
$var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,1);
if ($info->mods[$modname]->instances[$instance->id]->userinfo == 'true') {
print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
} else {
echo '<input type="hidden" name="'.$var.'" value="0" />';
}
echo '</td></tr>';
}
echo '</table></td></tr>';
}
} else {
//Module isn't restorable
$nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
$nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
}
} else {
//Module isn't restorable
$nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
$nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
}
} else {
//Module isn't restorable
$nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
$nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
}
$currentrow = ($currentrow + 1) % 2;
}
//Line
echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
//Now print the Metacourse tr
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
echo '<label for="menurestore_metacourse">'.get_string ("metacourse").'</label>';
echo "</b></td><td colspan=\"2\">";
//If metacourse are in the backup file, show menu, else fixed to no
if ($info->backup_metacourse == "true") {
$metacourse_options = array();
$metacourse_options[0] = get_string("no");
$metacourse_options[1] = get_string("yes");
choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
} else {
echo get_string("no");
echo "<input type=\"hidden\" id=\"menurestore_metacourse\" name=\"restore_metacourse\" value=\"0\" />";
}
echo "</td></tr>";
//Now print the Users tr
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
echo '<label for="menurestore_users">'.get_string ("users").'</label>';
echo "</b></td><td colspan=\"2\">";
//If some user is present in the backup file
if ($info->backup_users == "all" or $info->backup_users == "course") {
$user_options = array();
//If all users are in the backup file
if ($info->backup_users == "all") {
$user_options[0] = get_string("all");
}
$user_options[1] = get_string("course");
$user_options[2] = get_string("none");
choose_from_menu($user_options, "restore_users", $restore_users, "");
} else {
echo get_string("none");
echo "<input type=\"hidden\" id=\"menurestore_users\" name=\"restore_users\" value=\"2\" />";
}
echo "</td></tr>";
//Now print the Groups tr (assume there is no $info->backup_groups)
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
$helplink = helpbutton('grouprestore', get_string('groups'), '', true, false, '', true);
if (empty($CFG->enablegroupings)) {
echo '<label for="menurestore_groups">'.get_string ("groups").'</label>'.$helplink;
echo "</b></td><td colspan=\"2\">";
$group_options[RESTORE_GROUPS_NONE] = get_string('no');
$group_options[RESTORE_GROUPS_ONLY] = get_string('yes');
} else {
echo '<label for="menurestore_groups">'.get_string ('groupsgroupings', 'group').'</label>'.$helplink;
echo "</b></td><td colspan=\"2\">";
$group_options[RESTORE_GROUPS_NONE] = get_string('none');
$group_options[RESTORE_GROUPS_ONLY] = get_string('groupsonly', 'group');
$group_options[RESTORE_GROUPINGS_ONLY] = get_string('groupingsonly', 'group');
$group_options[RESTORE_GROUPS_GROUPINGS] = get_string('groupsgroupings', 'group'); //all.
} /*else {
echo get_string('none');
echo "<input type=\"hidden\" name=\"restore_groups\" value=\"2\" />";
}*/
choose_from_menu($group_options, 'restore_groups', $restore_groups, '');
echo "</td></tr>";
//Now print the Logs tr
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
echo '<label for="menurestore_logs">'.get_string ("logs").'</label>';
echo "</b></td><td colspan=\"2\">";
//If logs are in the backup file, show menu, else fixed to no
if ($info->backup_logs == "true") {
$log_options = array();
$log_options[0] = get_string("no");
$log_options[1] = get_string("yes");
choose_from_menu($log_options, "restore_logs", $restore_logs, "");
} else {
echo get_string("no");
echo "<input type=\"hidden\" id=\"menurestore_logs\" name=\"restore_logs\" value=\"0\" />";
}
echo "</td></tr>";
//Now print the User Files tr
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
echo '<label for="menurestore_user_files">'.get_string ("userfiles").'</label>';
echo "</b></td><td colspan=\"2\">";
//If user files are in the backup file, show menu, else fixed to no
if ($info->backup_user_files == "true") {
$user_file_options = array();
$user_file_options[0] = get_string("no");
$user_file_options[1] = get_string("yes");
choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
} else {
echo get_string("no");
echo "<input type=\"hidden\" id=\"menurestore_user_files\" name=\"restore_user_files\" value=\"0\" />";
}
echo "</td></tr>";
//Now print the Course Files tr
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
echo '<label for="menurestore_course_files">'.get_string ("coursefiles").'</label>';
echo "</b></td><td colspan=\"2\">";
echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
//If course files are in the backup file, show menu, else fixed to no
if ($info->backup_course_files == "true") {
$course_file_options = array();
$course_file_options[0] = get_string("no");
$course_file_options[1] = get_string("yes");
choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
} else {
echo get_string("no");
echo "<input type=\"hidden\" id=\"menurestore_course_files\" name=\"restore_course_files\" value=\"0\" />";
}
echo "</td></tr>";
//Now print the Site Files tr
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
echo '<label for="menurestore_site_files">'.get_string ("sitefiles").'</label>';
echo "</b></td><td colspan=\"2\">";
//If site files are in the backup file, show menu, else fixed to no
if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
$site_file_options = array();
$site_file_options[0] = get_string("no");
$site_file_options[1] = get_string("yes");
choose_from_menu($site_file_options, "restore_site_files", $restore_site_files, "");
} else {
echo get_string("no");
echo "<input type=\"hidden\" id=\"menurestore_site_files\" name=\"restore_site_files\" value=\"0\" />";
}
echo "</td></tr>";
// do you want grade histories to be restored?
if (empty($CFG->disablegradehistory)) {
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
echo '<label for="menurestore_gradebook_history">'.get_string ('gradebookhistories', 'grades').'</label>';
echo "</b></td><td colspan=\"2\">";
if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
$gradebook_history_options = array();
$gradebook_history_options[0] = get_string("no");
$gradebook_history_options[1] = get_string("yes");
choose_from_menu($gradebook_history_options, "restore_gradebook_history", $restore_gradebook_history, "");
} else {
echo get_string("no");
echo "<input type=\"hidden\" id=\"menurestore_gradebook_history\" name=\"restore_gradebook_history\" value=\"0\" />";
}
echo "</td></tr>";
} else {
$hidden_options .= '<input type="hidden" name="restore_gradebook_history" value="0" />';
}
//This tr is slighty different. Everything becomes hidden if
//we haven't messages is the backup, to avoid confusions to users.
//If messages are in the backup file, show menu, else fixed to no and show nothing
//Also, messaging must be enabled in the destination site
if (isset($info->backup_messages) && $info->backup_messages == "true" && !empty($CFG->messaging)) {
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
echo '<label for="menurestore_messages">'.get_string ('messages', 'message').'</label>';
echo "</b></td><td colspan=\"2\">";
$message_options = array();
$message_options[0] = get_string("no");
$message_options[1] = get_string("yes");
choose_from_menu($message_options, "restore_messages", $restore_messages, "");
echo "</td></tr>";
} else {
$hidden_options .= '<input type="hidden" name="restore_messages" value="0" />';
}
//This tr is slighty different. Everything becomes hidden if
//we haven't blogs is the backup, to avoid confusions to users.
//If blogs are in the backup file, show menu, else fixed to no and show nothing
//Also, blogs must be enabled in the destination site
if (isset($info->backup_blogs) && $info->backup_blogs == "true" && !empty($CFG->bloglevel)) {
echo "<tr>";
echo "<td align=\"right\" colspan=\"2\"><b>";
echo '<label for="menurestore_blogs">'.get_string ('blogs', 'blog').'</label>';
echo "</b></td><td colspan=\"2\">";
$blog_options = array();
$blog_options[0] = get_string("no");
$blog_options[1] = get_string("yes");
choose_from_menu($blog_options, "restore_blogs", $restore_blogs, "");
echo "</td></tr>";
} else {
$hidden_options .= '<input type="hidden" name="restore_blogs" value="0" />';
}
}
?>
</table>
<hr />
<?php
print_heading(get_string('rolemappings'));
$xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
$info = restore_read_xml_info($xml_file);
// fix for MDL-9068, front page course is just a normal course
$siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname", ROLENAME_ORIGINAL);
$siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name", ROLENAME_ORIGINAL);
$allroles = $DB->get_records('role');
echo ('<table width="100%" class="restore-form-instances">');
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>');
if ($info->backup_moodle_version < 2006092801) {
// 1.6 and below backup
/// Editting teacher
echo ('<tr><td align="right">');
echo '<label for="menudefaultteacheredit">'.get_string ('defaultcourseteacher').'</label>';
echo ('</td><td algin="left">');
// get the first teacheredit legacy
$roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
$editteacher = reset($roles);
choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id, 'new role', '', '0');
echo ('</td></tr>');
/// Non-editting teacher
echo ('<tr><td align="right">');
echo '<label for="menudefaultteacher">'.get_string ('noneditingteacher').'</label>';
print_string('noneditingteacher');
echo ('</td><td algin="left">');
// get the first teacheredit legacy
$roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
$teacher = reset($roles);
choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id, 'new role', '', '0');
echo ('</td></tr>');
/// Student
echo ('<tr><td align="right">');
echo '<label for="menudefaultstudent">'.get_string ('defaultcoursestudent').'</label>';
echo ('</td><td algin="left">');
// get the first teacheredit legacy
$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
$studentrole = array_shift($roles);
choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id, 'new role', '', '0');
echo ('</td></tr>');
} else {
// 1.7 and above backup
$roles = restore_read_xml_roles($xml_file);
if (!empty($roles->roles)) { // possible to have course with no roles
foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
$siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
}
foreach ($roles->roles as $roleid=>$role) {
$mappableroles = $siteroleschoicearray;
echo ('<tr><td align="right">');
echo '<label for="menuroles_'.$roleid.'">'.$role->name." (".($role->shortname).")".'</label>';
echo ('</td><td align="left">');
/// first, we see if any exact role definition is found
/// if found, that is the only option of restoring to
if ($samerole = restore_samerole($roleid, $role)) {
$matchrole = $samerole->id;
// if an exact role is found, it does not matter whether this user can assign this role or not,
// this will be presented as a valid option regardless
$mappableroles[$samerole->id] = $allroles[$samerole->id]->name." (". $allroles[$samerole->id]->shortname.")";
} else {
// no exact role found, let's try to match shortname
// this is useful in situations where basic roles differ slightly in definition
$matchrole = 0;
foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
if ($siteroleshortname == $role->shortname) {
$matchrole = $siteroleid;
break;
}
}
}
choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, 'new role', '', '0');
echo ('</td></tr>');
}
}
} // end else
echo ('</table>'); // end of role mappings table
?>
<br />
<div style="text-align:center">
<?php
/// Print captured hidden options, now that we have closed the table
echo $hidden_options;
?>
<input type="hidden" name="id" value="<?php p($id) ?>" />
<input type="hidden" name="launch" value="check" />
<input type="hidden" name="fromform" value="1" />
<input type="submit" value="<?php print_string("continue") ?>" />
<input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
</div>
</div>
</form>
<?php
/**
* Look for a role in the database with exactly the same definition as the one in the backup file.
*
* @param integer $roleid the id that the role in the backup files had on the old server.
* @param object $role the rest of the definition of the role from the backup file.
*/
function restore_samerole($roleid, $rolefromxml) {
global $CFG, $DB;
// First we try some intelligent guesses, then, if none of those work, we do a more extensive
// search.
// First guess, try let's use the id
if (restore_is_samerole($roleid, $rolefromxml)) {
return $DB->get_record('role', array('id'=>$roleid));
}
// Second guess, try the shortname
$testroleid = $DB->get_field('role', 'id', array('shortname'=>$rolefromxml->shortname));
if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
return $DB->get_record('role', array('id'=>$testroleid));
}
// Finally, search all other roles. In orter to speed things up, we exclude the ones we have
// already tested, and we only search roles with the same number of capabilities set in their
// definition.
$extracondition = '';
if ($testroleid) {
$extracondition = "AND roleid <> $testroleid";
}
$candidateroleids = $DB->get_records_sql(
"SELECT roleid
FROM {role_capabilities}
WHERE roleid <> $roleid $extracondition
GROUP BY roleid
HAVING COUNT(capability) = ".count($rolefromxml->capabilities));
if (!empty($candidateroleids)) {
foreach ($candidateroleids as $testroleid => $notused) {
if (restore_is_samerole($testroleid, $rolefromxml)) {
return $DB->get_record('role', array('id'=>$testroleid));
}
}
}
return false;
}
/**
* Compare a role in the database with one loaded from the backup file, and determine whether
* they have identical permissions for each capability.
* @param integer $testroleid the id of the role from the database to test against.
* @param object $rolefromxml the role definition loaded from the backup file.
* @return boolean true if the two roles are identical.
*/
function restore_is_samerole($testroleid, $rolefromxml) {
global $DB;
// Load the role definition from the databse.
$rolefromdb = $DB->get_records('role_capabilities', array('roleid'=>$testroleid), '', 'capability,permission');
if (!$rolefromdb) {
return false;
}
// Quick check, do they have the permissions on the same number of capabilities?
if (count($rolefromdb) != count($rolefromxml->capabilities)) {
return false;
}
// If they do, check each one.
foreach ($rolefromdb as $capability => $permissions) {
if (!isset($rolefromxml->capabilities[$capability]) ||
$permissions->permission != $rolefromxml->capabilities[$capability]->permission) {
return false;
}
}
return true;
}
?>