Commit ec43371a authored by Marina Glancy's avatar Marina Glancy
Browse files

MDL-45068 groups: fixes to group import

- If 'coursename' is specified in the CSV it should match the course short name - thanks Yusuf Yılmaz for the patch
- If 'idnumber' is specified but 'groupidnumber' is not, idnumber should be used for matching the course idnumber only
- If 'groupingname' is not specified, there should be no notices (regression from MDL-42514)
- If 'coursename' or 'idnumber' column is present, it can contain empty values in some/all lines
parent 5bde2c2b
......@@ -118,7 +118,7 @@ if ($mform_post->is_cancelled()) {
//decode encoded commas
$record[$header[$key]] = preg_replace($csv_encode, $csv_delimiter, trim($value));
}
if ($record[$header[0]]) {
if (trim($rawline) !== '') {
// add a new group to the database
// add fields to object $user
......@@ -134,28 +134,32 @@ if ($mform_post->is_cancelled()) {
}
}
if (isset($newgroup->idnumber)){
if (!empty($newgroup->idnumber)) {
//if idnumber is set, we use that.
//unset invalid courseid
if (!$mycourse = $DB->get_record('course', array('idnumber'=>$newgroup->idnumber))) {
echo $OUTPUT->notification(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber));
unset($newgroup->courseid);//unset so 0 doesn't get written to database
} else {
$newgroup->courseid = $mycourse->id;
}
$newgroup->courseid = $mycourse->id;
} else if (isset($newgroup->coursename)){
} else if (!empty($newgroup->coursename)) {
//else use course short name to look up
//unset invalid coursename (if no id)
if (!$mycourse = $DB->get_record('course', array('shortname', $newgroup->coursename))) {
if (!$mycourse = $DB->get_record('course', array('shortname' => $newgroup->coursename))) {
echo $OUTPUT->notification(get_string('unknowncourse', 'error', $newgroup->coursename));
unset($newgroup->courseid);//unset so 0 doesn't get written to database
} else {
$newgroup->courseid = $mycourse->id;
}
$newgroup->courseid = $mycourse->id;
} else {
//else use use current id
$newgroup->courseid = $id;
}
unset($newgroup->idnumber);
unset($newgroup->coursename);
//if courseid is set
if (isset($newgroup->courseid)) {
......@@ -196,7 +200,7 @@ if ($mform_post->is_cancelled()) {
}
// Add group to grouping
if (!empty($newgroup->groupingname) || is_numeric($newgroup->groupingname)) {
if (isset($newgroup->groupingname) && strlen($newgroup->groupingname)) {
$groupingname = $newgroup->groupingname;
if (! $groupingid = groups_get_grouping_by_name($newgroup->courseid, $groupingname)) {
$data = new stdClass();
......
......@@ -8,12 +8,14 @@ Feature: Importing of groups and groupings
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
| Course 2 | C2 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| teacher1 | C2 | editingteacher |
@javascript
Scenario: Import groups and groupings as teacher
......@@ -110,3 +112,32 @@ Feature: Importing of groups and groupings
And I press "Edit group settings"
And the field "id_idnumber" matches value ""
And I press "Cancel"
@javascript
Scenario: Import groups into multiple courses as a teacher
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Users > Groups" in current page administration
And I press "Import groups"
When I upload "group/tests/fixtures/groups_import_multicourse.csv" file to "Import" filemanager
And I press "Import groups"
Then I should see "Group group7 added successfully"
And I should see "Unknown course named \"C-non-existing\""
And I should see "Group group8 added successfully"
And I should not see "group-will-not-be-created"
And I should see "Group group9 added successfully"
And I should see "Group group10 added successfully"
And I press "Continue"
And I should see "group10"
And I should see "group7"
And I should see "group8"
And I should not see "group9"
And I should not see "group-will-not-be-created"
And I am on "Course 2" course homepage
And I navigate to "Users > Groups" in current page administration
And I should see "group9"
And I should not see "group-will-not-be-created"
And I should not see "group7"
And I should not see "group8"
And I should not see "group10"
And I log out
coursename,groupname
C1,group7
C-non-existing,group-will-not-be-created
C1,group8
C2,group9
,group10
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment