MDL-71964 dashboard: add welcome message for all users

This commit is contained in:
Dongsheng Cai 2021-09-05 23:08:30 +10:00 committed by Jun Pataleta
parent 5ea3545115
commit a2d5d43223
9 changed files with 134 additions and 5 deletions

View File

@ -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}!

View File

@ -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';

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -25,7 +25,8 @@
"settingsmenu": "settings_html",
"hasnavbar": false,
"navbar": "navbar_if_available",
"courseheader": "course_header_html"
"courseheader": "course_header_html",
"welcomemessage": "welcomemessage"
}
}}
<header id="page-header" class="row">
@ -65,5 +66,6 @@
</div>
</div>
</div>
{{> core/welcome }}
</div>
</header>

View File

@ -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 <http://www.gnu.org/licenses/>.
}}
{{!
@template core/welcome
This template renders welcome message.
Example context (json):
{
"welcomemessage": "welcomemessage"
}
}}
{{#welcomemessage}}
<h2 class="mb-3 mt-3">
{{.}}
</h2>
{{/welcomemessage}}

View File

@ -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"

View File

@ -24,7 +24,8 @@
"contextheader": "context_header_html",
"hasnavbar": false,
"navbar": "navbar_if_available",
"courseheader": "course_header_html"
"courseheader": "course_header_html",
"welcomemessage": "welcomemessage"
}
}}
<header id="page-header" class="row">
@ -56,5 +57,6 @@
</div>
</div>
</div>
{{> core/welcome }}
</div>
</header>
</header>

View File

@ -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')
);
}
}
}