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