2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * The class for displaying the forum report table.
20 * @package forumreport_summary
21 * @copyright 2019 Michael Hawkins <michaelh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace forumreport_summary;
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/tablelib.php');
34 * The class for displaying the forum report table.
36 * @copyright 2019 Michael Hawkins <michaelh@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class summary_table extends table_sql {
41 /** Forum filter type */
42 const FILTER_FORUM = 1;
44 /** Groups filter type */
45 const FILTER_GROUPS = 2;
47 /** Table to store summary data extracted from the log table */
48 const LOG_SUMMARY_TEMP_TABLE = 'forum_report_summary_counts';
50 /** @var \stdClass The various SQL segments that will be combined to form queries to fetch various information. */
53 /** @var int The number of rows to be displayed per page. */
54 protected $perpage = 25;
56 /** @var \stdClass The course module object of the forum being reported on. */
60 * @var int The user ID if only one user's summary will be generated.
61 * This will apply to users without permission to view others' summaries.
66 * @var \core\log\sql_reader|null
68 protected $logreader = null;
73 protected $context = null;
76 * Forum report table constructor.
78 * @param int $courseid The ID of the course the forum(s) exist within.
79 * @param array $filters Report filters in the format 'type' => [values].
80 * @param bool $bulkoperations Is the user allowed to perform bulk operations?
82 public function __construct(int $courseid, array $filters, bool $bulkoperations) {
83 global $USER, $OUTPUT;
85 $forumid = $filters['forums'][0];
87 parent::__construct("summaryreport_{$courseid}_{$forumid}");
89 $this->cm = get_coursemodule_from_instance('forum', $forumid, $courseid);
90 $this->context = \context_module::instance($this->cm->id);
92 // Only show their own summary unless they have permission to view all.
93 if (!has_capability('forumreport/summary:viewall', $this->context)) {
94 $this->userid = $USER->id;
99 if ($bulkoperations) {
100 $mastercheckbox = new \core\output\checkbox_toggleall('summaryreport-table', true, [
101 'id' => 'select-all-users',
102 'name' => 'select-all-users',
103 'label' => get_string('selectall'),
104 'labelclasses' => 'sr-only',
108 $columnheaders['select'] = $OUTPUT->render($mastercheckbox);
112 'fullname' => get_string('fullnameuser'),
113 'postcount' => get_string('postcount', 'forumreport_summary'),
114 'replycount' => get_string('replycount', 'forumreport_summary'),
115 'attachmentcount' => get_string('attachmentcount', 'forumreport_summary'),
118 $this->logreader = $this->get_internal_log_reader();
119 if ($this->logreader) {
120 $columnheaders['viewcount'] = get_string('viewcount', 'forumreport_summary');
123 $columnheaders['earliestpost'] = get_string('earliestpost', 'forumreport_summary');
124 $columnheaders['latestpost'] = get_string('latestpost', 'forumreport_summary');
126 $this->define_columns(array_keys($columnheaders));
127 $this->define_headers(array_values($columnheaders));
130 $this->define_table_configs();
132 // Define the basic SQL data and object format.
133 $this->define_base_sql();
135 // Apply relevant filters.
136 $this->apply_filters($filters);
140 * Provides the string name of each filter type.
142 * @param int $filtertype Type of filter
143 * @return string Name of the filter
145 public function get_filter_name(int $filtertype): string {
147 self::FILTER_FORUM => 'Forum',
148 self::FILTER_GROUPS => 'Groups',
151 return $filternames[$filtertype];
155 * Generate the select column.
157 * @param \stdClass $data
160 public function col_select($data) {
163 $checkbox = new \core\output\checkbox_toggleall('summaryreport-table', false, [
164 'classes' => 'usercheckbox m-1',
165 'id' => 'user' . $data->userid,
166 'name' => 'user' . $data->userid,
168 'label' => get_string('selectitem', 'moodle', fullname($data)),
169 'labelclasses' => 'accesshide',
172 return $OUTPUT->render($checkbox);
176 * Generate the fullname column.
178 * @param \stdClass $data The row data.
179 * @return string User's full name.
181 public function col_fullname($data): string {
182 if ($this->is_downloading()) {
183 return fullname($data);
187 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->cm->course, 'includefullname' => true));
191 * Generate the postcount column.
193 * @param \stdClass $data The row data.
194 * @return int number of discussion posts made by user.
196 public function col_postcount(\stdClass $data): int {
197 return $data->postcount;
201 * Generate the replycount column.
203 * @param \stdClass $data The row data.
204 * @return int number of replies made by user.
206 public function col_replycount(\stdClass $data): int {
207 return $data->replycount;
211 * Generate the attachmentcount column.
213 * @param \stdClass $data The row data.
214 * @return int number of files attached to posts by user.
216 public function col_attachmentcount(\stdClass $data): int {
217 return $data->attachmentcount;
221 * Generate the earliestpost column.
223 * @param \stdClass $data The row data.
224 * @return string Timestamp of user's earliest post, or a dash if no posts exist.
226 public function col_earliestpost(\stdClass $data): string {
229 return empty($data->earliestpost) ? '-' : userdate($data->earliestpost, "", \core_date::get_user_timezone($USER));
233 * Generate the latestpost column.
235 * @param \stdClass $data The row data.
236 * @return string Timestamp of user's most recent post, or a dash if no posts exist.
238 public function col_latestpost(\stdClass $data): string {
241 return empty($data->latestpost) ? '-' : userdate($data->latestpost, "", \core_date::get_user_timezone($USER));
245 * Override the default implementation to set a decent heading level.
249 public function print_nothing_to_display(): void {
252 echo $OUTPUT->heading(get_string('nothingtodisplay'), 4);
256 * Query the db. Store results in the table object for use by build_table.
258 * @param int $pagesize Size of page for paginated displayed table.
259 * @param bool $useinitialsbar Overridden but unused.
262 public function query_db($pagesize, $useinitialsbar = false): void {
265 // Set up pagination if not downloading the whole report.
266 if (!$this->is_downloading()) {
267 $totalsql = $this->get_full_sql(false);
269 // Set up pagination.
270 $totalrows = $DB->count_records_sql($totalsql, $this->sql->params);
271 $this->pagesize($pagesize, $totalrows);
275 $sql = $this->get_full_sql();
277 // Only paginate when not downloading.
278 if (!$this->is_downloading()) {
279 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
281 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
286 * Adds the relevant SQL to apply a filter to the report.
288 * @param int $filtertype Filter type as defined by class constants.
289 * @param array $values Optional array of values passed into the filter type.
291 * @throws coding_exception
293 public function add_filter(int $filtertype, array $values = []): void {
296 $paramcounterror = false;
298 switch($filtertype) {
299 case self::FILTER_FORUM:
300 // Requires exactly one forum ID.
301 if (count($values) != 1) {
302 $paramcounterror = true;
304 // No select fields required - displayed in title.
305 // No extra joins required, forum is already joined.
306 $this->sql->filterwhere .= ' AND f.id = :forumid';
307 $this->sql->params['forumid'] = $values[0];
312 case self::FILTER_GROUPS:
313 // Filter data to only include content within specified groups (and/or no groups).
314 // Additionally, only display users who can post within the selected option(s).
316 // Only filter by groups the user has access to.
317 $groups = $this->get_filter_groups($values);
319 // Skip adding filter if not applied, or all valid options are selected.
320 if (!empty($groups)) {
321 // Posts within selected groups and/or not in any groups (group ID -1) are included.
322 // No user filtering as anyone enrolled can potentially post to unrestricted discussions.
323 if (array_search(-1, $groups) !== false) {
324 list($groupidin, $groupidparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED);
326 $this->sql->filterwhere .= " AND d.groupid {$groupidin}";
327 $this->sql->params += $groupidparams;
330 // Only posts and users within selected groups are included.
331 list($groupusersin, $groupusersparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED);
332 list($groupidin, $groupidparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED);
334 // No joins required (handled by where to prevent data duplication).
335 $this->sql->filterwhere .= "
338 FROM {groups_members} gm
339 WHERE gm.userid = u.id
340 AND gm.groupid {$groupusersin}
343 AND d.groupid {$groupidin}";
344 $this->sql->params += $groupusersparams + $groupidparams;
351 throw new coding_exception("Report filter type '{$filtertype}' not found.");
355 if ($paramcounterror) {
356 $filtername = $this->get_filter_name($filtertype);
357 throw new coding_exception("An invalid number of values have been passed for the '{$filtername}' filter.");
362 * Define various table config options.
366 protected function define_table_configs(): void {
367 $this->collapsible(false);
368 $this->sortable(true, 'firstname', SORT_ASC);
369 $this->pageable(true);
370 $this->is_downloadable(true);
371 $this->no_sorting('select');
372 $this->set_attribute('id', 'forumreport_summary_table');
376 * Define the object to store all for the table SQL and initialises the base SQL required.
380 protected function define_base_sql(): void {
381 $this->sql = new \stdClass();
383 $userfields = get_extra_user_fields($this->context);
384 $userfieldssql = \user_picture::fields('u', $userfields);
386 // Define base SQL query format.
387 // Ignores private replies as they are not visible to all participants.
388 $this->sql->basefields = ' ue.userid AS userid,
389 e.courseid AS courseid,
391 SUM(CASE WHEN p.parent = 0 THEN 1 ELSE 0 END) AS postcount,
392 SUM(CASE WHEN p.parent != 0 THEN 1 ELSE 0 END) AS replycount,
393 ' . $userfieldssql . ',
394 SUM(CASE WHEN att.attcount IS NULL THEN 0 ELSE att.attcount END) AS attachmentcount,
395 MIN(p.created) AS earliestpost,
396 MAX(p.created) AS latestpost';
398 $this->sql->basefromjoins = ' {enrol} e
399 JOIN {user_enrolments} ue ON ue.enrolid = e.id
400 JOIN {user} u ON u.id = ue.userid
401 JOIN {forum} f ON f.course = e.courseid
402 JOIN {forum_discussions} d ON d.forum = f.id
403 LEFT JOIN {forum_posts} p ON p.discussion = d.id
404 AND p.userid = ue.userid
405 AND p.privatereplyto = 0
407 SELECT COUNT(fi.id) AS attcount, fi.itemid AS postid, fi.userid
409 WHERE fi.component = :component
411 GROUP BY fi.itemid, fi.userid
412 ) att ON att.postid = p.id
413 AND att.userid = ue.userid';
415 $this->sql->basewhere = 'e.courseid = :courseid';
417 $this->sql->basegroupby = 'ue.userid, e.courseid, f.id, u.id, ' . $userfieldssql;
419 if ($this->logreader) {
420 $this->fill_log_summary_temp_table($this->context->id);
422 $this->sql->basefields .= ', CASE WHEN tmp.viewcount IS NOT NULL THEN tmp.viewcount ELSE 0 END AS viewcount';
423 $this->sql->basefromjoins .= ' LEFT JOIN {' . self::LOG_SUMMARY_TEMP_TABLE . '} tmp ON tmp.userid = u.id';
424 $this->sql->basegroupby .= ', tmp.viewcount';
427 $this->sql->params = [
428 'component' => 'mod_forum',
429 'courseid' => $this->cm->course,
432 // Handle if a user is limited to viewing their own summary.
433 if (!empty($this->userid)) {
434 $this->sql->basewhere .= ' AND ue.userid = :userid';
435 $this->sql->params['userid'] = $this->userid;
438 // Filter values will be populated separately where required.
439 $this->sql->filterfields = '';
440 $this->sql->filterfromjoins = '';
441 $this->sql->filterwhere = '';
442 $this->sql->filtergroupby = '';
446 * Overriding the parent method because it should not be used here.
447 * Filters are applied, so the structure of $this->sql is now different to the way this is set up in the parent.
449 * @param string $fields Unused.
450 * @param string $from Unused.
451 * @param string $where Unused.
452 * @param array $params Unused.
455 * @throws coding_exception
457 public function set_sql($fields, $from, $where, array $params = []) {
458 throw new coding_exception('The set_sql method should not be used by the summary_table class.');
462 * Convenience method to call a number of methods for you to display the table.
463 * Overrides the parent so SQL for filters is handled.
465 * @param int $pagesize Number of rows to fetch.
466 * @param bool $useinitialsbar Whether to include the initials bar with the table.
467 * @param string $downloadhelpbutton Unused.
471 public function out($pagesize, $useinitialsbar, $downloadhelpbutton = ''): void {
474 if (!$this->columns) {
475 $sql = $this->get_full_sql();
477 $onerow = $DB->get_record_sql($sql, $this->sql->params, IGNORE_MULTIPLE);
479 // If columns is not set, define columns as the keys of the rows returned from the db.
480 $this->define_columns(array_keys((array)$onerow));
481 $this->define_headers(array_keys((array)$onerow));
485 $this->query_db($pagesize, $useinitialsbar);
486 $this->build_table();
487 $this->close_recordset();
488 $this->finish_output();
492 * Apply the relevant filters to the report.
494 * @param array $filters Report filters in the format 'type' => [values].
497 protected function apply_filters(array $filters): void {
498 // Apply the forums filter.
499 $this->add_filter(self::FILTER_FORUM, $filters['forums']);
501 // Apply groups filter.
502 $this->add_filter(self::FILTER_GROUPS, $filters['groups']);
506 * Prepares a complete SQL statement from the base query and any filters defined.
508 * @param bool $fullselect Whether to select all relevant columns.
509 * False selects a count only (used to calculate pagination).
510 * @return string The complete SQL statement.
512 protected function get_full_sql(bool $fullselect = true): string {
517 $selectfields = "{$this->sql->basefields}
518 {$this->sql->filterfields}";
520 $groupby = ' GROUP BY ' . $this->sql->basegroupby . $this->sql->filtergroupby;
522 if (($sort = $this->get_sql_sort())) {
523 $orderby = " ORDER BY {$sort}";
526 $selectfields = 'COUNT(DISTINCT(ue.userid))';
529 $sql = "SELECT {$selectfields}
530 FROM {$this->sql->basefromjoins}
531 {$this->sql->filterfromjoins}
532 WHERE {$this->sql->basewhere}
533 {$this->sql->filterwhere}
541 * Returns an internal and enabled log reader.
543 * @return \core\log\sql_reader|false
545 protected function get_internal_log_reader(): ?\core\log\sql_reader {
548 $readers = get_log_manager()->get_readers('core\log\sql_reader');
549 foreach ($readers as $reader) {
551 // If reader is not a sql_internal_table_reader and not legacy store then return.
552 if (!($reader instanceof \core\log\sql_internal_table_reader) && !($reader instanceof logstore_legacy\log\store)) {
555 $logreader = $reader;
558 if (empty($logreader)) {
566 * Fills the log summary temp table.
568 * @param int $contextid
571 protected function fill_log_summary_temp_table(int $contextid) {
574 $this->create_log_summary_temp_table();
576 if ($this->logreader instanceof logstore_legacy\log\store) {
578 // Anonymous actions are never logged in legacy log.
581 $logtable = $this->logreader->get_internal_log_table_name();
582 $nonanonymous = 'AND anonymous = 0';
584 $params = ['contextid' => $contextid];
585 $sql = "INSERT INTO {" . self::LOG_SUMMARY_TEMP_TABLE . "} (userid, viewcount)
586 SELECT userid, COUNT(*) AS viewcount
587 FROM {" . $logtable . "}
588 WHERE contextid = :contextid
591 $DB->execute($sql, $params);
595 * Creates a temp table to store summary data from the log table for this request.
599 protected function create_log_summary_temp_table() {
602 $dbman = $DB->get_manager();
603 $temptablename = self::LOG_SUMMARY_TEMP_TABLE;
604 $xmldbtable = new \xmldb_table($temptablename);
605 $xmldbtable->add_field('userid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null);
606 $xmldbtable->add_field('viewcount', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null);
607 $xmldbtable->add_key('primary', XMLDB_KEY_PRIMARY, array('userid'));
609 $dbman->create_temp_table($xmldbtable);
613 * Get the final list of groups to filter by, based on the groups submitted,
614 * and those the user has access to.
617 * @param array $groups The group IDs submitted.
618 * @return array Group objects of groups to use in groups filter.
619 * If no filtering required (all groups selected), returns [].
621 protected function get_filter_groups(array $groups): array {
624 $groupmode = groups_get_activity_groupmode($this->cm);
625 $aag = has_capability('moodle/site:accessallgroups', $this->context);
629 // Filtering only valid if a forum groups mode is enabled.
630 if (in_array($groupmode, [VISIBLEGROUPS, SEPARATEGROUPS])) {
631 $allgroupsobj = groups_get_all_groups($this->cm->course, 0, $this->cm->groupingid);
634 foreach ($allgroupsobj as $group) {
635 $allgroups[] = $group->id;
638 if ($groupmode == VISIBLEGROUPS || $aag) {
639 $nogroups = new \stdClass();
641 $nogroups->name = get_string('groupsnone');
643 // Any groups and no groups.
644 $allowedgroupsobj = $allgroupsobj + [$nogroups];
646 // Only assigned groups.
647 $allowedgroupsobj = groups_get_all_groups($this->cm->course, $USER->id, $this->cm->groupingid);
650 foreach ($allowedgroupsobj as $group) {
651 $allowedgroups[] = $group->id;
654 // If not all groups in course are selected, filter by allowed groups submitted.
655 if (!empty($groups) && !empty(array_diff($allowedgroups, $groups))) {
656 $filtergroups = array_intersect($groups, $allowedgroups);
657 } else if (!empty(array_diff($allgroups, $allowedgroups))) {
658 // If user's 'all groups' is a subset of the course groups, filter by all groups available to them.
659 $filtergroups = $allowedgroups;
663 return $filtergroups;
667 * Download the summary report in the selected format.
669 * @param string $format The format to download the report.
671 public function download($format) {
672 $filename = 'summary_report_' . userdate(time(), get_string('backupnameformat', 'langconfig'),
675 $this->is_downloading($format, $filename);
676 $this->out($this->perpage, false);