Skip to content

Commit 30146bc

Browse files
ELIS-9111: Update calls to Moodle API email_to_user() to include all required user properties.
1 parent c7e6519 commit 30146bc

File tree

2 files changed

+12
-55
lines changed

2 files changed

+12
-55
lines changed

lib.php

+12-27
Original file line numberDiff line numberDiff line change
@@ -1153,28 +1153,6 @@ function rlip_get_notification_emails($plugin) {
11531153
return array_values($result);
11541154
}
11551155

1156-
/**
1157-
* Obtain an object that we can use as the target user in email_to_user
1158-
*
1159-
* @param string $email Email of the target recipient
1160-
* @return object A user object, either from the db, or a simulated user record
1161-
* for non-moodle emails
1162-
*/
1163-
function rlip_get_email_recipient($email) {
1164-
global $DB;
1165-
1166-
if ($result = $DB->get_record('user', array('email' => $email))) {
1167-
//user exists, so use their user record
1168-
return $result;
1169-
}
1170-
1171-
//fake user record for non-moodle recipient
1172-
$result = new stdClass;
1173-
$result->email = $email;
1174-
1175-
return $result;
1176-
}
1177-
11781156
/**
11791157
* Send a log email to a specific recipient
11801158
*
@@ -1204,7 +1182,7 @@ function rlip_send_log_email($plugin, $recipient, $archive_name) {
12041182
* @param boolean $manual True if manual, false if scheduled
12051183
*/
12061184
function rlip_send_log_emails($plugin, $logids, $manual = false) {
1207-
global $CFG;
1185+
global $CFG, $USER;
12081186

12091187
//obtain the sanitized list of emails
12101188
$emails = rlip_get_notification_emails($plugin);
@@ -1216,14 +1194,21 @@ function rlip_send_log_emails($plugin, $logids, $manual = false) {
12161194
return false;
12171195
}
12181196

1219-
//send to all appropriate users
1197+
// send to all appropriate users
1198+
// A valid $user->id & all other fields now required by Moodle 2.6+ email_to_user() API which also calls fullname($user).
1199+
$duser = new stdClass;
1200+
$allfields = get_all_user_name_fields();
1201+
foreach ($allfields as $field) {
1202+
$duser->$field = null;
1203+
}
1204+
$duser->id = $USER->id; // let's just use this user as default (TBD)
1205+
$duser->mailformat = 1;
12201206
foreach ($emails as $email) {
1221-
$recipient = rlip_get_email_recipient($email);
1222-
rlip_send_log_email($plugin, $recipient, $archive_name);
1207+
$duser->email = trim($email);
1208+
rlip_send_log_email($plugin, $duser, $archive_name);
12231209
}
12241210

12251211
@unlink($CFG->dataroot.'/'.$archive_name);
1226-
12271212
return true;
12281213
}
12291214

tests/utility_methods_test.php

-28
Original file line numberDiff line numberDiff line change
@@ -1384,34 +1384,6 @@ public function test_getnoficiationemailsremovesinvalidemails() {
13841384
$this->assertEquals($emails, $expectedresult);
13851385
}
13861386

1387-
/**
1388-
* Validate that email recipient retrieval creates a mock user record for
1389-
* an email address that is not within Moodle
1390-
*/
1391-
public function test_getemailrecipientreturnsmockuserfornonexistentemailaddress() {
1392-
$recipient = rlip_get_email_recipient('[email protected]');
1393-
1394-
$expectedresult = new stdClass;
1395-
$expectedresult->email = '[email protected]';
1396-
1397-
$this->assertEquals($recipient, $expectedresult);
1398-
}
1399-
1400-
/**
1401-
* Validate that the email recipient retrieval uses an existing user record
1402-
* for an email address that is within Moodle
1403-
*/
1404-
public function test_getemailrecipientreturnsvaliduserforexistingemailaddress() {
1405-
global $DB;
1406-
1407-
$this->create_complete_test_user();
1408-
1409-
$recipient = rlip_get_email_recipient('[email protected]');
1410-
1411-
$expectedresult = $DB->get_record('user', array('username' => 'rlipusername'));
1412-
$this->assertEquals($recipient, $expectedresult);
1413-
}
1414-
14151387
/**
14161388
* Validate zip file name construction for logging emails related to an
14171389
* import plugin

0 commit comments

Comments
 (0)