MDL-66372 forumreport_summary: Tidy up per page and bulk ops handling
[moodle.git] / mod / forum / report / summary / classes / summary_table.php
CommitLineData
6388b657
MH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * The class for displaying the forum report table.
19 *
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
23 */
24
25namespace forumreport_summary;
26defined('MOODLE_INTERNAL') || die();
27
28require_once($CFG->libdir . '/tablelib.php');
29
30use coding_exception;
31use table_sql;
32
33/**
34 * The class for displaying the forum report table.
35 *
36 * @copyright 2019 Michael Hawkins <michaelh@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class summary_table extends table_sql {
40
41 /** Forum filter type */
42 const FILTER_FORUM = 1;
43
b29de56d
MH
44 /** Groups filter type */
45 const FILTER_GROUPS = 2;
46
a9496531
MH
47 /** Dates filter type */
48 const FILTER_DATES = 3;
49
235a6db7
DM
50 /** Table to store summary data extracted from the log table */
51 const LOG_SUMMARY_TEMP_TABLE = 'forum_report_summary_counts';
52
cf8393be
MH
53 /** Default number of rows to display per page */
54 const DEFAULT_PER_PAGE = 50;
55
6388b657
MH
56 /** @var \stdClass The various SQL segments that will be combined to form queries to fetch various information. */
57 public $sql;
58
59 /** @var int The number of rows to be displayed per page. */
cf8393be 60 protected $perpage = self::DEFAULT_PER_PAGE;
20b064be
MH
61
62 /** @var array The values available for pagination size per page. */
63 protected $perpageoptions = [50, 100, 200];
6388b657 64
b29de56d
MH
65 /** @var \stdClass The course module object of the forum being reported on. */
66 protected $cm;
67
6388b657
MH
68 /**
69 * @var int The user ID if only one user's summary will be generated.
70 * This will apply to users without permission to view others' summaries.
71 */
72 protected $userid;
73
235a6db7
DM
74 /**
75 * @var \core\log\sql_reader|null
76 */
77 protected $logreader = null;
78
79 /**
80 * @var \context|null
81 */
82 protected $context = null;
83
20b064be 84 /** @var bool Whether the user has the capability/capabilities to perform bulk operations. */
cf8393be 85 protected $allowbulkoperations = false;
20b064be 86
74f94dfc
DM
87 /**
88 * @var bool
89 */
90 private $showwordcharcounts = null;
91
354cbf48
SR
92 /**
93 * @var bool Whether the user can see all private replies or not.
94 */
95 protected $canseeprivatereplies;
96
6388b657
MH
97 /**
98 * Forum report table constructor.
99 *
100 * @param int $courseid The ID of the course the forum(s) exist within.
b29de56d 101 * @param array $filters Report filters in the format 'type' => [values].
cf8393be 102 * @param bool $allowbulkoperations Is the user allowed to perform bulk operations?
354cbf48 103 * @param bool $canseeprivatereplies Whether the user can see all private replies or not.
20b064be 104 * @param int $perpage The number of rows to display per page.
6388b657 105 */
cf8393be 106 public function __construct(int $courseid, array $filters, bool $allowbulkoperations, bool $canseeprivatereplies, int $perpage) {
0942e58c 107 global $USER, $OUTPUT;
6388b657 108
b29de56d
MH
109 $forumid = $filters['forums'][0];
110
6388b657
MH
111 parent::__construct("summaryreport_{$courseid}_{$forumid}");
112
b29de56d 113 $this->cm = get_coursemodule_from_instance('forum', $forumid, $courseid);
235a6db7 114 $this->context = \context_module::instance($this->cm->id);
cf8393be 115 $this->allowbulkoperations = $allowbulkoperations;
354cbf48 116 $this->canseeprivatereplies = $canseeprivatereplies;
20b064be 117 $this->perpage = $perpage;
6388b657
MH
118
119 // Only show their own summary unless they have permission to view all.
235a6db7 120 if (!has_capability('forumreport/summary:viewall', $this->context)) {
6388b657
MH
121 $this->userid = $USER->id;
122 }
123
0942e58c
SR
124 $columnheaders = [];
125
cf8393be 126 if ($allowbulkoperations) {
0942e58c
SR
127 $mastercheckbox = new \core\output\checkbox_toggleall('summaryreport-table', true, [
128 'id' => 'select-all-users',
129 'name' => 'select-all-users',
130 'label' => get_string('selectall'),
131 'labelclasses' => 'sr-only',
132 'classes' => 'm-1',
133 'checked' => false
134 ]);
135 $columnheaders['select'] = $OUTPUT->render($mastercheckbox);
136 }
137
138 $columnheaders += [
6388b657
MH
139 'fullname' => get_string('fullnameuser'),
140 'postcount' => get_string('postcount', 'forumreport_summary'),
141 'replycount' => get_string('replycount', 'forumreport_summary'),
d0e125cf 142 'attachmentcount' => get_string('attachmentcount', 'forumreport_summary'),
6388b657
MH
143 ];
144
b9669dd3
MH
145 $this->logreader = $this->get_internal_log_reader();
146 if ($this->logreader) {
147 $columnheaders['viewcount'] = get_string('viewcount', 'forumreport_summary');
235a6db7
DM
148 }
149
74f94dfc
DM
150 if ($this->show_word_char_counts()) {
151 $columnheaders['wordcount'] = get_string('wordcount', 'forumreport_summary');
152 $columnheaders['charcount'] = get_string('charcount', 'forumreport_summary');
153 }
154
235a6db7
DM
155 $columnheaders['earliestpost'] = get_string('earliestpost', 'forumreport_summary');
156 $columnheaders['latestpost'] = get_string('latestpost', 'forumreport_summary');
157
6388b657
MH
158 $this->define_columns(array_keys($columnheaders));
159 $this->define_headers(array_values($columnheaders));
160
161 // Define configs.
162 $this->define_table_configs();
163
b29de56d 164 // Apply relevant filters.
a9496531 165 $this->define_base_filter_sql();
b29de56d 166 $this->apply_filters($filters);
a9496531
MH
167
168 // Define the basic SQL data and object format.
169 $this->define_base_sql();
6388b657
MH
170 }
171
172 /**
a9496531
MH
173 * Provides the string name of each filter type, to be used by errors.
174 * Note: This does not use language strings as the value is injected into error strings.
6388b657
MH
175 *
176 * @param int $filtertype Type of filter
177 * @return string Name of the filter
178 */
a9496531 179 protected function get_filter_name(int $filtertype): string {
6388b657
MH
180 $filternames = [
181 self::FILTER_FORUM => 'Forum',
b29de56d 182 self::FILTER_GROUPS => 'Groups',
a9496531 183 self::FILTER_DATES => 'Dates',
6388b657
MH
184 ];
185
186 return $filternames[$filtertype];
187 }
188
0942e58c
SR
189 /**
190 * Generate the select column.
191 *
192 * @param \stdClass $data
193 * @return string
194 */
195 public function col_select($data) {
196 global $OUTPUT;
197
198 $checkbox = new \core\output\checkbox_toggleall('summaryreport-table', false, [
199 'classes' => 'usercheckbox m-1',
200 'id' => 'user' . $data->userid,
201 'name' => 'user' . $data->userid,
202 'checked' => false,
203 'label' => get_string('selectitem', 'moodle', fullname($data)),
204 'labelclasses' => 'accesshide',
205 ]);
206
207 return $OUTPUT->render($checkbox);
208 }
209
6388b657
MH
210 /**
211 * Generate the fullname column.
212 *
213 * @param \stdClass $data The row data.
214 * @return string User's full name.
215 */
216 public function col_fullname($data): string {
850ce974
SL
217 if ($this->is_downloading()) {
218 return fullname($data);
219 }
6388b657 220
850ce974 221 global $OUTPUT;
806d6343 222 return $OUTPUT->user_picture($data, array('size' => 35, 'courseid' => $this->cm->course, 'includefullname' => true));
6388b657
MH
223 }
224
225 /**
226 * Generate the postcount column.
227 *
228 * @param \stdClass $data The row data.
229 * @return int number of discussion posts made by user.
230 */
231 public function col_postcount(\stdClass $data): int {
232 return $data->postcount;
233 }
234
235 /**
236 * Generate the replycount column.
237 *
238 * @param \stdClass $data The row data.
239 * @return int number of replies made by user.
240 */
241 public function col_replycount(\stdClass $data): int {
242 return $data->replycount;
243 }
244
d0e125cf
MH
245 /**
246 * Generate the attachmentcount column.
247 *
248 * @param \stdClass $data The row data.
249 * @return int number of files attached to posts by user.
250 */
251 public function col_attachmentcount(\stdClass $data): int {
252 return $data->attachmentcount;
253 }
254
382168a0
MH
255 /**
256 * Generate the earliestpost column.
257 *
258 * @param \stdClass $data The row data.
259 * @return string Timestamp of user's earliest post, or a dash if no posts exist.
260 */
261 public function col_earliestpost(\stdClass $data): string {
262 global $USER;
263
264 return empty($data->earliestpost) ? '-' : userdate($data->earliestpost, "", \core_date::get_user_timezone($USER));
265 }
266
267 /**
268 * Generate the latestpost column.
269 *
270 * @param \stdClass $data The row data.
271 * @return string Timestamp of user's most recent post, or a dash if no posts exist.
272 */
273 public function col_latestpost(\stdClass $data): string {
274 global $USER;
275
276 return empty($data->latestpost) ? '-' : userdate($data->latestpost, "", \core_date::get_user_timezone($USER));
277 }
278
6388b657
MH
279 /**
280 * Override the default implementation to set a decent heading level.
281 *
282 * @return void.
283 */
284 public function print_nothing_to_display(): void {
285 global $OUTPUT;
286
287 echo $OUTPUT->heading(get_string('nothingtodisplay'), 4);
288 }
289
290 /**
291 * Query the db. Store results in the table object for use by build_table.
292 *
293 * @param int $pagesize Size of page for paginated displayed table.
294 * @param bool $useinitialsbar Overridden but unused.
295 * @return void
296 */
297 public function query_db($pagesize, $useinitialsbar = false): void {
298 global $DB;
299
300 // Set up pagination if not downloading the whole report.
301 if (!$this->is_downloading()) {
302 $totalsql = $this->get_full_sql(false);
303
304 // Set up pagination.
305 $totalrows = $DB->count_records_sql($totalsql, $this->sql->params);
306 $this->pagesize($pagesize, $totalrows);
307 }
308
309 // Fetch the data.
310 $sql = $this->get_full_sql();
311
312 // Only paginate when not downloading.
313 if (!$this->is_downloading()) {
314 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
315 } else {
316 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
317 }
318 }
319
320 /**
321 * Adds the relevant SQL to apply a filter to the report.
322 *
323 * @param int $filtertype Filter type as defined by class constants.
324 * @param array $values Optional array of values passed into the filter type.
325 * @return void
326 * @throws coding_exception
327 */
328 public function add_filter(int $filtertype, array $values = []): void {
b29de56d
MH
329 global $DB;
330
6388b657
MH
331 $paramcounterror = false;
332
333 switch($filtertype) {
334 case self::FILTER_FORUM:
335 // Requires exactly one forum ID.
336 if (count($values) != 1) {
337 $paramcounterror = true;
338 } else {
339 // No select fields required - displayed in title.
340 // No extra joins required, forum is already joined.
341 $this->sql->filterwhere .= ' AND f.id = :forumid';
342 $this->sql->params['forumid'] = $values[0];
343 }
344
345 break;
346
b29de56d 347 case self::FILTER_GROUPS:
5e43937c
MH
348 // Filter data to only include content within specified groups (and/or no groups).
349 // Additionally, only display users who can post within the selected option(s).
b29de56d 350
2970b8fa
MH
351 // Only filter by groups the user has access to.
352 $groups = $this->get_filter_groups($values);
b29de56d 353
2970b8fa
MH
354 // Skip adding filter if not applied, or all valid options are selected.
355 if (!empty($groups)) {
5e43937c
MH
356 // Posts within selected groups and/or not in any groups (group ID -1) are included.
357 // No user filtering as anyone enrolled can potentially post to unrestricted discussions.
2970b8fa
MH
358 if (array_search(-1, $groups) !== false) {
359 list($groupidin, $groupidparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED);
b29de56d 360
5e43937c 361 $this->sql->filterwhere .= " AND d.groupid {$groupidin}";
b29de56d 362 $this->sql->params += $groupidparams;
b29de56d 363
2970b8fa 364 } else {
5e43937c 365 // Only posts and users within selected groups are included.
2970b8fa
MH
366 list($groupusersin, $groupusersparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED);
367 list($groupidin, $groupidparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED);
b29de56d 368
b29de56d
MH
369 // No joins required (handled by where to prevent data duplication).
370 $this->sql->filterwhere .= "
a575ab15
MH
371 AND u.id IN (
372 SELECT gm.userid
373 FROM {groups_members} gm
374 WHERE gm.groupid {$groupusersin}
375 )
5e43937c
MH
376 AND d.groupid {$groupidin}";
377 $this->sql->params += $groupusersparams + $groupidparams;
b29de56d
MH
378 }
379 }
380
381 break;
382
a9496531
MH
383 case self::FILTER_DATES:
384 if (!isset($values['from']['enabled']) || !isset($values['to']['enabled']) ||
385 ($values['from']['enabled'] && !isset($values['from']['timestamp'])) ||
386 ($values['to']['enabled'] && !isset($values['to']['timestamp']))) {
387 $paramcounterror = true;
388 } else {
389 $this->sql->filterbase['dates'] = '';
390 $this->sql->filterbase['dateslog'] = '';
391 $this->sql->filterbase['dateslogparams'] = [];
392
393 // From date.
394 if ($values['from']['enabled']) {
395 // If the filter was enabled, include the date restriction.
396 // Needs to form part of the base join to posts, so will be injected by define_base_sql().
397 $this->sql->filterbase['dates'] .= " AND p.created >= :fromdate";
398 $this->sql->params['fromdate'] = $values['from']['timestamp'];
399 $this->sql->filterbase['dateslog'] .= ' AND timecreated >= :fromdate';
400 $this->sql->filterbase['dateslogparams']['fromdate'] = $values['from']['timestamp'];
401 }
402
403 // To date.
404 if ($values['to']['enabled']) {
405 // If the filter was enabled, include the date restriction.
406 // Needs to form part of the base join to posts, so will be injected by define_base_sql().
407 $this->sql->filterbase['dates'] .= " AND p.created <= :todate";
408 $this->sql->params['todate'] = $values['to']['timestamp'];
409 $this->sql->filterbase['dateslog'] .= ' AND timecreated <= :todate';
410 $this->sql->filterbase['dateslogparams']['todate'] = $values['to']['timestamp'];
411 }
412 }
413
414 break;
6388b657
MH
415 default:
416 throw new coding_exception("Report filter type '{$filtertype}' not found.");
417 break;
418 }
419
420 if ($paramcounterror) {
421 $filtername = $this->get_filter_name($filtertype);
422 throw new coding_exception("An invalid number of values have been passed for the '{$filtername}' filter.");
423 }
424 }
425
426 /**
427 * Define various table config options.
428 *
429 * @return void.
430 */
431 protected function define_table_configs(): void {
432 $this->collapsible(false);
433 $this->sortable(true, 'firstname', SORT_ASC);
434 $this->pageable(true);
850ce974 435 $this->is_downloadable(true);
6388b657 436 $this->no_sorting('select');
d0e125cf 437 $this->set_attribute('id', 'forumreport_summary_table');
a9496531
MH
438 $this->sql = new \stdClass();
439 $this->sql->params = [];
6388b657
MH
440 }
441
442 /**
443 * Define the object to store all for the table SQL and initialises the base SQL required.
444 *
445 * @return void.
446 */
447 protected function define_base_sql(): void {
354cbf48
SR
448 global $USER;
449
806d6343
SR
450 $userfields = get_extra_user_fields($this->context);
451 $userfieldssql = \user_picture::fields('u', $userfields);
452
6388b657 453 // Define base SQL query format.
6388b657 454 $this->sql->basefields = ' ue.userid AS userid,
806d6343
SR
455 e.courseid AS courseid,
456 f.id as forumid,
457 SUM(CASE WHEN p.parent = 0 THEN 1 ELSE 0 END) AS postcount,
458 SUM(CASE WHEN p.parent != 0 THEN 1 ELSE 0 END) AS replycount,
459 ' . $userfieldssql . ',
460 SUM(CASE WHEN att.attcount IS NULL THEN 0 ELSE att.attcount END) AS attachmentcount,
461 MIN(p.created) AS earliestpost,
462 MAX(p.created) AS latestpost';
6388b657 463
354cbf48
SR
464 // Handle private replies.
465 $privaterepliessql = '';
466 $privaterepliesparams = [];
467 if (!$this->canseeprivatereplies) {
468 $privaterepliessql = ' AND (p.privatereplyto = :privatereplyto
469 OR p.userid = :privatereplyfrom
470 OR p.privatereplyto = 0)';
471 $privaterepliesparams['privatereplyto'] = $USER->id;
472 $privaterepliesparams['privatereplyfrom'] = $USER->id;
473 }
474
6388b657
MH
475 $this->sql->basefromjoins = ' {enrol} e
476 JOIN {user_enrolments} ue ON ue.enrolid = e.id
477 JOIN {user} u ON u.id = ue.userid
478 JOIN {forum} f ON f.course = e.courseid
479 JOIN {forum_discussions} d ON d.forum = f.id
480 LEFT JOIN {forum_posts} p ON p.discussion = d.id
481 AND p.userid = ue.userid
a9496531
MH
482 ' . $privaterepliessql
483 . $this->sql->filterbase['dates'] . '
d0e125cf
MH
484 LEFT JOIN (
485 SELECT COUNT(fi.id) AS attcount, fi.itemid AS postid, fi.userid
486 FROM {files} fi
487 WHERE fi.component = :component
488 AND fi.filesize > 0
489 GROUP BY fi.itemid, fi.userid
490 ) att ON att.postid = p.id
491 AND att.userid = ue.userid';
6388b657
MH
492
493 $this->sql->basewhere = 'e.courseid = :courseid';
494
9523f0e0 495 $this->sql->basegroupby = 'ue.userid, e.courseid, f.id, u.id, ' . $userfieldssql;
6388b657 496
235a6db7
DM
497 if ($this->logreader) {
498 $this->fill_log_summary_temp_table($this->context->id);
499
500 $this->sql->basefields .= ', CASE WHEN tmp.viewcount IS NOT NULL THEN tmp.viewcount ELSE 0 END AS viewcount';
74f94dfc 501 $this->sql->basefromjoins .= ' LEFT JOIN {' . self::LOG_SUMMARY_TEMP_TABLE . '} tmp ON tmp.userid = u.id ';
235a6db7
DM
502 $this->sql->basegroupby .= ', tmp.viewcount';
503 }
504
74f94dfc
DM
505 if ($this->show_word_char_counts()) {
506 // All p.wordcount values should be NOT NULL, this CASE WHEN is an extra just-in-case.
507 $this->sql->basefields .= ', SUM(CASE WHEN p.wordcount IS NOT NULL THEN p.wordcount ELSE 0 END) AS wordcount';
508 $this->sql->basefields .= ', SUM(CASE WHEN p.charcount IS NOT NULL THEN p.charcount ELSE 0 END) AS charcount';
509 }
510
a9496531 511 $this->sql->params += [
d0e125cf 512 'component' => 'mod_forum',
14f92164 513 'courseid' => $this->cm->course,
354cbf48 514 ] + $privaterepliesparams;
6388b657
MH
515
516 // Handle if a user is limited to viewing their own summary.
517 if (!empty($this->userid)) {
518 $this->sql->basewhere .= ' AND ue.userid = :userid';
519 $this->sql->params['userid'] = $this->userid;
520 }
a9496531 521 }
6388b657 522
a9496531
MH
523 /**
524 * Instantiate the properties to store filter values.
525 *
526 * @return void.
527 */
528 protected function define_base_filter_sql(): void {
6388b657
MH
529 // Filter values will be populated separately where required.
530 $this->sql->filterfields = '';
531 $this->sql->filterfromjoins = '';
532 $this->sql->filterwhere = '';
533 $this->sql->filtergroupby = '';
534 }
535
536 /**
537 * Overriding the parent method because it should not be used here.
538 * Filters are applied, so the structure of $this->sql is now different to the way this is set up in the parent.
539 *
540 * @param string $fields Unused.
541 * @param string $from Unused.
542 * @param string $where Unused.
543 * @param array $params Unused.
544 * @return void.
545 *
546 * @throws coding_exception
547 */
548 public function set_sql($fields, $from, $where, array $params = []) {
549 throw new coding_exception('The set_sql method should not be used by the summary_table class.');
550 }
551
552 /**
553 * Convenience method to call a number of methods for you to display the table.
554 * Overrides the parent so SQL for filters is handled.
555 *
556 * @param int $pagesize Number of rows to fetch.
557 * @param bool $useinitialsbar Whether to include the initials bar with the table.
558 * @param string $downloadhelpbutton Unused.
559 *
560 * @return void.
561 */
562 public function out($pagesize, $useinitialsbar, $downloadhelpbutton = ''): void {
563 global $DB;
564
565 if (!$this->columns) {
566 $sql = $this->get_full_sql();
567
568 $onerow = $DB->get_record_sql($sql, $this->sql->params, IGNORE_MULTIPLE);
569
570 // If columns is not set, define columns as the keys of the rows returned from the db.
571 $this->define_columns(array_keys((array)$onerow));
572 $this->define_headers(array_keys((array)$onerow));
573 }
574
575 $this->setup();
576 $this->query_db($pagesize, $useinitialsbar);
577 $this->build_table();
578 $this->close_recordset();
579 $this->finish_output();
580 }
581
b29de56d
MH
582 /**
583 * Apply the relevant filters to the report.
584 *
585 * @param array $filters Report filters in the format 'type' => [values].
586 * @return void.
587 */
588 protected function apply_filters(array $filters): void {
589 // Apply the forums filter.
590 $this->add_filter(self::FILTER_FORUM, $filters['forums']);
591
592 // Apply groups filter.
593 $this->add_filter(self::FILTER_GROUPS, $filters['groups']);
a9496531
MH
594
595 // Apply dates filter.
596 $datevalues = [
597 'from' => $filters['datefrom'],
598 'to' => $filters['dateto'],
599 ];
600 $this->add_filter(self::FILTER_DATES, $datevalues);
b29de56d
MH
601 }
602
6388b657
MH
603 /**
604 * Prepares a complete SQL statement from the base query and any filters defined.
605 *
606 * @param bool $fullselect Whether to select all relevant columns.
607 * False selects a count only (used to calculate pagination).
608 * @return string The complete SQL statement.
609 */
610 protected function get_full_sql(bool $fullselect = true): string {
6388b657
MH
611 $groupby = '';
612 $orderby = '';
613
614 if ($fullselect) {
615 $selectfields = "{$this->sql->basefields}
616 {$this->sql->filterfields}";
617
618 $groupby = ' GROUP BY ' . $this->sql->basegroupby . $this->sql->filtergroupby;
619
620 if (($sort = $this->get_sql_sort())) {
621 $orderby = " ORDER BY {$sort}";
622 }
623 } else {
624 $selectfields = 'COUNT(DISTINCT(ue.userid))';
625 }
626
627 $sql = "SELECT {$selectfields}
628 FROM {$this->sql->basefromjoins}
629 {$this->sql->filterfromjoins}
630 WHERE {$this->sql->basewhere}
631 {$this->sql->filterwhere}
632 {$groupby}
633 {$orderby}";
634
635 return $sql;
636 }
235a6db7
DM
637
638 /**
639 * Returns an internal and enabled log reader.
640 *
641 * @return \core\log\sql_reader|false
642 */
643 protected function get_internal_log_reader(): ?\core\log\sql_reader {
644 global $DB;
645
646 $readers = get_log_manager()->get_readers('core\log\sql_reader');
647 foreach ($readers as $reader) {
648
649 // If reader is not a sql_internal_table_reader and not legacy store then return.
650 if (!($reader instanceof \core\log\sql_internal_table_reader) && !($reader instanceof logstore_legacy\log\store)) {
651 continue;
652 }
653 $logreader = $reader;
654 }
655
656 if (empty($logreader)) {
657 return null;
658 }
659
660 return $logreader;
661 }
662
663 /**
664 * Fills the log summary temp table.
665 *
666 * @param int $contextid
667 * @return null
668 */
669 protected function fill_log_summary_temp_table(int $contextid) {
670 global $DB;
671
672 $this->create_log_summary_temp_table();
673
674 if ($this->logreader instanceof logstore_legacy\log\store) {
675 $logtable = 'log';
676 // Anonymous actions are never logged in legacy log.
677 $nonanonymous = '';
678 } else {
679 $logtable = $this->logreader->get_internal_log_table_name();
680 $nonanonymous = 'AND anonymous = 0';
681 }
a9496531
MH
682
683 // Apply dates filter if applied.
684 $datewhere = $this->sql->filterbase['dateslog'] ?? '';
685 $dateparams = $this->sql->filterbase['dateslogparams'] ?? [];
686
687 $params = ['contextid' => $contextid] + $dateparams;
235a6db7
DM
688 $sql = "INSERT INTO {" . self::LOG_SUMMARY_TEMP_TABLE . "} (userid, viewcount)
689 SELECT userid, COUNT(*) AS viewcount
690 FROM {" . $logtable . "}
691 WHERE contextid = :contextid
a9496531 692 $datewhere
235a6db7
DM
693 $nonanonymous
694 GROUP BY userid";
695 $DB->execute($sql, $params);
696 }
697
698 /**
699 * Creates a temp table to store summary data from the log table for this request.
700 *
701 * @return null
702 */
703 protected function create_log_summary_temp_table() {
704 global $DB;
705
706 $dbman = $DB->get_manager();
707 $temptablename = self::LOG_SUMMARY_TEMP_TABLE;
708 $xmldbtable = new \xmldb_table($temptablename);
709 $xmldbtable->add_field('userid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null);
710 $xmldbtable->add_field('viewcount', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null);
711 $xmldbtable->add_key('primary', XMLDB_KEY_PRIMARY, array('userid'));
712
713 $dbman->create_temp_table($xmldbtable);
714 }
9eebd58a
MH
715
716 /**
2970b8fa
MH
717 * Get the final list of groups to filter by, based on the groups submitted,
718 * and those the user has access to.
719 *
9eebd58a 720 *
2970b8fa
MH
721 * @param array $groups The group IDs submitted.
722 * @return array Group objects of groups to use in groups filter.
723 * If no filtering required (all groups selected), returns [].
9eebd58a 724 */
2970b8fa
MH
725 protected function get_filter_groups(array $groups): array {
726 global $USER;
9eebd58a 727
2970b8fa
MH
728 $groupmode = groups_get_activity_groupmode($this->cm);
729 $aag = has_capability('moodle/site:accessallgroups', $this->context);
730 $allowedgroups = [];
731 $filtergroups = [];
9eebd58a 732
2970b8fa
MH
733 // Filtering only valid if a forum groups mode is enabled.
734 if (in_array($groupmode, [VISIBLEGROUPS, SEPARATEGROUPS])) {
735 $allgroupsobj = groups_get_all_groups($this->cm->course, 0, $this->cm->groupingid);
736 $allgroups = [];
737
738 foreach ($allgroupsobj as $group) {
739 $allgroups[] = $group->id;
740 }
9eebd58a 741
2970b8fa
MH
742 if ($groupmode == VISIBLEGROUPS || $aag) {
743 $nogroups = new \stdClass();
744 $nogroups->id = -1;
745 $nogroups->name = get_string('groupsnone');
746
747 // Any groups and no groups.
748 $allowedgroupsobj = $allgroupsobj + [$nogroups];
749 } else {
750 // Only assigned groups.
751 $allowedgroupsobj = groups_get_all_groups($this->cm->course, $USER->id, $this->cm->groupingid);
752 }
753
754 foreach ($allowedgroupsobj as $group) {
755 $allowedgroups[] = $group->id;
756 }
757
758 // If not all groups in course are selected, filter by allowed groups submitted.
759 if (!empty($groups) && !empty(array_diff($allowedgroups, $groups))) {
760 $filtergroups = array_intersect($groups, $allowedgroups);
761 } else if (!empty(array_diff($allgroups, $allowedgroups))) {
762 // If user's 'all groups' is a subset of the course groups, filter by all groups available to them.
763 $filtergroups = $allowedgroups;
764 }
9eebd58a
MH
765 }
766
2970b8fa 767 return $filtergroups;
9eebd58a 768 }
850ce974
SL
769
770 /**
771 * Download the summary report in the selected format.
772 *
773 * @param string $format The format to download the report.
774 */
775 public function download($format) {
776 $filename = 'summary_report_' . userdate(time(), get_string('backupnameformat', 'langconfig'),
777 99, false);
778
779 $this->is_downloading($format, $filename);
780 $this->out($this->perpage, false);
781 }
74f94dfc
DM
782
783 /*
784 * Should the word / char counts be displayed?
785 *
786 * We don't want to show word/char columns if there is any null value because this means
787 * that they have not been calculated yet.
788 * @return bool
789 */
68f2d677 790 protected function show_word_char_counts(): bool {
74f94dfc
DM
791 global $DB;
792
68f2d677
MH
793 if (is_null($this->showwordcharcounts)) {
794 // This should be really fast.
795 $sql = "SELECT 'x'
796 FROM {forum_posts} fp
797 JOIN {forum_discussions} fd ON fd.id = fp.discussion
798 WHERE fd.forum = :forumid AND (fp.wordcount IS NULL OR fp.charcount IS NULL)";
799
800 if ($DB->record_exists_sql($sql, ['forumid' => $this->cm->instance])) {
801 $this->showwordcharcounts = false;
802 } else {
803 $this->showwordcharcounts = true;
804 }
74f94dfc
DM
805 }
806
807 return $this->showwordcharcounts;
808 }
20b064be 809
20b064be
MH
810 /**
811 * Fetch the number of items to be displayed per page.
812 *
813 * @return int
814 */
815 public function get_perpage(): int {
816 return $this->perpage;
817 }
818
819 /**
820 * Overriding method to render the bulk actions and items per page pagination options directly below the table.
821 *
822 * @return void
823 */
824 public function wrap_html_finish(): void {
825 global $OUTPUT;
826
827 $data = new \stdClass();
cf8393be 828 $data->showbulkactions = $this->allowbulkoperations;
20b064be
MH
829
830 if ($data->showbulkactions) {
831 $data->id = 'formactionid';
832 $data->attributes = [
833 [
834 'name' => 'data-action',
835 'value' => 'toggle'
836 ],
837 [
838 'name' => 'data-togglegroup',
839 'value' => 'summaryreport-table'
840 ],
841 [
842 'name' => 'data-toggle',
843 'value' => 'action'
844 ],
845 [
846 'name' => 'disabled',
847 'value' => true
848 ]
849 ];
850 $data->actions = [
851 [
852 'value' => '#messageselect',
853 'name' => get_string('messageselectadd')
854 ]
855 ];
856 }
857
858 // Include the pagination size selector.
859 $perpageoptions = array_combine($this->perpageoptions, $this->perpageoptions);
860 $selected = in_array($this->perpage, $this->perpageoptions) ? $this->perpage : $this->perpageoptions[0];
861 $perpageselect = new \single_select(new \moodle_url(''), 'perpage',
862 $perpageoptions, $selected, null, 'selectperpage');
cf8393be 863 $perpageselect->set_label(get_string('perpage', 'moodle'));
20b064be 864
cf8393be 865 $data->perpage = $perpageselect->export_for_template($OUTPUT);
20b064be
MH
866
867 echo $OUTPUT->render_from_template('forumreport_summary/bulk_action_menu', $data);
868 }
6388b657 869}