info)) { print_error('sessionmissing' ,'debug', '', 'info'); } if (!($course_header = $SESSION->course_header)) { print_error('sessionmissing' ,'debug', '', 'course_header'); } $restore_gradebook_history = optional_param('restore_gradebook_history', 0, PARAM_INT); //Check that we have all we need //backup_unique_code $backup_unique_code = required_param('backup_unique_code', PARAM_INT); //file $file = required_param('file', PARAM_PATH); //Check login require_login(); $loginurl = get_login_url(); //Init restoreuserinfo $restoreuserinfo = false; //Check admin if (!empty($id)) { if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $id))) { print_error('cannotuseadminadminorteacher', '', $loginurl); } $restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_COURSE, $id)); } else { if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_SYSTEM))) { print_error('cannotuseadmin', '', $loginurl); } $restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_SYSTEM)); } //Check site $site = get_site(); //Checks for the required files/functions to restore every mod $count = 0; if ($allmods = $DB->get_records("modules") ) { foreach ($allmods as $mod) { $modname = $mod->name; $modfile = "$CFG->dirroot/mod/$modname/restorelib.php"; $modrestore = $modname."_restore_mods"; if (file_exists($modfile)) { include_once($modfile); if (function_exists($modrestore)) { $var = "exists_".$modname; $$var = true; $count++; } } //Check data //Check module info $var = "restore_".$modname; if (!isset($$var)) { $$var = 1; } //Check include user info $var = "restore_user_info_".$modname; if (!isset($$var) && $restoreuserinfo) { $$var = 1; } else { $$var = 0; } } } if (!isset($restore_users)) { $restore_users = 1; } if (!isset($restore_groups)) { $restore_groups = RESTORE_GROUPS_GROUPINGS; } if (!isset($restore_logs)) { $restore_logs = 1; } if (!isset($restore_user_files)) { $restore_user_files = 1; } if (!isset($restore_course_files)) { $restore_course_files = 1; } if (!isset($restore_site_files)) { $restore_site_files = 1; } if (!isset($restore_messages)) { $restore_messages = 1; } if (!isset($restore_blogs)) { $restore_blogs = 1; } $cancreatecourses = user_can_create_courses(); if (!isset($restore_restoreto)) { if (!$cancreatecourses) { $restore_restoreto = RESTORETO_CURRENT_ADDING; } else { $restore_restoreto = RESTORETO_NEW_COURSE; } } if (!isset($course_header->category->id)) { $course_header->category->id = 0; } if(!isset($form1->startdate)) { $form1->startdate = $course_header->course_startdate; //$course_header->course_startdate; } if (empty($form1->shortname)) { $form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname; } if (empty($form1->fullname)) { $form1->fullname = $course_header->course_fullname; // '_fullname'; //$course_header->course_fullname; } if ($count == 0) { notice("No restorable modules are installed!"); } ?>
"; echo ""; if ($cancreatecourses) { //display these fields conditionally // find the list of cates user can edit echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } else { // nothing to show here if the user cannot create users } // If user can roll dates (in any course is enough for now) show the roll dates option. // Important: The final restore check step will validate, once we know the final category/course // where the restore is happening, if the user is really capable of rolling dates there, noticing // and disabling roll if not allowed. if (restore_user_can_roll_dates()) { echo " "; echo ""; } else { /// If user lacks the moodle/restore:rolldates completely, prevent any change in startyear/month/day (so restore won't perform any roll) echo ''; echo ''; echo ''; } //Line echo ""; //Now, check modules and info and show posibilities if ($allmods = $DB->get_records("modules") ) { //Print option to select/deselect everything with 1 click. echo ""; echo ""; if ($restoreuserinfo) { echo ""; } else { echo ""; } echo ""; echo ""; $currentrow = 0; $nonrestmod = ''; foreach ($allmods as $mod) { $modname = $mod->name; $modrestore = $modname."_restore_mods"; //If exists the lib & function $exist = "exists_".$modname; $restore_var = "restore_".$modname; $user_info_var = "restore_user_info_".$modname; if (isset($$exist)) { if ($$exist) { //Now check that we have that module info in the backup file if (isset($info->mods[$modname]) && $info->mods[$modname]->backup == "true") { //Print the full tr echo ""; echo ""; if (isset($info->mods[$modname]->instances)) { $instances = $info->mods[$modname]->instances; } if (!empty($instances) && is_array($instances)) { echo ''; } } else { //Module isn't restorable $nonrestmod .= ""; $nonrestmod .= ""; } } else { //Module isn't restorable $nonrestmod .= ""; $nonrestmod .= ""; } } else { //Module isn't restorable $nonrestmod .= ""; $nonrestmod .= ""; } $currentrow = ($currentrow + 1) % 2; } //Line echo ""; //Now print the Users tr echo ""; echo ""; //Now print the Groups tr (assume there is no $info->backup_groups) echo ""; echo ""; //Now print the Logs tr echo ""; echo ""; //Now print the User Files tr echo ""; echo ""; //Now print the Course Files tr echo ""; echo ""; //Now print the Site Files tr echo ""; echo ""; // do you want grade histories to be restored? if (empty($CFG->disablegradehistory) and $restoreuserinfo) { echo ""; echo ""; } else { $hidden_options .= ''; } //This tr is slighty different. Everything becomes hidden if //we haven't messages is the backup, to avoid confusions to users. //If messages are in the backup file, show menu, else fixed to no and show nothing //Also, messaging must be enabled in the destination site if (isset($info->backup_messages) && $info->backup_messages == "true" && !empty($CFG->messaging) and $restoreuserinfo) { echo ""; echo ""; } else { $hidden_options .= ''; } //This tr is slighty different. Everything becomes hidden if //we haven't blogs is the backup, to avoid confusions to users. //If blogs are in the backup file, show menu, else fixed to no and show nothing //Also, blogs must be enabled in the destination site if (isset($info->backup_blogs) && $info->backup_blogs == "true" && !empty($CFG->bloglevel) and $restoreuserinfo) { echo ""; echo ""; } else { $hidden_options .= ''; } } ?>
"; echo ''; echo ""; echo ""; // permission should have been checked already //TODO: use better function which includes all courses for admins $mycourses = get_user_courses_bycap($USER->id, 'moodle/restore:restorecourse'); // if the user can restore to current course, grant the "current" options if (has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $id))){ $restore_restoreto_options[RESTORETO_CURRENT_DELETING] = get_string("currentcoursedeleting"); $restore_restoreto_options[RESTORETO_CURRENT_ADDING] = get_string("currentcourseadding"); } // if user can create any course, grant the "new course" option if ($cancreatecourses) { $restore_restoreto_options[RESTORETO_NEW_COURSE] = get_string("newcourse"); } // if the user can restore to 2 or more courses (or to just 1 but different from current), grant the "existing" options if (count($mycourses) > 1 || (count($mycourses) == 1 && $mycourses[0]->id != $id)) { $restore_restoreto_options[RESTORETO_EXISTING_DELETING] = get_string("existingcoursedeleting"); $restore_restoreto_options[RESTORETO_EXISTING_ADDING] = get_string("existingcourseadding"); } /// Acummulator for hidden options and proper XHTML output $hidden_options = ''; echo html_writer::select($restore_restoreto_options, "restore_restoreto", $restore_restoreto, false); echo "
"; echo ''; echo ""; // Get the list of categories where the user can create courses make_categories_list($categories, $parents, 'moodle/course:create'); $category_default = null; // Best default 1: restoring in same site, use category of original course if available if (backup_is_same_site($info) && !empty($course_header->category->id) && array_key_exists($course_header->category->id, $categories)) { $category_default = $course_header->category->id; // Best default 2: restore to the category of the course we are restoring from if available } else { $fromcat = $DB->get_field('course', 'category', array('id' => $id)); if (!empty($fromcat) && array_key_exists($fromcat, $categories)) { $category_default = $fromcat; } } echo html_writer::select($categories, 'restore_restorecatto', $category_default, false); echo "
"; echo ''; echo "shortname)."\" alt=\"".get_string("shortname")."\" />" ; echo $OUTPUT->help_icon('shortnamecourse')); if (isset($err["shortname"])) echo $OUTPUT->error_text($err["shortname"]); echo "
"; echo ''; echo "fullname)."\" alt=\" ".get_string("fullname")."\" />" ; echo $OUTPUT->help_icon('fullnamecourse'); if (isset($err["fullname"])) echo $OUTPUT->error_text($err["fullname"]); echo"
"; print_string("startdate"); echo ""; /// Show the roll dates option only if the backup course has a start date /// (some formats like main page, social..., haven't it and rolling dates /// from 0 produces crazy dates. MDL-10125 if ($form1->startdate) { $dayselector = html_writer::select_time('days', "startday", $form1->startdate); $monthselector = html_writer::select_time('months', "startmonth", $form1->startdate); $yearselector = html_writer::select_time('years', "startyear", $form1->startdate); echo $dayselector . $monthselector . $yearselector; echo $OUTPUT->help_icon('startdate')); } else { print_string('notavailable'); echo ''; echo ''; echo ''; } echo "

"; echo ''.get_string("include").""; echo ""; echo "". get_string("all")."/"; echo "". get_string("none").""; echo ""; echo ' '; echo ""; echo "". get_string("all")."/"; echo "". get_string("none").""; echo " 

 "; echo ""; $restore_options[1] = get_string("yes"); $restore_options[0] = get_string("no"); //Print the checkbox print_checkbox($restore_var, $$restore_var, $$restore_var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'restore_'.$modname.'\',this.checked)'); //If backup contains user data, then show menu, else fix it to //without user data echo " "; echo ""; if ($info->mods[$modname]->userinfo == "true" && $restoreuserinfo) { $restore_user_options[1] = get_string("yes"); $restore_user_options[0] = get_string("no"); print_checkbox($user_info_var, $$user_info_var, $$user_info_var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'restore_user_info_'.$modname.'\',this.checked)'); } else { //Module haven't userdata echo get_string("withoutuserdata"); echo ""; } echo "
'; foreach ($instances as $instance) { echo ''; } echo '
'; $var = 'restore_'.$modname.'_instance_'.$instance->id; $$var = optional_param($var,1, PARAM_CLEAN); print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'restore_'.$modname.'\'].checked=1;'); echo ' '; $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id; $$var = optional_param($var,1, PARAM_CLEAN); if (!empty($info->mods[$modname]->instances) && ($info->mods[$modname]->instances[$instance->id]->userinfo == 'true') && $restoreuserinfo) { print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'restore_user_info_'.$modname.'\'].checked=1;'); } else { echo ''; } echo '
$nonrestmod
"; echo ''; echo ""; //If some user is present in the backup file if (($info->backup_users == "all" or $info->backup_users == "course") and $restoreuserinfo) { $user_options = array(); //If all users are in the backup file and user has 'moodle/restore:restorecourse' at system level if ($info->backup_users == "all" and has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_SYSTEM))) { $user_options[0] = get_string("all"); } $user_options[1] = get_string("course"); $user_options[2] = get_string("none"); echo html_writer::select($user_options, "restore_users", $restore_users, false); } else { echo get_string("none"); echo ""; } echo "
"; echo ''.$helplink; echo ""; $group_options[RESTORE_GROUPS_NONE] = get_string('none'); $group_options[RESTORE_GROUPS_ONLY] = get_string('groupsonly', 'group'); $group_options[RESTORE_GROUPINGS_ONLY] = get_string('groupingsonly', 'group'); $group_options[RESTORE_GROUPS_GROUPINGS] = get_string('groupsgroupings', 'group'); //all. echo html_writer::select($group_options, 'restore_groups', $restore_groups, false); echo "
"; echo ''; echo ""; //If logs are in the backup file, show menu, else fixed to no if ($info->backup_logs == "true" and $restoreuserinfo) { $log_options = array(); $log_options[0] = get_string("no"); $log_options[1] = get_string("yes"); echo html_writer::select($log_options, "restore_logs", $restore_logs, false); } else { echo get_string("no"); echo ""; } echo "
"; echo ''; echo ""; //If user files are in the backup file, show menu, else fixed to no if ($info->backup_user_files == "true" and $restoreuserinfo) { $user_file_options = array(); $user_file_options[0] = get_string("no"); $user_file_options[1] = get_string("yes"); echo html_writer::select($user_file_options, "restore_user_files", $restore_user_files, false); } else { echo get_string("no"); echo ""; } echo "
"; echo ''; echo ""; echo ""; echo ""; //If course files are in the backup file, show menu, else fixed to no if ($info->backup_course_files == "true") { $course_file_options = array(); $course_file_options[0] = get_string("no"); $course_file_options[1] = get_string("yes"); echo html_writer::select($course_file_options, "restore_course_files", $restore_course_files, false); } else { echo get_string("no"); echo ""; } echo "
"; echo ''; echo ""; //If site files are in the backup file, show menu, else fixed to no if (isset($info->backup_site_files) && $info->backup_site_files == "true") { $site_file_options = array(); $site_file_options[0] = get_string("no"); $site_file_options[1] = get_string("yes"); echo html_writer::select($site_file_options, "restore_site_files", $restore_site_files, false); } else { echo get_string("no"); echo ""; } echo "
"; echo ''; echo ""; if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") { $gradebook_history_options = array(); $gradebook_history_options[0] = get_string("no"); $gradebook_history_options[1] = get_string("yes"); echo html_writer::select($gradebook_history_options, "restore_gradebook_history", $restore_gradebook_history, false); } else { echo get_string("no"); echo ""; } echo "
"; echo ''; echo ""; $message_options = array(); $message_options[0] = get_string("no"); $message_options[1] = get_string("yes"); echo html_writer::select($message_options, "restore_messages", $restore_messages, false); echo "
"; echo ''; echo ""; $blog_options = array(); $blog_options[0] = get_string("no"); $blog_options[1] = get_string("yes"); echo html_writer::select($blog_options, "restore_blogs", $restore_blogs, false); echo "

heading(get_string('rolemappings')); $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml"; $info = restore_read_xml_info($xml_file); // fix for MDL-9068, front page course is just a normal course $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname", ROLENAME_ORIGINAL); $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name", ROLENAME_ORIGINAL); $allroles = get_records('role'); echo (''); if ($info->backup_moodle_version < 2006092801) { echo (''); // 1.6 and below backup /// Editting teacher echo (''); /// Non-editting teacher echo (''); /// Student echo (''); } else { // 1.7 and above backup $roles = restore_read_xml_roles($xml_file); // Calculate if any role can be mapped $str_new = ''; $canmaproles = false; if (has_capability('moodle/role:manage', get_context_instance(CONTEXT_SYSTEM))) { $str_new = 'new role'; $canmaproles = true; } else if (!empty($siterolesarray)) { $canmaproles = true; } if ($canmaproles) { echo (''); } if (!empty($roles->roles)) { // possible to have course with no roles foreach ($siterolesarray as $siteroleid=>$siteroleshortname) { $siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")"; } foreach ($roles->roles as $roleid=>$role) { if (!$canmaproles) { // User cannot map roles at all, add hidden element and continue echo ""; continue; } $mappableroles = !empty($siteroleschoicearray) ? $siteroleschoicearray : array(); echo (''); } } } // end else echo ('
'.get_string('sourcerole').''.get_string('targetrole').'
'); echo ''; echo (''); // get the first teacheredit legacy $roles = get_archetype_roles('editingteacher'); $editteacher = reset($roles); echo html_writer::select($siterolesarray, "defaultteacheredit", $editteacher->id, array(''=>'new role')); echo ('
'); echo ''; print_string('noneditingteacher'); echo (''); // get the first teacheredit legacy $roles = get_archetype_roles('teacher'); $teacher = reset($roles); echo html_writer::select($siterolesarray, "defaultteacher", $teacher->id, array(''=>'new role')); echo ('
'); echo ''; echo (''); // get the first teacheredit legacy $roles = get_archetype_roles('student'); $studentrole = array_shift($roles); echo html_writer::select($siterolesarray, "defaultstudent", $studentrole->id, array(''=>'new role')); echo ('
'.get_string('sourcerole').''.get_string('targetrole').'
'); echo ''; echo (''); /// first, we see if any exact role definition is found /// if found, that is the only option of restoring to if ($samerole = restore_samerole($roleid, $role)) { $matchrole = $samerole->id; // if an exact role is found, it does not matter whether this user can assign this role or not, // this will be presented as a valid option regardless $mappableroles[$samerole->id] = format_string($allroles[$samerole->id]->name)." (". $allroles[$samerole->id]->shortname.")"; } else { // no exact role found, let's try to match shortname // this is useful in situations where basic roles differ slightly in definition $matchrole = 0; foreach ($siterolesarray as $siteroleid=>$siteroleshortname) { if ($siteroleshortname == $role->shortname) { $matchrole = $siteroleid; break; } } // Hack a bit, so, if role continues unmatched, and was "editingteacher", and "teacher" is available // preselect it as fallback matching (can happen a lot) if ($matchrole == 0 && $role->shortname == 'editingteacher' && in_array('teacher', $siterolesarray)) { $matchrole = array_search('teacher', $siterolesarray); } // If arrived here, role continues unmatched and the user lacks the 'moodle/role:manage' capability // perform automatic matching to site defaultcourseroleid (only if available in the list) if ($matchrole == 0 && !has_capability('moodle/role:manage', get_context_instance(CONTEXT_SYSTEM))) { if (array_key_exists($CFG->defaultcourseroleid, $siterolesarray)) { $matchrole = $CFG->defaultcourseroleid; } } } echo html_writer::select($mappableroles, "roles_".$roleid, $matchrole, array('' => $str_new)); echo ('
'); // end of role mappings table } ?>
" /> " />
get_record('role', array('id'=>$roleid)); } // Second guess, try the shortname $testroleid = $DB->get_field('role', 'id', array('shortname'=>$rolefromxml->shortname)); if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) { return $DB->get_record('role', array('id'=>$testroleid)); } // Finally, search all other roles. In orter to speed things up, we exclude the ones we have // already tested, and we only search roles with the same number of capabilities set in their // definition. $extracondition = ''; if ($testroleid) { $extracondition = "AND roleid <> $testroleid"; } $candidateroleids = $DB->get_records_sql( "SELECT roleid FROM {role_capabilities} WHERE roleid <> $roleid $extracondition GROUP BY roleid HAVING COUNT(capability) = ".count($rolefromxml->capabilities)); if (!empty($candidateroleids)) { foreach ($candidateroleids as $testroleid => $notused) { if (restore_is_samerole($testroleid, $rolefromxml)) { return $DB->get_record('role', array('id'=>$testroleid)); } } } return false; } /** * Compare a role in the database with one loaded from the backup file, and determine whether * they have identical permissions for each capability. * @param integer $testroleid the id of the role from the database to test against. * @param object $rolefromxml the role definition loaded from the backup file. * @return boolean true if the two roles are identical. */ function restore_is_samerole($testroleid, $rolefromxml) { global $DB; // Load the role definition from the databse. $rolefromdb = $DB->get_records('role_capabilities', array('roleid'=>$testroleid), '', 'capability,permission'); if (!$rolefromdb) { return false; } // Quick check, do they have the permissions on the same number of capabilities? if (count($rolefromdb) != count($rolefromxml->capabilities)) { return false; } // If they do, check each one. foreach ($rolefromdb as $capability => $permissions) { if (!isset($rolefromxml->capabilities[$capability]) || $permissions->permission != $rolefromxml->capabilities[$capability]->permission) { return false; } } return true; }