Permalink
Browse files

Merge branch 'w40_MDL-31437_m24_cohortsync' of git://github.com/skoda…

…k/moodle

Conflicts:
	lib/upgrade.txt
  • Loading branch information...
stronk7 committed Oct 2, 2012
2 parents 60e5d95 + 7881024 commit c740703c188b94afef1e66e37f51491aa9ac152b
@@ -76,6 +76,9 @@ public function build() {
$this->add_step(new restore_enrolments_structure_step('course_enrolments', 'enrolments.xml'));
}
+ // Populate groups, this must be done after enrolments because only enrolled users may be in groups.
+ $this->add_step(new restore_groups_members_structure_step('create_groups_members', '../groups.xml'));
+
// Restore course role assignments and overrides (internally will observe the role_assignments setting),
// this must be done after all users are enrolled.
$this->add_step(new restore_ras_and_caps_structure_step('course_ras_and_caps', 'roles.xml'));
@@ -715,9 +715,6 @@ protected function define_structure() {
$paths = array(); // Add paths here
$paths[] = new restore_path_element('group', '/groups/group');
- if ($this->get_setting_value('users')) {
- $paths[] = new restore_path_element('member', '/groups/group/group_members/group_member');
- }
$paths[] = new restore_path_element('grouping', '/groups/groupings/grouping');
$paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group');
@@ -769,33 +766,6 @@ public function process_group($data) {
$this->set_mapping('group', $oldid, $newitemid, $restorefiles);
}
- public function process_member($data) {
- global $DB;
-
- $data = (object)$data; // handy
-
- // get parent group->id
- $data->groupid = $this->get_new_parentid('group');
-
- // map user newitemid and insert if not member already
- if ($data->userid = $this->get_mappingid('user', $data->userid)) {
- if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) {
- // Check the componment, if any, exists
- if (!empty($data->component)) {
- $dir = get_component_directory($data->component);
- if (!$dir || !is_dir($dir)) {
- // Component does not exist on restored system; clear
- // component and itemid
- unset($data->component);
- unset($data->itemid);
- }
- }
-
- $DB->insert_record('groups_members', $data);
- }
- }
- }
-
public function process_grouping($data) {
global $DB;
@@ -866,6 +836,86 @@ protected function after_execute() {
}
+/**
+ * Structure step that will create all the needed group memberships
+ * by loading them from the groups.xml file performing the required matches.
+ */
+class restore_groups_members_structure_step extends restore_structure_step {
+
+ protected $plugins = null;
+
+ protected function define_structure() {
+
+ $paths = array(); // Add paths here
+
+ if ($this->get_setting_value('users')) {
+ $paths[] = new restore_path_element('group', '/groups/group');
+ $paths[] = new restore_path_element('member', '/groups/group/group_members/group_member');
+ }
+
+ return $paths;
+ }
+
+ public function process_group($data) {
+ $data = (object)$data; // handy
+
+ // HACK ALERT!
+ // Not much to do here, this groups mapping should be already done from restore_groups_structure_step.
+ // Let's fake internal state to make $this->get_new_parentid('group') work.
+
+ $this->set_mapping('group', $data->id, $this->get_mappingid('group', $data->id));
+ }
+
+ public function process_member($data) {
+ global $DB, $CFG;
+ require_once("$CFG->dirroot/group/lib.php");
+
+ // NOTE: Always use groups_add_member() because it triggers events and verifies if user is enrolled.
+
+ $data = (object)$data; // handy
+
+ // get parent group->id
+ $data->groupid = $this->get_new_parentid('group');
+
+ // map user newitemid and insert if not member already
+ if ($data->userid = $this->get_mappingid('user', $data->userid)) {
+ if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) {
+ // Check the component, if any, exists.
+ if (empty($data->component)) {
+ groups_add_member($data->groupid, $data->userid);
+
+ } else if ((strpos($data->component, 'enrol_') === 0)) {
+ // Deal with enrolment groups - ignore the component and just find out the instance via new id,
+ // it is possible that enrolment was restored using different plugin type.
+ if (!isset($this->plugins)) {
+ $this->plugins = enrol_get_plugins(true);
+ }
+ if ($enrolid = $this->get_mappingid('enrol', $data->itemid)) {
+ if ($instance = $DB->get_record('enrol', array('id'=>$enrolid))) {
+ if (isset($this->plugins[$instance->enrol])) {
+ $this->plugins[$instance->enrol]->restore_group_member($instance, $data->groupid, $data->userid);
+ }
+ }
+ }
+
+ } else {
+ $dir = get_component_directory($data->component);
+ if ($dir and is_dir($dir)) {
+ if (component_callback($data->component, 'restore_group_member', array($this, $data), true)) {
+ return;
+ }
+ }
+ // Bad luck, plugin could not restore the data, let's add normal membership.
+ groups_add_member($data->groupid, $data->userid);
+ $message = "Restore of '$data->component/$data->itemid' group membership is not supported, using standard group membership instead.";
+ debugging($message);
+ $this->log($message, backup::LOG_WARNING);
+ }
+ }
+ }
+ }
+}
+
/**
* Structure step that will create all the needed scales
* by loading them from the scales.xml
@@ -1475,6 +1525,22 @@ public function process_assignment($data) {
}
}
}
+
+ } else {
+ $data->roleid = $newroleid;
+ $data->userid = $newuserid;
+ $data->contextid = $contextid;
+ $dir = get_component_directory($data->component);
+ if ($dir and is_dir($dir)) {
+ if (component_callback($data->component, 'restore_role_assignment', array($this, $data), true)) {
+ return;
+ }
+ }
+ // Bad luck, plugin could not restore the data, let's add normal membership.
+ role_assign($data->roleid, $data->userid, $data->contextid);
+ $message = "Restore of '$data->component/$data->itemid' role assignments is not supported, using manual role assignments instead.";
+ debugging($message);
+ $this->log($message, backup::LOG_WARNING);
}
}
@@ -1594,8 +1660,10 @@ public function process_enrol($data) {
} else {
if (!enrol_is_enabled($data->enrol) or !isset($this->plugins[$data->enrol])) {
- debugging("Enrol plugin data can not be restored because it is not enabled, use migration to manual enrolments");
$this->set_mapping('enrol', $oldid, 0);
+ $message = "Enrol plugin '$data->enrol' data can not be restored because it is not enabled, use migration to manual enrolments";
+ debugging($message);
+ $this->log($message, backup::LOG_WARNING);
return;
}
if ($task = $this->get_task() and $task->get_target() == backup::TARGET_NEW_COURSE) {
@@ -1796,7 +1864,7 @@ protected function define_structure() {
}
public function process_calendarevents($data) {
- global $DB;
+ global $DB, $SITE;
$data = (object)$data;
$oldid = $data->id;
@@ -3204,10 +3272,10 @@ protected function define_execution() {
*/
class restore_process_file_aliases_queue extends restore_execution_step {
- /** @var array internal cache for {@link choose_repository() */
+ /** @var array internal cache for {@link choose_repository()} */
private $cachereposbyid = array();
- /** @var array internal cache for {@link choose_repository() */
+ /** @var array internal cache for {@link choose_repository()} */
private $cachereposbytype = array();
/**
@@ -1,71 +0,0 @@
-<?php
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * Adds new instance of enrol_cohort to specified course.
- *
- * @package enrol_cohort
- * @copyright 2010 Petr Skoda {@link http://skodak.org}
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-require('../../config.php');
-require_once("$CFG->dirroot/enrol/cohort/addinstance_form.php");
-require_once("$CFG->dirroot/enrol/cohort/locallib.php");
-
-$id = required_param('id', PARAM_INT); // Course id.
-
-$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
-$context = context_course::instance($course->id, MUST_EXIST);
-
-require_login($course);
-require_capability('moodle/course:enrolconfig', $context);
-require_capability('enrol/cohort:config', $context);
-
-$PAGE->set_url('/enrol/cohort/addinstance.php', array('id'=>$course->id));
-$PAGE->set_pagelayout('admin');
-
-navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
-
-// Try and make the manage instances node on the navigation active.
-$courseadmin = $PAGE->settingsnav->get('courseadmin');
-if ($courseadmin && $courseadmin->get('users') && $courseadmin->get('users')->get('manageinstances')) {
- $courseadmin->get('users')->get('manageinstances')->make_active();
-}
-
-
-$enrol = enrol_get_plugin('cohort');
-if (!$enrol->get_newinstance_link($course->id)) {
- redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
-}
-
-$mform = new enrol_cohort_addinstance_form(NULL, $course);
-
-if ($mform->is_cancelled()) {
- redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
-
-} else if ($data = $mform->get_data()) {
- $enrol->add_instance($course, array('customint1'=>$data->cohortid, 'roleid'=>$data->roleid));
- enrol_cohort_sync($course->id);
- redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
-}
-
-$PAGE->set_heading($course->fullname);
-$PAGE->set_title(get_string('pluginname', 'enrol_cohort'));
-
-echo $OUTPUT->header();
-$mform->display();
-echo $OUTPUT->footer();
@@ -1,90 +0,0 @@
-<?php
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * Adds instance form
- *
- * @package enrol_cohort
- * @copyright 2010 Petr Skoda {@link http://skodak.org}
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-defined('MOODLE_INTERNAL') || die();
-
-require_once("$CFG->libdir/formslib.php");
-
-class enrol_cohort_addinstance_form extends moodleform {
-
- protected $course;
-
- function definition() {
- global $CFG, $DB;
-
- $mform = $this->_form;
- $this->course = $this->_customdata;
- $coursecontext = context_course::instance($this->course->id);
-
- $enrol = enrol_get_plugin('cohort');
-
- $cohorts = array('' => get_string('choosedots'));
- list($sqlparents, $params) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids());
- $sql = "SELECT id, name, contextid
- FROM {cohort}
- WHERE contextid $sqlparents
- ORDER BY name ASC";
- $rs = $DB->get_recordset_sql($sql, $params);
- foreach ($rs as $c) {
- $context = context::instance_by_id($c->contextid);
- if (!has_capability('moodle/cohort:view', $context)) {
- continue;
- }
- $cohorts[$c->id] = format_string($c->name);
- }
- $rs->close();
-
- $roles = get_assignable_roles($coursecontext);
- $roles[0] = get_string('none');
- $roles = array_reverse($roles, true); // Descending default sortorder.
-
- $mform->addElement('header','general', get_string('pluginname', 'enrol_cohort'));
-
- $mform->addElement('select', 'cohortid', get_string('cohort', 'cohort'), $cohorts);
- $mform->addRule('cohortid', get_string('required'), 'required', null, 'client');
-
- $mform->addElement('select', 'roleid', get_string('role'), $roles);
- $mform->addRule('roleid', get_string('required'), 'required', null, 'client');
- $mform->setDefault('roleid', $enrol->get_config('roleid'));
-
- $mform->addElement('hidden', 'id', null);
- $mform->setType('id', PARAM_INT);
-
- $this->add_action_buttons(true, get_string('addinstance', 'enrol'));
-
- $this->set_data(array('id'=>$this->course->id));
- }
-
- function validation($data, $files) {
- global $DB;
-
- $errors = parent::validation($data, $files);
-
- if ($DB->record_exists('enrol', array('roleid'=>$data['roleid'], 'customint1'=>$data['cohortid'], 'courseid'=>$this->course->id, 'enrol'=>'cohort'))) {
- $errors['cohortid'] = get_string('instanceexists', 'enrol_cohort');
- }
-
- return $errors;
- }
-}
Oops, something went wrong.

0 comments on commit c740703

Please sign in to comment.