Skip to content

Commit 26e22be

Browse files
committed
add helper functions to post class
1 parent 48f31c1 commit 26e22be

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

classes/post/post.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@
2626
namespace mod_moodleoverflow\post;
2727

2828
use coding_exception;
29+
use context_module;
30+
use core\output\html_writer;
2931
use core_user\fields;
3032
use dml_exception;
33+
use mod_moodleoverflow\anonymous;
34+
use mod_moodleoverflow\capabilities;
3135
use mod_moodleoverflow\event\post_deleted;
3236
use mod_moodleoverflow\ratings;
3337
use mod_moodleoverflow\readtracking;
@@ -550,6 +554,58 @@ public function moodleoverflow_get_attachments(): array {
550554
return $attachments;
551555
}
552556

557+
/**
558+
* Get a link to the users profile.
559+
* Returns a html link embedded in the users name.
560+
* @return moodle_url
561+
* @throws moodle_exception
562+
*/
563+
public function get_userlink(): string {
564+
global $USER, $DB;
565+
$this->existence_check();
566+
567+
$courseid = $this->get_discussion()->get_courseid();
568+
$modulecontext = context_module::instance($this->get_coursemodule()->id);
569+
$userid = $this->get_userid();
570+
571+
if (anonymous::is_post_anonymous($this->get_discussion()->get_db_object(), $this->get_moodleoverflow(), $userid)) {
572+
if ($userid == $USER->id) {
573+
$fullname = get_string('anonym_you', 'mod_moodleoverflow');
574+
$profilelink = new moodle_url('/user/view.php', ['id' => $userid, 'course' => $courseid]);
575+
return html_writer::link($profilelink, $fullname);
576+
} else {
577+
$usermapping = anonymous::get_userid_mapping($this->get_moodleoverflow(), $this->get_discussionid());
578+
return $usermapping[$userid];
579+
}
580+
}
581+
$user = $DB->get_record('user', ['id' => $userid]);
582+
$fullname = fullname($user, capabilities::has('moodle/site:viewfullnames', $modulecontext));
583+
$profilelink = new moodle_url('/user/view.php', ['id' => $userid, 'course' => $courseid]);
584+
return html_writer::link($profilelink, $fullname);
585+
}
586+
587+
/**
588+
* Returns the post message in a formatted way ready to display.
589+
* @return string
590+
* @throws moodle_exception
591+
*/
592+
public function get_message_formatted(): string {
593+
$context = context_module::instance($this->get_coursemodule()->id);
594+
$message = file_rewrite_pluginfile_urls(
595+
$this->message,
596+
'pluginfile.php',
597+
$context->id,
598+
'mod_moodleoverflow',
599+
'post',
600+
$this->get_id(),
601+
['includetoken' => true]
602+
);
603+
$options = new stdClass();
604+
$options->para = true;
605+
$options->context = $context;
606+
return format_text($message, $this->messageformat, $options);
607+
}
608+
553609
// Getter.
554610

555611
/**

0 commit comments

Comments
 (0)