Skip to content

Commit 8ec1882

Browse files
authored
Merge pull request #249 from learnweb/feature/render_parent_post
Feature: Display the post a reply is referring to.
2 parents 48f31c1 + 2604efd commit 8ec1882

File tree

11 files changed

+278
-8
lines changed

11 files changed

+278
-8
lines changed

amd/build/show_post.min.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/show_post.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/show_post.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// This file is part of Moodle - http://moodle.org/
2+
//
3+
// Moodle is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// Moodle is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15+
16+
import {getString} from "core/str";
17+
import {prefetchStrings} from 'core/prefetch';
18+
/**
19+
* JavaScript for
20+
*
21+
* @module mod_moodleoverflow/show_post
22+
* @copyright 2025 Tamaro Walter
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
const showpostButton = document.getElementById('moodleoverflow_showpost');
27+
const postElement = document.getElementById('moodleoverflow_original_post');
28+
29+
const Selectors = {
30+
actions: {
31+
showpostbutton: '[data-action="mod_moodleoverflow/showpost_button"]',
32+
},
33+
};
34+
35+
/**
36+
* Init function.
37+
*/
38+
export function init() {
39+
prefetchStrings('moodleoverflow', ['showpost_expand', 'showpost_collapse',]);
40+
postElement.setAttribute('expanded', 'false');
41+
postElement.style.maxHeight = '0px';
42+
addEventListener();
43+
}
44+
45+
/**
46+
* Event listener.
47+
*/
48+
const addEventListener = () => {
49+
document.addEventListener('click', async e => {
50+
if (e.target.closest(Selectors.actions.showpostbutton)) {
51+
if (postElement.getAttribute('expanded') === 'true') {
52+
showpostButton.textContent = await getString('showpost_expand', 'moodleoverflow');
53+
postElement.style.maxHeight = '0px';
54+
postElement.setAttribute('expanded', 'false');
55+
} else {
56+
showpostButton.textContent = await getString('showpost_collapse', 'moodleoverflow');
57+
postElement.style.maxHeight = `${postElement.scrollHeight}px`;
58+
postElement.setAttribute('expanded', 'true');
59+
}
60+
}
61+
});
62+
};

classes/post/post.php

Lines changed: 63 additions & 8 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
/**
@@ -707,14 +763,13 @@ public function moodleoverflow_get_post_ratings(): object {
707763
$discussionid = $this->get_discussion()->get_id();
708764
$postratings = ratings::moodleoverflow_get_ratings_by_discussion($discussionid, $this->id);
709765

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+
];
718773
}
719774

720775
/**

classes/post/post_control.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
// Import namespace from the locallib, needs a check later which namespaces are really needed.
2929
use coding_exception;
30+
use context_module;
3031
use core\notification;
3132
use dml_exception;
3233
use html_writer;
@@ -671,6 +672,28 @@ public function build_postform(array $pageparams): mod_moodleoverflow_post_form
671672
return $mformpost;
672673
}
673674

675+
/**
676+
* Display the original post when a user replies to it.
677+
*
678+
* @throws moodle_exception|dml_exception
679+
*/
680+
public function display_original_post(): string {
681+
global $PAGE, $DB;
682+
if ($this->interaction == 'reply') {
683+
$PAGE->requires->js_call_amd('mod_moodleoverflow/show_post', 'init');
684+
$post = post::from_record($DB->get_record('moodleoverflow_posts', ['id' => $this->info->relatedpost->get_id()]));
685+
$data = (object) [
686+
'postid' => $post->get_id(),
687+
'postcontent' => $post->get_message_formatted(),
688+
'attachments' => $post->moodleoverflow_get_attachments(),
689+
'byname' => $post->get_userlink(),
690+
'byshortdate' => userdate($post->modified, get_string('strftimedatetimeshort', 'core_langconfig')),
691+
];
692+
return $PAGE->get_renderer('mod_moodleoverflow')->render_post_original($data);
693+
}
694+
return '';
695+
}
696+
674697
// Helper functions.
675698

676699
// Error handling functions.

lang/en/moodleoverflow.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
$string['nowtracking'] = '{$a->name} is now tracking \'{$a->moodleoverflow}\'.';
271271
$string['oldpostdays'] = 'Read after days';
272272
$string['original_post'] = 'Original post';
273+
$string['original_post_reply'] = 'You are adressing the post from {$a}:';
273274
$string['parent'] = 'Show parent';
274275
$string['pending_review'] = 'Pending review';
275276
$string['pending_review_but_cannot_now'] = 'Pending review, but cannot be approved until {$a} after the creation of this post to allow the author a bit of time to edit it.';
@@ -369,6 +370,8 @@
369370
$string['scalefactor_help'] = 'The user rating is divided by the scale factor to obtain each user\'s grade. If the resulting grade is greater than the maximum grade, the value is limited to the specified maximum grade';
370371
$string['scalefactorerror'] = 'Scale factor must be a positive integer different than 0';
371372
$string['seeuserstats'] = 'View user statistics';
373+
$string['showpost_collapse'] = 'collapse';
374+
$string['showpost_expand'] = 'expand';
372375
$string['showuserstats'] = 'Show cumulative user statistics';
373376
$string['smallmessage'] = '{$a->user} posted in {$a->moodleoverflowname}';
374377
$string['starterrating'] = 'Helpful';

post.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,6 @@
168168

169169
// Display all.
170170
echo $OUTPUT->header();
171+
echo $postcontrol->display_original_post();
171172
$mformpost->display();
172173
echo $OUTPUT->footer();

renderer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ public function render_post($data) {
7979
return $this->render_from_template('mod_moodleoverflow/post', $data);
8080
}
8181

82+
/**
83+
* Renders a simplified version of any post. Used to display the post a reply is referring to.
84+
*
85+
* @param object $data The submitted variables.
86+
*
87+
* @return bool|string
88+
*/
89+
public function render_post_original(object $data): bool|string {
90+
return $this->render_from_template('mod_moodleoverflow/post_original', $data);
91+
}
92+
8293
/**
8394
* Display a moodleoverflow post in the relevant context.
8495
*

styles.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@
123123
white-space: nowrap;
124124
}
125125

126+
#moodleoverflow_original_post {
127+
overflow: hidden;
128+
max-height: 0;
129+
transition: max-height 0.4s ease;
130+
}
131+
132+
#moodleoverflow_showpost {
133+
cursor: pointer;
134+
background-color: #6a737b;
135+
font-size: 0.8rem;
136+
}
137+
138+
.original-post-text {
139+
margin-bottom: 5px;
140+
overflow-wrap: break-word;
141+
width: 100%;
142+
word-wrap: break-word;
143+
}
144+
126145
/*
127146
* The Index Page.
128147
*/

templates/post_original.mustache

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{{!
2+
This file is part of Moodle - http://moodle.org/
3+
4+
Moodle is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
Moodle is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
}}
17+
{{!
18+
@template mod_moodleoverflow/post_original
19+
20+
Moodleoverflow post original template.
21+
In the post.php this template is used to show the post that a new answer post is referring to.
22+
It's a simpler version of the post template.
23+
24+
Example context (json):
25+
{
26+
}
27+
}}
28+
29+
{{! Start the post. Mark it read or unread. }}
30+
<h2>Reply to discussion</h2>
31+
<div id="p{{postid}}" class="moodleoverflowpost bg-light moodleoverflowcomment
32+
border" role="region" data-moodleoverflow-postid="{{postid}}">
33+
<div class="d-flex p-2 w-100">
34+
<div class="answercell d-flex flex-column">
35+
<div class="post-info mb-2">
36+
<div class="leftbox">
37+
<span class="text-muted">{{#str}} original_post_reply, moodleoverflow, {{{ byname }}} {{/str}}</span>
38+
</div>
39+
<div class="rightbox">
40+
<div class="post-menu">
41+
<span class="badge rounded-pill" id="moodleoverflow_showpost"
42+
data-action="mod_moodleoverflow/showpost_button">
43+
{{#str}} showpost_expand, moodleoverflow {{/str}}
44+
</span>
45+
</div>
46+
</div>
47+
</div>
48+
<div id="moodleoverflow_original_post">
49+
<div class="original-post-text">
50+
{{{ postcontent }}}
51+
</div>
52+
<div class="attachments flex-grow-1">
53+
{{#attachments}}
54+
{{#image}}
55+
<img src="{{filepath}}" alt=""/>
56+
<br>
57+
{{/image}}
58+
{{^image}}
59+
<a class="icon-size-6" href="{{filepath}}">
60+
{{{icon}}}
61+
</a>
62+
<a href="{{filepath}}">
63+
{{filename}}
64+
</a>
65+
{{/image}}
66+
<br>
67+
{{/attachments}}
68+
</div>
69+
</div>
70+
</div>
71+
</div>
72+
</div>

0 commit comments

Comments
 (0)