Merge branch 'wip-MDL-49380-master' of git://github.com/abgreeve/moodle

This commit is contained in:
David Monllao
2015-04-06 10:16:38 +08:00
5 changed files with 136 additions and 2 deletions
+16 -1
View File
@@ -89,9 +89,24 @@ if ($mform->is_cancelled()) {
$instance->roleid = $data->roleid;
$instance->customint2 = $data->customint2;
$instance->timemodified = time();
// Create a new group for the cohort if requested.
if ($data->customint2 == COHORT_CREATE_GROUP) {
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));
// Create a new group for the cohort if requested.
if ($data->customint2 == COHORT_CREATE_GROUP) {
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');
+4 -1
View File
@@ -38,8 +38,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[COHORT_CREATE_GROUP] = get_string('creategroup', 'enrol_cohort');
}
foreach (groups_get_all_groups($course->id) as $group) {
$groups[$group->id] = format_string($group->name, true, array('context'=>$coursecontext));
}
+2
View File
@@ -26,7 +26,9 @@ $string['addgroup'] = 'Add to group';
$string['assignrole'] = 'Assign role';
$string['cohort:config'] = 'Configure cohort instances';
$string['cohort:unenrol'] = 'Unenrol suspended users';
$string['defaultgroupnametext'] = '{$a->name} cohort{$a->increment}';
$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';
+40
View File
@@ -24,6 +24,11 @@
defined('MOODLE_INTERNAL') || die();
/**
* COHORT_CREATEGROUP constant for automatically creating a group for a cohort.
*/
define('COHORT_CREATE_GROUP', -1);
/**
* Cohort enrolment plugin implementation.
* @author Petr Skoda
@@ -323,3 +328,38 @@ class enrol_cohort_plugin extends enrol_plugin {
function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {
return false;
}
/**
* Create a new group with the cohorts name.
*
* @param int $courseid
* @param int $cohortid
* @return int $groupid Group ID for this cohort.
*/
function enrol_cohort_create_new_group($courseid, $cohortid) {
global $DB;
$groupname = $DB->get_field('cohort', 'name', array('id' => $cohortid), MUST_EXIST);
$a = new stdClass();
$a->name = $groupname;
$a->increment = '';
$groupname = get_string('defaultgroupnametext', 'enrol_cohort', $a);
// Check to see if the cohort group name already exists. Add an incremented number if it does.
while ($DB->record_exists('groups', array('name' => $groupname))) {
$matches = array();
if (!preg_match('/(.*?)\(([0-9]+)\)$/', $groupname, $matches)) {
$a->increment = '(2)';
} else {
$a->increment = '(' . $matches[2]+1 . ')';
}
$newshortname = get_string('defaultgroupnametext', 'enrol_cohort', $a);
$groupname = $newshortname;
}
// Create a new group for the cohort.
$groupdata = new stdClass();
$groupdata->courseid = $courseid;
$groupdata->name = $groupname;
$groupid = groups_create_group($groupdata);
return $groupid;
}
+74
View File
@@ -0,0 +1,74 @@
<?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/>.
/**
* Cohort enrolment sync functional test.
*
* @package enrol_cohort
* @category phpunit
* @copyright 2015 Adrian Greeve <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/cohort/lib.php');
require_once($CFG->dirroot.'/group/lib.php');
/**
* Contains tests for the cohort library.
*
* @package enrol_cohort
* @copyright 2015 Adrian Greeve <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_cohort_lib_testcase extends advanced_testcase {
/**
* Test that a new group with the name of the cohort is created.
*/
public function test_enrol_cohort_create_new_group() {
global $DB;
$this->resetAfterTest();
// Create a category.
$category = $this->getDataGenerator()->create_category();
// Create two courses.
$course = $this->getDataGenerator()->create_course(array('category' => $category->id));
$course2 = $this->getDataGenerator()->create_course(array('category' => $category->id));
// Create a cohort.
$cohort = $this->getDataGenerator()->create_cohort(array('context' => context_coursecat::instance($category->id)->id));
// Run the function.
$groupid = enrol_cohort_create_new_group($course->id, $cohort->id);
// Check the results.
$group = $DB->get_record('groups', array('id' => $groupid));
// The group name should match the cohort name.
$this->assertEquals($cohort->name . ' cohort', $group->name);
// Group course id should match the course id.
$this->assertEquals($course->id, $group->courseid);
// Create a group that will have the same name as the cohort
$groupdata = new stdClass();
$groupdata->courseid = $course2->id;
$groupdata->name = $cohort->name . ' cohort';
groups_create_group($groupdata);
// Create a group for the cohort in course 2.
$groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
$groupinfo = $DB->get_record('groups', array('id' => $groupid));
// Check that the group name has been changed.
$this->assertEquals($cohort->name . ' cohort(2)', $groupinfo->name);
}
}