diff --git a/enrol/self/lib.php b/enrol/self/lib.php index 5ecfb19aee1..a4f8390695b 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -425,7 +425,7 @@ class enrol_self_plugin extends enrol_plugin { $subject = get_string('welcometocourse', 'enrol_self', format_string($course->fullname, true, array('context'=>$context))); $sendoption = $instance->customint4; - $contact = $this->get_welcome_email_contact($sendoption, $context); + $contact = $this->get_welcome_message_contact($sendoption, $context); // Directly emailing welcome message rather than using messaging. email_to_user($user, $contact, $subject, $messagetext, $messagehtml); @@ -1154,46 +1154,17 @@ class enrol_self_plugin extends enrol_plugin { * @param int $sendoption send email from constant ENROL_SEND_EMAIL_FROM_* * @param $context context where the user will be fetched * @return mixed|stdClass the contact user object. + * @deprecated since Moodle 4.4 + * @see \enrol_plugin::get_welcome_message_contact() + * @todo MDL-81185 Final deprecation in Moodle 4.8. */ + #[\core\attribute\deprecated('enrol_plugin::get_welcome_message_contact', since: '4.4', mdl: 'MDL-4188')] public function get_welcome_email_contact($sendoption, $context) { - global $CFG; - - $contact = null; - // Send as the first user assigned as the course contact. - if ($sendoption == ENROL_SEND_EMAIL_FROM_COURSE_CONTACT) { - $rusers = array(); - if (!empty($CFG->coursecontact)) { - $croles = explode(',', $CFG->coursecontact); - list($sort, $sortparams) = users_order_by_sql('u'); - // We only use the first user. - $i = 0; - do { - $userfieldsapi = \core_user\fields::for_name(); - $allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects; - $rusers = get_role_users($croles[$i], $context, true, 'u.id, u.confirmed, u.username, '. $allnames . ', - u.email, r.sortorder, ra.id', 'r.sortorder, ra.id ASC, ' . $sort, null, '', '', '', '', $sortparams); - $i++; - } while (empty($rusers) && !empty($croles[$i])); - } - if ($rusers) { - $contact = array_values($rusers)[0]; - } - } else if ($sendoption == ENROL_SEND_EMAIL_FROM_KEY_HOLDER) { - // Send as the first user with enrol/self:holdkey capability assigned in the course. - list($sort) = users_order_by_sql('u'); - $keyholders = get_users_by_capability($context, 'enrol/self:holdkey', 'u.*', $sort); - if (!empty($keyholders)) { - $contact = array_values($keyholders)[0]; - } - } - - // If send welcome email option is set to no reply or if none of the previous options have - // returned a contact send welcome message as noreplyuser. - if ($sendoption == ENROL_SEND_EMAIL_FROM_NOREPLY || empty($contact)) { - $contact = core_user::get_noreply_user(); - } - - return $contact; + \core\deprecation::emit_deprecation_if_present(__FUNCTION__); + return $this->get_welcome_message_contact( + sendoption: $sendoption, + context: $context, + ); } /** diff --git a/enrol/self/tests/self_test.php b/enrol/self/tests/self_test.php index 66e37d5e7d4..722fea9b965 100644 --- a/enrol/self/tests/self_test.php +++ b/enrol/self/tests/self_test.php @@ -1014,22 +1014,26 @@ class self_test extends \advanced_testcase { $DB->update_record('enrol', $instance1); $selfplugin->update_status($instance1, ENROL_INSTANCE_ENABLED); - // We do not have a teacher enrolled at this point, so it should send as no reply user. - $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, $context); - $this->assertEquals($noreplyuser, $contact); + // This should return null. + $contact = $selfplugin->get_welcome_message_contact(ENROL_DO_NOT_SEND_EMAIL, $context); + $this->assertNull($contact); + + // We do not have a teacher enrolled at this point, so it should return null. + $contact = $selfplugin->get_welcome_message_contact(ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, $context); + $this->assertNull($contact); // By default, course contact is assigned to teacher role. // Enrol a teacher, now it should send emails from teacher email's address. $selfplugin->enrol_user($instance1, $user1->id, $editingteacherrole->id); // We should get the teacher email. - $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, $context); + $contact = $selfplugin->get_welcome_message_contact(ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, $context); $this->assertEquals($user1->username, $contact->username); $this->assertEquals($user1->email, $contact->email); // Now let's enrol another teacher. $selfplugin->enrol_user($instance1, $user2->id, $editingteacherrole->id); - $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, $context); + $contact = $selfplugin->get_welcome_message_contact(ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, $context); $this->assertEquals($user1->username, $contact->username); $this->assertEquals($user1->email, $contact->email); @@ -1044,21 +1048,25 @@ class self_test extends \advanced_testcase { assign_capability('enrol/self:holdkey', CAP_ALLOW, $managerrole->id, $context); // We should get the manager email contact. - $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_KEY_HOLDER, $context); + $contact = $selfplugin->get_welcome_message_contact(ENROL_SEND_EMAIL_FROM_KEY_HOLDER, $context); $this->assertEquals($user3->username, $contact->username); $this->assertEquals($user3->email, $contact->email); // Now let's enrol another manager. $selfplugin->enrol_user($instance1, $user4->id, $managerrole->id); - $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_KEY_HOLDER, $context); + $contact = $selfplugin->get_welcome_message_contact(ENROL_SEND_EMAIL_FROM_KEY_HOLDER, $context); $this->assertEquals($user3->username, $contact->username); $this->assertEquals($user3->email, $contact->email); $instance1->customint4 = ENROL_SEND_EMAIL_FROM_NOREPLY; $DB->update_record('enrol', $instance1); - $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_NOREPLY, $context); + $contact = $selfplugin->get_welcome_message_contact(ENROL_SEND_EMAIL_FROM_NOREPLY, $context); $this->assertEquals($noreplyuser, $contact); + + $this->expectException(\moodle_exception::class); + $this->expectExceptionMessage('Invalid send option'); + $contact = $selfplugin->get_welcome_message_contact(10, $context); } /** diff --git a/enrol/upgrade.txt b/enrol/upgrade.txt index b369e119886..add63582cce 100644 --- a/enrol/upgrade.txt +++ b/enrol/upgrade.txt @@ -13,6 +13,8 @@ information provided here is intended especially for developers. plugin since it is already supported in CSV course upload. For self enrolment plugin, find_instance() returns first available instance in the course. * A sesskey is no longer passed to the enrol/test_settings.php page so it can no longer be required in test_settings(). +* enrol_self_plugin::get_welcome_email_contact() has been deprecated. + Please use enrol_plugin::get_welcome_message_contact() instead. === 4.3 === diff --git a/lib/enrollib.php b/lib/enrollib.php index e8e74811637..61e0f5119d2 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -3571,4 +3571,65 @@ abstract class enrol_plugin { // Plugins can override this if they can uniquely identify an instance. return null; } + + /** + * Get the "from" contact which the message will be sent from. + * + * @param int $sendoption send email from constant ENROL_SEND_EMAIL_FROM_* + * @param context $context where the user will be fetched from. + * @return null|stdClass the contact user object. + */ + public function get_welcome_message_contact( + int $sendoption, + context $context, + ): ?stdClass { + global $CFG; + + $acceptedsendoptions = [ + ENROL_DO_NOT_SEND_EMAIL, + ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, + ENROL_SEND_EMAIL_FROM_KEY_HOLDER, + ENROL_SEND_EMAIL_FROM_NOREPLY, + ]; + if (!in_array($sendoption, $acceptedsendoptions)) { + throw new coding_exception('Invalid send option'); + } + if ($sendoption === ENROL_DO_NOT_SEND_EMAIL) { + return null; + } + $contact = null; + // Send as the first user assigned as the course contact. + if ($sendoption === ENROL_SEND_EMAIL_FROM_COURSE_CONTACT) { + $rusers = []; + if (!empty($CFG->coursecontact)) { + $croles = explode(',', $CFG->coursecontact); + [$sort, $sortparams] = users_order_by_sql('u'); + // We only use the first user. + $i = 0; + do { + $userfieldsapi = \core_user\fields::for_name(); + $allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects; + $rusers = get_role_users($croles[$i], $context, true, 'u.id, u.confirmed, u.username, '. $allnames . ', + u.email, r.sortorder, ra.id', 'r.sortorder, ra.id ASC, ' . $sort, null, '', '', '', '', $sortparams); + $i++; + } while (empty($rusers) && !empty($croles[$i])); + } + if ($rusers) { + $contact = array_values($rusers)[0]; + } + } else if ($sendoption === ENROL_SEND_EMAIL_FROM_KEY_HOLDER) { + // Send as the first user with enrol/self:holdkey capability assigned in the course. + [$sort] = users_order_by_sql('u'); + $keyholders = get_users_by_capability($context, 'enrol/self:holdkey', 'u.*', $sort); + if (!empty($keyholders)) { + $contact = array_values($keyholders)[0]; + } + } + + if ($sendoption === ENROL_SEND_EMAIL_FROM_NOREPLY) { + $contact = core_user::get_noreply_user(); + } + + return $contact; + } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 163bde8bb81..68adac414c7 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -137,6 +137,7 @@ information provided here is intended especially for developers. because there aren't cases in core. - Deprecation: Cannot use the "Test" suffix on abstract test case classes. Proceed to rename them to end with "TestCase" instead. +* There is a new method called enrol_plugin::get_welcome_message_contact() that returns the contact details for the course welcome message. === 4.3 ===