From 097a6d46e0976fea7f1db4fcb868d6257bb41c45 Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Thu, 4 Apr 2024 16:07:25 +0700 Subject: [PATCH] MDL-4188 enrol_self: Send course welcome message on enrolment Including in this commit: - Use language strings from core_enrol to match with enrol_manual - Minor update for UI so Custom welcome message text area will not be shown if the Send course welcome message is set to No - enrol_self now using Hook API to send the welcome message - enrol_self_plugin::email_welcome_message() has been deprecated - Added Behat test to test the welcome message --- .../self/classes/user_enrolment_callbacks.php | 46 +++++++ enrol/self/db/hooks.php | 32 +++++ enrol/self/lang/en/deprecated.txt | 4 + enrol/self/lang/en/enrol_self.php | 24 ++-- enrol/self/lib.php | 79 ++++++------ enrol/self/tests/behat/welcomemessage.feature | 115 ++++++++++++++++++ enrol/upgrade.txt | 2 + 7 files changed, 250 insertions(+), 52 deletions(-) create mode 100644 enrol/self/classes/user_enrolment_callbacks.php create mode 100644 enrol/self/db/hooks.php create mode 100644 enrol/self/lang/en/deprecated.txt create mode 100644 enrol/self/tests/behat/welcomemessage.feature diff --git a/enrol/self/classes/user_enrolment_callbacks.php b/enrol/self/classes/user_enrolment_callbacks.php new file mode 100644 index 00000000000..61177fdf0eb --- /dev/null +++ b/enrol/self/classes/user_enrolment_callbacks.php @@ -0,0 +1,46 @@ +. + +namespace enrol_self; + +/** + * Hook callbacks to get the enrolment information. + * + * @package enrol_self + * @copyright 2024 Huong Nguyen + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class user_enrolment_callbacks { + + /** + * Callback for the user_enrolment hook. + * + * @param \core_enrol\hook\after_user_enrolled $hook + */ + public static function send_course_welcome_message(\core_enrol\hook\after_user_enrolled $hook): void { + $instance = $hook->get_enrolinstance(); + // Send welcome message. + if ($instance->enrol == 'self' && $instance->customint4 && $instance->customint4 !== ENROL_DO_NOT_SEND_EMAIL) { + $plugin = enrol_get_plugin($instance->enrol); + $plugin->send_course_welcome_message_to_user( + instance: $instance, + userid: $hook->get_userid(), + sendoption: $instance->customint4, + message: $instance->customtext1, + ); + } + } +} diff --git a/enrol/self/db/hooks.php b/enrol/self/db/hooks.php new file mode 100644 index 00000000000..8625dab8978 --- /dev/null +++ b/enrol/self/db/hooks.php @@ -0,0 +1,32 @@ +. + +/** + * Hook callbacks for enrol_self + * + * @package enrol_self + * @copyright 2024 Huong Nguyen + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$callbacks = [ + [ + 'hook' => core_enrol\hook\after_user_enrolled::class, + 'callback' => 'enrol_self\user_enrolment_callbacks::send_course_welcome_message', + ], +]; diff --git a/enrol/self/lang/en/deprecated.txt b/enrol/self/lang/en/deprecated.txt new file mode 100644 index 00000000000..3865b9499d5 --- /dev/null +++ b/enrol/self/lang/en/deprecated.txt @@ -0,0 +1,4 @@ +customwelcomemessage,enrol_self +customwelcomemessage_help,enrol_self +welcometocourse,enrol_self +welcometocoursetext,enrol_self diff --git a/enrol/self/lang/en/enrol_self.php b/enrol/self/lang/en/enrol_self.php index c5c0afa10f6..3cd6068521c 100644 --- a/enrol/self/lang/en/enrol_self.php +++ b/enrol/self/lang/en/enrol_self.php @@ -29,16 +29,6 @@ $string['cohortnonmemberinfo'] = 'Only members of cohort \'{$a}\' can self-enrol $string['cohortonly'] = 'Only cohort members'; $string['cohortonly_help'] = 'Self enrolment may be restricted to members of a specified cohort only. Note that changing this setting has no effect on existing enrolments.'; $string['confirmbulkdeleteenrolment'] = 'Are you sure you want to delete these user enrolments?'; -$string['customwelcomemessage'] = 'Custom welcome message'; -$string['customwelcomemessage_help'] = 'Accepted formats: Plain text or Moodle-auto format. HTML tags and multi-lang tags are also accepted, as well as the following placeholders: -
-* Course name {$a->coursename}
-* Link to user\'s profile page {$a->profileurl}
-* User email {$a->email}
-* User fullname {$a->fullname}
-* User first name {$a->firstname}
-* User last name {$a->lastname}
-* User course role {$a->courserole}
'; $string['defaultrole'] = 'Default role assignment'; $string['defaultrole_desc'] = 'Select role which should be assigned to users during self enrolment'; $string['deleteselectedusers'] = 'Delete selected user enrolments'; @@ -126,10 +116,22 @@ $string['unenroluser'] = 'Do you really want to unenrol "{$a->user}" from course $string['unenrolusers'] = 'Unenrol users'; $string['usepasswordpolicy'] = 'Use password policy'; $string['usepasswordpolicy_desc'] = 'Use standard password policy for enrolment keys.'; +$string['privacy:metadata'] = 'The Self enrolment plugin does not store any personal data.'; + +// Deprecated since Moodle 4.4. +$string['customwelcomemessage'] = 'Custom welcome message'; +$string['customwelcomemessage_help'] = 'Accepted formats: Plain text or Moodle-auto format. HTML tags and multi-lang tags are also accepted, as well as the following placeholders: +
+* Course name {$a->coursename}
+* Link to user\'s profile page {$a->profileurl}
+* User email {$a->email}
+* User fullname {$a->fullname}
+* User first name {$a->firstname}
+* User last name {$a->lastname}
+* User course role {$a->courserole}
'; $string['welcometocourse'] = 'Welcome to {$a}'; $string['welcometocoursetext'] = 'Welcome to {$a->coursename}! If you have not done so already, you should edit your profile page so that we can learn more about you: {$a->profileurl}'; -$string['privacy:metadata'] = 'The Self enrolment plugin does not store any personal data.'; diff --git a/enrol/self/lib.php b/enrol/self/lib.php index a4f8390695b..2001f77e97d 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -174,10 +174,6 @@ class enrol_self_plugin extends enrol_plugin { } } } - // Send welcome message. - if ($instance->customint4 != ENROL_DO_NOT_SEND_EMAIL) { - $this->email_welcome_message($instance, $USER); - } } /** @@ -392,43 +388,19 @@ class enrol_self_plugin extends enrol_plugin { * @param stdClass $instance * @param stdClass $user user record * @return void + * @deprecated since Moodle 4.4 + * @see \enrol_plugin::send_course_welcome_message_to_user() + * @todo MDL-81185 Final deprecation in Moodle 4.8. */ + #[\core\attribute\deprecated('enrol_plugin::send_course_welcome_message_to_user', since: '4.4', mdl: 'MDL-4188')] protected function email_welcome_message($instance, $user) { - global $CFG, $DB; - - $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST); - $context = context_course::instance($course->id); - - $a = new stdClass(); - $a->coursename = format_string($course->fullname, true, array('context'=>$context)); - $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id"; - - if (!is_null($instance->customtext1) && trim($instance->customtext1) !== '') { - $message = $instance->customtext1; - $key = array('{$a->coursename}', '{$a->profileurl}', '{$a->fullname}', '{$a->email}'); - $value = array($a->coursename, $a->profileurl, fullname($user), $user->email); - $message = str_replace($key, $value, $message); - if (strpos($message, '<') === false) { - // Plain text only. - $messagetext = $message; - $messagehtml = text_to_html($messagetext, null, false, true); - } else { - // This is most probably the tag/newline soup known as FORMAT_MOODLE. - $messagehtml = format_text($message, FORMAT_MOODLE, array('context'=>$context, 'para'=>false, 'newlines'=>true, 'filter'=>true)); - $messagetext = html_to_text($messagehtml); - } - } else { - $messagetext = get_string('welcometocoursetext', 'enrol_self', $a); - $messagehtml = text_to_html($messagetext, null, false, true); - } - - $subject = get_string('welcometocourse', 'enrol_self', format_string($course->fullname, true, array('context'=>$context))); - - $sendoption = $instance->customint4; - $contact = $this->get_welcome_message_contact($sendoption, $context); - - // Directly emailing welcome message rather than using messaging. - email_to_user($user, $contact, $subject, $messagetext, $messagehtml); + \core\deprecation::emit_deprecation_if_present(__FUNCTION__); + $this->send_course_welcome_message_to_user( + instance: $instance, + userid: $user->id, + sendoption: $instance->customint4, + message: $instance->customtext1, + ); } /** @@ -960,8 +932,33 @@ class enrol_self_plugin extends enrol_plugin { $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); $options = array('cols' => '60', 'rows' => '8'); - $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), $options); - $mform->addHelpButton('customtext1', 'customwelcomemessage', 'enrol_self'); + $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'core_enrol'), $options); + $mform->setDefault('customtext1', get_string('customwelcomemessageplaceholder', 'core_enrol')); + $mform->hideIf( + elementname: 'customtext1', + dependenton: 'customint4', + condition: 'eq', + value: ENROL_DO_NOT_SEND_EMAIL, + ); + + // Static form elements cannot be hidden by hideIf() so we need to add a dummy group. + // See: https://tracker.moodle.org/browse/MDL-66251. + $group[] = $mform->createElement( + 'static', + 'customwelcomemessage_extra_help', + null, + get_string( + identifier: 'customwelcomemessage_help', + component: 'core_enrol', + ), + ); + $mform->addGroup($group, 'group_customwelcomemessage_extra_help', '', ' ', false); + $mform->hideIf( + elementname: 'group_customwelcomemessage_extra_help', + dependenton: 'customint4', + condition: 'eq', + value: ENROL_DO_NOT_SEND_EMAIL, + ); if (enrol_accessing_via_instance($instance)) { $warntext = get_string('instanceeditselfwarningtext', 'core_enrol'); diff --git a/enrol/self/tests/behat/welcomemessage.feature b/enrol/self/tests/behat/welcomemessage.feature new file mode 100644 index 00000000000..3cc8e7df22f --- /dev/null +++ b/enrol/self/tests/behat/welcomemessage.feature @@ -0,0 +1,115 @@ +@enrol @enrol_self +Feature: A course welcome message will be sent to the user when they auto-enrol themself in a course + In order to let the user know they have been auto-enrol themself in a course successfully + As a teacher + I want the user to receive a welcome message when they auto-enrol themself in a course + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | manager | Manager | User | manager@example.com | + | teacher | Teacher | User | teacher@example.com | + | user1 | First | User | first@example.com | + | user2 | Second | User | second@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + | Course 2 | C2 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | manager | C1 | manager | + | teacher | C1 | editingteacher | + | teacher | C2 | editingteacher | + And I log in as "admin" + And I add "Self enrolment" enrolment method in "Course 1" with: + | Custom instance name | Test student enrolment | + And I add "Self enrolment" enrolment method in "Course 2" with: + | Custom instance name | Test student enrolment | + + @javascript + Scenario: Manager should see the new settings for course welcome message + Given I am on the "C1" "Enrolled users" page logged in as manager + And I set the field "Participants tertiary navigation" to "Enrolment methods" + When I click on "Edit" "link" in the "Test student enrolment" "table_row" + Then I should see "Send course welcome message" + And the field "Send course welcome message" matches value "From the course contact" + And I should see "Custom welcome message" + And the field "Custom welcome message" matches value "Dear {$a->fullname}, you have successfully been enrolled to course {$a->coursename}" + And I should see "Accepted formats: Plain text or Moodle-auto format. HTML tags and multi-lang tags are also accepted, as well as the following placeholders:" + And I set the field "Send course welcome message" to "No" + And I should not see "Custom welcome message" + And I should not see "Accepted formats: Plain text or Moodle-auto format. HTML tags and multi-lang tags are also accepted, as well as the following placeholders:" + + @javascript + Scenario: Student should not receive a welcome message if the setting is disabled + Given I am on the "C1" "Enrolled users" page logged in as manager + And I set the field "Participants tertiary navigation" to "Enrolment methods" + And I click on "Edit" "link" in the "Test student enrolment" "table_row" + And I set the field "Send course welcome message" to "No" + And I press "Save changes" + And I log in as "user1" + And I am on "Course 1" course homepage + When I press "Enrol me" + Then I should not see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element" + + @javascript + Scenario: Students should receive a welcome message if the setting is enabled - Default message + # Login as first user and check the notification. + Given I log in as "user1" + And I am on "Course 1" course homepage + When I press "Enrol me" + Then I should see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element" + And I open the notification popover + And I should see "Welcome to Course 1" + And I click on "View full notification" "link" in the ".popover-region-notifications" "css_element" + And I should see "Dear First User, you have successfully been enrolled to course Course 1" + # Login as second user and check the notification. + And I log in as "user2" + And I am on "Course 2" course homepage + And I press "Enrol me" + And I should see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element" + And I open the notification popover + And I should see "Welcome to Course 2" + And I click on "View full notification" "link" in the ".popover-region-notifications" "css_element" + And I should see "Dear Second User, you have successfully been enrolled to course Course 2" + + @javascript + Scenario: Students should receive a welcome message if the setting is enabled - Custom message + Given I am on the "C1" "Enrolled users" page logged in as manager + And I set the field "Participants tertiary navigation" to "Enrolment methods" + And I click on "Edit" "link" in the "Test student enrolment" "table_row" + And I set the field "Custom welcome message" to multiline: + """ + Dear {$a->fullname}, you have successfully been enrolled to course {$a->coursename}. + Your email address: {$a->email} + Your first name: {$a->firstname} + Your last name: {$a->lastname} + Your course role: {$a->courserole} + """ + And I press "Save changes" + # Login as first user and check the notification. + And I log in as "user1" + And I am on "Course 1" course homepage + When I press "Enrol me" + Then I should see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element" + And I open the notification popover + And I should see "Welcome to Course 1" + And I click on "View full notification" "link" in the ".popover-region-notifications" "css_element" + And I should see "Dear First User, you have successfully been enrolled to course Course 1" + And I should see "Your email address: first@example.com" + And I should see "Your first name: First" + And I should see "Your last name: User" + And I should see "Your course role: student" + # Login as second user and check the notification. + And I log in as "user2" + And I am on "Course 1" course homepage + And I press "Enrol me" + And I should see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element" + And I open the notification popover + And I should see "Welcome to Course 1" + And I click on "View full notification" "link" in the ".popover-region-notifications" "css_element" + And I should see "Dear Second User, you have successfully been enrolled to course Course 1" + And I should see "Your email address: second@example.com" + And I should see "Your first name: Second" + And I should see "Your last name: User" + And I should see "Your course role: student" diff --git a/enrol/upgrade.txt b/enrol/upgrade.txt index add63582cce..6b1d2450f7b 100644 --- a/enrol/upgrade.txt +++ b/enrol/upgrade.txt @@ -15,6 +15,8 @@ information provided here is intended especially for developers. * 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. +* enrol_self_plugin::email_welcome_message() has been deprecated. + Please use enrol_plugin::send_course_welcome_message_to_user() instead. === 4.3 ===