MDL-29693 report_configlog: re-factor output to use renderers.

AMOS BEGIN
 MOV [value,report_configlog],[valuenew,report_configlog]
 MOV [oldvalue,report_configlog],[valueold,report_configlog]
AMOS END
This commit is contained in:
Paul Holden 2019-06-06 10:39:52 +01:00 committed by Paul Holden
parent 800563e415
commit a00801c071
5 changed files with 207 additions and 103 deletions

View File

@ -0,0 +1,57 @@
<?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/>.
/**
* Plugin renderer class.
*
* @package report_configlog
* @copyright 2019 Paul Holden (pholden@greenhead.ac.uk)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_configlog\output;
defined('MOODLE_INTERNAL') || die();
/**
* Plugin renderer class.
*
* @package report_configlog
* @copyright 2019 Paul Holden (pholden@greenhead.ac.uk)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends \plugin_renderer_base {
/** @var int Page size for displaying report table. */
const REPORT_TABLE_PAGESIZE = 30;
/**
* Return output to be rendered to page
*
* @param report_table $table
* @return string HTML rendered table
*/
protected function render_report_table(report_table $table) {
ob_start();
$table->out(self::REPORT_TABLE_PAGESIZE, false);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}

View File

@ -0,0 +1,144 @@
<?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/>.
/**
* Report table class.
*
* @package report_configlog
* @copyright 2019 Paul Holden (pholden@greenhead.ac.uk)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_configlog\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/tablelib.php');
/**
* Report table class.
*
* @package report_configlog
* @copyright 2019 Paul Holden (pholden@greenhead.ac.uk)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_table extends \table_sql implements \renderable {
/**
* Constructor
*
*/
public function __construct() {
parent::__construct('report-configlog-report-table');
// Define columns.
$columns = [
'timemodified' => get_string('timemodified', 'report_configlog'),
'fullname' => get_string('name'),
'plugin' => get_string('plugin', 'report_configlog'),
'name' => get_string('setting', 'report_configlog'),
'value' => get_string('valuenew', 'report_configlog'),
'oldvalue' => get_string('valueold', 'report_configlog'),
];
$this->define_columns(array_keys($columns));
$this->define_headers(array_values($columns));
// Table configuration.
$this->set_attribute('cellspacing', '0');
$this->sortable(true, 'timemodified', SORT_DESC);
$this->initialbars(false);
$this->collapsible(false);
$this->useridfield = 'userid';
// Initialize table SQL properties.
$this->init_sql();
}
/**
* Initializes table SQL properties
*
* @return void
*/
protected function init_sql() {
$userfields = get_all_user_name_fields(true, 'u');
$fields = 'cl.id, cl.timemodified, cl.plugin, cl.name, cl.value, cl.oldvalue, cl.userid, ' . $userfields;
$from = '{config_log} cl
JOIN {user} u ON u.id = cl.userid';
$this->set_sql($fields, $from, '1=1');
$this->set_count_sql('SELECT COUNT(1) FROM ' . $from);
}
/**
* Cross DB text-compatible sorting for value/oldvalue fields
*
* @return string
*/
public function get_sql_sort() {
global $DB;
$sort = preg_replace_callback('/\b(value|oldvalue)\b/', function(array $matches) use ($DB) {
return $DB->sql_order_by_text($matches[1], 255);
}, parent::get_sql_sort());
return $sort;
}
/**
* Format report timemodified field
*
* @param stdClass $row
* @return string
*/
public function col_timemodified(\stdClass $row) {
return userdate($row->timemodified);
}
/**
* Format report plugin field
*
* @param stdClass $row
* @return string
*/
public function col_plugin(\stdClass $row) {
return $row->plugin ?? 'core';
}
/**
* Format report value field
*
* @param stdClass $row
* @return string
*/
public function col_value(\stdClass $row) {
return $this->format_text($row->value, FORMAT_PLAIN);
}
/**
* Format report old value field
*
* @param stdClass $row
* @return string
*/
public function col_oldvalue(\stdClass $row) {
return $this->format_text($row->oldvalue, FORMAT_PLAIN);;
}
}

View File

@ -26,111 +26,14 @@
require(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/adminlib.php');
// page parameters
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 30, PARAM_INT); // how many per page
$sort = optional_param('sort', 'timemodified', PARAM_ALPHA);
$dir = optional_param('dir', 'DESC', PARAM_ALPHA);
admin_externalpage_setup('reportconfiglog', '', null, '', array('pagelayout'=>'report'));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('configlog', 'report_configlog'));
$changescount = $DB->count_records('config_log');
$table = new \report_configlog\output\report_table();
$table->define_baseurl($PAGE->url);
$columns = array('firstname' => get_string('firstname'),
'lastname' => get_string('lastname'),
'timemodified' => get_string('timemodified', 'report_configlog'),
'plugin' => get_string('plugin', 'report_configlog'),
'name' => get_string('setting', 'report_configlog'),
'value' => get_string('value', 'report_configlog'),
'oldvalue' => get_string('oldvalue', 'report_configlog'),
);
$hcolumns = array();
if (!isset($columns[$sort])) {
$sort = 'lastname';
}
foreach ($columns as $column=>$strcolumn) {
if ($sort != $column) {
$columnicon = '';
if ($column == 'lastaccess') {
$columndir = 'DESC';
} else {
$columndir = 'ASC';
}
} else {
$columndir = $dir == 'ASC' ? 'DESC':'ASC';
if ($column == 'lastaccess') {
$columnicon = $dir == 'ASC' ? 'up':'down';
} else {
$columnicon = $dir == 'ASC' ? 'down':'up';
}
$columnicon = $OUTPUT->pix_icon('t/' . $columnicon, '');
}
$hcolumns[$column] = "<a href=\"index.php?sort=$column&amp;dir=$columndir&amp;page=$page&amp;perpage=$perpage\">".$strcolumn."</a>$columnicon";
}
$baseurl = new moodle_url('index.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
echo $OUTPUT->paging_bar($changescount, $page, $perpage, $baseurl);
$override = new stdClass();
$override->firstname = 'firstname';
$override->lastname = 'lastname';
$fullnamelanguage = get_string('fullnamedisplay', '', $override);
if (($CFG->fullnamedisplay == 'firstname lastname') or
($CFG->fullnamedisplay == 'firstname') or
($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
$fullnamedisplay = $hcolumns['firstname'].' / '.$hcolumns['lastname'];
} else { // ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'lastname firstname')
$fullnamedisplay = $hcolumns['lastname'].' / '.$hcolumns['firstname'];
}
$table = new html_table();
$table->head = array($hcolumns['timemodified'], $fullnamedisplay, $hcolumns['plugin'], $hcolumns['name'], $hcolumns['value'], $hcolumns['oldvalue']);
$table->colclasses = array('leftalign date', 'leftalign name', 'leftalign plugin', 'leftalign setting', 'leftalign newvalue', 'leftalign originalvalue');
$table->id = 'configchanges';
$table->attributes['class'] = 'admintable generaltable';
$table->data = array();
if ($sort == 'firstname' or $sort == 'lastname') {
$orderby = "u.$sort $dir";
} else if ($sort == 'value' or $sort == 'oldvalue') {
// cross-db text-compatible sorting.
$orderby = $DB->sql_order_by_text("cl.$sort", 255) . ' ' . $dir;
} else {
$orderby = "cl.$sort $dir";
}
$ufields = user_picture::fields('u');
$sql = "SELECT $ufields,
cl.timemodified, cl.plugin, cl.name, cl.value, cl.oldvalue
FROM {config_log} cl
JOIN {user} u ON u.id = cl.userid
ORDER BY $orderby";
$rs = $DB->get_recordset_sql($sql, array(), $page*$perpage, $perpage);
foreach ($rs as $log) {
$row = array();
$row[] = userdate($log->timemodified);
$row[] = fullname($log);
if (is_null($log->plugin)) {
$row[] = 'core';
} else {
$row[] = $log->plugin;
}
$row[] = $log->name;
$row[] = s($log->value);
$row[] = s($log->oldvalue);
$table->data[] = $row;
}
$rs->close();
echo html_writer::table($table);
echo $PAGE->get_renderer('report_configlog')->render($table);
echo $OUTPUT->footer();

View File

@ -24,10 +24,10 @@
*/
$string['configlog'] = 'Config changes';
$string['oldvalue'] = 'Original value';
$string['plugin'] = 'Plugin';
$string['pluginname'] = 'Config changes';
$string['setting'] = 'Setting';
$string['timemodified'] = 'Date';
$string['value'] = 'New value';
$string['valuenew'] = 'New value';
$string['valueold'] = 'Original value';
$string['privacy:metadata'] = 'The Config changes plugin does not store any personal data.';

View File

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die;
$plugin->version = 2019111800; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2019120200; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2019111200; // Requires this Moodle version
$plugin->component = 'report_configlog'; // Full name of the plugin (used for diagnostics)