From 268167903bab50e94983c56351480d7d72c44a60 Mon Sep 17 00:00:00 2001 From: Sumaiya Javed Date: Thu, 19 Sep 2024 07:46:46 +1200 Subject: [PATCH] Display sender fullname in dialogue depending on loggedin user capability. Replace core capability check with mod_dialogue 'open' capability in webservice call --- classes/external/search_users.php | 4 +++- classes/message.php | 14 ++++++++------ formlib.php | 5 ++--- lang/en/dialogue.php | 4 ++-- locallib.php | 16 ++++++++++++++++ renderer.php | 16 +++++++++------- 6 files changed, 40 insertions(+), 19 deletions(-) diff --git a/classes/external/search_users.php b/classes/external/search_users.php index f5922bb..35d619f 100644 --- a/classes/external/search_users.php +++ b/classes/external/search_users.php @@ -100,7 +100,9 @@ public static function execute(int $cmid, string $search, bool $searchanywhere, $exceptionparam->courseid = $params['courseid']; throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); } - course_require_view_participants($context); + + require_capability('mod/dialogue:open', $context); + if (!has_capability('moodle/site:accessallgroups', $context) && $DB->record_exists('dialogue', ['id' => $cm->instance, 'usecoursegroups' => 1])) { diff --git a/classes/message.php b/classes/message.php index 7654ac8..52ec222 100644 --- a/classes/message.php +++ b/classes/message.php @@ -450,17 +450,11 @@ public function send() { $context = $this->dialogue->context; $userfrom = $DB->get_record('user', array('id' => $this->_authorid), '*', MUST_EXIST); $subject = format_string($this->conversation->subject, true, array('context' => $context)); - $a = new \stdClass(); - $a->userfrom = fullname($userfrom); $a->subject = $subject; $url = new \moodle_url('/mod/dialogue/view.php', array('id' => $cm->id)); $a->url = $url->out(false); - $posthtml = get_string('messageapibasicmessage', 'dialogue', $a); - $posttext = html_to_text($posthtml); - $smallmessage = get_string('messageapismallmessage', 'dialogue', fullname($userfrom)); - $contexturlparams = array('id' => $cm->id, 'conversationid' => $conversationid); $contexturl = new \moodle_url('/mod/dialogue/conversation.php', $contexturlparams); $contexturl->set_anchor('m' . $this->_messageid); @@ -478,6 +472,14 @@ public function send() { $userto = $DB->get_record('user', array('id' => $participant->id), '*', MUST_EXIST); + $a->userfrom = dialogue_add_user_fullname($userfrom, $userto, $cm); + $a->course = $course->shortname; + + $posthtml = get_string('messageapibasicmessage', 'dialogue', $a); + $posttext = html_to_text($posthtml); + $smallmessage = get_string('messageapismallmessage', 'dialogue', + dialogue_add_user_fullname($userfrom, $userto, $cm)); + $eventdata = new \core\message\message(); $eventdata->courseid = $course->id; $eventdata->component = 'mod_dialogue'; diff --git a/formlib.php b/formlib.php index 8f56cd7..7a0b257 100644 --- a/formlib.php +++ b/formlib.php @@ -254,15 +254,14 @@ protected function definition() { 'ajax' => 'mod_dialogue/form-user-selector', 'multiple' => true, 'cmid' => $cm->id, - 'valuehtmlcallback' => function($value) { + 'valuehtmlcallback' => function($value, $USER, $cm) { global $OUTPUT; - $userfieldsapi = \core_user\fields::for_name(); $allusernames = $userfieldsapi->get_sql('', false, '', '', false)->selects; $fields = 'id, ' . $allusernames; $user = \core_user::get_user($value, $fields); $useroptiondata = [ - 'fullname' => fullname($user), + 'fullname' => dialogue_add_user_fullname($user, $USER, $cm), ]; return $OUTPUT->render_from_template('mod_dialogue/form-user-selector-suggestion', $useroptiondata); } diff --git a/lang/en/dialogue.php b/lang/en/dialogue.php index d63119d..abb155d 100644 --- a/lang/en/dialogue.php +++ b/lang/en/dialogue.php @@ -124,8 +124,8 @@ $string['maxattachmentsize_help'] = 'This setting specifies the largest size of file that can be attached to a dialogue post.'; $string['message'] = 'Message'; $string['messageapibasicmessage'] = ' -

{$a->userfrom} posted a new message to a conversation you are participating -in with subject: {$a->subject} +

{$a->userfrom} posted a new message to a conversation you are participating in course: {$a->course} +with subject: {$a->subject}

View in Moodle

'; $string['messageapismallmessage'] = '{$a} posted a new message to a conversation you are participating in'; $string['messageprovider:post'] = 'Dialogue notifications'; diff --git a/locallib.php b/locallib.php index d4e5579..5cfc1ee 100644 --- a/locallib.php +++ b/locallib.php @@ -487,3 +487,19 @@ function dialogue_contains_draft_files($draftid) { return (count($draftfiles) > 1) ? true : false; } +/** + * Get the name for a user - hiding their full name if the required capability + * is missing. + * + * @param stdClass $userviewed the user whose details are being viewed + * @param stdClass $userviewedby the user who is viewing these details + * @param stdClass $cm the course module object + * + * @return string fullname + */ +function dialogue_add_user_fullname(stdClass $userviewed, stdClass $userviewedby, stdClass $cm) { + $capability = 'moodle/site:viewfullnames'; + $context = context_module::instance($cm->id); + $hasviewfullnames = has_capability($capability, $context, $userviewedby); + return fullname($userviewed, $hasviewfullnames); +} diff --git a/renderer.php b/renderer.php index 766eaa4..ee418cd 100644 --- a/renderer.php +++ b/renderer.php @@ -71,7 +71,7 @@ public function render_conversation(mod_dialogue\conversation $conversation) { $html .= html_writer::start_div('conversation-body'); $datestrings = (object) dialogue_get_humanfriendly_dates($conversation->timemodified); - $datestrings->fullname = fullname($conversation->author); + $datestrings->fullname = dialogue_add_user_fullname($conversation->author, $USER, $cm); if ($conversation->timemodified >= $today) { $openedbyheader = get_string('openedbytoday', 'dialogue', $datestrings); } else if ($conversation->timemodified >= $yearago) { @@ -145,7 +145,7 @@ public function render_conversation(mod_dialogue\conversation $conversation) { $html .= html_writer::start_tag('tr'); $picture = $this->output->user_picture($person, array('class' => 'userpicture img-rounded', 'size' => 20)); $html .= html_writer::tag('td', $picture); - $html .= html_writer::tag('td', fullname($person)); + $html .= html_writer::tag('td', dialogue_add_user_fullname($person, $USER, $cm)); $html .= html_writer::tag('td', $sentonstring . userdate($receivedby->timemodified)); $html .= html_writer::end_tag('tr'); } @@ -162,7 +162,8 @@ public function render_conversation(mod_dialogue\conversation $conversation) { $html .= ' ' . get_string('participants', 'dialogue'); foreach ($participants as $participant) { $picture = $this->output->user_picture($participant, array('class' => 'userpicture img-rounded', 'size' => 20)); - $html .= html_writer::tag('span', $picture . ' ' . fullname($participant), array('class' => 'participant')); + $html .= html_writer::tag('span', $picture . ' ' + . dialogue_add_user_fullname($participant, $USER, $cm), array('class' => 'participant')); } $html .= html_writer::end_div(); } @@ -179,6 +180,7 @@ public function render_conversation(mod_dialogue\conversation $conversation) { * @return string */ public function conversation_listing(\mod_dialogue\conversations $conversations) { + global $USER; $dialogue = $conversations->dialogue; $cm = $conversations->dialogue->cm; @@ -241,7 +243,7 @@ public function conversation_listing(\mod_dialogue\conversations $conversations) $displayuser = dialogue_get_user_details($dialogue, $record->userid); $avatar = $this->output->user_picture($displayuser, array('class' => 'userpicture img-rounded', 'size' => 48)); $html .= html_writer::tag('td', $avatar); - $html .= html_writer::tag('td', fullname($displayuser)); + $html .= html_writer::tag('td', dialogue_add_user_fullname($displayuser, $USER, $cm)); } if (isset($record->subject) && isset($record->body)) { @@ -257,7 +259,7 @@ public function conversation_listing(\mod_dialogue\conversations $conversations) $participant = dialogue_get_user_details($dialogue, $participantid); $picture = $this->output->user_picture($participant, array('class' => 'userpicture img-rounded', 'size' => 16)); - $html .= html_writer::tag('span', $picture.' '.fullname($participant), + $html .= html_writer::tag('span', $picture.' '.dialogue_add_user_fullname($participant, $USER, $cm), array('class' => 'participant')); } @@ -306,7 +308,7 @@ public function conversation_listing(\mod_dialogue\conversations $conversations) * @return string */ public function render_reply(\mod_dialogue\reply $reply) { - + global $USER; $context = $reply->dialogue->context; // Fetch context from parent dialogue. $cm = $reply->dialogue->cm; // Fetch course module from parent dialogue. $conversation = $reply->conversation; // Fetch parent conversation. @@ -326,7 +328,7 @@ public function render_reply(\mod_dialogue\reply $reply) { $html .= html_writer::start_div('conversation-body'); $datestrings = (object) dialogue_get_humanfriendly_dates($reply->timemodified); - $datestrings->fullname = fullname($reply->author); + $datestrings->fullname = dialogue_add_user_fullname($reply->author, $USER, $cm); if ($reply->timemodified >= $today) { $repliedbyheader = get_string('repliedbytoday', 'dialogue', $datestrings); } else if ($reply->timemodified >= $yearago) {