Skip to content

Commit

Permalink
Merge pull request #136 from sumaiyamannan/MOODLE_401_STABLE-fullname…
Browse files Browse the repository at this point in the history
…_capability_check

Display user fullname and replace capability check in dialogue creation
  • Loading branch information
danmarsden authored Sep 19, 2024
2 parents c7f6768 + 2681679 commit 85eb852
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 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,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);
Expand All @@ -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';
Expand Down
5 changes: 2 additions & 3 deletions formlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
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
16 changes: 16 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
16 changes: 9 additions & 7 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, $USER, $cm);
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, $USER, $cm));
$html .= html_writer::tag('td', $sentonstring . userdate($receivedby->timemodified));
$html .= html_writer::end_tag('tr');
}
Expand All @@ -162,7 +162,8 @@ 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, $USER, $cm), array('class' => 'participant'));
}
$html .= html_writer::end_div();
}
Expand All @@ -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;

Expand Down Expand Up @@ -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)) {
Expand All @@ -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'));

}
Expand Down Expand Up @@ -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.
Expand All @@ -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) {
Expand Down

0 comments on commit 85eb852

Please sign in to comment.