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
This commit is contained in:
Huong Nguyen
2024-04-04 20:13:46 +07:00
parent b6af21bec0
commit 097a6d46e0
7 changed files with 250 additions and 52 deletions
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace enrol_self;
/**
* Hook callbacks to get the enrolment information.
*
* @package enrol_self
* @copyright 2024 Huong Nguyen <[email protected]>
* @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,
);
}
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Hook callbacks for enrol_self
*
* @package enrol_self
* @copyright 2024 Huong Nguyen <[email protected]>
* @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',
],
];
+4
View File
@@ -0,0 +1,4 @@
customwelcomemessage,enrol_self
customwelcomemessage_help,enrol_self
welcometocourse,enrol_self
welcometocoursetext,enrol_self
+13 -11
View File
@@ -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:
<br>
* Course name {$a->coursename}<br>
* Link to user\'s profile page {$a->profileurl}<br>
* User email {$a->email}<br>
* User fullname {$a->fullname}<br>
* User first name {$a->firstname}<br>
* User last name {$a->lastname}<br>
* User course role {$a->courserole}<br>';
$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:
<br>
* Course name {$a->coursename}<br>
* Link to user\'s profile page {$a->profileurl}<br>
* User email {$a->email}<br>
* User fullname {$a->fullname}<br>
* User first name {$a->firstname}<br>
* User last name {$a->lastname}<br>
* User course role {$a->courserole}<br>';
$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.';
+38 -41
View File
@@ -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');
@@ -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"
+2
View File
@@ -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 ===