~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to backup/restore_form.html

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php //$Id: restore_form.html,v 1.43.2.3 2007/06/14 08:51:55 toyomoyo Exp $
 
1
<?php //$Id: restore_form.html,v 1.60.2.17 2009/01/02 16:53:30 stronk7 Exp $
2
2
    //This page prints the restore form to select everything yo want
3
3
    //to restore. Form is dinamically buid, depending of "info" object
4
4
    //that contains the backup contents and depending of every mod
12
12
      error( 'course_header object missing from session' );
13
13
    }
14
14
 
 
15
    $restore_gradebook_history =  optional_param('restore_gradebook_history', 0, PARAM_INT);
 
16
 
15
17
    //Check that we have all we need
16
18
    //backup_unique_code
17
19
    $backup_unique_code = required_param( 'backup_unique_code' );
27
29
            error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
28
30
        }
29
31
    } else {
30
 
        if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
31
 
            error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");   
 
32
        if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM))) {
 
33
            error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
32
34
        }
33
35
    }
34
36
 
35
37
    //Check site
36
38
    if (!$site = get_site()) {
37
39
        error("Site not found!");
38
 
    } 
 
40
    }
39
41
 
40
42
    //Checks for the required files/functions to restore every mod
41
43
    $count = 0;
42
 
    if ($allmods = get_records("modules") ) { 
 
44
    if ($allmods = get_records("modules") ) {
43
45
        foreach ($allmods as $mod) {
44
46
            $modname = $mod->name;
45
47
            $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
70
72
    if (!isset($restore_metacourse)) {
71
73
        $restore_metacourse = 1;
72
74
    }
73
 
   
 
75
 
74
76
    if (!isset($restore_users)) {
75
77
        $restore_users = 1;
76
78
    }
77
 
   
 
79
 
 
80
    if (!isset($restore_groups)) {
 
81
        if (empty($CFG->enablegroupings)) {
 
82
            $restore_groups = RESTORE_GROUPS_ONLY;
 
83
        } else {
 
84
            $restore_groups = RESTORE_GROUPS_GROUPINGS;
 
85
        }
 
86
    }
 
87
 
78
88
    if (!isset($restore_logs)) {
79
89
        $restore_logs = 1;
80
90
    }
87
97
        $restore_course_files = 1;
88
98
    }
89
99
 
 
100
    if (!isset($restore_site_files)) {
 
101
        $restore_site_files = 1;
 
102
    }
 
103
 
90
104
    if (!isset($restore_messages)) {
91
105
        $restore_messages = 1;
92
 
        }
93
 
 
 
106
    }
 
107
 
 
108
    if (!isset($restore_blogs)) {
 
109
        $restore_blogs = 1;
 
110
    }
 
111
 
 
112
    $cancreatecourses = user_can_create_courses();
94
113
    if (!isset($restore_restoreto)) {
95
 
        if (!user_can_create_courses()) {
 
114
        if (!$cancreatecourses) {
96
115
            $restore_restoreto = 1;
97
116
        } else {
98
117
            $restore_restoreto = 2;
99
118
        }
100
119
    }
101
120
 
 
121
    if (!isset($course_header->category->id)) {
 
122
        $course_header->category->id = 0;
 
123
    }
 
124
 
102
125
    if(!isset($form1->startdate)) {
103
 
        $form1->startdate = $course_header->course_startdate; //$course_header->course_startdate;    
 
126
        $form1->startdate = $course_header->course_startdate; //$course_header->course_startdate;
104
127
    }
105
128
 
106
129
    if (empty($form1->shortname)) {
107
 
       $form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname; 
 
130
       $form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname;
108
131
    }
109
132
 
110
133
    if (empty($form1->fullname)) {
111
 
      $form1->fullname = $course_header->course_fullname; // '_fullname';   //$course_header->course_fullname; 
 
134
      $form1->fullname = $course_header->course_fullname; // '_fullname';   //$course_header->course_fullname;
112
135
    }
113
136
 
114
137
    if ($count == 0) {
167
190
    //Print the full tr
168
191
    echo "<tr>";
169
192
    echo "<td align=\"right\"><b>";
170
 
    echo get_string("restoreto").":</b>";
 
193
    echo '<label for="menurestore_restoreto">'.get_string ('restoreto').'</label>';
 
194
    echo "</b>";
171
195
    echo "</td><td colspan=\"3\">";
172
 
    
 
196
 
 
197
 
173
198
    // permission should have been checked already
174
 
    
 
199
 
175
200
    /**
176
 
     * if user has manageactivities in any course, we show 
177
 
     *      existingcoursedeleting 
 
201
     * if user has manageactivities in any course and we are not restoring from within SITEID
 
202
     *      existingcoursedeleting
178
203
     *      existingcourseadding
179
204
     * else we show
180
205
     *      currentcoursedeleting
182
207
     * if user has course:create in any category, we show
183
208
     *      newcourse
184
209
     */
185
 
    
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
 
210
 
 
211
    // Non-cached - get accessinfo
 
212
    if (isset($USER->access)) {
 
213
        $accessinfo = $USER->access;
 
214
    } else {
 
215
        $accessinfo = get_user_access_sitewide($USER->id);
 
216
    }
 
217
 
 
218
    $mycourses = get_user_courses_bycap($USER->id, 'moodle/site:restore', $accessinfo, true);
 
219
 
 
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
 
222
 
 
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");
198
230
    }
199
 
    
 
231
 
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");
203
235
    }
204
 
   
 
236
 
 
237
/// Acummulator for hidden options and proper XHTML output
 
238
    $hidden_options = '';
 
239
 
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
208
 
        
 
242
    if ($cancreatecourses) {  //display these fields conditionally
 
243
 
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');
213
 
        echo " :</td>";
 
247
        echo '<label for="menurestore_restorecatto">'.get_string ('category').'</label>';
 
248
        echo "</td>";
214
249
        echo "<td>";
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
 
251
        if ($id != SITEID) {
 
252
            make_categories_list($categories, $parents);
 
253
            choose_from_menu($categories, "restore_restorecatto", $course_header->category->id, "");
 
254
        } else {
 
255
            print_string('notavailable');
 
256
            echo '<input type="hidden" name="restore_restorecatto" id="menurestore_restorecatto" value="0" />';
 
257
        }
216
258
        echo "</td>";
217
259
        echo "</tr>";
218
 
        
 
260
 
219
261
        echo "<tr valign=\"top\" >";
220
262
        echo "<td align=\"right\">";
221
 
        print_string("shortname");
222
 
        echo " :</td>";
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>';
 
264
        echo "</td>";
 
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"]);
 
268
        echo "</td>";
 
269
        echo "</tr>";
226
270
        echo "<tr valign=\"top\" >";
227
271
        echo "<td align=\"right\">";
228
 
        print_string("fullname");
229
 
        echo " :</td>";
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>';
 
273
        echo "</td>";
 
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"); 
237
 
        echo ":</td><td>"; 
238
 
        print_date_selector("startday", "startmonth", "startyear", $form1->startdate);
239
 
        helpbutton("coursestartdate", get_string("startdate"));
 
280
        print_string("startdate");
 
281
        echo "</td><td>";
 
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"));
 
288
        } else {
 
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" />';
 
293
        }
240
294
        echo "</td></tr>";
 
295
    } else {
 
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" />';
241
300
    }
242
301
    //Line
243
302
    echo "<tr><td colspan=\"4\"><hr /></td></tr>";
246
305
        //Print option to select/deselect everything with 1 click.
247
306
        echo "<tr>";
248
307
        echo "<td align=\"right\">";
249
 
        echo '<b>'.get_string("include").":</b>";
 
308
        echo '<b>'.get_string("include")."</b>";
250
309
        echo "</td><td>";
251
310
        echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
252
311
             get_string("all")."</a>/";
264
323
        echo "</tr>";
265
324
        echo "<tr><td colspan=\"4\"><hr /></td></tr>";
266
325
        $currentrow = 0;
 
326
        $nonrestmod = '';
267
327
        foreach ($allmods as $mod) {
268
328
            $modname = $mod->name;
269
329
            $modrestore = $modname."_restore_mods";
280
340
                        echo "<td align=\"right\">&nbsp;";
281
341
                        echo "</td><td>";
282
342
                        $restore_options[1] = get_string("yes");
283
 
                        $restore_options[0] = get_string("no"); 
 
343
                        $restore_options[0] = get_string("no");
284
344
                        //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
285
345
                        //choose_from_radio($restore_options, $restore_var, $$restore_var);
286
346
                        //Print the checkbox
291
351
                        echo "</td><td>";
292
352
                        if ($info->mods[$modname]->userinfo == "true") {
293
353
                            $restore_user_options[1] = get_string("yes");
294
 
                            $restore_user_options[0] = get_string("no"); 
 
354
                            $restore_user_options[0] = get_string("no");
295
355
                            //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
296
356
                            //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
297
357
                            print_checkbox($user_info_var, $$user_info_var, $$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
314
374
                                echo '</td><td align="right">&nbsp;';
315
375
                                $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
316
376
                                $$var = optional_param($var,1);
317
 
                                if ($info->mods[$modname]->instances[$instance->id]->userinfo == 'true') {
 
377
                                if (!empty($info->mods[$modname]->instances) && ($info->mods[$modname]->instances[$instance->id]->userinfo == 'true')) {
318
378
                                    print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
319
379
                                } else {
320
380
                                    echo '<input type="hidden" name="'.$var.'" value="0" />';
346
406
        //Now print the Metacourse tr
347
407
        echo "<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, "");
356
417
        } else {
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\" />";
359
420
        }
360
421
        echo "</td></tr>";
361
422
        //Now print the Users tr
362
423
        echo "<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, "");
375
437
        } else {
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\" />";
378
440
 
379
441
        }
380
442
        echo "</td></tr>";
381
443
 
 
444
        //Now print the Groups tr (assume there is no $info->backup_groups)
 
445
        echo "<tr>";
 
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');
 
453
        } else {
 
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.
 
460
 
 
461
        } /*else {
 
462
            echo get_string('none');
 
463
            echo "<input type=\"hidden\" name=\"restore_groups\" value=\"2\" />";
 
464
        }*/
 
465
        choose_from_menu($group_options, 'restore_groups', $restore_groups, '');
 
466
        echo "</td></tr>";
 
467
 
382
468
        //Now print the Logs tr
383
469
        echo "<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, "");
392
479
        } else {
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\" />";
395
482
        }
396
483
        echo "</td></tr>";
397
484
 
398
485
        //Now print the User Files tr
399
486
        echo "<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, "");
408
496
        } else {
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\" />";
411
499
        }
412
500
        echo "</td></tr>";
413
501
 
414
502
        //Now print the Course Files tr
415
503
        echo "<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, "");
426
515
        } else {
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\" />";
429
518
        }
430
519
        echo "</td></tr>";
431
520
 
432
 
        //Now print the Messages tr
 
521
 
 
522
        //Now print the Site Files tr
433
523
        echo "<tr>";
434
524
        echo "<td align=\"right\" colspan=\"2\"><b>";
435
 
        
 
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, "");
 
533
        } else {
 
534
            echo get_string("no");
 
535
            echo "<input type=\"hidden\" id=\"menurestore_site_files\" name=\"restore_site_files\" value=\"0\" />";
 
536
        }
 
537
        echo "</td></tr>";
 
538
 
 
539
        // do you want grade histories to be restored?
 
540
        if (empty($CFG->disablegradehistory)) {
 
541
            echo "<tr>";
 
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, "");
 
550
            } else {
 
551
                echo get_string("no");
 
552
                echo "<input type=\"hidden\" id=\"menurestore_gradebook_history\" name=\"restore_gradebook_history\" value=\"0\" />";
 
553
            }
 
554
            echo "</td></tr>";
 
555
        } else {
 
556
            $hidden_options .= '<input type="hidden" name="restore_gradebook_history" value="0" />';
 
557
        }
 
558
 
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)) {
 
564
            echo "<tr>";
 
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, "");
445
 
        } else {
446
 
            echo "&nbsp;</b></td><td colspan=\"2\">";
447
 
            echo "<input type=\"hidden\" name=\"restore_messages\" value=\"0\" />";
448
 
        }
449
 
        echo "</td></tr>";
 
572
            echo "</td></tr>";
 
573
        } else {
 
574
            $hidden_options .= '<input type="hidden" name="restore_messages" value="0" />';
 
575
        }
 
576
 
 
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)) {
 
582
            echo "<tr>";
 
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, "");
 
590
            echo "</td></tr>";
 
591
        } else {
 
592
            $hidden_options .= '<input type="hidden" name="restore_blogs" value="0" />';
 
593
        }
 
594
 
450
595
    }
451
596
?>
452
597
</table>
453
598
 
454
 
<hr/>
 
599
<hr />
455
600
<?php
456
601
 
457
602
print_heading(get_string('rolemappings'));
460
605
$info = restore_read_xml_info($xml_file);
461
606
 
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');
465
611
 
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>');
468
614
 
469
615
if ($info->backup_moodle_version < 2006092801) {
470
616
    // 1.6 and below backup
471
 
    
 
617
 
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">');
476
 
    
 
622
 
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));
479
625
 
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>');
483
 
    
 
629
 
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">');
488
 
    
 
635
 
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);
492
 
    
 
637
    $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
 
638
    $teacher = reset($roles);
 
639
 
493
640
    choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id, 'new role', '', '0');
494
641
    echo ('</td></tr>');
495
 
     
496
 
    
 
642
 
 
643
 
497
644
    /// Student
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">');
501
 
    
 
648
 
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);
505
 
    
 
652
 
506
653
    choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id, 'new role', '', '0');
507
654
    echo ('</td></tr>');
508
 
  
 
655
 
509
656
} else {
510
657
    // 1.7 and above backup
511
658
    $roles = restore_read_xml_roles($xml_file);
512
 
    
 
659
 
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].")";
516
663
        }
517
664
 
518
665
        foreach ($roles->roles as $roleid=>$role) {
 
666
 
 
667
            $mappableroles = $siteroleschoicearray;
 
668
 
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">');
522
 
    
523
 
            // see if any short name match
524
 
            $matchrole = 0;
525
 
            foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
526
 
                if ($siteroleshortname == $role->shortname) {
527
 
                    $matchrole = $siteroleid;
528
 
                    break;  
529
 
                }  
 
672
 
 
673
            /// first, we see if any exact role definition is found
 
674
            /// if found, that is the only option of restoring to
 
675
 
 
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.")";
 
681
            } else {
 
682
                // no exact role found, let's try to match shortname
 
683
                // this is useful in situations where basic roles differ slightly in definition
 
684
                $matchrole = 0;
 
685
                foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
 
686
                    if ($siteroleshortname == $role->shortname) {
 
687
                        $matchrole = $siteroleid;
 
688
                        break;
 
689
                    }
 
690
                }
530
691
            }
531
 
    
532
 
            choose_from_menu ($siteroleschoicearray, "roles_".$roleid, $matchrole, 'new role', '', '0');
533
 
            echo ('</td></tr>'); 
 
692
            choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, 'new role', '', '0');
 
693
            echo ('</td></tr>');
534
694
        }
535
695
    }
536
 
    
 
696
 
537
697
} // end else
538
698
 
539
699
echo ('</table>'); // end of role mappings table
541
701
?>
542
702
<br />
543
703
<div style="text-align:center">
 
704
<?php
 
705
/// Print captured hidden options, now that we have closed the table
 
706
    echo $hidden_options;
 
707
?>
544
708
<input type="hidden" name="id"     value="<?php  p($id) ?>" />
545
709
<input type="hidden" name="launch" value="check" />
546
710
<input type="hidden" name="fromform" value="1" />
549
713
</div>
550
714
</div>
551
715
</form>
 
716
 
 
717
<?php
 
718
 
 
719
/**
 
720
 * Look for a role in the database with exactly the same definition as the one in the backup file.
 
721
 *
 
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.
 
724
 */
 
725
function restore_samerole($roleid, $rolefromxml) {
 
726
    global $CFG;
 
727
 
 
728
    // First we try some intelligent guesses, then, if none of those work, we do a more extensive
 
729
    // search.
 
730
 
 
731
    // First guess, try let's use the id
 
732
    if (restore_is_samerole($roleid, $rolefromxml)) {
 
733
        return get_record('role', 'id', $roleid);
 
734
    }
 
735
 
 
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);
 
740
    }
 
741
 
 
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
 
744
    // definition.
 
745
    $extracondition = '';
 
746
    if ($testroleid) {
 
747
        $extracondition = "AND roleid <> $testroleid";
 
748
    }
 
749
    $candidateroleids = get_records_sql("SELECT roleid
 
750
           FROM {$CFG->prefix}role_capabilities
 
751
           WHERE roleid <> $roleid $extracondition
 
752
           GROUP BY roleid
 
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);
 
758
            }
 
759
        }
 
760
    }
 
761
 
 
762
    return false;
 
763
}
 
764
 
 
765
/**
 
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.
 
771
 */
 
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');
 
775
    if (!$rolefromdb) {
 
776
        return false;
 
777
    }
 
778
 
 
779
    // Quick check, do they have the permissions on the same number of capabilities?
 
780
    if (count($rolefromdb) != count($rolefromxml->capabilities)) {
 
781
        return false;
 
782
    }
 
783
 
 
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) {
 
788
            return false;
 
789
        }
 
790
    }
 
791
    return true;
 
792
}
 
793
?>