Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions amd/build/show_post.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/show_post.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions amd/src/show_post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

import {getString} from "core/str";
import {prefetchStrings} from 'core/prefetch';
/**
* JavaScript for
*
* @module mod_moodleoverflow/show_post
* @copyright 2025 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

const showpostButton = document.getElementById('moodleoverflow_showpost');
const postElement = document.getElementById('moodleoverflow_original_post');

const Selectors = {
actions: {
showpostbutton: '[data-action="mod_moodleoverflow/showpost_button"]',
},
};

/**
* Init function.
*/
export function init() {
prefetchStrings('moodleoverflow', ['showpost_expand', 'showpost_collapse',]);
postElement.setAttribute('expanded', 'false');
postElement.style.maxHeight = '0px';
addEventListener();
}

/**
* Event listener.
*/
const addEventListener = () => {
document.addEventListener('click', async e => {
if (e.target.closest(Selectors.actions.showpostbutton)) {
if (postElement.getAttribute('expanded') === 'true') {
showpostButton.textContent = await getString('showpost_expand', 'moodleoverflow');
postElement.style.maxHeight = '0px';
postElement.setAttribute('expanded', 'false');
} else {
showpostButton.textContent = await getString('showpost_collapse', 'moodleoverflow');
postElement.style.maxHeight = `${postElement.scrollHeight}px`;
postElement.setAttribute('expanded', 'true');
}
}
});
};
71 changes: 63 additions & 8 deletions classes/post/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
namespace mod_moodleoverflow\post;

use coding_exception;
use context_module;
use core\output\html_writer;
use core_user\fields;
use dml_exception;
use mod_moodleoverflow\anonymous;
use mod_moodleoverflow\capabilities;
use mod_moodleoverflow\event\post_deleted;
use mod_moodleoverflow\ratings;
use mod_moodleoverflow\readtracking;
Expand Down Expand Up @@ -550,6 +554,58 @@ public function moodleoverflow_get_attachments(): array {
return $attachments;
}

/**
* Get a link to the users profile.
* Returns a html link embedded in the users name.
* @return moodle_url
* @throws moodle_exception
*/
public function get_userlink(): string {
global $USER, $DB;
$this->existence_check();

$courseid = $this->get_discussion()->get_courseid();
$modulecontext = context_module::instance($this->get_coursemodule()->id);
$userid = $this->get_userid();

if (anonymous::is_post_anonymous($this->get_discussion()->get_db_object(), $this->get_moodleoverflow(), $userid)) {
if ($userid == $USER->id) {
$fullname = get_string('anonym_you', 'mod_moodleoverflow');
$profilelink = new moodle_url('/user/view.php', ['id' => $userid, 'course' => $courseid]);
return html_writer::link($profilelink, $fullname);
} else {
$usermapping = anonymous::get_userid_mapping($this->get_moodleoverflow(), $this->get_discussionid());
return $usermapping[$userid];
}
}
$user = $DB->get_record('user', ['id' => $userid]);
$fullname = fullname($user, capabilities::has('moodle/site:viewfullnames', $modulecontext));
$profilelink = new moodle_url('/user/view.php', ['id' => $userid, 'course' => $courseid]);
return html_writer::link($profilelink, $fullname);
}

/**
* Returns the post message in a formatted way ready to display.
* @return string
* @throws moodle_exception
*/
public function get_message_formatted(): string {
$context = context_module::instance($this->get_coursemodule()->id);
$message = file_rewrite_pluginfile_urls(
$this->message,
'pluginfile.php',
$context->id,
'mod_moodleoverflow',
'post',
$this->get_id(),
['includetoken' => true]
);
$options = new stdClass();
$options->para = true;
$options->context = $context;
return format_text($message, $this->messageformat, $options);
}

// Getter.

/**
Expand Down Expand Up @@ -707,14 +763,13 @@ public function moodleoverflow_get_post_ratings(): object {
$discussionid = $this->get_discussion()->get_id();
$postratings = ratings::moodleoverflow_get_ratings_by_discussion($discussionid, $this->id);

$ratingsobject = new stdClass();
$ratingsobject->upvotes = $postratings->upvotes;
$ratingsobject->downvotes = $postratings->downvotes;
$ratingsobject->votesdifference = $postratings->upvotes - $postratings->downvotes;
$ratingsobject->markedhelpful = $postratings->ishelpful;
$ratingsobject->markedsolution = $postratings->issolved;

return $ratingsobject;
return (object) [
'upvotes' => $postratings->upvotes,
'downvotes' => $postratings->downvotes,
'votesdifference' => $postratings->upvotes - $postratings->downvotes,
'markedhelpful' => $postratings->ishelpful,
'markedsolution' => $postratings->issolved,
];
}

/**
Expand Down
23 changes: 23 additions & 0 deletions classes/post/post_control.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

// Import namespace from the locallib, needs a check later which namespaces are really needed.
use coding_exception;
use context_module;
use core\notification;
use dml_exception;
use html_writer;
Expand Down Expand Up @@ -671,6 +672,28 @@ public function build_postform(array $pageparams): mod_moodleoverflow_post_form
return $mformpost;
}

/**
* Display the original post when a user replies to it.
*
* @throws moodle_exception|dml_exception
*/
public function display_original_post(): string {
global $PAGE, $DB;
if ($this->interaction == 'reply') {
$PAGE->requires->js_call_amd('mod_moodleoverflow/show_post', 'init');
$post = post::from_record($DB->get_record('moodleoverflow_posts', ['id' => $this->info->relatedpost->get_id()]));
$data = (object) [
'postid' => $post->get_id(),
'postcontent' => $post->get_message_formatted(),
'attachments' => $post->moodleoverflow_get_attachments(),
'byname' => $post->get_userlink(),
'byshortdate' => userdate($post->modified, get_string('strftimedatetimeshort', 'core_langconfig')),
];
return $PAGE->get_renderer('mod_moodleoverflow')->render_post_original($data);
}
return '';
}

// Helper functions.

// Error handling functions.
Expand Down
3 changes: 3 additions & 0 deletions lang/en/moodleoverflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
$string['nowtracking'] = '{$a->name} is now tracking \'{$a->moodleoverflow}\'.';
$string['oldpostdays'] = 'Read after days';
$string['original_post'] = 'Original post';
$string['original_post_reply'] = 'You are adressing the post from {$a}:';
$string['parent'] = 'Show parent';
$string['pending_review'] = 'Pending review';
$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.';
Expand Down Expand Up @@ -369,6 +370,8 @@
$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';
$string['scalefactorerror'] = 'Scale factor must be a positive integer different than 0';
$string['seeuserstats'] = 'View user statistics';
$string['showpost_collapse'] = 'collapse';
$string['showpost_expand'] = 'expand';
$string['showuserstats'] = 'Show cumulative user statistics';
$string['smallmessage'] = '{$a->user} posted in {$a->moodleoverflowname}';
$string['starterrating'] = 'Helpful';
Expand Down
1 change: 1 addition & 0 deletions post.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,6 @@

// Display all.
echo $OUTPUT->header();
echo $postcontrol->display_original_post();
$mformpost->display();
echo $OUTPUT->footer();
11 changes: 11 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ public function render_post($data) {
return $this->render_from_template('mod_moodleoverflow/post', $data);
}

/**
* Renders a simplified version of any post. Used to display the post a reply is referring to.
*
* @param object $data The submitted variables.
*
* @return bool|string
*/
public function render_post_original(object $data): bool|string {
return $this->render_from_template('mod_moodleoverflow/post_original', $data);
}

/**
* Display a moodleoverflow post in the relevant context.
*
Expand Down
19 changes: 19 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@
white-space: nowrap;
}

#moodleoverflow_original_post {
overflow: hidden;
max-height: 0;
transition: max-height 0.4s ease;
}

#moodleoverflow_showpost {
cursor: pointer;
background-color: #6a737b;
font-size: 0.8rem;
}

.original-post-text {
margin-bottom: 5px;
overflow-wrap: break-word;
width: 100%;
word-wrap: break-word;
}

/*
* The Index Page.
*/
Expand Down
72 changes: 72 additions & 0 deletions templates/post_original.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{{!
This file is part of Moodle - http://moodle.org/

Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template mod_moodleoverflow/post_original

Moodleoverflow post original template.
In the post.php this template is used to show the post that a new answer post is referring to.
It's a simpler version of the post template.

Example context (json):
{
}
}}

{{! Start the post. Mark it read or unread. }}
<h2>Reply to discussion</h2>
<div id="p{{postid}}" class="moodleoverflowpost bg-light moodleoverflowcomment
border" role="region" data-moodleoverflow-postid="{{postid}}">
<div class="d-flex p-2 w-100">
<div class="answercell d-flex flex-column">
<div class="post-info mb-2">
<div class="leftbox">
<span class="text-muted">{{#str}} original_post_reply, moodleoverflow, {{{ byname }}} {{/str}}</span>
</div>
<div class="rightbox">
<div class="post-menu">
<span class="badge rounded-pill" id="moodleoverflow_showpost"
data-action="mod_moodleoverflow/showpost_button">
{{#str}} showpost_expand, moodleoverflow {{/str}}
</span>
</div>
</div>
</div>
<div id="moodleoverflow_original_post">
<div class="original-post-text">
{{{ postcontent }}}
</div>
<div class="attachments flex-grow-1">
{{#attachments}}
{{#image}}
<img src="{{filepath}}" alt=""/>
<br>
{{/image}}
{{^image}}
<a class="icon-size-6" href="{{filepath}}">
{{{icon}}}
</a>
<a href="{{filepath}}">
{{filename}}
</a>
{{/image}}
<br>
{{/attachments}}
</div>
</div>
</div>
</div>
</div>
12 changes: 12 additions & 0 deletions tests/behat/reply_post.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@mod @mod_moodleoverflow @javascript
Feature: Replying to posts in a moodleoverflow

Scenario: While replying to a post I need to see the original post I'm answering to
Given I add a moodleoverflow discussion with posts from different users
When I navigate as "teacher1" to "C1" "Moodleoverflow 1" "Discussion 1"
And I click on "Answer" "link"
Then I should see "You are adressing the post from Tamaro Walter:"
When I click on "#moodleoverflow_showpost" "css_element"
Then I should see "Message from teacher"
When I click on "#moodleoverflow_showpost" "css_element"
Then I should not see "Message from teacher"
Loading