Skip to content

Commit a8acfc4

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 a8acfc4

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,11 @@ 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);
456454
$a->subject = $subject;
457455
$url = new \moodle_url('/mod/dialogue/view.php', array('id' => $cm->id));
458456
$a->url = $url->out(false);
459457

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

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

475+
$a->userfrom = dialogue_add_user_fullname($userfrom, $userto, $cm);
476+
$a->course = $course->shortname;
477+
478+
$posthtml = get_string('messageapibasicmessage', 'dialogue', $a);
479+
$posttext = html_to_text($posthtml);
480+
$smallmessage = get_string('messageapismallmessage', 'dialogue',
481+
dialogue_add_user_fullname($userfrom, $userto, $cm));
482+
481483
$eventdata = new \core\message\message();
482484
$eventdata->courseid = $course->id;
483485
$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

+9-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,8 @@ 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;'
166+
. dialogue_add_user_fullname($participant, $USER, $cm), array('class' => 'participant'));
166167
}
167168
$html .= html_writer::end_div();
168169
}
@@ -179,6 +180,7 @@ public function render_conversation(mod_dialogue\conversation $conversation) {
179180
* @return string
180181
*/
181182
public function conversation_listing(\mod_dialogue\conversations $conversations) {
183+
global $USER;
182184
$dialogue = $conversations->dialogue;
183185
$cm = $conversations->dialogue->cm;
184186

@@ -241,7 +243,7 @@ public function conversation_listing(\mod_dialogue\conversations $conversations)
241243
$displayuser = dialogue_get_user_details($dialogue, $record->userid);
242244
$avatar = $this->output->user_picture($displayuser, array('class' => 'userpicture img-rounded', 'size' => 48));
243245
$html .= html_writer::tag('td', $avatar);
244-
$html .= html_writer::tag('td', fullname($displayuser));
246+
$html .= html_writer::tag('td', dialogue_add_user_fullname($displayuser, $USER, $cm));
245247
}
246248

247249
if (isset($record->subject) && isset($record->body)) {
@@ -257,7 +259,7 @@ public function conversation_listing(\mod_dialogue\conversations $conversations)
257259
$participant = dialogue_get_user_details($dialogue, $participantid);
258260
$picture = $this->output->user_picture($participant,
259261
array('class' => 'userpicture img-rounded', 'size' => 16));
260-
$html .= html_writer::tag('span', $picture.' '.fullname($participant),
262+
$html .= html_writer::tag('span', $picture.' '.dialogue_add_user_fullname($participant, $USER, $cm),
261263
array('class' => 'participant'));
262264

263265
}
@@ -306,7 +308,7 @@ public function conversation_listing(\mod_dialogue\conversations $conversations)
306308
* @return string
307309
*/
308310
public function render_reply(\mod_dialogue\reply $reply) {
309-
311+
global $USER;
310312
$context = $reply->dialogue->context; // Fetch context from parent dialogue.
311313
$cm = $reply->dialogue->cm; // Fetch course module from parent dialogue.
312314
$conversation = $reply->conversation; // Fetch parent conversation.
@@ -326,7 +328,7 @@ public function render_reply(\mod_dialogue\reply $reply) {
326328
$html .= html_writer::start_div('conversation-body');
327329

328330
$datestrings = (object) dialogue_get_humanfriendly_dates($reply->timemodified);
329-
$datestrings->fullname = fullname($reply->author);
331+
$datestrings->fullname = dialogue_add_user_fullname($reply->author, $USER, $cm);
330332
if ($reply->timemodified >= $today) {
331333
$repliedbyheader = get_string('repliedbytoday', 'dialogue', $datestrings);
332334
} else if ($reply->timemodified >= $yearago) {

0 commit comments

Comments
 (0)