|
26 | 26 | namespace mod_moodleoverflow\post; |
27 | 27 |
|
28 | 28 | use coding_exception; |
| 29 | +use context_module; |
| 30 | +use core\output\html_writer; |
29 | 31 | use core_user\fields; |
30 | 32 | use dml_exception; |
| 33 | +use mod_moodleoverflow\anonymous; |
| 34 | +use mod_moodleoverflow\capabilities; |
31 | 35 | use mod_moodleoverflow\event\post_deleted; |
32 | 36 | use mod_moodleoverflow\ratings; |
33 | 37 | use mod_moodleoverflow\readtracking; |
@@ -550,6 +554,58 @@ public function moodleoverflow_get_attachments(): array { |
550 | 554 | return $attachments; |
551 | 555 | } |
552 | 556 |
|
| 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 | + |
553 | 609 | // Getter. |
554 | 610 |
|
555 | 611 | /** |
@@ -707,14 +763,13 @@ public function moodleoverflow_get_post_ratings(): object { |
707 | 763 | $discussionid = $this->get_discussion()->get_id(); |
708 | 764 | $postratings = ratings::moodleoverflow_get_ratings_by_discussion($discussionid, $this->id); |
709 | 765 |
|
710 | | - $ratingsobject = new stdClass(); |
711 | | - $ratingsobject->upvotes = $postratings->upvotes; |
712 | | - $ratingsobject->downvotes = $postratings->downvotes; |
713 | | - $ratingsobject->votesdifference = $postratings->upvotes - $postratings->downvotes; |
714 | | - $ratingsobject->markedhelpful = $postratings->ishelpful; |
715 | | - $ratingsobject->markedsolution = $postratings->issolved; |
716 | | - |
717 | | - return $ratingsobject; |
| 766 | + return (object) [ |
| 767 | + 'upvotes' => $postratings->upvotes, |
| 768 | + 'downvotes' => $postratings->downvotes, |
| 769 | + 'votesdifference' => $postratings->upvotes - $postratings->downvotes, |
| 770 | + 'markedhelpful' => $postratings->ishelpful, |
| 771 | + 'markedsolution' => $postratings->issolved, |
| 772 | + ]; |
718 | 773 | } |
719 | 774 |
|
720 | 775 | /** |
|
0 commit comments