Skip to content

Commit

Permalink
Display sender fullname in dialogue depending on loggedin user capabi…
Browse files Browse the repository at this point in the history
…lity.

Replace core capability check with mod_dialogue 'open' capability in webservice call
  • Loading branch information
sumaiyamannan committed Sep 18, 2024
1 parent c7f6768 commit 44c4834
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 18 deletions.
4 changes: 3 additions & 1 deletion classes/external/search_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {

Expand Down
14 changes: 8 additions & 6 deletions classes/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,12 @@ 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);
Expand All @@ -478,6 +473,13 @@ public function send() {

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

$a->userfrom = dialogue_add_user_fullname($userfrom, $userto);
$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));

$eventdata = new \core\message\message();
$eventdata->courseid = $course->id;
$eventdata->component = 'mod_dialogue';
Expand Down
2 changes: 1 addition & 1 deletion formlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ protected function definition() {
$fields = 'id, ' . $allusernames;
$user = \core_user::get_user($value, $fields);
$useroptiondata = [
'fullname' => fullname($user),
'fullname' => dialogue_add_user_fullname($user),
];
return $OUTPUT->render_from_template('mod_dialogue/form-user-selector-suggestion', $useroptiondata);
}
Expand Down
4 changes: 2 additions & 2 deletions lang/en/dialogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '
<p>{$a->userfrom} posted a new message to a conversation you are participating
in with subject: <i>{$a->subject}</i>
<p>{$a->userfrom} posted a new message to a conversation you are participating in course: <i>{$a->course}</i>
with subject: <i>{$a->subject}</i>
<br/><br/><a href="{$a->url}">View in Moodle</a></p>';
$string['messageapismallmessage'] = '{$a} posted a new message to a conversation you are participating in';
$string['messageprovider:post'] = 'Dialogue notifications';
Expand Down
2 changes: 1 addition & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function dialogue_process_bulk_openrules() {
// Mark the sent in automated conversation, so can track who sent to.
$conversation->set_flag(\mod_dialogue\dialogue::FLAG_SENT, $user);
unset($copy);
mtrace(' opened '. $conversation->subject . ' with ' . fullname($user));
mtrace(' opened '. $conversation->subject . ' with ' . dialogue_add_user_fullname($user));
// Up open count.
$conversationsopened++;

Expand Down
26 changes: 25 additions & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function dialogue_get_user_details(\mod_dialogue\dialogue $dialogue, $userid) {
function dialogue_add_user_picture_fields(stdClass &$user) {
global $PAGE;

$user->fullname = fullname($user);
$user->fullname = dialogue_add_user_fullname($user);
$userpic = new user_picture($user);
$imageurl = $userpic->get_url($PAGE);
$user->imageurl = $imageurl->out();
Expand Down Expand Up @@ -487,3 +487,27 @@ 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 $user
* @param string $capability
*
* @return string fullname
*/
function dialogue_add_user_fullname(stdClass $userviewed, stdClass $userviewedby = NULL, string $capability = 'moodle/site:viewfullnames') {
global $PAGE, $USER;
$cm = $PAGE->cm;
if (empty($userviewedby)) {
$userviewedby = $USER;
}
$context = context_module::instance($cm->id);
$hasviewfullnames = has_capability($capability, $context, $userviewedby);
if ($hasviewfullnames) {
$username = fullname($userviewed, true);
} else {
$username = fullname($userviewed);
}
return $username;
}
12 changes: 6 additions & 6 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
if ($conversation->timemodified >= $today) {
$openedbyheader = get_string('openedbytoday', 'dialogue', $datestrings);
} else if ($conversation->timemodified >= $yearago) {
Expand Down Expand Up @@ -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));
$html .= html_writer::tag('td', $sentonstring . userdate($receivedby->timemodified));
$html .= html_writer::end_tag('tr');
}
Expand All @@ -162,7 +162,7 @@ public function render_conversation(mod_dialogue\conversation $conversation) {
$html .= '&nbsp;' . 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 . '&nbsp;' . fullname($participant), array('class' => 'participant'));
$html .= html_writer::tag('span', $picture . '&nbsp;' . dialogue_add_user_fullname($participant), array('class' => 'participant'));
}
$html .= html_writer::end_div();
}
Expand Down Expand Up @@ -241,7 +241,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));
}

if (isset($record->subject) && isset($record->body)) {
Expand All @@ -257,7 +257,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),
array('class' => 'participant'));

}
Expand Down Expand Up @@ -326,7 +326,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);
if ($reply->timemodified >= $today) {
$repliedbyheader = get_string('repliedbytoday', 'dialogue', $datestrings);
} else if ($reply->timemodified >= $yearago) {
Expand Down

0 comments on commit 44c4834

Please sign in to comment.