MDL-12531, make the new member value available to all the affected plugins, thanks...
[moodle-linuxchix.git] / backup / restore_form.html
blob9ee95c35d05689b1e88b1b337708b1026b9085c8
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 //Check admin
27 if (!empty($id)) {
28 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
29 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
31 } else {
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");
37 //Check site
38 if (!$site = get_site()) {
39 error("Site not found!");
42 //Checks for the required files/functions to restore every mod
43 $count = 0;
44 if ($allmods = get_records("modules") ) {
45 foreach ($allmods as $mod) {
46 $modname = $mod->name;
47 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
48 $modrestore = $modname."_restore_mods";
49 if (file_exists($modfile)) {
50 include_once($modfile);
51 if (function_exists($modrestore)) {
52 $var = "exists_".$modname;
53 $$var = true;
54 $count++;
57 //Check data
58 //Check module info
59 $var = "restore_".$modname;
60 if (!isset($$var)) {
61 $$var = 1;
63 //Check include user info
64 $var = "restore_user_info_".$modname;
65 if (!isset($$var)) {
66 $$var = 1;
71 //Check other parameters
72 if (!isset($restore_metacourse)) {
73 $restore_metacourse = 1;
76 if (!isset($restore_users)) {
77 $restore_users = 1;
80 if (!isset($restore_groups)) {
81 if (empty($CFG->enablegroupings)) {
82 $restore_groups = RESTORE_GROUPS_ONLY;
83 } else {
84 $restore_groups = RESTORE_GROUPS_GROUPINGS;
88 if (!isset($restore_logs)) {
89 $restore_logs = 1;
92 if (!isset($restore_user_files)) {
93 $restore_user_files = 1;
96 if (!isset($restore_course_files)) {
97 $restore_course_files = 1;
100 if (!isset($restore_site_files)) {
101 $restore_site_files = 1;
104 if (!isset($restore_messages)) {
105 $restore_messages = 1;
108 if (!isset($restore_restoreto)) {
109 if (!user_can_create_courses()) {
110 $restore_restoreto = 1;
111 } else {
112 $restore_restoreto = 2;
116 if (!isset($course_header->category->id)) {
117 $course_header->category->id = 0;
120 if(!isset($form1->startdate)) {
121 $form1->startdate = $course_header->course_startdate; //$course_header->course_startdate;
124 if (empty($form1->shortname)) {
125 $form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname;
128 if (empty($form1->fullname)) {
129 $form1->fullname = $course_header->course_fullname; // '_fullname'; //$course_header->course_fullname;
132 if ($count == 0) {
133 notice("No restorable modules are installed!");
138 <script type="text/javascript">
139 <!--
140 function selectItemInMenuByName(formId, menuName, selectIndex ) {
141 myForm = document.getElementById(formId)
142 for (i=0,n=myForm.elements.length;i<n;i++) {
143 myLen = menuName.length;
144 myName = myForm.elements[i].name;
145 myType = myForm.elements[i].type;
146 if (myName.substring(0,myLen) == menuName && myType == "select-one") {
147 myForm.elements[i].options[selectIndex].selected = true;
152 function selectItemInRadioByName(formId, radioName, selectIndex ) {
153 myForm = document.getElementById(formId)
154 for (i=0,n=myForm.elements.length;i<n;i++) {
155 myLen = radioName.length;
156 myName = myForm.elements[i].name;
157 myType = myForm.elements[i].type;
158 if (myName.substring(0,myLen) == radioName && myType == "radio") {
159 myRadioGroup = myForm.elements[myName];
160 myRadioGroup[selectIndex].checked = true;
165 function selectItemInCheckboxByName(formId, checkName, checked ) {
166 myForm = document.getElementById(formId)
167 for (i=0,n=myForm.elements.length;i<n;i++) {
168 myLen = checkName.length;
169 myName = myForm.elements[i].name;
170 myType = myForm.elements[i].type;
171 if (myName.substring(0,myLen) == checkName && myType == "checkbox") {
172 myForm.elements[i].checked = checked;
177 </script>
179 <form id="form1" method="post" action="restore.php">
180 <div>
181 <table cellpadding="5" class="boxaligncenter">
182 <?php
184 //First, course destination
185 //Print the full tr
186 echo "<tr>";
187 echo "<td align=\"right\"><b>";
188 echo '<label for="menurestore_restoreto">'.get_string ('restoreto').'</label>';
189 echo "</b>";
190 echo "</td><td colspan=\"3\">";
193 // permission should have been checked already
196 * if user has manageactivities in any course, we show
197 * existingcoursedeleting
198 * existingcourseadding
199 * else we show
200 * currentcoursedeleting
201 * currentcourse
202 * if user has course:create in any category, we show
203 * newcourse
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);
214 // if the user can manage 2 or more courses,
215 // or if the only course the user can manage is not the current course
216 // we show options for existing courses
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 $restore_restoreto_options[0] = get_string("existingcoursedeleting");
221 $restore_restoreto_options[1] = get_string("existingcourseadding");
222 // else if the user can write to current course
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");
228 // if user can create any course at all, give the option
229 if (user_can_create_courses()) {
230 $restore_restoreto_options[2] = get_string("newcourse");
233 /// Acummulator for hidden options and proper XHTML output
234 $hidden_options = '';
236 choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
237 echo "</td></tr>";
238 if (user_can_create_courses()) { //display these fields conditionally
240 // find the list of cates user can edit
241 echo "<tr valign=\"top\" >";
242 echo "<td align=\"right\">";
243 echo '<label for="menurestore_restorecatto">'.get_string ('category').'</label>';
244 echo "</td>";
245 echo "<td>";
246 make_categories_list($categories, $parents);
247 choose_from_menu($categories, "restore_restorecatto", $course_header->category->id, "");
248 echo "</td>";
249 echo "</tr>";
251 echo "<tr valign=\"top\" >";
252 echo "<td align=\"right\">";
253 echo '<label for="shortnamefield">'.get_string ('shortname').'</label>';
254 echo "</td>";
255 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"]);
258 echo "</td>";
259 echo "</tr>";
260 echo "<tr valign=\"top\" >";
261 echo "<td align=\"right\">";
262 echo '<label for="fullnamefield">'.get_string ('fullname').'</label>';
263 echo "</td>";
264 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 echo"</td></tr>";
268 echo "<tr valign=\"top\"> ";
269 echo "<td align=\"right\"> ";
270 print_string("startdate");
271 echo "</td><td>";
272 /// Show the roll dates option only if the backup course has a start date
273 /// (some formats like main page, social..., haven't it and rolling dates
274 /// from 0 produces crazy dates. MDL-10125
275 if ($form1->startdate) {
276 print_date_selector("startday", "startmonth", "startyear", $form1->startdate);
277 } else {
278 print_string('notavailable');
279 echo '<input type="hidden" name="startyear" value="0" />';
280 echo '<input type="hidden" name="startmonth" value="0" />';
281 echo '<input type="hidden" name="startday" value="0" />';
283 helpbutton("coursestartdate", get_string("startdate"));
284 echo "</td></tr>";
286 //Line
287 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
288 //Now, check modules and info and show posibilities
289 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 echo "</td><td>";
295 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', true);\">".
296 get_string("all")."</a>/";
297 echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'restore_', false);\">".
298 get_string("none")."</a>";
299 echo "</td>";
300 echo "<td align=\"right\">";
301 echo '<b>&nbsp;</b>';
302 echo "</td><td>";
303 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 echo "<tr><td colspan=\"4\"><hr /></td></tr>";
310 $currentrow = 0;
311 $nonrestmod = '';
312 foreach ($allmods as $mod) {
313 $modname = $mod->name;
314 $modrestore = $modname."_restore_mods";
315 //If exists the lib & function
316 $exist = "exists_".$modname;
317 $restore_var = "restore_".$modname;
318 $user_info_var = "restore_user_info_".$modname;
319 if (isset($$exist)) {
320 if ($$exist) {
321 //Now check that we have that module info in the backup file
322 if (isset($info->mods[$modname]) && $info->mods[$modname]->backup == "true") {
323 //Print the full tr
324 echo "<tr class=\"r".$currentrow."\">";
325 echo "<td align=\"right\">&nbsp;";
326 echo "</td><td>";
327 $restore_options[1] = get_string("yes");
328 $restore_options[0] = get_string("no");
329 //choose_from_menu($restore_options, $restore_var, $$restore_var, "");
330 //choose_from_radio($restore_options, $restore_var, $$restore_var);
331 //Print the checkbox
332 print_checkbox($restore_var, $$restore_var, $$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)');
333 //If backup contains user data, then show menu, else fix it to
334 //without user data
335 echo "</td><td align=\"right\">&nbsp;";
336 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 //choose_from_menu($restore_user_options, $user_info_var, $$user_info_var, "");
341 //choose_from_radio($restore_user_options, $user_info_var, $$user_info_var);
342 print_checkbox($user_info_var, $$user_info_var, $$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)');
343 } else {
344 //Module haven't userdata
345 echo get_string("withoutuserdata");
346 echo "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
348 echo "</td></tr>";
349 if (isset($info->mods[$modname]->instances)) {
350 $instances = $info->mods[$modname]->instances;
352 if (!empty($instances) && is_array($instances)) {
353 echo '<tr><td></td><td colspan="3"><table class="restore-form-instances">';
354 foreach ($instances as $instance) {
355 echo '<tr><td>';
356 $var = 'restore_'.$modname.'_instance_'.$instance->id;
357 $$var = optional_param($var,1);
358 print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'restore_'.$modname.'\'].checked=1;');
359 echo '</td><td align="right">&nbsp;';
360 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
361 $$var = optional_param($var,1);
362 if ($info->mods[$modname]->instances[$instance->id]->userinfo == 'true') {
363 print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;');
364 } else {
365 echo '<input type="hidden" name="'.$var.'" value="0" />';
367 echo '</td></tr>';
369 echo '</table></td></tr>';
371 } else {
372 //Module isn't restorable
373 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
374 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
376 } else {
377 //Module isn't restorable
378 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
379 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
381 } else {
382 //Module isn't restorable
383 $nonrestmod .= "<input type=\"hidden\" name=\"$restore_var\" value=\"0\" />";
384 $nonrestmod .= "<input type=\"hidden\" name=\"$user_info_var\" value=\"0\" />";
386 $currentrow = ($currentrow + 1) % 2;
388 //Line
389 echo "<tr><td colspan=\"4\">$nonrestmod<hr /></td></tr>";
391 //Now print the Metacourse tr
392 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 //If metacourse are in the backup file, show menu, else fixed to no
397 if ($info->backup_metacourse == "true") {
398 $metacourse_options = array();
399 $metacourse_options[0] = get_string("no");
400 $metacourse_options[1] = get_string("yes");
401 choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, "");
402 } else {
403 echo get_string("no");
404 echo "<input type=\"hidden\" id=\"menurestore_metacourse\" name=\"restore_metacourse\" value=\"0\" />";
406 echo "</td></tr>";
407 //Now print the Users tr
408 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 //If some user is present in the backup file
413 if ($info->backup_users == "all" or $info->backup_users == "course") {
414 $user_options = array();
415 //If all users are in the backup file
416 if ($info->backup_users == "all") {
417 $user_options[0] = get_string("all");
419 $user_options[1] = get_string("course");
420 $user_options[2] = get_string("none");
421 choose_from_menu($user_options, "restore_users", $restore_users, "");
422 } else {
423 echo get_string("none");
424 echo "<input type=\"hidden\" id=\"menurestore_users\" name=\"restore_users\" value=\"2\" />";
427 echo "</td></tr>";
429 //Now print the Groups tr (assume there is no $info->backup_groups)
430 echo "<tr>";
431 echo "<td align=\"right\" colspan=\"2\"><b>";
432 $helplink = helpbutton('grouprestore', get_string('groups'), '', true, false, '', true);
433 if (empty($CFG->enablegroupings)) {
434 echo '<label for="menurestore_groups">'.get_string ("groups").'</label>'.$helplink;
435 echo "</b></td><td colspan=\"2\">";
436 $group_options[RESTORE_GROUPS_NONE] = get_string('no');
437 $group_options[RESTORE_GROUPS_ONLY] = get_string('yes');
438 } else {
439 echo '<label for="menurestore_groups">'.get_string ('groupsgroupings', 'group').'</label>'.$helplink;
440 echo "</b></td><td colspan=\"2\">";
441 $group_options[RESTORE_GROUPS_NONE] = get_string('none');
442 $group_options[RESTORE_GROUPS_ONLY] = get_string('groupsonly', 'group');
443 $group_options[RESTORE_GROUPINGS_ONLY] = get_string('groupingsonly', 'group');
444 $group_options[RESTORE_GROUPS_GROUPINGS] = get_string('groupsgroupings', 'group'); //all.
446 } /*else {
447 echo get_string('none');
448 echo "<input type=\"hidden\" name=\"restore_groups\" value=\"2\" />";
450 choose_from_menu($group_options, 'restore_groups', $restore_groups, '');
451 echo "</td></tr>";
453 //Now print the Logs tr
454 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 //If logs are in the backup file, show menu, else fixed to no
459 if ($info->backup_logs == "true") {
460 $log_options = array();
461 $log_options[0] = get_string("no");
462 $log_options[1] = get_string("yes");
463 choose_from_menu($log_options, "restore_logs", $restore_logs, "");
464 } else {
465 echo get_string("no");
466 echo "<input type=\"hidden\" id=\"menurestore_logs\" name=\"restore_logs\" value=\"0\" />";
468 echo "</td></tr>";
470 //Now print the User Files tr
471 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 //If user files are in the backup file, show menu, else fixed to no
476 if ($info->backup_user_files == "true") {
477 $user_file_options = array();
478 $user_file_options[0] = get_string("no");
479 $user_file_options[1] = get_string("yes");
480 choose_from_menu($user_file_options, "restore_user_files", $restore_user_files, "");
481 } else {
482 echo get_string("no");
483 echo "<input type=\"hidden\" id=\"menurestore_user_files\" name=\"restore_user_files\" value=\"0\" />";
485 echo "</td></tr>";
487 //Now print the Course Files tr
488 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\">";
492 echo "<input type=\"hidden\" name=\"backup_unique_code\" value=\"$backup_unique_code\" />";
493 echo "<input type=\"hidden\" name=\"file\" value=\"$file\" />";
494 //If course files are in the backup file, show menu, else fixed to no
495 if ($info->backup_course_files == "true") {
496 $course_file_options = array();
497 $course_file_options[0] = get_string("no");
498 $course_file_options[1] = get_string("yes");
499 choose_from_menu($course_file_options, "restore_course_files", $restore_course_files, "");
500 } else {
501 echo get_string("no");
502 echo "<input type=\"hidden\" id=\"menurestore_course_files\" name=\"restore_course_files\" value=\"0\" />";
504 echo "</td></tr>";
507 //Now print the Site Files tr
508 echo "<tr>";
509 echo "<td align=\"right\" colspan=\"2\"><b>";
510 echo '<label for="menurestore_site_files">'.get_string ("sitefiles").'</label>';
511 echo "</b></td><td colspan=\"2\">";
512 //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 $site_file_options[0] = get_string("no");
516 $site_file_options[1] = get_string("yes");
517 choose_from_menu($site_file_options, "restore_site_files", $restore_site_files, "");
518 } else {
519 echo get_string("no");
520 echo "<input type=\"hidden\" id=\"menurestore_site_files\" name=\"restore_site_files\" value=\"0\" />";
522 echo "</td></tr>";
524 // do you want grade histories to be restored?
525 if (empty($CFG->disablegradehistory)) {
526 echo "<tr>";
527 echo "<td align=\"right\" colspan=\"2\"><b>";
528 echo '<label for="menurestore_gradebook_history">'.get_string ('gradebookhistories', 'grades').'</label>';
529 echo "</b></td><td colspan=\"2\">";
530 if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
531 $gradebook_history_options = array();
532 $gradebook_history_options[0] = get_string("no");
533 $gradebook_history_options[1] = get_string("yes");
534 choose_from_menu($gradebook_history_options, "restore_gradebook_history", $restore_gradebook_history, "");
535 } else {
536 echo get_string("no");
537 echo "<input type=\"hidden\" id=\"menurestore_gradebook_history\" name=\"restore_gradebook_history\" value=\"0\" />";
539 echo "</td></tr>";
540 } else {
541 $hidden_options .= '<input type="hidden" name="restore_gradebook_history" value="0" />';
545 //This tr is slighty different. Everything becomes hidden if
546 //we haven't messages is the backup, to avoid confusions to users.
547 //If messages are in the backup file, show menu, else fixed to no and show nothing
548 if (isset($info->backup_messages) && $info->backup_messages == "true") {
549 echo "<tr>";
550 echo "<td align=\"right\" colspan=\"2\"><b>";
551 echo '<label for="menurestore_messages">'.get_string ('messages', 'messages').'</label>';
552 echo "</b></td><td colspan=\"2\">";
553 $message_options = array();
554 $message_options[0] = get_string("no");
555 $message_options[1] = get_string("yes");
556 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" />';
564 </table>
566 <hr />
567 <?php
569 print_heading(get_string('rolemappings'));
570 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
572 $info = restore_read_xml_info($xml_file);
574 // fix for MDL-9068, front page course is just a normal course
575 $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname");
576 $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name");
577 $allroles = get_records('role');
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>');
582 if ($info->backup_moodle_version < 2006092801) {
583 // 1.6 and below backup
585 /// Editting teacher
586 echo ('<tr><td align="right">');
587 echo '<label for="menudefaultteacheredit">'.get_string ('defaultcourseteacher').'</label>';
588 echo ('</td><td algin="left">');
590 // get the first teacheredit legacy
591 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
593 $editteacher = reset($roles);
594 choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id, 'new role', '', '0');
595 echo ('</td></tr>');
597 /// Non-editting teacher
598 echo ('<tr><td align="right">');
599 echo '<label for="menudefaultteacher">'.get_string ('noneditingteacher').'</label>';
600 print_string('noneditingteacher');
601 echo ('</td><td algin="left">');
603 // get the first teacheredit legacy
604 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
605 $teacher = reset($roles);
607 choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id, 'new role', '', '0');
608 echo ('</td></tr>');
611 /// Student
612 echo ('<tr><td align="right">');
613 echo '<label for="menudefaultstudent">'.get_string ('defaultcoursestudent').'</label>';
614 echo ('</td><td algin="left">');
616 // get the first teacheredit legacy
617 $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM));
618 $studentrole = array_shift($roles);
620 choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id, 'new role', '', '0');
621 echo ('</td></tr>');
623 } else {
624 // 1.7 and above backup
625 $roles = restore_read_xml_roles($xml_file);
627 if (!empty($roles->roles)) { // possible to have course with no roles
628 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
629 $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
632 foreach ($roles->roles as $roleid=>$role) {
634 $mappableroles = $siteroleschoicearray;
636 echo ('<tr><td align="right">');
637 echo '<label for="menuroles_'.$roleid.'">'.$role->name." (".($role->shortname).")".'</label>';
638 echo ('</td><td align="left">');
640 /// first, we see if any exact role definition is found
641 /// if found, that is the only option of restoring to
643 if ($samerole = restore_samerole($roleid, $role)) {
644 $matchrole = $samerole->id;
645 // if an exact role is found, it does not matter whether this user can assign this role or not,
646 // this will be presented as a valid option regardless
647 $mappableroles[$samerole->id] = $allroles[$samerole->id]->name." (". $allroles[$samerole->id]->shortname.")";
648 } else {
649 // no exact role found, let's try to match shortname
650 // this is useful in situations where basic roles differ slightly in definition
651 $matchrole = 0;
652 foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
653 if ($siteroleshortname == $role->shortname) {
654 $matchrole = $siteroleid;
655 break;
659 choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, 'new role', '', '0');
660 echo ('</td></tr>');
664 } // end else
666 echo ('</table>'); // end of role mappings table
669 <br />
670 <div style="text-align:center">
671 <?php
672 /// Print captured hidden options, now that we have closed the table
673 echo $hidden_options;
675 <input type="hidden" name="id" value="<?php p($id) ?>" />
676 <input type="hidden" name="launch" value="check" />
677 <input type="hidden" name="fromform" value="1" />
678 <input type="submit" value="<?php print_string("continue") ?>" />
679 <input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
680 </div>
681 </div>
682 </form>
684 <?php
687 * Look for a role in the database with exactly the same definition as the one in the backup file.
689 * @param integer $roleid the id that the role in the backup files had on the old server.
690 * @param object $role the rest of the definition of the role from the backup file.
692 function restore_samerole($roleid, $rolefromxml) {
693 global $CFG;
695 // First we try some intelligent guesses, then, if none of those work, we do a more extensive
696 // search.
698 // First guess, try let's use the id
699 if (restore_is_samerole($roleid, $rolefromxml)) {
700 return get_record('role', 'id', $roleid);
703 // Second guess, try the shortname
704 $testroleid = get_field('role', 'id', 'shortname', $rolefromxml->shortname);
705 if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
706 return get_record('role', 'id', $testroleid);
709 // Finally, search all other roles. In orter to speed things up, we exclude the ones we have
710 // already tested, and we only search roles with the same number of capabilities set in their
711 // definition.
712 $extracondition = '';
713 if ($testroleid) {
714 $extracondition = "AND roleid <> $testroleid";
716 $candidateroleids = get_records_sql("SELECT roleid
717 FROM {$CFG->prefix}role_capabilities
718 WHERE roleid <> $roleid $extracondition
719 GROUP BY roleid
720 HAVING COUNT(capability) = ".count($rolefromxml->capabilities));
721 if (!empty($candidateroleids)) {
722 foreach ($candidateroleids as $testroleid => $notused) {
723 if (restore_is_samerole($testroleid, $rolefromxml)) {
724 return get_record('role', 'id', $testroleid);
729 return false;
733 * Compare a role in the database with one loaded from the backup file, and determine whether
734 * they have identical permissions for each capability.
735 * @param integer $testroleid the id of the role from the database to test against.
736 * @param object $rolefromxml the role definition loaded from the backup file.
737 * @return boolean true if the two roles are identical.
739 function restore_is_samerole($testroleid, $rolefromxml) {
740 // Load the role definition from the databse.
741 $rolefromdb = get_records('role_capabilities', 'roleid', $testroleid, '', 'capability,permission');
742 if (!$rolefromdb) {
743 return false;
746 // Quick check, do they have the permissions on the same number of capabilities?
747 if (count($rolefromdb) != count($rolefromxml->capabilities)) {
748 return false;
751 // If they do, check each one.
752 foreach ($rolefromdb as $capability => $permissions) {
753 if (!isset($rolefromxml->capabilities[$capability]) ||
754 $permissions->permission != $rolefromxml->capabilities[$capability]->permission) {
755 return false;
758 return true;