Skip to content

Commit

Permalink
Merge branch 'MDL-29317-master' of git://github.com/junpataleta/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllaó committed Apr 15, 2019
2 parents 0163c6e + 2791334 commit 815c070
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 16 additions & 1 deletion grade/import/csv/classes/load_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,23 @@ protected function check_user_exists($value, $userfields) {
$errorkey = false;
// The user may use the incorrect field to match the user. This could result in an exception.
try {
$field = $userfields['field'];
// Fields that can be queried in a case-insensitive manner.
$caseinsensitivefields = [
'email',
'username',
];
// Build query predicate.
if (in_array($field, $caseinsensitivefields)) {
// Case-insensitive.
$select = $DB->sql_equal($field, ':' . $field, false);
} else {
// Exact-value.
$select = "{$field} = :{$field}";
}

// Make sure the record exists and that there's only one matching record found.
$user = $DB->get_record('user', array($userfields['field'] => $value), '*', MUST_EXIST);
$user = $DB->get_record_select('user', $select, array($userfields['field'] => $value), '*', MUST_EXIST);
} catch (dml_missing_record_exception $missingex) {
$errorkey = 'usermappingerror';
} catch (dml_multiple_records_exception $multiex) {
Expand Down
6 changes: 6 additions & 0 deletions grade/import/csv/tests/load_data_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ public function check_user_exists_provider() {
'Fetch by email' => [
'email', 's1@example.com', true
],
'Fetch by email, different case' => [
'email', 'S1@EXAMPLE.COM', true
],
'Fetch data using a non-existent email' => [
'email', 's2@example.com', false
],
Expand All @@ -267,6 +270,9 @@ public function check_user_exists_provider() {
'Fetch data using a valid username' => [
'username', 's1', true
],
'Fetch data using a valid username, different case' => [
'username', 'S1', true
],
'Fetch data using an invalid username' => [
'username', 's2', false
],
Expand Down

0 comments on commit 815c070

Please sign in to comment.