MDL-66594 forumreport_summary: Added first/last post date columns
[moodle.git] / mod / forum / report / summary / classes / summary_table.php
index bef374a..c59f3e2 100644 (file)
@@ -84,6 +84,9 @@ 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'),
+            'earliestpost' => get_string('earliestpost', 'forumreport_summary'),
+            'latestpost' => get_string('latestpost', 'forumreport_summary'),
         ];
 
         $this->define_columns(array_keys($columnheaders));
@@ -145,6 +148,40 @@ 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;
+    }
+
+    /**
+     * Generate the earliestpost column.
+     *
+     * @param \stdClass $data The row data.
+     * @return string Timestamp of user's earliest post, or a dash if no posts exist.
+     */
+    public function col_earliestpost(\stdClass $data): string {
+        global $USER;
+
+        return empty($data->earliestpost) ? '-' : userdate($data->earliestpost, "", \core_date::get_user_timezone($USER));
+    }
+
+    /**
+     * Generate the latestpost column.
+     *
+     * @param \stdClass $data The row data.
+     * @return string Timestamp of user's most recent post, or a dash if no posts exist.
+     */
+    public function col_latestpost(\stdClass $data): string {
+        global $USER;
+
+        return empty($data->latestpost) ? '-' : userdate($data->latestpost, "", \core_date::get_user_timezone($USER));
+    }
+
     /**
      * Override the default implementation to set a decent heading level.
      *
@@ -232,6 +269,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 +288,10 @@ 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,
+                                    MIN(p.created) AS earliestpost,
+                                    MAX(p.created) AS latestpost';
 
         $this->sql->basefromjoins = '    {enrol} e
                                     JOIN {user_enrolments} ue ON ue.enrolid = e.id
@@ -259,13 +300,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)) {