Skip to content

Commit a07054a

Browse files
committed
MDL-81428 core: Display the correct status for the contact request
1 parent d3ae139 commit a07054a

11 files changed

+168
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
issueNumber: MDL-81428
2+
notes:
3+
core_message:
4+
- message: >
5+
The `\core_message\helper::togglecontact_link_params` now accepts a new
6+
optional param called `isrequested` to indicate the status of the
7+
contact request
8+
type: changed
9+
- message: >
10+
The `core_message/remove_contact_button` template is deprecated and will
11+
be removed in the future version
12+
type: deprecated

lang/en/message.php

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@
269269
$string['viewmessageswith'] = 'View messages with {$a}';
270270
$string['viewnotificationresource'] = 'Go to: {$a}';
271271
$string['viewunreadmessageswith'] = 'View unread messages with {$a}';
272+
$string['waitingforcontactaccept'] = 'Waiting for your approval';
272273
$string['writeamessage'] = 'Write a message...';
273274
$string['wouldliketocontactyou'] = 'Would like to contact you';
274275
$string['you'] = 'You:';

lib/outputrenderers.php

+37-4
Original file line numberDiff line numberDiff line change
@@ -4566,9 +4566,42 @@ public function context_header($headerinfo = null, $headinglevel = 1) {
45664566

45674567
if ($USER->id != $user->id) {
45684568
$iscontact = \core_message\api::is_contact($USER->id, $user->id);
4569-
$contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
4570-
$contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
4571-
$contactimage = $iscontact ? 'removecontact' : 'addcontact';
4569+
$isrequested = \core_message\api::get_contact_requests_between_users($USER->id, $user->id);
4570+
$contacturlaction = '';
4571+
$linkattributes = \core_message\helper::togglecontact_link_params(
4572+
$user,
4573+
$iscontact,
4574+
true,
4575+
!empty($isrequested),
4576+
);
4577+
// If the user is not a contact.
4578+
if (!$iscontact) {
4579+
if ($isrequested) {
4580+
// We just need the first request.
4581+
$requests = array_shift($isrequested);
4582+
if ($requests->userid == $USER->id) {
4583+
// If the user has requested to be a contact.
4584+
$contacttitle = 'contactrequestsent';
4585+
} else {
4586+
// If the user has been requested to be a contact.
4587+
$contacttitle = 'waitingforcontactaccept';
4588+
}
4589+
$linkattributes = array_merge($linkattributes, [
4590+
'class' => 'disabled',
4591+
'tabindex' => '-1',
4592+
]);
4593+
} else {
4594+
// If the user is not a contact and has not requested to be a contact.
4595+
$contacttitle = 'addtoyourcontacts';
4596+
$contacturlaction = 'addcontact';
4597+
}
4598+
$contactimage = 'addcontact';
4599+
} else {
4600+
// If the user is a contact.
4601+
$contacttitle = 'removefromyourcontacts';
4602+
$contacturlaction = 'removecontact';
4603+
$contactimage = 'removecontact';
4604+
}
45724605
$userbuttons['togglecontact'] = array(
45734606
'buttontype' => 'togglecontact',
45744607
'title' => get_string($contacttitle, 'message'),
@@ -4579,7 +4612,7 @@ public function context_header($headerinfo = null, $headinglevel = 1) {
45794612
'sesskey' => sesskey())
45804613
),
45814614
'image' => $contactimage,
4582-
'linkattributes' => \core_message\helper::togglecontact_link_params($user, $iscontact),
4615+
'linkattributes' => $linkattributes,
45834616
'page' => $this->page
45844617
);
45854618
}

message/amd/build/toggle_contact_button.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

message/amd/build/toggle_contact_button.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

message/amd/src/toggle_contact_button.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,22 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
3535
};
3636

3737
/**
38-
* Record that the user is a contact.
38+
* Check the state of the element, if the current user has sent a contact request or not.
3939
*
40-
* @method setContact
40+
* @method isRequested
41+
* @param {object} element jQuery object for the button
42+
* @return {bool}
43+
*/
44+
let isRequested = (element) => element.attr('data-is-requested') == '1';
45+
46+
/**
47+
* Record that the user has sent a contact request.
48+
*
49+
* @method setContactRequested
4150
* @param {object} element jQuery object for the button
4251
*/
43-
var setContact = function(element) {
44-
element.attr('data-is-contact', '1');
52+
var setContactRequested = function(element) {
53+
element.attr('data-is-requested', '1');
4554
};
4655

4756
/**
@@ -144,11 +153,12 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
144153
}
145154
};
146155
sendRequest(element, request).done(function() {
147-
setContact(element);
156+
setContactRequested(element);
157+
element.addClass('disabled');
148158
const templateContext = {
149159
'displaytextlabel': displayTextLabel(element)
150160
};
151-
Templates.render('message/remove_contact_button', templateContext).done(function(html, js) {
161+
Templates.render('message/contact_request_sent', templateContext).done(function(html, js) {
152162
Templates.replaceNodeContents(element, html, js);
153163
});
154164
});
@@ -196,7 +206,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
196206
var enhance = function(element) {
197207
element = $(element);
198208

199-
if (!element.children('.loading-icon').length) {
209+
if (!element.children('.loading-icon').length && !isRequested(element)) {
200210
// Add the loading gif if it isn't already there.
201211
Templates.render('core/loading', {}).done(function(html, js) {
202212
element.append(html, js);
@@ -208,7 +218,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
208218
element.on(CustomEvents.events.activate, function(e, data) {
209219
if (isContact(element)) {
210220
removeContact(element);
211-
} else {
221+
} else if (!isRequested(element)) {
212222
addContact(element);
213223
}
214224
e.preventDefault();

message/classes/helper.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,21 @@ public static function togglecontact_requirejs() {
309309
* @param object $user User object.
310310
* @param bool $iscontact
311311
* @param bool $displaytextlabel Instructs whether to display a text label.
312+
* @param bool $isrequested Whether the contact request is sent or not.
312313
* @return array
313314
*/
314-
public static function togglecontact_link_params($user, $iscontact = false, bool $displaytextlabel = true) {
315+
public static function togglecontact_link_params(
316+
$user,
317+
$iscontact = false,
318+
bool $displaytextlabel = true,
319+
bool $isrequested = false,
320+
) {
315321
global $USER;
316322
$params = array(
317323
'data-currentuserid' => $USER->id,
318324
'data-userid' => $user->id,
319325
'data-is-contact' => $iscontact,
326+
'data-is-requested' => $isrequested,
320327
'data-display-text-label' => $displaytextlabel,
321328
'id' => 'toggle-contact-button',
322329
'role' => 'button',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{{!
2+
This file is part of Moodle - http://moodle.org/
3+
Moodle is free software: you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation, either version 3 of the License, or
6+
(at your option) any later version.
7+
Moodle is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
You should have received a copy of the GNU General Public License
12+
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
13+
}}
14+
{{!
15+
@template core_message/contact_request_sent
16+
Template for the contents of the add contact button on the user's profile page.
17+
Context variables required for this template:
18+
* displaytextlabel - Whether to display text next to the action icon.
19+
Example context (json):
20+
{
21+
"displaytextlabel": true
22+
}
23+
}}
24+
<span>
25+
{{^displaytextlabel}}
26+
{{#pix}} t/addcontact, core, {{#str}} contactrequestsent, message {{/str}} {{/pix}}
27+
{{/displaytextlabel}}
28+
{{#displaytextlabel}}
29+
{{#pix}} t/addcontact, core {{/pix}}
30+
<span class="header-button-title">{{#str}} contactrequestsent, message {{/str}}</span>
31+
{{/displaytextlabel}}
32+
</span>
33+
{{> core/loading }}

message/templates/remove_contact_button.mustache

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616
}}
1717
{{!
18+
@deprecated since Moodle 4.5
1819
@template core_message/remove_contact_button
1920
2021
Template for the contents of the add contact button on the user's profile page.

message/tests/behat/message_drawer_manage_contacts.feature

+19
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ Feature: Manage contacts
5959
And I click on "Contacts" "link"
6060
And I should see "Student 4" in the "//*[@data-section='contacts']" "xpath_element"
6161

62+
Scenario: Send a 'contact request' to someone to add a contact in the profile page
63+
Given I am on the "student4" "user > profile" page logged in as student3
64+
And I should see "Add to contacts"
65+
When I click on "Add to contacts" "link"
66+
Then I should see "Contact request sent"
67+
And I log out
68+
And I am on the "student3" "user > profile" page logged in as student4
69+
And I should see "Waiting for your approval"
70+
And I open messaging
71+
And I click on "Contacts" "link"
72+
And I click on "Requests" "link_or_button"
73+
And I click on "Student 3 Would like to contact you" "link"
74+
And I should see "Accept and add to contacts"
75+
And I click on "Accept and add to contacts" "link_or_button"
76+
And I should not see "Accept and add to contacts"
77+
And I log out
78+
And I am on the "student4" "user > profile" page logged in as student3
79+
And I should see "Remove from contacts"
80+
6281
Scenario: Decline a 'contact request' from someone
6382
Given I log in as "student1"
6483
Then I open messaging

theme/boost/classes/output/core_renderer.php

+37-4
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,42 @@ public function context_header($headerinfo = null, $headinglevel = 1): string {
129129

130130
if ($USER->id != $user->id) {
131131
$iscontact = \core_message\api::is_contact($USER->id, $user->id);
132-
$contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
133-
$contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
134-
$contactimage = $iscontact ? 'removecontact' : 'addcontact';
132+
$isrequested = \core_message\api::get_contact_requests_between_users($USER->id, $user->id);
133+
$contacturlaction = '';
134+
$linkattributes = \core_message\helper::togglecontact_link_params(
135+
$user,
136+
$iscontact,
137+
true,
138+
!empty($isrequested),
139+
);
140+
// If the user is not a contact.
141+
if (!$iscontact) {
142+
if ($isrequested) {
143+
// We just need the first request.
144+
$requests = array_shift($isrequested);
145+
if ($requests->userid == $USER->id) {
146+
// If the user has requested to be a contact.
147+
$contacttitle = 'contactrequestsent';
148+
} else {
149+
// If the user has been requested to be a contact.
150+
$contacttitle = 'waitingforcontactaccept';
151+
}
152+
$linkattributes = array_merge($linkattributes, [
153+
'class' => 'disabled',
154+
'tabindex' => '-1',
155+
]);
156+
} else {
157+
// If the user is not a contact and has not requested to be a contact.
158+
$contacttitle = 'addtoyourcontacts';
159+
$contacturlaction = 'addcontact';
160+
}
161+
$contactimage = 'addcontact';
162+
} else {
163+
// If the user is a contact.
164+
$contacttitle = 'removefromyourcontacts';
165+
$contacturlaction = 'removecontact';
166+
$contactimage = 'removecontact';
167+
}
135168
$userbuttons['togglecontact'] = array(
136169
'buttontype' => 'togglecontact',
137170
'title' => get_string($contacttitle, 'message'),
@@ -142,7 +175,7 @@ public function context_header($headerinfo = null, $headinglevel = 1): string {
142175
'sesskey' => sesskey())
143176
),
144177
'image' => $contactimage,
145-
'linkattributes' => \core_message\helper::togglecontact_link_params($user, $iscontact),
178+
'linkattributes' => $linkattributes,
146179
'page' => $this->page
147180
);
148181
}

0 commit comments

Comments
 (0)