restore_form.html 31.9 KB
Newer Older
1
<?php //$Id$
2
3
4
5
6
7
    //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.

    //Get objects from session
thepurpleblob's avatar
thepurpleblob committed
8
    if (!($info = $SESSION->info)) {
9
      print_error( 'info object missing from session' );
thepurpleblob's avatar
thepurpleblob committed
10
11
    }
    if (!($course_header = $SESSION->course_header)) {
12
      print_error( 'course_header object missing from session' );
thepurpleblob's avatar
thepurpleblob committed
13
    }
14

15
    $restore_gradebook_history =  optional_param('restore_gradebook_history', 0, PARAM_INT);
16

17
18
    //Check that we have all we need
    //backup_unique_code
thepurpleblob's avatar
thepurpleblob committed
19
    $backup_unique_code = required_param( 'backup_unique_code' );
20
    //file
thepurpleblob's avatar
thepurpleblob committed
21
    $file = required_param( 'file' );
22
23
24
25
26

    //Check login
    require_login();

    //Check admin
27
    if (!empty($id)) {
vyshane's avatar
vyshane committed
28
        if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
29
            print_error("You need to be a teacher or admin user to use this page.", '', "$CFG->wwwroot/login/index.php");
30
31
        }
    } else {
32
        if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM))) {
33
            print_error("You need to be an admin user to use this page.", '', "$CFG->wwwroot/login/index.php");
34
        }
35
36
37
38
    }

    //Check site
    if (!$site = get_site()) {
39
        print_error("Site not found!");
40
    }
41

42
    //Checks for the required files/functions to restore every mod
43
    $count = 0;
44
    if ($allmods = get_records("modules") ) {
45
46
        foreach ($allmods as $mod) {
            $modname = $mod->name;
47
            $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
48
49
50
            $modrestore = $modname."_restore_mods";
            if (file_exists($modfile)) {
               include_once($modfile);
51
               if (function_exists($modrestore)) {
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
                   $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
72
73
74
    if (!isset($restore_metacourse)) {
        $restore_metacourse = 1;
    }
75

76
77
78
    if (!isset($restore_users)) {
        $restore_users = 1;
    }
79

80
81
82
83
84
85
86
87
    if (!isset($restore_groups)) {
        if (empty($CFG->enablegroupings)) {
            $restore_groups = RESTORE_GROUPS_ONLY;
        } else {
            $restore_groups = RESTORE_GROUPS_GROUPINGS;
        }
    }

88
89
90
91
92
93
94
95
96
97
98
99
    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;
    }

100
101
102
103
    if (!isset($restore_site_files)) {
        $restore_site_files = 1;
    }

104
105
106
107
    if (!isset($restore_messages)) {
        $restore_messages = 1;
        }

108
    if (!isset($restore_restoreto)) {
109
        if (!user_can_create_courses()) {
110
            $restore_restoreto = 1;
111
        } else {
112
113
            $restore_restoreto = 2;
        }
114
115
    }

116
117
118
119
    if (!isset($course_header->category->id)) {
        $course_header->category->id = 0;
    }

120
    if(!isset($form1->startdate)) {
121
        $form1->startdate = $course_header->course_startdate; //$course_header->course_startdate;
122
123
124
    }

    if (empty($form1->shortname)) {
125
       $form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname;
126
127
128
    }

    if (empty($form1->fullname)) {
129
      $form1->fullname = $course_header->course_fullname; // '_fullname';   //$course_header->course_fullname;
130
131
    }

132
133
134
135
136
137
    if ($count == 0) {
        notice("No restorable modules are installed!");
    }

?>

138
<script type="text/javascript">
139
140
<!--
function selectItemInMenuByName(formId, menuName, selectIndex ) {
141
142
143
144
145
    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;
146
        if (myName.substring(0,myLen) == menuName && myType == "select-one") {
147
148
            myForm.elements[i].options[selectIndex].selected = true;
        }
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
    }
}

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;
        }
    }
175
176
177
178
}
-->
</script>

179
<form id="form1" method="post" action="restore.php">
180
181
<div>
<table cellpadding="5" class="boxaligncenter">
moodler's avatar
moodler committed
182
<?php
183
184
185
186

    //First, course destination
    //Print the full tr
    echo "<tr>";
stronk7's avatar
stronk7 committed
187
    echo "<td align=\"right\"><b>";
188
189
    echo '<label for="menurestore_restoreto">'.get_string ('restoreto').'</label>';
    echo "</b>";
190
    echo "</td><td colspan=\"3\">";
191
192


193
    // permission should have been checked already
194

195
    /**
196
197
     * if user has manageactivities in any course, we show
     *      existingcoursedeleting
198
199
200
201
202
203
204
     *      existingcourseadding
     * else we show
     *      currentcoursedeleting
     *      currentcourse
     * if user has course:create in any category, we show
     *      newcourse
     */
205
206
207
208
209
210
211
212
213
     
    // 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);
214
215
216
    // if the user can manage 2 or more courses,
    // or if the only course the user can manage is not the current course
    // we show options for existing courses
217
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
221
        $restore_restoreto_options[0] = get_string("existingcoursedeleting");
        $restore_restoreto_options[1] = get_string("existingcourseadding");
222
    // else if the user can write to current course
toyomoyo's avatar
toyomoyo committed
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");
226
    }
227

228
229
    // if user can create any course at all, give the option
    if (user_can_create_courses()) {
230
        $restore_restoreto_options[2] = get_string("newcourse");
231
    }
232

233
234
235
/// Acummulator for hidden options and proper XHTML output
    $hidden_options = '';

236
237
    choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
    echo "</td></tr>";
238
    if (user_can_create_courses()) {  //display these fields conditionally
239

240
241
242
        // find the list of cates user can edit
        echo "<tr valign=\"top\" >";
        echo "<td align=\"right\">";
243
244
        echo '<label for="menurestore_restorecatto">'.get_string ('category').'</label>';
        echo "</td>";
245
        echo "<td>";
246
247
        make_categories_list($categories, $parents);
        choose_from_menu($categories, "restore_restorecatto", $course_header->category->id, "");
248
249
        echo "</td>";
        echo "</tr>";
250

251
252
        echo "<tr valign=\"top\" >";
        echo "<td align=\"right\">";
253
254
255
        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")."\"  />" ;
256
        helpbutton("courseshortname", get_string("shortname")) ;
257
        if (isset($err["shortname"])) formerr($err["shortname"]);
moodler's avatar
moodler committed
258
259
        echo "</td>";
        echo "</tr>";
260
261
        echo "<tr valign=\"top\" >";
        echo "<td align=\"right\">";
262
263
264
        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")."\"  />" ;
265
        helpbutton("coursefullname", get_string("fullname")) ;
266
        if (isset($err["fullname"])) formerr($err["fullname"]);
267
268
269
        echo"</td></tr>";
        echo "<tr valign=\"top\"> ";
        echo "<td align=\"right\"> ";
270
        print_string("startdate");
271
        echo "</td><td>";
272
273
274
275
276
277
278
279
280
281
282
    /// 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);
        } 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" />';
        }
283
284
285
        helpbutton("coursestartdate", get_string("startdate"));
        echo "</td></tr>";
    }
286
    //Line
287
    echo "<tr><td colspan=\"4\"><hr /></td></tr>";
288
289
    //Now, check modules and info and show posibilities
    if ($allmods = get_records("modules") ) {
290
        //Print option to select/deselect everything with 1 click.
291
        echo "<tr>";
292
        echo "<td align=\"right\">";
293
        echo '<b>'.get_string("include")."</b>";
294
295
296
297
298
299
300
301
302
303
        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>&nbsp;</b>';
        echo "</td><td>";
        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>";
307
        echo "</td>";
308
        echo "</tr>";
309
310
        echo "<tr><td colspan=\"4\"><hr /></td></tr>";
        $currentrow = 0;
311
        $nonrestmod = '';
312
313
314
315
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modrestore = $modname."_restore_mods";
            //If exists the lib & function
316
            $exist = "exists_".$modname;
317
318
            $restore_var = "restore_".$modname;
            $user_info_var = "restore_user_info_".$modname;
319
320
321
322
323
            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
324
325
326
                        echo "<tr class=\"r".$currentrow."\">";
                        echo "<td align=\"right\">&nbsp;";
                        echo "</td><td>";
327
                        $restore_options[1] = get_string("yes");
328
                        $restore_options[0] = get_string("no");
329
330
331
                        //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
                        //choose_from_radio($restore_options, $restore_var, $$restore_var);
                        //Print the checkbox
mjollnir_'s avatar
mjollnir_ committed
332
                        print_checkbox($restore_var, $$restore_var, $$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
333
334
                        //If backup contains user data, then show menu, else fix it to
                        //without user data
335
336
                        echo "</td><td align=\"right\">&nbsp;";
                        echo "</td><td>";
337
                        if ($info->mods[$modname]->userinfo == "true") {
338
                            $restore_user_options[1] = get_string("yes");
339
                            $restore_user_options[0] = get_string("no");
340
341
                            //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
                            //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
mjollnir_'s avatar
mjollnir_ committed
342
                            print_checkbox($user_info_var, $$user_info_var, $$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
343
344
345
                        } else {
                            //Module haven't userdata
                            echo get_string("withoutuserdata");
stronk7's avatar
stronk7 committed
346
                            echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
347
348
                        }
                        echo "</td></tr>";
toyomoyo's avatar
toyomoyo committed
349
350
351
                        if (isset($info->mods[$modname]->instances)) {
                            $instances = $info->mods[$modname]->instances;
                        }
mjollnir_'s avatar
mjollnir_ committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
                        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">&nbsp;';
                                $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>';
                        }
371
                    } else {
372
                        //Module isn't restorable
373
374
                        $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
                        $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
375
376
377
                    }
                } else {
                    //Module isn't restorable
378
379
                    $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
                    $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
380
381
382
                }
            } else {
                //Module isn't restorable
383
384
                $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
                $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
385
            }
386
            $currentrow = ($currentrow + 1) % 2;
387
388
        }
        //Line
389
        echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
390

391
392
        //Now print the Metacourse tr
        echo "<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
397
        //If metacourse are in the backup file, show menu, else fixed to no
        if ($info->backup_metacourse == "true") {
398
            $metacourse_options = array();
399
            $metacourse_options[0] = get_string("no");
400
401
            $metacourse_options[1] = get_string("yes");
            choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
402
403
        } else {
            echo get_string("no");
404
            echo "<input type=\"hidden\" id=\"menurestore_metacourse\" name=\"restore_metacourse\" value=\"0\" />";
405
406
        }
        echo "</td></tr>";
407
408
        //Now print the Users tr
        echo "<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
413
        //If some user is present in the backup file
        if ($info->backup_users == "all" or $info->backup_users == "course") {
414
            $user_options = array();
415
416
417
418
419
420
421
422
423
            //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");
424
            echo "<input type=\"hidden\" id=\"menurestore_users\" name=\"restore_users\" value=\"2\" />";
425

426
        }
427
428
        echo "</td></tr>";

429
430
431
432
433
        //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)) {
434
            echo '<label for="menurestore_groups">'.get_string ("groups").'</label>'.$helplink;
435
436
437
438
            echo "</b></td><td colspan=\"2\">";
            $group_options[RESTORE_GROUPS_NONE] = get_string('no');
            $group_options[RESTORE_GROUPS_ONLY] = get_string('yes');
        } else {
439
            echo '<label for="menurestore_groups">'.get_string ('groupsgroupings', 'group').'</label>'.$helplink;
440
441
            echo "</b></td><td colspan=\"2\">";
            $group_options[RESTORE_GROUPS_NONE] = get_string('none');
442
            $group_options[RESTORE_GROUPS_ONLY] = get_string('groupsonly', 'group');
443
444
445
446
447
448
449
450
451
452
            $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>";

453
454
        //Now print the Logs tr
        echo "<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
459
        //If logs are in the backup file, show menu, else fixed to no
        if ($info->backup_logs == "true") {
460
            $log_options = array();
461
            $log_options[0] = get_string("no");
462
463
            $log_options[1] = get_string("yes");
            choose_from_menu($log_options, "restore_logs", $restore_logs, "");
464
465
        } else {
            echo get_string("no");
466
            echo "<input type=\"hidden\" id=\"menurestore_logs\" name=\"restore_logs\" value=\"0\" />";
467
        }
468
469
470
471
        echo "</td></tr>";

        //Now print the User Files tr
        echo "<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
476
        //If user files are in the backup file, show menu, else fixed to no
        if ($info->backup_user_files == "true") {
477
            $user_file_options = array();
478
479
            $user_file_options[0] = get_string("no");
            $user_file_options[1] = get_string("yes");
480
481
482
            choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
        } else {
            echo get_string("no");
483
            echo "<input type=\"hidden\" id=\"menurestore_user_files\" name=\"restore_user_files\" value=\"0\" />";
484
        }
485
486
487
488
        echo "</td></tr>";

        //Now print the Course Files tr
        echo "<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\">";
stronk7's avatar
stronk7 committed
492
493
        echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
        echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
494
495
        //If course files are in the backup file, show menu, else fixed to no
        if ($info->backup_course_files == "true") {
496
            $course_file_options = array();
497
498
499
500
501
            $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");
502
            echo "<input type=\"hidden\" id=\"menurestore_course_files\" name=\"restore_course_files\" value=\"0\" />";
503
        }
504
        echo "</td></tr>";
505

506
507
508
509

        //Now print the Site Files tr
        echo "<tr>";
        echo "<td align=\"right\" colspan=\"2\"><b>";
510
        echo '<label for="menurestore_site_files">'.get_string ("sitefiles").'</label>';
511
512
        echo "</b></td><td colspan=\"2\">";
        //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
516
517
518
519
            $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");
520
            echo "<input type=\"hidden\" id=\"menurestore_site_files\" name=\"restore_site_files\" value=\"0\" />";
521
522
        }
        echo "</td></tr>";
523
524
        
        // do you want grade histories to be restored?
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
        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" />';
        }
543
        
544

545
546
547
        //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
548
549
550
        if (isset($info->backup_messages) && $info->backup_messages == "true") {
            echo "<tr>";
            echo "<td align=\"right\" colspan=\"2\"><b>";
551
            echo '<label for="menurestore_messages">'.get_string ('messages', 'message').'</label>';
552
            echo "</b></td><td colspan=\"2\">";
553
            $message_options = array();
554
555
556
            $message_options[0] = get_string("no");
            $message_options[1] = get_string("yes");
            choose_from_menu($message_options, "restore_messages", $restore_messages, "");
557
            echo "</td></tr>";
558
        } else {
559
            $hidden_options .= '<input type="hidden" name="restore_messages" value="0" />';
560
        }
561
      
562
563
    }
?>
tjhunt's avatar
tjhunt committed
564
</table>
565

566
<hr />
567
568
569
570
571
<?php

print_heading(get_string('rolemappings'));
$xml_file  = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";

toyomoyo's avatar
toyomoyo committed
572
$info = restore_read_xml_info($xml_file);
573

toyomoyo's avatar
toyomoyo committed
574
575
576
// fix for MDL-9068, front page course is just a normal course
$siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname");
$siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name");
577
$allroles = get_records('role');
578
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>');
581

toyomoyo's avatar
toyomoyo committed
582
583
if ($info->backup_moodle_version < 2006092801) {
    // 1.6 and below backup
584

toyomoyo's avatar
toyomoyo committed
585
    /// Editting teacher
586
    echo ('<tr><td align="right">');
587
    echo '<label for="menudefaultteacheredit">'.get_string ('defaultcourseteacher').'</label>';
toyomoyo's avatar
toyomoyo committed
588
    echo ('</td><td algin="left">');
589

toyomoyo's avatar
toyomoyo committed
590
    // get the first teacheredit legacy
591
    $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
592

593
    $editteacher = reset($roles);
594
    choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id, 'new role', '', '0');
toyomoyo's avatar
toyomoyo committed
595
    echo ('</td></tr>');
596

toyomoyo's avatar
toyomoyo committed
597
598
    /// Non-editting teacher
    echo ('<tr><td align="right">');
599
    echo '<label for="menudefaultteacher">'.get_string ('noneditingteacher').'</label>';
600
    print_string('noneditingteacher');
toyomoyo's avatar
toyomoyo committed
601
    echo ('</td><td algin="left">');
602

toyomoyo's avatar
toyomoyo committed
603
    // get the first teacheredit legacy
604
605
    $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
    $teacher = reset($roles);
606

607
    choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id, 'new role', '', '0');
toyomoyo's avatar
toyomoyo committed
608
    echo ('</td></tr>');
609
610


toyomoyo's avatar
toyomoyo committed
611
612
    /// Student
    echo ('<tr><td align="right">');
613
    echo '<label for="menudefaultstudent">'.get_string ('defaultcoursestudent').'</label>';
toyomoyo's avatar
toyomoyo committed
614
    echo ('</td><td algin="left">');
615

toyomoyo's avatar
toyomoyo committed
616
    // get the first teacheredit legacy
617
    $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
toyomoyo's avatar
toyomoyo committed
618
    $studentrole = array_shift($roles);
619

620
    choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id, 'new role', '', '0');
toyomoyo's avatar
toyomoyo committed
621
    echo ('</td></tr>');
622

toyomoyo's avatar
toyomoyo committed
623
624
625
} else {
    // 1.7 and above backup
    $roles = restore_read_xml_roles($xml_file);
626

627
    if (!empty($roles->roles)) { // possible to have course with no roles
628
629
630
        foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
            $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
        }
toyomoyo's avatar
toyomoyo committed
631

632
        foreach ($roles->roles as $roleid=>$role) {
633

634
            $mappableroles = $siteroleschoicearray;
635

636
            echo ('<tr><td align="right">');
637
            echo '<label for="menuroles_'.$roleid.'">'.$role->name." (".($role->shortname).")".'</label>';
638
            echo ('</td><td align="left">');
639

640
641
            /// first, we see if any exact role definition is found
            /// if found, that is the only option of restoring to
642

643
644
645
646
647
648
649
650
651
652
653
654
655
656
            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;
                    }
657
                }
658
            }
659
            choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, 'new role', '', '0');
660
            echo ('</td></tr>');
661
        }
662
    }
663

toyomoyo's avatar
toyomoyo committed
664
665
666
667
} // end else

echo ('</table>'); // end of role mappings table

668
?>
669
<br />
670
<div style="text-align:center">
671
672
673
674
<?php
/// Print captured hidden options, now that we have closed the table
    echo $hidden_options;
?>
stronk7's avatar
stronk7 committed
675
676
<input type="hidden" name="id"     value="<?php  p($id) ?>" />
<input type="hidden" name="launch" value="check" />
677
<input type="hidden" name="fromform" value="1" />
stronk7's avatar
stronk7 committed
678
679
<input type="submit" value="<?php  print_string("continue") ?>" />
<input type="submit" name="cancel" value="<?php  print_string("cancel") ?>" />
680
681
</div>
</div>
682
</form>
683
684
685

<?php

686
687
688
689
690
/**
 * 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. 
691
 */
692
function restore_samerole($roleid, $rolefromxml) {
693
694
    global $CFG;

695
696
697
698
699
700
    // 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 get_record('role', 'id', $roleid); 
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

    // Second guess, try the shortname
    $testroleid = get_field('role', 'id', 'shortname', $rolefromxml->shortname);
    if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
        return get_record('role', '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 = get_records_sql("SELECT roleid
           FROM {$CFG->prefix}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 get_record('role', 'id', $testroleid);
            }
        }
    }

729
730
731
    return false;
}

732
733
734
735
736
737
/**
 * 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.
738
 */
739
740
741
742
743
function restore_is_samerole($testroleid, $rolefromxml) {
    // Load the role definition from the databse.
    $rolefromdb = get_records('role_capabilities', 'roleid', $testroleid, '', 'capability,permission'); 
    if (!$rolefromdb) {
        return false;
744
    }
745
746
747
748
749
750
751
752
753
754
755
756

    // 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;
        }
757
758
759
    }
    return true;
}
760
?>