diff --git a/enrol/self/lang/en/enrol_self.php b/enrol/self/lang/en/enrol_self.php index 1af38c69574..480489fe35a 100644 --- a/enrol/self/lang/en/enrol_self.php +++ b/enrol/self/lang/en/enrol_self.php @@ -98,7 +98,14 @@ $string['self:manage'] = 'Manage enrolled users'; $string['self:unenrol'] = 'Unenrol users from course'; $string['self:unenrolself'] = 'Unenrol self from the course'; $string['sendcoursewelcomemessage'] = 'Send course welcome message'; -$string['sendcoursewelcomemessage_help'] = 'If enabled, users receive a welcome message via email when they self-enrol in a course.'; +$string['sendcoursewelcomemessage_help'] = 'When a user self enrols in the course, they may be sent an welcome message email.
+If sent from the course contact (by default the teacher), and more than one user has this role, the email is sent from the first user to be assigned the role. + +The welcome email can be sent as: + +* First user with key holder capability assigned. +* First user with assigned as course contact. +* No reply address contact.'; $string['showhint'] = 'Show hint'; $string['showhint_desc'] = 'Show first letter of the guest access key.'; $string['status'] = 'Allow existing enrolments'; diff --git a/enrol/self/lib.php b/enrol/self/lib.php index 7b2827e329d..565f3ef5aeb 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -173,7 +173,7 @@ class enrol_self_plugin extends enrol_plugin { } } // Send welcome message. - if ($instance->customint4) { + if ($instance->customint4 !== ENROL_DO_NOT_SEND_EMAIL) { $this->email_welcome_message($instance, $USER); } } @@ -398,23 +398,8 @@ class enrol_self_plugin extends enrol_plugin { $subject = get_string('welcometocourse', 'enrol_self', format_string($course->fullname, true, array('context'=>$context))); - $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 { - $rusers = get_role_users($croles[$i], $context, true, '', - 'r.sortorder ASC, ' . $sort, null, '', '', '', '', $sortparams); - $i++; - } while (empty($rusers) && !empty($croles[$i])); - } - if ($rusers) { - $contact = reset($rusers); - } else { - $contact = core_user::get_support_user(); - } + $sendoption = $instance->customint4; + $contact = $this->get_welcome_email_contact($sendoption, $context); // Directly emailing welcome message rather than using messaging. email_to_user($user, $contact, $subject, $messagetext, $messagehtml); @@ -842,7 +827,8 @@ class enrol_self_plugin extends enrol_plugin { $mform->setConstant('customint5', 0); } - $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self')); + $mform->addElement('select', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self'), + enrol_send_welcome_email_options()); $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); $options = array('cols' => '60', 'rows' => '8'); @@ -938,7 +924,7 @@ class enrol_self_plugin extends enrol_plugin { 'customint1' => $validgroupkey, 'customint2' => $validlongtimenosee, 'customint3' => PARAM_INT, - 'customint4' => PARAM_BOOL, + 'customint4' => PARAM_INT, 'customint5' => PARAM_INT, 'customint6' => $validnewenrols, 'status' => $validstatus, @@ -1019,4 +1005,50 @@ class enrol_self_plugin extends enrol_plugin { } return $roles; } + + /** + * Get the "from" contact which the email will be sent from. + * + * @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. + */ + 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 { + $rusers = get_role_users($croles[$i], $context, true, '', + 'r.sortorder 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; + } } diff --git a/enrol/self/settings.php b/enrol/self/settings.php index 497c27edc53..25dadf0583e 100644 --- a/enrol/self/settings.php +++ b/enrol/self/settings.php @@ -111,6 +111,9 @@ if ($ADMIN->fulltree) { $settings->add(new admin_setting_configtext('enrol_self/maxenrolled', get_string('maxenrolled', 'enrol_self'), get_string('maxenrolled_help', 'enrol_self'), 0, PARAM_INT)); - $settings->add(new admin_setting_configcheckbox('enrol_self/sendcoursewelcomemessage', - get_string('sendcoursewelcomemessage', 'enrol_self'), get_string('sendcoursewelcomemessage_help', 'enrol_self'), 1)); + $settings->add(new admin_setting_configselect('enrol_self/sendcoursewelcomemessage', + get_string('sendcoursewelcomemessage', 'enrol_self'), + get_string('sendcoursewelcomemessage_help', 'enrol_self'), + ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, + enrol_send_welcome_email_options())); } diff --git a/enrol/self/tests/self_test.php b/enrol/self/tests/self_test.php index b6956003073..dd8c247001f 100644 --- a/enrol/self/tests/self_test.php +++ b/enrol/self/tests/self_test.php @@ -651,4 +651,79 @@ class enrol_self_testcase extends advanced_testcase { $this->assertFalse($result); } + + /** + * Test get_welcome_email_contact(). + */ + public function test_get_welcome_email_contact() { + global $DB; + self::resetAfterTest(true); + + $user1 = $this->getDataGenerator()->create_user(['lastname' => 'Marsh']); + $user2 = $this->getDataGenerator()->create_user(['lastname' => 'Victoria']); + $user3 = $this->getDataGenerator()->create_user(['lastname' => 'Burch']); + $user4 = $this->getDataGenerator()->create_user(['lastname' => 'Cartman']); + $noreplyuser = core_user::get_noreply_user(); + + $course1 = $this->getDataGenerator()->create_course(); + $context = context_course::instance($course1->id); + + // Get editing teacher role. + $editingteacherrole = $DB->get_record('role', ['archetype' => 'editingteacher']); + $this->assertNotEmpty($editingteacherrole); + + // Enable self enrolment plugin and set to send email from course contact. + $selfplugin = enrol_get_plugin('self'); + $instance1 = $DB->get_record('enrol', ['courseid' => $course1->id, 'enrol' => 'self'], '*', MUST_EXIST); + $instance1->customint6 = 1; + $instance1->customint4 = ENROL_SEND_EMAIL_FROM_COURSE_CONTACT; + $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); + + // 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); + $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); + $this->assertEquals($user1->username, $contact->username); + $this->assertEquals($user1->email, $contact->email); + + // Get manager role, and enrol user as manager. + $managerrole = $DB->get_record('role', ['archetype' => 'manager']); + $this->assertNotEmpty($managerrole); + $instance1->customint4 = ENROL_SEND_EMAIL_FROM_KEY_HOLDER; + $DB->update_record('enrol', $instance1); + $selfplugin->enrol_user($instance1, $user3->id, $managerrole->id); + + // Give manager role holdkey capability. + 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); + $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); + $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); + $this->assertEquals($noreplyuser, $contact); + } } diff --git a/lang/en/enrol.php b/lang/en/enrol.php index 018eb62d430..f9c026d5c45 100644 --- a/lang/en/enrol.php +++ b/lang/en/enrol.php @@ -116,6 +116,9 @@ $string['rolefromthiscourse'] = '{$a->role} (Assigned in this course)'; $string['rolefrommetacourse'] = '{$a->role} (Inherited from parent course)'; $string['rolefromcategory'] = '{$a->role} (Inherited from course category)'; $string['rolefromsystem'] = '{$a->role} (Assigned at site level)'; +$string['sendfromcoursecontact'] = 'From the course contact'; +$string['sendfromkeyholder'] = 'From the key holder'; +$string['sendfromnoreply'] = 'From the no-reply address'; $string['startdatetoday'] = 'Today'; $string['synced'] = 'Synced'; $string['testsettings'] = 'Test settings'; diff --git a/lib/enrollib.php b/lib/enrollib.php index c8ce48edb4f..6ef9c71e813 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -67,6 +67,26 @@ define('ENROL_EXT_REMOVED_SUSPEND', 2); * */ define('ENROL_EXT_REMOVED_SUSPENDNOROLES', 3); +/** + * Do not send email. + */ +define('ENROL_DO_NOT_SEND_EMAIL', 0); + +/** + * Send email from course contact. + */ +define('ENROL_SEND_EMAIL_FROM_COURSE_CONTACT', 1); + +/** + * Send email from enrolment key holder. + */ +define('ENROL_SEND_EMAIL_FROM_KEY_HOLDER', 2); + +/** + * Send email from no reply address. + */ +define('ENROL_SEND_EMAIL_FROM_NOREPLY', 3); + /** * Returns instances of enrol plugins * @param bool $enabled return enabled only @@ -1362,6 +1382,20 @@ function count_enrolled_users(context $context, $withcapability = '', $groupid = return $DB->count_records_sql($sql, $capjoin->params); } +/** + * Send welcome email "from" options. + * + * @return array list of from options + */ +function enrol_send_welcome_email_options() { + return [ + ENROL_DO_NOT_SEND_EMAIL => get_string('no'), + ENROL_SEND_EMAIL_FROM_COURSE_CONTACT => get_string('sendfromcoursecontact', 'enrol'), + ENROL_SEND_EMAIL_FROM_KEY_HOLDER => get_string('sendfromkeyholder', 'enrol'), + ENROL_SEND_EMAIL_FROM_NOREPLY => get_string('sendfromnoreply', 'enrol') + ]; +} + /** * All enrol plugins should be based on this class, * this is also the main source of documentation.