Always send emails from 'noreplyuser' (#165)

This commit is contained in:
Mark Nelson 2019-04-16 19:37:07 +08:00
parent a6b180454f
commit 1746ec47dc
2 changed files with 4 additions and 13 deletions

View file

@ -73,13 +73,10 @@ class email_certificate_task extends \core\task\scheduled_task {
$context = \context::instance_by_id($customcert->contextid); $context = \context::instance_by_id($customcert->contextid);
// Get the person we are going to send this email on behalf of. // Get the person we are going to send this email on behalf of.
// Look through the teachers. $userfrom = \core_user::get_noreply_user();
if ($teachers = get_enrolled_users($context, 'moodle/course:update')) {
$teachers = sort_by_roleassignment_authority($teachers, $context); // Store teachers for later.
$userfrom = reset($teachers); $teachers = get_enrolled_users($context, 'moodle/course:update');
} else { // Ok, no teachers, use administrator name.
$userfrom = get_admin();
}
$courseshortname = format_string($customcert->courseshortname, true, array('context' => $context)); $courseshortname = format_string($customcert->courseshortname, true, array('context' => $context));
$coursefullname = format_string($customcert->coursefullname, true, array('context' => $context)); $coursefullname = format_string($customcert->coursefullname, true, array('context' => $context));

View file

@ -95,11 +95,9 @@ class mod_customcert_task_email_certificate_task_testcase extends advanced_testc
// Confirm that we sent out emails to the two users. // Confirm that we sent out emails to the two users.
$this->assertCount(2, $emails); $this->assertCount(2, $emails);
$this->assertContains(fullname($user3), $emails[0]->header);
$this->assertEquals($CFG->noreplyaddress, $emails[0]->from); $this->assertEquals($CFG->noreplyaddress, $emails[0]->from);
$this->assertEquals($user1->email, $emails[0]->to); $this->assertEquals($user1->email, $emails[0]->to);
$this->assertContains(fullname($user3), $emails[1]->header);
$this->assertEquals($CFG->noreplyaddress, $emails[1]->from); $this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
$this->assertEquals($user2->email, $emails[1]->to); $this->assertEquals($user2->email, $emails[1]->to);
@ -150,11 +148,9 @@ class mod_customcert_task_email_certificate_task_testcase extends advanced_testc
// Confirm that we only sent out 2 emails, both emails to the teacher for the two students. // Confirm that we only sent out 2 emails, both emails to the teacher for the two students.
$this->assertCount(2, $emails); $this->assertCount(2, $emails);
$this->assertContains(fullname($user3), utf8_encode($emails[0]->header));
$this->assertEquals($CFG->noreplyaddress, $emails[0]->from); $this->assertEquals($CFG->noreplyaddress, $emails[0]->from);
$this->assertEquals($user3->email, $emails[0]->to); $this->assertEquals($user3->email, $emails[0]->to);
$this->assertContains(fullname($user3), utf8_encode($emails[1]->header));
$this->assertEquals($CFG->noreplyaddress, $emails[1]->from); $this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
$this->assertEquals($user3->email, $emails[1]->to); $this->assertEquals($user3->email, $emails[1]->to);
} }
@ -189,11 +185,9 @@ class mod_customcert_task_email_certificate_task_testcase extends advanced_testc
// Confirm that we only sent out 2 emails, both emails to the other address that was valid for the two students. // Confirm that we only sent out 2 emails, both emails to the other address that was valid for the two students.
$this->assertCount(2, $emails); $this->assertCount(2, $emails);
$this->assertContains(fullname(get_admin()), utf8_encode($emails[0]->header));
$this->assertEquals($CFG->noreplyaddress, $emails[0]->from); $this->assertEquals($CFG->noreplyaddress, $emails[0]->from);
$this->assertEquals('testcustomcert@example.com', $emails[0]->to); $this->assertEquals('testcustomcert@example.com', $emails[0]->to);
$this->assertContains(fullname(get_admin()), utf8_encode($emails[1]->header));
$this->assertEquals($CFG->noreplyaddress, $emails[1]->from); $this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
$this->assertEquals('testcustomcert@example.com', $emails[1]->to); $this->assertEquals('testcustomcert@example.com', $emails[1]->to);
} }