Skip to content

Commit 8b3bac1

Browse files
committed
Display sender fullname in dialogue depending on loggedin user capability.
Replace core capability check with mod_dialogue 'open' capability in webservice call
1 parent c7f6768 commit 8b3bac1

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

classes/external/search_users.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ public static function execute(int $cmid, string $search, bool $searchanywhere,
100100
$exceptionparam->courseid = $params['courseid'];
101101
throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
102102
}
103-
course_require_view_participants($context);
103+
104+
require_capability('mod/dialogue:open', $context);
105+
104106
if (!has_capability('moodle/site:accessallgroups', $context) &&
105107
$DB->record_exists('dialogue', ['id' => $cm->instance, 'usecoursegroups' => 1])) {
106108

classes/message.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,12 @@ public function send() {
450450
$context = $this->dialogue->context;
451451
$userfrom = $DB->get_record('user', array('id' => $this->_authorid), '*', MUST_EXIST);
452452
$subject = format_string($this->conversation->subject, true, array('context' => $context));
453-
454453
$a = new \stdClass();
455-
$a->userfrom = fullname($userfrom);
454+
456455
$a->subject = $subject;
457456
$url = new \moodle_url('/mod/dialogue/view.php', array('id' => $cm->id));
458457
$a->url = $url->out(false);
459458

460-
$posthtml = get_string('messageapibasicmessage', 'dialogue', $a);
461-
$posttext = html_to_text($posthtml);
462-
$smallmessage = get_string('messageapismallmessage', 'dialogue', fullname($userfrom));
463-
464459
$contexturlparams = array('id' => $cm->id, 'conversationid' => $conversationid);
465460
$contexturl = new \moodle_url('/mod/dialogue/conversation.php', $contexturlparams);
466461
$contexturl->set_anchor('m' . $this->_messageid);
@@ -478,6 +473,14 @@ public function send() {
478473

479474
$userto = $DB->get_record('user', array('id' => $participant->id), '*', MUST_EXIST);
480475

476+
$a->userfrom = dialogue_add_user_fullname($userfrom, $userto, $cm);
477+
$a->course = $course->shortname;
478+
479+
$posthtml = get_string('messageapibasicmessage', 'dialogue', $a);
480+
$posttext = html_to_text($posthtml);
481+
$smallmessage = get_string('messageapismallmessage', 'dialogue',
482+
dialogue_add_user_fullname($userfrom, $userto, $cm));
483+
481484
$eventdata = new \core\message\message();
482485
$eventdata->courseid = $course->id;
483486
$eventdata->component = 'mod_dialogue';

formlib.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,14 @@ protected function definition() {
254254
'ajax' => 'mod_dialogue/form-user-selector',
255255
'multiple' => true,
256256
'cmid' => $cm->id,
257-
'valuehtmlcallback' => function($value) {
257+
'valuehtmlcallback' => function($value, $USER, $cm) {
258258
global $OUTPUT;
259-
260259
$userfieldsapi = \core_user\fields::for_name();
261260
$allusernames = $userfieldsapi->get_sql('', false, '', '', false)->selects;
262261
$fields = 'id, ' . $allusernames;
263262
$user = \core_user::get_user($value, $fields);
264263
$useroptiondata = [
265-
'fullname' => fullname($user),
264+
'fullname' => dialogue_add_user_fullname($user, $USER, $cm),
266265
];
267266
return $OUTPUT->render_from_template('mod_dialogue/form-user-selector-suggestion', $useroptiondata);
268267
}

lang/en/dialogue.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@
124124
$string['maxattachmentsize_help'] = 'This setting specifies the largest size of file that can be attached to a dialogue post.';
125125
$string['message'] = 'Message';
126126
$string['messageapibasicmessage'] = '
127-
<p>{$a->userfrom} posted a new message to a conversation you are participating
128-
in with subject: <i>{$a->subject}</i>
127+
<p>{$a->userfrom} posted a new message to a conversation you are participating in course: <i>{$a->course}</i>
128+
with subject: <i>{$a->subject}</i>
129129
<br/><br/><a href="{$a->url}">View in Moodle</a></p>';
130130
$string['messageapismallmessage'] = '{$a} posted a new message to a conversation you are participating in';
131131
$string['messageprovider:post'] = 'Dialogue notifications';

locallib.php

+17
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,20 @@ function dialogue_contains_draft_files($draftid) {
487487
return (count($draftfiles) > 1) ? true : false;
488488
}
489489

490+
/**
491+
* Get the name for a user - hiding their full name if the required capability
492+
* is missing.
493+
*
494+
* @param stdClass $userviewed the user whose details are being viewed
495+
* @param stdClass $userviewedby the user who is viewing these details
496+
* @param stdClass $cm the course module object
497+
*
498+
* @return string fullname
499+
*/
500+
function dialogue_add_user_fullname(stdClass $userviewed, stdClass $userviewedby, stdClass $cm) {
501+
$capability = 'moodle/site:viewfullnames';
502+
$hasviewfullnames = false;
503+
$context = context_module::instance($cm->id);
504+
$hasviewfullnames = has_capability($capability, $context, $userviewedby);
505+
return fullname($userviewed, $hasviewfullnames);
506+
}

renderer.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function render_conversation(mod_dialogue\conversation $conversation) {
7171
$html .= html_writer::start_div('conversation-body');
7272

7373
$datestrings = (object) dialogue_get_humanfriendly_dates($conversation->timemodified);
74-
$datestrings->fullname = fullname($conversation->author);
74+
$datestrings->fullname = dialogue_add_user_fullname($conversation->author, $USER, $cm);
7575
if ($conversation->timemodified >= $today) {
7676
$openedbyheader = get_string('openedbytoday', 'dialogue', $datestrings);
7777
} else if ($conversation->timemodified >= $yearago) {
@@ -145,7 +145,7 @@ public function render_conversation(mod_dialogue\conversation $conversation) {
145145
$html .= html_writer::start_tag('tr');
146146
$picture = $this->output->user_picture($person, array('class' => 'userpicture img-rounded', 'size' => 20));
147147
$html .= html_writer::tag('td', $picture);
148-
$html .= html_writer::tag('td', fullname($person));
148+
$html .= html_writer::tag('td', dialogue_add_user_fullname($person, $USER, $cm));
149149
$html .= html_writer::tag('td', $sentonstring . userdate($receivedby->timemodified));
150150
$html .= html_writer::end_tag('tr');
151151
}
@@ -162,7 +162,7 @@ public function render_conversation(mod_dialogue\conversation $conversation) {
162162
$html .= '&nbsp;' . get_string('participants', 'dialogue');
163163
foreach ($participants as $participant) {
164164
$picture = $this->output->user_picture($participant, array('class' => 'userpicture img-rounded', 'size' => 20));
165-
$html .= html_writer::tag('span', $picture . '&nbsp;' . fullname($participant), array('class' => 'participant'));
165+
$html .= html_writer::tag('span', $picture . '&nbsp;' . dialogue_add_user_fullname($participant, $USER, $cm), array('class' => 'participant'));
166166
}
167167
$html .= html_writer::end_div();
168168
}
@@ -179,6 +179,7 @@ public function render_conversation(mod_dialogue\conversation $conversation) {
179179
* @return string
180180
*/
181181
public function conversation_listing(\mod_dialogue\conversations $conversations) {
182+
global $USER;
182183
$dialogue = $conversations->dialogue;
183184
$cm = $conversations->dialogue->cm;
184185

@@ -241,7 +242,7 @@ public function conversation_listing(\mod_dialogue\conversations $conversations)
241242
$displayuser = dialogue_get_user_details($dialogue, $record->userid);
242243
$avatar = $this->output->user_picture($displayuser, array('class' => 'userpicture img-rounded', 'size' => 48));
243244
$html .= html_writer::tag('td', $avatar);
244-
$html .= html_writer::tag('td', fullname($displayuser));
245+
$html .= html_writer::tag('td', dialogue_add_user_fullname($displayuser, $USER, $cm));
245246
}
246247

247248
if (isset($record->subject) && isset($record->body)) {
@@ -257,7 +258,7 @@ public function conversation_listing(\mod_dialogue\conversations $conversations)
257258
$participant = dialogue_get_user_details($dialogue, $participantid);
258259
$picture = $this->output->user_picture($participant,
259260
array('class' => 'userpicture img-rounded', 'size' => 16));
260-
$html .= html_writer::tag('span', $picture.' '.fullname($participant),
261+
$html .= html_writer::tag('span', $picture.' '.dialogue_add_user_fullname($participant, $USER, $cm),
261262
array('class' => 'participant'));
262263

263264
}
@@ -306,7 +307,7 @@ public function conversation_listing(\mod_dialogue\conversations $conversations)
306307
* @return string
307308
*/
308309
public function render_reply(\mod_dialogue\reply $reply) {
309-
310+
global $USER;
310311
$context = $reply->dialogue->context; // Fetch context from parent dialogue.
311312
$cm = $reply->dialogue->cm; // Fetch course module from parent dialogue.
312313
$conversation = $reply->conversation; // Fetch parent conversation.
@@ -326,7 +327,7 @@ public function render_reply(\mod_dialogue\reply $reply) {
326327
$html .= html_writer::start_div('conversation-body');
327328

328329
$datestrings = (object) dialogue_get_humanfriendly_dates($reply->timemodified);
329-
$datestrings->fullname = fullname($reply->author);
330+
$datestrings->fullname = dialogue_add_user_fullname($reply->author, $USER, $cm);
330331
if ($reply->timemodified >= $today) {
331332
$repliedbyheader = get_string('repliedbytoday', 'dialogue', $datestrings);
332333
} else if ($reply->timemodified >= $yearago) {

0 commit comments

Comments
 (0)