diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 40ff7e97c45..577fd3648c9 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -2266,6 +2266,8 @@ $string['weeks'] = 'weeks'; $string['weekhide'] = 'Hide this week from {$a}'; $string['weeklyoutline'] = 'Weekly outline'; $string['weekshow'] = 'Show this week to {$a}'; +$string['welcomeback'] = 'Welcome back, {$a}! 👋'; +$string['welcometosite'] = 'Welcome, {$a}! 👋'; $string['welcometocourse'] = 'Welcome to {$a}'; $string['welcometocoursetext'] = 'Welcome to {$a->coursename}! diff --git a/lang/en/user.php b/lang/en/user.php index 83c4e01d5d8..a47dacd0f5a 100644 --- a/lang/en/user.php +++ b/lang/en/user.php @@ -110,6 +110,7 @@ $string['privacy:metadata:reason'] = 'The reason for requesting this course.'; $string['privacy:metadata:requester'] = 'The ID of the user who requested the course'; $string['privacy:metadata:requestsummary'] = 'Stores information about requests for courses that users make.'; $string['privacy:metadata:suspended'] = 'A flag to show if the user has been suspended on this system.'; +$string['privacy:metadata:user_preference:core_user_welcome'] = 'Timestamp logged for when the welcome message was shown to the user for the first time.'; $string['privacy:metadata:user_preferences'] = 'Preferences associated with the given user'; $string['privacy:metadata:user_preferences:name'] = 'Preference name'; $string['privacy:metadata:user_preferences:userid'] = 'The user ID'; diff --git a/lib/classes/user.php b/lib/classes/user.php index 774d5a807f0..c1e81c63198 100644 --- a/lib/classes/user.php +++ b/lib/classes/user.php @@ -1169,4 +1169,28 @@ class core_user { return false; } + + /** + * Get welcome message. + * + * @return lang_string welcome message + */ + public static function welcome_message(): ?lang_string { + global $USER; + + $isloggedinas = \core\session\manager::is_loggedinas(); + if (!isloggedin() || isguestuser() || $isloggedinas) { + return null; + } + if (empty($USER->core_welcome_message)) { + $USER->core_welcome_message = true; + $messagekey = 'welcomeback'; + if (empty(get_user_preferences('core_user_welcome', null))) { + $messagekey = 'welcometosite'; + set_user_preference('core_user_welcome', time()); + } + return new lang_string($messagekey, 'core', $USER->firstname); + }; + return null; + } } diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 8deb104e90c..39b3533350c 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -4326,7 +4326,14 @@ EOD; * @return string HTML to display the main header. */ public function full_header() { - + $pagetype = $this->page->pagetype; + $homepage = get_home_page(); + $homepagetype = null; + if ($homepage == HOMEPAGE_MY) { + $homepagetype = 'my-index'; + } else if ($homepage == HOMEPAGE_SITE) { + $homepagetype = 'site-index'; + } if ($this->page->include_region_main_settings_in_header_actions() && !$this->page->blocks->is_block_present('settings')) { // Only include the region main settings if the page has requested it and it doesn't already have @@ -4347,6 +4354,9 @@ EOD; $header->pageheadingbutton = $this->page_heading_button(); $header->courseheader = $this->course_header(); $header->headeractions = $this->page->get_header_actions(); + if (!empty($pagetype) && !empty($homepagetype) && $pagetype == $homepagetype) { + $header->welcomemessage = \core_user::welcome_message(); + } return $this->render_from_template('core/full_header', $header); } diff --git a/lib/templates/full_header.mustache b/lib/templates/full_header.mustache index f4fc8d6f8e8..37ff595f2dd 100644 --- a/lib/templates/full_header.mustache +++ b/lib/templates/full_header.mustache @@ -25,7 +25,8 @@ "settingsmenu": "settings_html", "hasnavbar": false, "navbar": "navbar_if_available", - "courseheader": "course_header_html" + "courseheader": "course_header_html", + "welcomemessage": "welcomemessage" } }} diff --git a/lib/templates/welcome.mustache b/lib/templates/welcome.mustache new file mode 100644 index 00000000000..60b02db4786 --- /dev/null +++ b/lib/templates/welcome.mustache @@ -0,0 +1,31 @@ +{{! + 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 . +}} +{{! + @template core/welcome + + This template renders welcome message. + + Example context (json): + { + "welcomemessage": "welcomemessage" + } +}} +{{#welcomemessage}} +

+ {{.}} +

+{{/welcomemessage}} diff --git a/my/tests/behat/welcome.feature b/my/tests/behat/welcome.feature new file mode 100644 index 00000000000..318322b4171 --- /dev/null +++ b/my/tests/behat/welcome.feature @@ -0,0 +1,32 @@ +@core @core_my +Feature: Welcome message + In order to welcome new or existing user + As a user + I will see welcome message when I log into moodle + + Scenario: Log in and being redirected to course page + Given the following "users" exist: + | username | password | firstname | lastname | email | + | wf | test | Fei | Wang | wf@z.com | + And the following "courses" exist: + | fullname | shortname | + | Math 101 | M1O1 | + When I am on "Math 101" course homepage + And I should see "You are not logged in" in the "page-footer" "region" + And I set the field "Username" to "wf" + And I set the field "Password" to "test" + And I press "Log in" + And I should see "Math 101" in the "page-header" "region" + And I should not see "Welcome, Fei!" in the "page-header" "region" + And I follow "Dashboard" in the user menu + Then I should see "Welcome, Fei!" in the "page-header" "region" + + @javascript + Scenario: Log in and being redirected to default home page + When I log in as "admin" + And I should see "You are logged in as Admin User" in the "page-footer" "region" + And I should see "Welcome, Admin!" in the "page-header" "region" + And I log out + And I should see "You are not logged in" in the "page-footer" "region" + And I log in as "admin" + Then I should see "Welcome back, Admin!" in the "page-header" "region" diff --git a/theme/classic/templates/core/full_header.mustache b/theme/classic/templates/core/full_header.mustache index 7b61cb0b778..3928046ef52 100644 --- a/theme/classic/templates/core/full_header.mustache +++ b/theme/classic/templates/core/full_header.mustache @@ -24,7 +24,8 @@ "contextheader": "context_header_html", "hasnavbar": false, "navbar": "navbar_if_available", - "courseheader": "course_header_html" + "courseheader": "course_header_html", + "welcomemessage": "welcomemessage" } }} \ No newline at end of file + diff --git a/user/classes/privacy/provider.php b/user/classes/privacy/provider.php index 87eb4869eb1..ca36898fa33 100644 --- a/user/classes/privacy/provider.php +++ b/user/classes/privacy/provider.php @@ -44,7 +44,8 @@ use \core_privacy\local\request\approved_userlist; class provider implements \core_privacy\local\metadata\provider, \core_privacy\local\request\core_userlist_provider, - \core_privacy\local\request\subsystem\provider { + \core_privacy\local\request\subsystem\provider, + \core_privacy\local\request\user_preference_provider { /** * Returns information about the user data stored in this component. @@ -177,6 +178,11 @@ class provider implements $collection->add_database_table('user_preferences', $userpreferences, 'privacy:metadata:user_preferences'); $collection->add_subsystem_link('core_files', [], 'privacy:metadata:filelink'); + $collection->add_user_preference( + 'core_user_welcome', + 'privacy:metadata:core_user:preference:core_user_welcome' + ); + return $collection; } @@ -537,4 +543,23 @@ class provider implements writer::with_context($context)->export_data([get_string('privacy:sessionpath', 'user')], $sessiondata); } } + + /** + * Export all user preferences for the plugin. + * + * @param int $userid The userid of the user whose data is to be exported. + */ + public static function export_user_preferences(int $userid) { + $userwelcomepreference = get_user_preferences('core_user_welcome', null, $userid); + + if ($userwelcomepreference !== null) { + writer::export_user_preference( + 'core_user', + 'core_user_welcome', + $userwelcomepreference, + get_string('privacy:metadata:user_preference:core_user_welcome', 'core_user') + ); + } + } + }