Skip to content

Commit

Permalink
MDL-81428 core: Display the correct status for the contact request
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Jun 10, 2024
1 parent d3ae139 commit a07054a
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 19 deletions.
12 changes: 12 additions & 0 deletions .upgradenotes/MDL-81428-2024061009440935.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
issueNumber: MDL-81428
notes:
core_message:
- message: >
The `\core_message\helper::togglecontact_link_params` now accepts a new
optional param called `isrequested` to indicate the status of the
contact request
type: changed
- message: >
The `core_message/remove_contact_button` template is deprecated and will
be removed in the future version
type: deprecated
1 change: 1 addition & 0 deletions lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
$string['viewmessageswith'] = 'View messages with {$a}';
$string['viewnotificationresource'] = 'Go to: {$a}';
$string['viewunreadmessageswith'] = 'View unread messages with {$a}';
$string['waitingforcontactaccept'] = 'Waiting for your approval';
$string['writeamessage'] = 'Write a message...';
$string['wouldliketocontactyou'] = 'Would like to contact you';
$string['you'] = 'You:';
Expand Down
41 changes: 37 additions & 4 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4566,9 +4566,42 @@ public function context_header($headerinfo = null, $headinglevel = 1) {

if ($USER->id != $user->id) {
$iscontact = \core_message\api::is_contact($USER->id, $user->id);
$contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
$contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
$contactimage = $iscontact ? 'removecontact' : 'addcontact';
$isrequested = \core_message\api::get_contact_requests_between_users($USER->id, $user->id);
$contacturlaction = '';
$linkattributes = \core_message\helper::togglecontact_link_params(
$user,
$iscontact,
true,
!empty($isrequested),
);
// If the user is not a contact.
if (!$iscontact) {
if ($isrequested) {
// We just need the first request.
$requests = array_shift($isrequested);
if ($requests->userid == $USER->id) {
// If the user has requested to be a contact.
$contacttitle = 'contactrequestsent';
} else {
// If the user has been requested to be a contact.
$contacttitle = 'waitingforcontactaccept';
}
$linkattributes = array_merge($linkattributes, [
'class' => 'disabled',
'tabindex' => '-1',
]);
} else {
// If the user is not a contact and has not requested to be a contact.
$contacttitle = 'addtoyourcontacts';
$contacturlaction = 'addcontact';
}
$contactimage = 'addcontact';
} else {
// If the user is a contact.
$contacttitle = 'removefromyourcontacts';
$contacturlaction = 'removecontact';
$contactimage = 'removecontact';
}
$userbuttons['togglecontact'] = array(
'buttontype' => 'togglecontact',
'title' => get_string($contacttitle, 'message'),
Expand All @@ -4579,7 +4612,7 @@ public function context_header($headerinfo = null, $headinglevel = 1) {
'sesskey' => sesskey())
),
'image' => $contactimage,
'linkattributes' => \core_message\helper::togglecontact_link_params($user, $iscontact),
'linkattributes' => $linkattributes,
'page' => $this->page
);
}
Expand Down
2 changes: 1 addition & 1 deletion message/amd/build/toggle_contact_button.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion message/amd/build/toggle_contact_button.min.js.map

Large diffs are not rendered by default.

26 changes: 18 additions & 8 deletions message/amd/src/toggle_contact_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
};

/**
* Record that the user is a contact.
* Check the state of the element, if the current user has sent a contact request or not.
*
* @method setContact
* @method isRequested
* @param {object} element jQuery object for the button
* @return {bool}
*/
let isRequested = (element) => element.attr('data-is-requested') == '1';

/**
* Record that the user has sent a contact request.
*
* @method setContactRequested
* @param {object} element jQuery object for the button
*/
var setContact = function(element) {
element.attr('data-is-contact', '1');
var setContactRequested = function(element) {
element.attr('data-is-requested', '1');
};

/**
Expand Down Expand Up @@ -144,11 +153,12 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
}
};
sendRequest(element, request).done(function() {
setContact(element);
setContactRequested(element);
element.addClass('disabled');
const templateContext = {
'displaytextlabel': displayTextLabel(element)
};
Templates.render('message/remove_contact_button', templateContext).done(function(html, js) {
Templates.render('message/contact_request_sent', templateContext).done(function(html, js) {
Templates.replaceNodeContents(element, html, js);
});
});
Expand Down Expand Up @@ -196,7 +206,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
var enhance = function(element) {
element = $(element);

if (!element.children('.loading-icon').length) {
if (!element.children('.loading-icon').length && !isRequested(element)) {
// Add the loading gif if it isn't already there.
Templates.render('core/loading', {}).done(function(html, js) {
element.append(html, js);
Expand All @@ -208,7 +218,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
element.on(CustomEvents.events.activate, function(e, data) {
if (isContact(element)) {
removeContact(element);
} else {
} else if (!isRequested(element)) {
addContact(element);
}
e.preventDefault();
Expand Down
9 changes: 8 additions & 1 deletion message/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,21 @@ public static function togglecontact_requirejs() {
* @param object $user User object.
* @param bool $iscontact
* @param bool $displaytextlabel Instructs whether to display a text label.
* @param bool $isrequested Whether the contact request is sent or not.
* @return array
*/
public static function togglecontact_link_params($user, $iscontact = false, bool $displaytextlabel = true) {
public static function togglecontact_link_params(
$user,
$iscontact = false,
bool $displaytextlabel = true,
bool $isrequested = false,
) {
global $USER;
$params = array(
'data-currentuserid' => $USER->id,
'data-userid' => $user->id,
'data-is-contact' => $iscontact,
'data-is-requested' => $isrequested,
'data-display-text-label' => $displaytextlabel,
'id' => 'toggle-contact-button',
'role' => 'button',
Expand Down
33 changes: 33 additions & 0 deletions message/templates/contact_request_sent.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{!
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_message/contact_request_sent
Template for the contents of the add contact button on the user's profile page.
Context variables required for this template:
* displaytextlabel - Whether to display text next to the action icon.
Example context (json):
{
"displaytextlabel": true
}
}}
<span>
{{^displaytextlabel}}
{{#pix}} t/addcontact, core, {{#str}} contactrequestsent, message {{/str}} {{/pix}}
{{/displaytextlabel}}
{{#displaytextlabel}}
{{#pix}} t/addcontact, core {{/pix}}
<span class="header-button-title">{{#str}} contactrequestsent, message {{/str}}</span>
{{/displaytextlabel}}
</span>
{{> core/loading }}
1 change: 1 addition & 0 deletions message/templates/remove_contact_button.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@deprecated since Moodle 4.5
@template core_message/remove_contact_button
Template for the contents of the add contact button on the user's profile page.
Expand Down
19 changes: 19 additions & 0 deletions message/tests/behat/message_drawer_manage_contacts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ Feature: Manage contacts
And I click on "Contacts" "link"
And I should see "Student 4" in the "//*[@data-section='contacts']" "xpath_element"

Scenario: Send a 'contact request' to someone to add a contact in the profile page
Given I am on the "student4" "user > profile" page logged in as student3
And I should see "Add to contacts"
When I click on "Add to contacts" "link"
Then I should see "Contact request sent"
And I log out
And I am on the "student3" "user > profile" page logged in as student4
And I should see "Waiting for your approval"
And I open messaging
And I click on "Contacts" "link"
And I click on "Requests" "link_or_button"
And I click on "Student 3 Would like to contact you" "link"
And I should see "Accept and add to contacts"
And I click on "Accept and add to contacts" "link_or_button"
And I should not see "Accept and add to contacts"
And I log out
And I am on the "student4" "user > profile" page logged in as student3
And I should see "Remove from contacts"

Scenario: Decline a 'contact request' from someone
Given I log in as "student1"
Then I open messaging
Expand Down
41 changes: 37 additions & 4 deletions theme/boost/classes/output/core_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,42 @@ public function context_header($headerinfo = null, $headinglevel = 1): string {

if ($USER->id != $user->id) {
$iscontact = \core_message\api::is_contact($USER->id, $user->id);
$contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
$contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
$contactimage = $iscontact ? 'removecontact' : 'addcontact';
$isrequested = \core_message\api::get_contact_requests_between_users($USER->id, $user->id);
$contacturlaction = '';
$linkattributes = \core_message\helper::togglecontact_link_params(
$user,
$iscontact,
true,
!empty($isrequested),
);
// If the user is not a contact.
if (!$iscontact) {
if ($isrequested) {
// We just need the first request.
$requests = array_shift($isrequested);
if ($requests->userid == $USER->id) {
// If the user has requested to be a contact.
$contacttitle = 'contactrequestsent';
} else {
// If the user has been requested to be a contact.
$contacttitle = 'waitingforcontactaccept';
}
$linkattributes = array_merge($linkattributes, [
'class' => 'disabled',
'tabindex' => '-1',
]);
} else {
// If the user is not a contact and has not requested to be a contact.
$contacttitle = 'addtoyourcontacts';
$contacturlaction = 'addcontact';
}
$contactimage = 'addcontact';
} else {
// If the user is a contact.
$contacttitle = 'removefromyourcontacts';
$contacturlaction = 'removecontact';
$contactimage = 'removecontact';
}
$userbuttons['togglecontact'] = array(
'buttontype' => 'togglecontact',
'title' => get_string($contacttitle, 'message'),
Expand All @@ -142,7 +175,7 @@ public function context_header($headerinfo = null, $headinglevel = 1): string {
'sesskey' => sesskey())
),
'image' => $contactimage,
'linkattributes' => \core_message\helper::togglecontact_link_params($user, $iscontact),
'linkattributes' => $linkattributes,
'page' => $this->page
);
}
Expand Down

0 comments on commit a07054a

Please sign in to comment.