Skip to content

Commit

Permalink
Merge branch 'w47_MDL-27233_m22_restoreenrol' of git://github.com/sko…
Browse files Browse the repository at this point in the history
…dak/moodle
  • Loading branch information
stronk7 committed Nov 24, 2011
2 parents 7355a8e + 1f238ad commit 0620787
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 3 deletions.
6 changes: 6 additions & 0 deletions admin/settings/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
$defaultuserid = 0;
}

$restorersnewrole = $creatornewroles;
$restorersnewrole[0] = get_string('none');

$temp->add(new admin_setting_configselect('notloggedinroleid', get_string('notloggedinroleid', 'admin'),
get_string('confignotloggedinroleid', 'admin'), $defaultguestid, ($guestroles + $otherroles)));
$temp->add(new admin_setting_configselect('guestroleid', get_string('guestroleid', 'admin'),
Expand All @@ -97,12 +100,15 @@
get_string('configdefaultuserroleid', 'admin'), $defaultuserid, ($userroles + $otherroles)));
$temp->add(new admin_setting_configselect('creatornewroleid', get_string('creatornewroleid', 'admin'),
get_string('creatornewroleid_help', 'admin'), $defaultteacherid, $creatornewroles));
$temp->add(new admin_setting_configselect('restorernewroleid', get_string('restorernewroleid', 'admin'),
get_string('restorernewroleid_help', 'admin'), $defaultteacherid, $restorersnewrole));

// release memory
unset($otherroles);
unset($guestroles);
unset($userroles);
unset($creatornewroles);
unset($restorersnewrole);
}

$temp->add(new admin_setting_configcheckbox('autologinguests', get_string('autologinguests', 'admin'), get_string('configautologinguests', 'admin'), 0));
Expand Down
23 changes: 23 additions & 0 deletions backup/moodle2/restore_course_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public function build() {
$this->add_step(new restore_enrolments_structure_step('course_enrolments', 'enrolments.xml'));
}

// Now make sure the user that is running the restore can actually access the course
$this->add_step(new restore_fix_restorer_access_step('fix_restorer_access'));

// Restore course filters (conditionally)
if ($this->get_setting_value('filters')) {
$this->add_step(new restore_filters_structure_step('course_filters', 'filters.xml'));
Expand Down Expand Up @@ -133,6 +136,26 @@ protected function define_settings() {
$startdate->set_ui(new backup_setting_ui_dateselector($startdate, get_string('setting_course_startdate', 'backup')));
$this->add_setting($startdate);

$keep_enrols = new restore_course_generic_setting('keep_roles_and_enrolments', base_setting::IS_BOOLEAN, false);
$keep_enrols->set_ui(new backup_setting_ui_select($keep_enrols, $keep_enrols->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$keep_enrols->get_ui()->set_label(get_string('setting_keep_roles_and_enrolments', 'backup'));
if ($this->get_target() != backup::TARGET_CURRENT_DELETING and $this->get_target() != backup::TARGET_EXISTING_DELETING) {
$keep_enrols->set_value(false);
$keep_enrols->set_status(backup_setting::LOCKED_BY_CONFIG);
$keep_enrols->set_visibility(backup_setting::HIDDEN);
}
$this->add_setting($keep_enrols);

$keep_groups = new restore_course_generic_setting('keep_groups_and_groupings', base_setting::IS_BOOLEAN, false);
$keep_groups->set_ui(new backup_setting_ui_select($keep_groups, $keep_groups->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$keep_groups->get_ui()->set_label(get_string('setting_keep_groups_and_groupings', 'backup'));
if ($this->get_target() != backup::TARGET_CURRENT_DELETING and $this->get_target() != backup::TARGET_EXISTING_DELETING) {
$keep_groups->set_value(false);
$keep_groups->set_status(backup_setting::LOCKED_BY_CONFIG);
$keep_groups->set_visibility(backup_setting::HIDDEN);
}
$this->add_setting($keep_groups);

// Define overwrite_conf to decide if course configuration will be restored over existing one
$overwrite = new restore_course_overwrite_conf_setting('overwrite_conf', base_setting::IS_BOOLEAN, false);
$overwrite->set_ui(new backup_setting_ui_select($overwrite, $overwrite->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
Expand Down
51 changes: 51 additions & 0 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,57 @@ public function process_enrolment($data) {
}


/**
* Make sure the user restoring the course can actually access it.
*/
class restore_fix_restorer_access_step extends restore_execution_step {
protected function define_execution() {
global $CFG, $DB;

if (!$userid = $this->task->get_userid()) {
return;
}

if (empty($CFG->restorernewroleid)) {
// Bad luck, no fallback role for restorers specified
return;
}

$courseid = $this->get_courseid();
$context = context_course::instance($courseid);

if (is_enrolled($context, $userid, 'moodle/course:update', true) or is_viewing($context, $userid, 'moodle/course:update')) {
// Current user may access the course (admin, category manager or restored teacher enrolment usually)
return;
}

// Try to add role only - we do not need enrolment if user has moodle/course:view or is already enrolled
role_assign($CFG->restorernewroleid, $userid, $context);

if (is_enrolled($context, $userid, 'moodle/course:update', true) or is_viewing($context, $userid, 'moodle/course:update')) {
// Extra role is enough, yay!
return;
}

// The last chance is to create manual enrol if it does not exist and and try to enrol the current user,
// hopefully admin selected suitable $CFG->restorernewroleid ...
if (!enrol_is_enabled('manual')) {
return;
}
if (!$enrol = enrol_get_plugin('manual')) {
return;
}
if (!$DB->record_exists('enrol', array('enrol'=>'manual', 'courseid'=>$courseid))) {
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$fields = array('status'=>ENROL_INSTANCE_ENABLED, 'enrolperiod'=>$enrol->get_config('enrolperiod', 0), 'roleid'=>$enrol->get_config('roleid', 0));
$enrol->add_instance($course, $fields);
}

enrol_try_internal_enrol($courseid, $userid);
}
}


/**
* This structure steps restores the filters and their configs
*/
Expand Down
5 changes: 3 additions & 2 deletions backup/util/dbops/restore_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1358,10 +1358,11 @@ public static function create_new_course($fullname, $shortname, $categoryid) {
/**
* Deletes all of the content associated with the given course (courseid)
* @param int $courseid
* @param array $options
* @return bool True for success
*/
public static function delete_course_content($courseid) {
return remove_course_contents($courseid, false);
public static function delete_course_content($courseid, array $options = null) {
return remove_course_contents($courseid, false, $options);
}
}

Expand Down
5 changes: 4 additions & 1 deletion backup/util/ui/restore_ui.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ public function execute() {
throw new restore_ui_exception('restoreuifinalisedbeforeexecute');
}
if ($this->controller->get_target() == backup::TARGET_CURRENT_DELETING || $this->controller->get_target() == backup::TARGET_EXISTING_DELETING) {
restore_dbops::delete_course_content($this->controller->get_courseid());
$options = array();
$options['keep_roles_and_enrolments'] = $this->get_setting_value('keep_roles_and_enrolments');
$options['keep_groups_and_groupings'] = $this->get_setting_value('keep_groups_and_groupings');
restore_dbops::delete_course_content($this->controller->get_courseid(), $options);
}
$this->controller->execute_plan();
$this->progress = self::PROGRESS_EXECUTED;
Expand Down
2 changes: 2 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@
$string['purgecaches']= 'Purge all caches';
$string['purgecachesconfirm']= 'Moodle can cache themes, javascript, language strings, filtered text, rss feeds and many other pieces of calculated data. Purging these caches will delete that data from the server and force browsers to refetch data, so that you can be sure you are seeing the most up-to-date values produced by the current code. There is no danger in purging caches, but your site may appear slower for a while until the server and clients calculate new information and cache it.';
$string['purgecachesfinished']= 'All caches were purged.';
$string['restorernewroleid'] = 'Restorers\' role in courses';
$string['restorernewroleid_help'] = 'If the user does not already have the permission to manage the newly restored course, the user is automatically assigned this role and enrolled if necessary. Select "None" if you do not want restorers to be able to manage every restored course.';
$string['restrictbydefault'] = 'Restrict modules by default';
$string['restrictmodulesfor'] = 'Restrict modules for';
$string['reverseproxy'] = 'Reverse proxy';
Expand Down
2 changes: 2 additions & 0 deletions lang/en/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,7 @@
$string['setting_course_fullname'] = 'Course name';
$string['setting_course_shortname'] = 'Course short name';
$string['setting_course_startdate'] = 'Course startdate';
$string['setting_keep_roles_and_enrolments'] = 'Keep current roles and enrolments';
$string['setting_keep_groups_and_groupings'] = 'Keep current groups and groupings';
$string['totalcategorysearchresults'] = 'Total categories: {$a}';
$string['totalcoursesearchresults'] = 'Total courses: {$a}';

0 comments on commit 0620787

Please sign in to comment.