MDL-76591 comment: convert existing overview page to system report.

Re-use the entity definitions previously created in fdf2f8f3 for
custom reporting, to create a new system report class.
This commit is contained in:
Paul Holden 2022-12-07 16:47:14 +00:00
parent 880462a168
commit 648b80c201
6 changed files with 147 additions and 26 deletions

View File

@ -202,6 +202,7 @@ class comment extends base {
->add_joins($this->get_joins())
->set_type(column::TYPE_TIMESTAMP)
->add_fields("{$commentalias}.timecreated")
->set_is_sortable(true)
->add_callback([format::class, 'userdate']);
return $columns;

View File

@ -0,0 +1,116 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_comment\reportbuilder\local\systemreports;
use context_system;
use lang_string;
use moodle_url;
use pix_icon;
use stdClass;
use core_reportbuilder\system_report;
use core_reportbuilder\local\entities\user;
use core_reportbuilder\local\report\action;
use core_comment\reportbuilder\local\entities\comment;
/**
* Comments system report
*
* @package core_comment
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comments extends system_report {
/**
* Initialise report, we need to set the main table, load our entities and set columns/filters
*/
protected function initialise(): void {
$commententity = new comment();
$commentalias = $commententity->get_table_alias('comments');
$this->set_main_table('comments', $commentalias);
$this->add_entity($commententity);
// Base fields required for action callbacks and checkbox toggle.
$this->add_base_fields("{$commentalias}.id");
$this->set_checkbox_toggleall(static function(stdClass $row): array {
return [$row->id, get_string('select')];
});
// Join the user entity to the comment userid (author).
$userentity = new user();
$useralias = $userentity->get_table_alias('user');
$this->add_entity($userentity
->add_join("LEFT JOIN {user} {$useralias} ON {$useralias}.id = {$commentalias}.userid"));
$this->add_columns();
$this->add_filters();
$this->add_actions();
$this->set_downloadable(true, get_string('comments'));
}
/**
* Validates access to view this report
*
* @return bool
*/
protected function can_view(): bool {
return has_capability('moodle/comment:delete', context_system::instance());
}
/**
* Add columns to the report
*/
protected function add_columns(): void {
$this->add_columns_from_entities([
'user:fullnamewithlink',
'comment:content',
'comment:contexturl',
'comment:timecreated',
]);
// Default sorting.
$this->set_initial_sort_column('comment:timecreated', SORT_DESC);
}
/**
* Add filters to the report
*/
protected function add_filters(): void {
$this->add_filters_from_entities([
'user:fullname',
'comment:content',
'comment:timecreated',
]);
}
/**
* Add actions to report
*/
protected function add_actions(): void {
$this->add_action(new action(
new moodle_url('#'),
new pix_icon('t/delete', ''),
['data-action' => 'comment-delete', 'data-comment-id' => ':id'],
false,
new lang_string('delete')
));
}
}

View File

@ -26,6 +26,9 @@ require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/comment/locallib.php');
use core_reportbuilder\system_report_factory;
use core_comment\reportbuilder\local\systemreports\comments;
admin_externalpage_setup('comments', '', null, '', array('pagelayout'=>'report'));
$PAGE->requires->js_init_call('M.core_comment.init_admin', null, true);
@ -75,20 +78,21 @@ if ($action === 'delete') {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('comments'));
echo $OUTPUT->box_start('generalbox commentsreport');
if (!empty($err)) {
throw new \moodle_exception($err, 'error', $CFG->wwwroot.'/comment/');
}
if (empty($action)) {
echo '<form method="post">';
$return = $manager->print_comments($page);
// if no comments available, $return will be false
if ($return) {
echo '<input type="submit" class="btn btn-primary" id="comments_delete" name="batchdelete"
value="'.get_string('delete').'" />';
}
echo '</form>';
$report = system_report_factory::create(comments::class, context_system::instance());
$report->set_default_per_page($CFG->commentsperpage);
echo $report->output();
// Render delete selected button.
if ($DB->record_exists('comments', [])) {
echo $OUTPUT->render(new single_button(
new moodle_url('#'),
get_string('deleteselected'),
'post',
single_button::BUTTON_PRIMARY,
['data-action' => 'comment-delete-selected']
));
}
echo $OUTPUT->box_end();
echo $OUTPUT->footer();

View File

@ -15,19 +15,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Functions and classes for comments management
*
* @package core
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* comment_manager is helper class to manage moodle comments in admin page (Reports->Comments)
*
* @package core
* @package core_comment
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -145,10 +136,15 @@ class comment_manager {
* Print comments
* @param int $page
* @return bool return false if no comments available
*
* @deprecated since Moodle 4.2 - please do not use this function any more
*/
public function print_comments($page = 0) {
global $OUTPUT, $CFG, $OUTPUT, $DB;
debugging('The function ' . __FUNCTION__ . '() is deprecated, please do not use it any more. ' .
'See \'comments\' system report class for replacement', DEBUG_DEVELOPER);
$count = $DB->count_records('comments');
$comments = $this->get_comments($page);
if (count($comments) == 0) {

View File

@ -1,6 +1,10 @@
This files describes API changes in /comment/* ,
information provided here is intended especially for developers.
=== 4.2 ===
* The comment manager `print_comments` method has been deprecated, in favour of new system report class for listing
comment data
=== 3.8 ===
* External function get_comments now returns the total count of comments and the number of comments per page.
It also has a new parameter to indicate the sorting direction (defaulted to DESC).

View File

@ -102,8 +102,8 @@ class system_report_table extends base_report_table {
$columnheaders = $columnsattributes = [];
// Check whether report has checkbox toggle defined.
if ($checkbox = $this->report->get_checkbox_toggleall(true)) {
// Check whether report has checkbox toggle defined, note that select all is excluded during download.
if (($checkbox = $this->report->get_checkbox_toggleall(true)) && !$this->is_downloading()) {
$columnheaders['selectall'] = $PAGE->get_renderer('core')->render($checkbox);
$this->no_sorting('selectall');
}