diff --git a/comment/classes/reportbuilder/local/entities/comment.php b/comment/classes/reportbuilder/local/entities/comment.php index 9a3d92137cd..a231dfe8755 100644 --- a/comment/classes/reportbuilder/local/entities/comment.php +++ b/comment/classes/reportbuilder/local/entities/comment.php @@ -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; diff --git a/comment/classes/reportbuilder/local/systemreports/comments.php b/comment/classes/reportbuilder/local/systemreports/comments.php new file mode 100644 index 00000000000..0e94123212e --- /dev/null +++ b/comment/classes/reportbuilder/local/systemreports/comments.php @@ -0,0 +1,116 @@ +. + +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 + * @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') + )); + } +} diff --git a/comment/index.php b/comment/index.php index 8d4bd59cca5..676359bb4b4 100644 --- a/comment/index.php +++ b/comment/index.php @@ -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 '
'; - $return = $manager->print_comments($page); - // if no comments available, $return will be false - if ($return) { - echo ''; - } - echo '
'; + +$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(); diff --git a/comment/locallib.php b/comment/locallib.php index 08265610de2..3e771cca166 100644 --- a/comment/locallib.php +++ b/comment/locallib.php @@ -15,19 +15,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * 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) { diff --git a/comment/upgrade.txt b/comment/upgrade.txt index 029e8a3b6f7..0fd3d274ab6 100644 --- a/comment/upgrade.txt +++ b/comment/upgrade.txt @@ -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). diff --git a/reportbuilder/classes/table/system_report_table.php b/reportbuilder/classes/table/system_report_table.php index 4cc90939480..d28dd6a534c 100644 --- a/reportbuilder/classes/table/system_report_table.php +++ b/reportbuilder/classes/table/system_report_table.php @@ -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'); }