Skip to content

Commit

Permalink
Merge branch 'MDL-51225-master' of git://github.com/junpataleta/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jan 21, 2020
2 parents 790735f + 7cce925 commit 703f2b6
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 22 deletions.
106 changes: 85 additions & 21 deletions mod/forum/lib.php
Expand Up @@ -632,13 +632,22 @@ function forum_print_overview() {
* @return bool success
*/
function forum_print_recent_activity($course, $viewfullnames, $timestart) {
global $CFG, $USER, $DB, $OUTPUT;
global $USER, $DB, $OUTPUT;

// do not use log table if possible, it may be huge and is expensive to join with other tables

$allnamefields = user_picture::fields('u', null, 'duserid');
if (!$posts = $DB->get_records_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid,
d.timestart, d.timeend, $allnamefields
if (!$posts = $DB->get_records_sql("SELECT p.*,
f.course, f.type AS forumtype, f.name AS forumname, f.intro, f.introformat, f.duedate,
f.cutoffdate, f.assessed AS forumassessed, f.assesstimestart, f.assesstimefinish,
f.scale, f.grade_forum, f.maxbytes, f.maxattachments, f.forcesubscribe,
f.trackingtype, f.rsstype, f.rssarticles, f.timemodified, f.warnafter, f.blockafter,
f.blockperiod, f.completiondiscussions, f.completionreplies, f.completionposts,
f.displaywordcount, f.lockdiscussionafter, f.grade_forum_notify,
d.name AS discussionname, d.firstpost, d.userid AS discussionstarter,
d.assessed AS discussionassessed, d.timemodified, d.usermodified, d.forum, d.groupid,
d.timestart, d.timeend, d.pinned, d.timelocked,
$allnamefields
FROM {forum_posts} p
JOIN {forum_discussions} d ON d.id = p.discussion
JOIN {forum} f ON f.id = d.forum
Expand All @@ -650,12 +659,14 @@ function forum_print_recent_activity($course, $viewfullnames, $timestart) {

$modinfo = get_fast_modinfo($course);

$groupmodes = array();
$cms = array();

$strftimerecent = get_string('strftimerecent');

$printposts = array();
$managerfactory = mod_forum\local\container::get_manager_factory();
$entityfactory = mod_forum\local\container::get_entity_factory();

$discussions = [];
$capmanagers = [];
$printposts = [];
foreach ($posts as $post) {
if (!isset($modinfo->instances['forum'][$post->forum])) {
// not visible
Expand All @@ -665,28 +676,81 @@ function forum_print_recent_activity($course, $viewfullnames, $timestart) {
if (!$cm->uservisible) {
continue;
}
$context = context_module::instance($cm->id);

if (!has_capability('mod/forum:viewdiscussion', $context)) {
continue;
// Get the discussion. Cache if not yet available.
if (!isset($discussions[$post->discussion])) {
// Build the discussion record object from the post data.
$discussionrecord = (object)[
'id' => $post->discussion,
'course' => $post->course,
'forum' => $post->forum,
'name' => $post->discussionname,
'firstpost' => $post->firstpost,
'userid' => $post->discussionstarter,
'groupid' => $post->groupid,
'assessed' => $post->discussionassessed,
'timemodified' => $post->timemodified,
'usermodified' => $post->usermodified,
'timestart' => $post->timestart,
'timeend' => $post->timeend,
'pinned' => $post->pinned,
'timelocked' => $post->timelocked
];
// Build the discussion entity from the factory and cache it.
$discussions[$post->discussion] = $entityfactory->get_discussion_from_stdclass($discussionrecord);
}
$discussionentity = $discussions[$post->discussion];

if (!empty($CFG->forum_enabletimedposts) and $USER->id != $post->duserid
and (($post->timestart > 0 and $post->timestart > time()) or ($post->timeend > 0 and $post->timeend < time()))) {
if (!has_capability('mod/forum:viewhiddentimedposts', $context)) {
continue;
}
// Get the capability manager. Cache if not yet available.
if (!isset($capmanagers[$post->forum])) {
$context = context_module::instance($cm->id);
$coursemodule = $cm->get_course_module_record();
// Build the forum record object from the post data.
$forumrecord = (object)[
'id' => $post->forum,
'course' => $post->course,
'type' => $post->forumtype,
'name' => $post->forumname,
'intro' => $post->intro,
'introformat' => $post->introformat,
'duedate' => $post->duedate,
'cutoffdate' => $post->cutoffdate,
'assessed' => $post->forumassessed,
'assesstimestart' => $post->assesstimestart,
'assesstimefinish' => $post->assesstimefinish,
'scale' => $post->scale,
'grade_forum' => $post->grade_forum,
'maxbytes' => $post->maxbytes,
'maxattachments' => $post->maxattachments,
'forcesubscribe' => $post->forcesubscribe,
'trackingtype' => $post->trackingtype,
'rsstype' => $post->rsstype,
'rssarticles' => $post->rssarticles,
'timemodified' => $post->timemodified,
'warnafter' => $post->warnafter,
'blockafter' => $post->blockafter,
'blockperiod' => $post->blockperiod,
'completiondiscussions' => $post->completiondiscussions,
'completionreplies' => $post->completionreplies,
'completionposts' => $post->completionposts,
'displaywordcount' => $post->displaywordcount,
'lockdiscussionafter' => $post->lockdiscussionafter,
'grade_forum_notify' => $post->grade_forum_notify
];
// Build the forum entity from the factory.
$forumentity = $entityfactory->get_forum_from_stdclass($forumrecord, $context, $coursemodule, $course);
// Get the capability manager of this forum and cache it.
$capmanagers[$post->forum] = $managerfactory->get_capability_manager($forumentity);
}
$capabilitymanager = $capmanagers[$post->forum];

if (!forum_post_is_visible_privately($post, $cm)) {
continue;
}
// Get the post entity.
$postentity = $entityfactory->get_post_from_stdclass($post);

// Check that the user can see the discussion.
if (forum_is_user_group_discussion($cm, $post->groupid)) {
// Check if the user can view the post.
if ($capabilitymanager->can_view_post($USER, $discussionentity, $postentity)) {
$printposts[] = $post;
}

}
unset($posts);

Expand Down
11 changes: 11 additions & 0 deletions mod/forum/tests/behat/behat_mod_forum.php
Expand Up @@ -49,6 +49,17 @@ public function i_add_a_new_topic_to_forum_with($forumname, TableNode $table) {
$this->add_new_discussion($forumname, $table, get_string('addanewtopic', 'forum'));
}

/**
* Adds a Q&A discussion to the Q&A-type forum specified by it's name with the provided table data.
*
* @Given /^I add a new question to "(?P<forum_name_string>(?:[^"]|\\")*)" forum with:$/
* @param string $forumname
* @param TableNode $table
*/
public function i_add_a_new_question_to_forum_with($forumname, TableNode $table) {
$this->add_new_discussion($forumname, $table, get_string('addanewquestion', 'forum'));
}

/**
* Adds a discussion to the forum specified by it's name with the provided table data (usually Subject and Message). The step begins from the forum's course page.
*
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/tests/behat/edit_post_student.feature
Expand Up @@ -40,7 +40,7 @@ Feature: Students can edit or delete their forum posts within a set time limit
And I press "Continue"
Then I should not see "Forum post subject"

@javascript @block_recent_activity
@block_recent_activity
Scenario: Time limit expires
Given I log out
And I log in as "admin"
Expand Down
141 changes: 141 additions & 0 deletions mod/forum/tests/behat/recent_activity.feature
@@ -0,0 +1,141 @@
@mod @mod_forum @block_recent_activity
Feature: Users can see the relevant recent forum posts from the recent activity block
In order to quickly see the updates from forums in my course
As a user
I need to be able to see the recent forum posts in the recent activity block

Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
| student2 | C1 | student |
| teacher1 | C1 | editingteacher |
And the following "groups" exist:
| name | course | idnumber |
| Group 1 | C1 | G1 |
| Group 2 | C1 | G2 |
And the following "group members" exist:
| user | group |
| student1 | G1 |
| student2 | G2 |
| teacher1 | G1 |
| teacher1 | G2 |
And the following "activities" exist:
| activity | name | intro | course | idnumber | type | groupmode | visible |
| forum | Separate groups forum | Separate groups description | C1 | forum1 | general | 1 | 1 |
| forum | Visible groups forum | Visible groups description | C1 | forum2 | general | 2 | 1 |
| forum | Standard forum | Standard description | C1 | forum3 | general | 0 | 1 |
| forum | Hidden forum | Hidden description | C1 | forum4 | general | 0 | 0 |
| forum | Q&A forum | Q&A description | C1 | forum5 | qanda | 0 | 1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I add the "Recent activity" block

Scenario: Recent forum activity with separate group discussion
Given I add a new discussion to "Separate groups forum" forum with:
| Subject | Group 1 separate discussion |
| Message | Group 1 members only |
| Group | Group 1 |
And I log out
And I log in as "student1"
When I am on "Course 1" course homepage
Then I should see "Group 1 separate discussion" in the "Recent activity" "block"
And I log out
And I log in as "student2"
And I am on "Course 1" course homepage
And I should not see "Group 1 separate discussion" in the "Recent activity" "block"

Scenario: Recent forum activity with visible groups discussion
Given I add a new discussion to "Visible groups forum" forum with:
| Subject | Group 1 visible discussion |
| Message | Not just for group 1 members |
| Group | Group 1 |
And I log out
And I log in as "student1"
When I am on "Course 1" course homepage
Then I should see "Group 1 visible discussion" in the "Recent activity" "block"
And I log out
And I log in as "student2"
And I am on "Course 1" course homepage
And I should see "Group 1 visible discussion" in the "Recent activity" "block"

Scenario: Recent forum activity with recent post as a private reply
Given I add a new discussion to "Standard forum" forum with:
| Subject | Standard forum discussion |
| Message | Discuss anything under the sun here! |
And I reply "Standard forum discussion" post from "Standard forum" forum with:
| Subject | Teacher's private reply |
| Message | This is a private reply |
| Reply privately | 1 |
And I am on "Course 1" course homepage
And I should see "Standard forum discussion" in the "Recent activity" "block"
And I should see "Teacher's private reply" in the "Recent activity" "block"
And I log out
When I log in as "student1"
And I am on "Course 1" course homepage
Then I should see "Standard forum discussion" in the "Recent activity" "block"
But I should not see "Teacher's private reply" in the "Recent activity" "block"

Scenario: Recent forum activity with recent post in a hidden forum
Given I add a new discussion to "Hidden forum" forum with:
| Subject | Hidden discussion |
| Message | Should be hidden! |
And I am on "Course 1" course homepage
And I should see "Hidden discussion" in the "Recent activity" "block"
And I log out
And I log in as "student1"
When I am on "Course 1" course homepage
Then I should not see "Hidden discussion" in the "Recent activity" "block"

Scenario: Recent forum activity with question and answer forum
Given I add a new question to "Q&A forum" forum with:
| Subject | The egg vs the chicken |
| Message | Which came first? The egg or the chicken? |
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I reply "The egg vs the chicken" post from "Q&A forum" forum with:
| Subject | Student 1's answer |
| Message | The egg! |
And I am on "Course 1" course homepage
And I should see "The egg vs the chicken" in the "Recent activity" "block"
And I should see "Student 1's answer" in the "Recent activity" "block"
And I log out
And I log in as "admin"
And the following config values are set as admin:
| maxeditingtime | 1 |
And I log out
When I log in as "student2"
And I am on "Course 1" course homepage
Then I should see "The egg vs the chicken" in the "Recent activity" "block"
But I should not see "Student 1's answer" in the "Recent activity" "block"
And I reply "The egg vs the chicken" post from "Q&A forum" forum with:
| Subject | Student 2's answer |
| Message | The chicken, duh! |
And I wait "2" seconds
And I am on "Course 1" course homepage
And I should see "Student 1's answer" in the "Recent activity" "block"
And I should see "Student 2's answer" in the "Recent activity" "block"

Scenario: Recent forum activity with timed discussion
Given I add a new discussion to "Standard forum" forum with:
| Subject | Timed discussion |
| Message | Discuss anything under the sun here... no more!!! |
| timeend[enabled] | 1 |
| timeend[year] | 2020 |
| timeend[month] | 1 |
| timeend[day] | 1 |
And I am on "Course 1" course homepage
And I should see "Timed discussion" in the "Recent activity" "block"
And I log out
When I log in as "student1"
And I am on "Course 1" course homepage
Then I should not see "Timed discussion" in the "Recent activity" "block"

0 comments on commit 703f2b6

Please sign in to comment.