MDL-66299 forumreport_summary: Added attachments count column
authorMichael Hawkins <michaelh@moodle.com>
Tue, 3 Sep 2019 11:35:35 +0000 (19:35 +0800)
committerJun Pataleta <jun@moodle.com>
Fri, 11 Oct 2019 03:10:10 +0000 (11:10 +0800)
Part of MDL-66076.

mod/forum/report/summary/classes/summary_table.php
mod/forum/report/summary/lang/en/forumreport_summary.php
mod/forum/report/summary/tests/behat/summary_data_attachments.feature [new file with mode: 0644]

index bef374a..2e45e55 100644 (file)
@@ -84,6 +84,7 @@ class summary_table extends table_sql {
             'fullname' => get_string('fullnameuser'),
             'postcount' => get_string('postcount', 'forumreport_summary'),
             'replycount' => get_string('replycount', 'forumreport_summary'),
+            'attachmentcount' => get_string('attachmentcount', 'forumreport_summary'),
         ];
 
         $this->define_columns(array_keys($columnheaders));
@@ -145,6 +146,16 @@ class summary_table extends table_sql {
         return $data->replycount;
     }
 
+    /**
+     * Generate the attachmentcount column.
+     *
+     * @param \stdClass $data The row data.
+     * @return int number of files attached to posts by user.
+     */
+    public function col_attachmentcount(\stdClass $data): int {
+        return $data->attachmentcount;
+    }
+
     /**
      * Override the default implementation to set a decent heading level.
      *
@@ -232,6 +243,7 @@ class summary_table extends table_sql {
         $this->sortable(true, 'firstname', SORT_ASC);
         $this->pageable(true);
         $this->no_sorting('select');
+        $this->set_attribute('id', 'forumreport_summary_table');
     }
 
     /**
@@ -250,7 +262,8 @@ class summary_table extends table_sql {
                                     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,
                                     u.firstname,
-                                    u.lastname';
+                                    u.lastname,
+                                    SUM(CASE WHEN att.attcount IS NULL THEN 0 ELSE att.attcount END) AS attachmentcount';
 
         $this->sql->basefromjoins = '    {enrol} e
                                     JOIN {user_enrolments} ue ON ue.enrolid = e.id
@@ -259,13 +272,24 @@ class summary_table extends table_sql {
                                     JOIN {forum_discussions} d ON d.forum = f.id
                                LEFT JOIN {forum_posts} p ON p.discussion =  d.id
                                      AND p.userid = ue.userid
-                                     AND p.privatereplyto = 0';
+                                     AND p.privatereplyto = 0
+                               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, f.id, u.firstname, u.lastname';
 
-        $this->sql->params = ['courseid' => $this->courseid];
+        $this->sql->params = [
+            'component' => 'mod_forum',
+            'courseid' => $this->courseid,
+        ];
 
         // Handle if a user is limited to viewing their own summary.
         if (!empty($this->userid)) {
index 4d14078..779d8a5 100644 (file)
@@ -22,6 +22,7 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+$string['attachmentcount'] = 'Number of attachments';
 $string['nodetitle'] = 'Summary report';
 $string['pluginname'] = 'Forum summary report';
 $string['postcount'] = 'Number of discussions posted';
diff --git a/mod/forum/report/summary/tests/behat/summary_data_attachments.feature b/mod/forum/report/summary/tests/behat/summary_data_attachments.feature
new file mode 100644 (file)
index 0000000..a715a70
--- /dev/null
@@ -0,0 +1,50 @@
+@mod @mod_forum @forumreport @forumreport_summary
+Feature: Attachments count column data available
+  In order to gather data on users' forum attachments
+  As a teacher
+  I need to view accurate attachment count data in the forum summary report
+
+  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           |
+      | teacher1 | C1     | editingteacher |
+      | student1 | C1     | student        |
+      | student2 | C1     | student        |
+      | teacher1 | C2     | editingteacher |
+    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 | message3 | att4.jpg           | in2.jpg           |
+      | student2 | forum1 | discussion4 | 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 | reply3   | att7.jpg, att8.jpg | in2.jpg           |
+      | student2 | forum1 | discussion4 | reply4   |                    |                   |
+    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 "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 2" row "Number of attachments" column of "forumreport_summary_table" table should contain "0"