Skip to content

Commit

Permalink
MDL-71029 forum: fix post count for users have more than 1 enrolment
Browse files Browse the repository at this point in the history
  • Loading branch information
dcai committed Jun 22, 2021
1 parent 2d82188 commit 9a50919
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 27 deletions.
51 changes: 24 additions & 27 deletions mod/forum/report/summary/classes/summary_table.php
Expand Up @@ -548,8 +548,8 @@ protected function define_base_sql(): void {
$userfieldssql = $userfieldsapi->get_sql('u', false, '', '', false)->selects;

// Define base SQL query format.
$this->sql->basefields = ' ue.userid AS userid,
e.courseid AS courseid,
$this->sql->basefields = ' u.id AS userid,
d.course AS courseid,
SUM(CASE WHEN p.parent = 0 THEN 1 ELSE 0 END) AS postcount,
SUM(CASE WHEN p.parent != 0 THEN 1 ELSE 0 END) AS replycount,
' . $userfieldssql . ',
Expand All @@ -568,27 +568,24 @@ protected function define_base_sql(): void {
$privaterepliesparams['privatereplyfrom'] = $USER->id;
}

$this->sql->basefromjoins = ' {enrol} e
JOIN {user_enrolments} ue ON ue.enrolid = e.id
JOIN {user} u ON u.id = ue.userid
JOIN {forum} f ON f.course = e.courseid
JOIN {forum_discussions} d ON d.forum = f.id
LEFT JOIN {forum_posts} p ON p.discussion = d.id
AND p.userid = ue.userid
' . $privaterepliessql
. $this->sql->filterbase['dates'] . '
LEFT JOIN (
SELECT COUNT(fi.id) AS attcount, fi.itemid AS postid, fi.userid
FROM {files} fi
WHERE fi.component = :component
AND fi.filesize > 0
GROUP BY fi.itemid, fi.userid
) att ON att.postid = p.id
AND att.userid = ue.userid';

$this->sql->basewhere = 'e.courseid = :courseid';

$this->sql->basegroupby = 'ue.userid, e.courseid, u.id, ' . $userfieldssql;
list($enrolleduserssql, $enrolledusersparams) = get_enrolled_sql($this->get_context());
$this->sql->params += $enrolledusersparams;

$queryattachments = 'SELECT COUNT(fi.id) AS attcount, fi.itemid AS postid, fi.userid
FROM {files} fi
WHERE fi.component = :component AND fi.filesize > 0
GROUP BY fi.itemid, fi.userid';
$this->sql->basefromjoins = ' {user} u
JOIN (' . $enrolleduserssql . ') enrolledusers ON enrolledusers.id = u.id
JOIN {forum} f ON f.course = :forumcourseid
JOIN {forum_discussions} d ON d.forum = f.id
LEFT JOIN {forum_posts} p ON p.discussion = d.id AND p.userid = u.id '
. $privaterepliessql
. $this->sql->filterbase['dates'] . '
LEFT JOIN (' . $queryattachments . ') att ON att.postid = p.id AND att.userid = u.id';

$this->sql->basewhere = '1 = 1';
$this->sql->basegroupby = "$userfieldssql, d.course";

if ($this->logreader) {
$this->fill_log_summary_temp_table();
Expand All @@ -606,12 +603,12 @@ protected function define_base_sql(): void {

$this->sql->params += [
'component' => 'mod_forum',
'courseid' => $this->courseid,
'forumcourseid' => $this->courseid,
] + $privaterepliesparams;

// Handle if a user is limited to viewing their own summary.
if (!empty($this->userid)) {
$this->sql->basewhere .= ' AND ue.userid = :userid';
$this->sql->basewhere .= ' AND u.id = :userid';
$this->sql->params['userid'] = $this->userid;
}
}
Expand Down Expand Up @@ -720,11 +717,11 @@ protected function get_full_sql(bool $fullselect = true): string {

$groupby = ' GROUP BY ' . $this->sql->basegroupby . $this->sql->filtergroupby;

if (($sort = $this->get_sql_sort())) {
if ($sort = $this->get_sql_sort()) {
$orderby = " ORDER BY {$sort}";
}
} else {
$selectfields = 'COUNT(DISTINCT(ue.userid))';
$selectfields = 'COUNT(u.id)';
}

$sql = "SELECT {$selectfields}
Expand Down
@@ -0,0 +1,61 @@
@mod @mod_forum @forumreport @forumreport_summary
Feature: forum report shows post/reply/word counts correctly
In order to gather data on users' forum activities
As a teacher
I need to view accurate forum summary report when students have more than 1 enrolment

Scenario: Add discussions and replies with attached files
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
| Course 2 | C2 | 0 |
And the following "course enrolments" exist:
| user | course | role | enrol |
| teacher1 | C1 | editingteacher | manual |
| student1 | C1 | student | manual |
| student1 | C1 | student | self |
| student2 | C1 | student | manual |
| student2 | C1 | student | self |
| teacher1 | C2 | editingteacher | manual |
And the following "activities" exist:
| activity | name | description | course | idnumber |
| forum | forum1 | C1 first forum | C1 | forum1 |
| forum | forum2 | C1 second forum | C1 | forum2 |
| forum | forum1 | C2 first forum | C2 | forum1 |
And the following forum discussions exist in course "Course 1":
| user | forum | name | message | attachments | inlineattachments |
| teacher1 | forum1 | discussion1 | message1 | att1.jpg, att2.txt | |
| teacher1 | forum2 | discussion2 | message2 | att3.jpg | in1.jpg |
| student1 | forum1 | discussion3 | my message3 | att4.jpg | in2.jpg |
| student2 | forum1 | discussion4 | my message4 | | |
And the following forum replies exist in course "Course 1":
| user | forum | discussion | message | attachments | inlineattachments |
| teacher1 | forum1 | discussion1 | reply1 | att5.jpg | in3.txt |
| teacher1 | forum1 | discussion1 | reply2 | att5.jpg | in3.txt |
| teacher1 | forum2 | discussion2 | reply2 | att6.jpg | |
| student1 | forum1 | discussion3 | my reply3 | att7.jpg, att8.jpg | in2.jpg |
| student2 | forum1 | discussion4 | my reply4 | | |
| student2 | forum1 | discussion4 | my reply5 | | |
And the following forum discussions exist in course "Course 2":
| user | forum | name | message | attachments | inlineattachments |
| teacher1 | forum1 | discussion1 | message1 | att1.jpg, att2.txt | |
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "forum1"
And I navigate to "Forum summary report" in current page administration
Then "Teacher 1" row "Number of attachments" column of "forumreport_summary_table" table should contain "6"
And "Student 1" row "Number of attachments" column of "forumreport_summary_table" table should contain "5"
And "Student 1" row "Word count" column of "forumreport_summary_table" table should contain "4"
And "Student 1" row "Character count" column of "forumreport_summary_table" table should contain "18"
And "Student 1" row "Number of discussions posted" column of "forumreport_summary_table" table should contain "1"
And "Student 1" row "Number of replies posted" column of "forumreport_summary_table" table should contain "1"
And "Student 2" row "Number of attachments" column of "forumreport_summary_table" table should contain "0"
And "Student 2" row "Word count" column of "forumreport_summary_table" table should contain "6"
And "Student 2" row "Character count" column of "forumreport_summary_table" table should contain "26"
And "Student 2" row "Number of discussions posted" column of "forumreport_summary_table" table should contain "1"
And "Student 2" row "Number of replies posted" column of "forumreport_summary_table" table should contain "2"

0 comments on commit 9a50919

Please sign in to comment.