MDL-49380 enrol_cohort: Automatically create a group if needed
This commit is contained in:
+14
-1
@@ -89,9 +89,22 @@ if ($mform->is_cancelled()) {
|
||||
$instance->roleid = $data->roleid;
|
||||
$instance->customint2 = $data->customint2;
|
||||
$instance->timemodified = time();
|
||||
if ((int)$data->customint2 == -1) {
|
||||
require_capability('moodle/course:managegroups', $context);
|
||||
$groupid = enrol_cohort_create_new_group($course->id, $data->customint1);
|
||||
$instance->customint2 = $groupid;
|
||||
}
|
||||
$DB->update_record('enrol', $instance);
|
||||
} else {
|
||||
$enrol->add_instance($course, array('name'=>$data->name, 'status'=>$data->status, 'customint1'=>$data->customint1, 'roleid'=>$data->roleid, 'customint2'=>$data->customint2));
|
||||
if ((int)$data->customint2 == -1) {
|
||||
require_capability('moodle/course:managegroups', $context);
|
||||
$groupid = enrol_cohort_create_new_group($course->id, $data->customint1);
|
||||
$enrol->add_instance($course, array('name' => $data->name, 'status' => $data->status,
|
||||
'customint1' => $data->customint1, 'roleid' => $data->roleid, 'customint2' => $groupid));
|
||||
} else {
|
||||
$enrol->add_instance($course, array('name' => $data->name, 'status' => $data->status,
|
||||
'customint1' => $data->customint1, 'roleid' => $data->roleid, 'customint2' => $data->customint2));
|
||||
}
|
||||
if (!empty($data->submitbuttonnext)) {
|
||||
$returnurl = new moodle_url($PAGE->url);
|
||||
$returnurl->param('message', 'added');
|
||||
|
||||
@@ -39,7 +39,11 @@ class enrol_cohort_edit_form extends moodleform {
|
||||
$enrol = enrol_get_plugin('cohort');
|
||||
|
||||
|
||||
$groups = array(0 => get_string('none'));
|
||||
if (has_capability('moodle/course:managegroups', $coursecontext)) {
|
||||
$groups = array(0 => get_string('none'), -1 => get_string('creategroup', 'enrol_cohort'));
|
||||
} else {
|
||||
$groups = array(0 => get_string('none'));
|
||||
}
|
||||
foreach (groups_get_all_groups($course->id) as $group) {
|
||||
$groups[$group->id] = format_string($group->name, true, array('context'=>$coursecontext));
|
||||
}
|
||||
|
||||
@@ -30,3 +30,4 @@ $string['instanceexists'] = 'Cohort is already synchronised with selected role';
|
||||
$string['pluginname'] = 'Cohort sync';
|
||||
$string['pluginname_desc'] = 'Cohort enrolment plugin synchronises cohort members with course participants.';
|
||||
$string['status'] = 'Active';
|
||||
$string['creategroup'] = 'Create new group';
|
||||
|
||||
@@ -323,3 +323,24 @@ class enrol_cohort_plugin extends enrol_plugin {
|
||||
function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* If group has been created in course returns id group, if not, create a group and return id group.
|
||||
* @param int $courseid
|
||||
* @param int $cohortid
|
||||
* @return int $groupid
|
||||
*/
|
||||
function enrol_cohort_create_new_group($courseid, $cohortid) {
|
||||
global $DB;
|
||||
$cohort = $DB->get_record('cohort', array('id' => $cohortid));
|
||||
$groupid = $DB->get_record('groups', array('name' => $cohort->name, 'courseid' => $courseid));
|
||||
if (isset($groupid->id)) {
|
||||
$groupid = $groupid->id;
|
||||
} else {
|
||||
$groupdata = new stdClass();
|
||||
$groupdata->courseid = $courseid;
|
||||
$groupdata->name = $cohort->name;
|
||||
$groupid = groups_create_group($groupdata);
|
||||
}
|
||||
return $groupid;
|
||||
}
|
||||
Reference in New Issue
Block a user