From fdd3ccaf07e1a660fb042e020859da8f49cdb8df Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 18 Oct 2019 16:06:38 +0100 Subject: [PATCH] MDL-29693 report_configlog: implement report search filtering. --- report/configlog/classes/form/search.php | 72 +++++++++++++++++++ .../configlog/classes/output/report_table.php | 41 +++++++++-- report/configlog/index.php | 43 ++++++++++- report/configlog/lang/en/report_configlog.php | 6 ++ .../configlog/tests/behat/view_report.feature | 40 +++++++++++ 5 files changed, 194 insertions(+), 8 deletions(-) create mode 100644 report/configlog/classes/form/search.php create mode 100644 report/configlog/tests/behat/view_report.feature diff --git a/report/configlog/classes/form/search.php b/report/configlog/classes/form/search.php new file mode 100644 index 00000000000..b881b292f1d --- /dev/null +++ b/report/configlog/classes/form/search.php @@ -0,0 +1,72 @@ +. + +/** + * Report search form class. + * + * @package report_configlog + * @copyright 2019 Paul Holden (paulh@moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace report_configlog\form; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir . '/formslib.php'); + +/** + * Report search form class. + * + * @package report_configlog + * @copyright 2019 Paul Holden (paulh@moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class search extends \moodleform { + + /** + * Form definition + * + * @return void + */ + public function definition() { + $mform = $this->_form; + + // By default just show the 'setting' field. + $mform->addElement('header', 'heading', get_string('search')); + $mform->addElement('text', 'setting', get_string('setting', 'report_configlog')); + $mform->setType('setting', PARAM_TEXT); + + // Rest of the search fields. + $mform->addElement('text', 'value', get_string('value', 'report_configlog')); + $mform->setType('value', PARAM_TEXT); + $mform->addHelpButton('value', 'value', 'report_configlog'); + $mform->setAdvanced('value', true); + + $mform->addElement('text', 'user', get_string('user', 'report_configlog')); + $mform->setType('user', PARAM_TEXT); + $mform->addHelpButton('user', 'user', 'report_configlog'); + $mform->setAdvanced('user', true); + + $mform->addElement('date_selector', 'datefrom', get_string('datefrom', 'report_configlog'), ['optional' => true]); + $mform->setAdvanced('datefrom', true); + + $mform->addElement('date_selector', 'dateto', get_string('dateto', 'report_configlog'), ['optional' => true]); + $mform->setAdvanced('dateto', true); + + $this->add_action_buttons(false, get_string('search')); + } +} \ No newline at end of file diff --git a/report/configlog/classes/output/report_table.php b/report/configlog/classes/output/report_table.php index e67f60f3388..5927f782ec7 100644 --- a/report/configlog/classes/output/report_table.php +++ b/report/configlog/classes/output/report_table.php @@ -26,6 +26,7 @@ namespace report_configlog\output; defined('MOODLE_INTERNAL') || die(); +require_once($CFG->libdir . '/searchlib.php'); require_once($CFG->libdir . '/tablelib.php'); /** @@ -37,13 +38,19 @@ require_once($CFG->libdir . '/tablelib.php'); */ class report_table extends \table_sql implements \renderable { + /** @var string $search */ + protected $search; + /** * Constructor * + * @param string $search */ - public function __construct() { + public function __construct(string $search) { parent::__construct('report-configlog-report-table'); + $this->search = trim($search); + // Define columns. $columns = [ 'timemodified' => get_string('timemodified', 'report_configlog'), @@ -57,6 +64,7 @@ class report_table extends \table_sql implements \renderable { $this->define_headers(array_values($columns)); // Table configuration. + $this->set_attribute('id', $this->uniqueid); $this->set_attribute('cellspacing', '0'); $this->sortable(true, 'timemodified', SORT_DESC); @@ -76,15 +84,38 @@ class report_table extends \table_sql implements \renderable { * @return void */ protected function init_sql() { - $userfields = get_all_user_name_fields(true, 'u'); + global $DB; + $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); + // Report search. + $where = '1=1'; + $params = []; + + if (!empty($this->search)) { + // Clean quotes, allow search by 'setting:' prefix. + $searchstring = str_replace(["\\\"", 'setting:'], ["\"", 'subject:'], $this->search); + + $parser = new \search_parser(); + $lexer = new \search_lexer($parser); + + if ($lexer->parse($searchstring)) { + $parsearray = $parser->get_parsed_array(); + + // Data fields should contain both value/oldvalue. + $datafields = $DB->sql_concat_join("':'", ['cl.value', 'cl.oldvalue']); + + list($where, $params) = search_generate_SQL($parsearray, $datafields, 'cl.name', 'cl.userid', 'u.id', + 'u.firstname', 'u.lastname', 'cl.timemodified', 'cl.id'); + } + } + + $this->set_sql($fields, $from, $where, $params); + $this->set_count_sql('SELECT COUNT(1) FROM ' . $from . ' WHERE ' . $where, $params); } /** @@ -141,4 +172,4 @@ class report_table extends \table_sql implements \renderable { public function col_oldvalue(\stdClass $row) { return $this->format_text($row->oldvalue, FORMAT_PLAIN);; } -} +} \ No newline at end of file diff --git a/report/configlog/index.php b/report/configlog/index.php index 129fdff6eef..027eeda078a 100644 --- a/report/configlog/index.php +++ b/report/configlog/index.php @@ -27,13 +27,50 @@ require(__DIR__.'/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); admin_externalpage_setup('reportconfiglog', '', null, '', array('pagelayout'=>'report')); -echo $OUTPUT->header(); +echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('configlog', 'report_configlog')); -$table = new \report_configlog\output\report_table(); +$mform = new \report_configlog\form\search(); + +/** @var cache_session $cache */ +$cache = cache::make_from_params(cache_store::MODE_SESSION, 'report_customlog', 'search'); +if ($cachedata = $cache->get('data')) { + $mform->set_data($cachedata); +} + +$searchclauses = []; + +// Check if we have a form submission, or a cached submission. +$data = ($mform->is_submitted() ? $mform->get_data() : fullclone($cachedata)); +if ($data instanceof stdClass) { + if (!empty($data->value)) { + $searchclauses[] = $data->value; + } + if (!empty($data->setting)) { + $searchclauses[] = "setting:{$data->setting}"; + } + if (!empty($data->user)) { + $searchclauses[] = "user:{$data->user}"; + } + if (!empty($data->datefrom)) { + $searchclauses[] = "datefrom:{$data->datefrom}"; + } + if (!empty($data->dateto)) { + $dateto = $data->dateto + DAYSECS - 1; + $searchclauses[] = "dateto:{$dateto}"; + } + + // Cache form submission so that it is preserved while paging through the report. + unset($data->submitbutton); + $cache->set('data', $data); +} + +$mform->display(); + +$table = new \report_configlog\output\report_table(implode(' ', $searchclauses)); $table->define_baseurl($PAGE->url); echo $PAGE->get_renderer('report_configlog')->render($table); -echo $OUTPUT->footer(); +echo $OUTPUT->footer(); \ No newline at end of file diff --git a/report/configlog/lang/en/report_configlog.php b/report/configlog/lang/en/report_configlog.php index 785d3d96b16..40645fd4c18 100644 --- a/report/configlog/lang/en/report_configlog.php +++ b/report/configlog/lang/en/report_configlog.php @@ -24,10 +24,16 @@ */ $string['configlog'] = 'Config changes'; +$string['datefrom'] = 'Date from'; +$string['dateto'] = 'Date to'; $string['plugin'] = 'Plugin'; $string['pluginname'] = 'Config changes'; $string['setting'] = 'Setting'; $string['timemodified'] = 'Date'; +$string['user'] = 'User'; +$string['user_help'] = 'Search by user first name or surname'; +$string['value'] = 'Value'; +$string['value_help'] = 'Search by new or original value of the configuration'; $string['valuenew'] = 'New value'; $string['valueold'] = 'Original value'; $string['privacy:metadata'] = 'The Config changes plugin does not store any personal data.'; diff --git a/report/configlog/tests/behat/view_report.feature b/report/configlog/tests/behat/view_report.feature new file mode 100644 index 00000000000..99efc12e1e0 --- /dev/null +++ b/report/configlog/tests/behat/view_report.feature @@ -0,0 +1,40 @@ +@report @report_configlog +Feature: In a report, admin can see configuration changes + In order see configuration changes + As an admin + I need to view the configuration changes report and use search to filter the report + + # Set some config values so the report contains known data. + Background: + Given I log in as "admin" + And I change the window size to "large" + And I set the following administration settings values: + | Initial number of overall feedback fields | 5 | + | Maximum folder download size | 2048 | + | Default city | Perth | + + @javascript + Scenario: Display configuration changes report + When I navigate to "Reports > Config changes" in site administration + Then the following should exist in the "report-configlog-report-table" table: + | User | Plugin | Setting | New value | Original value | + | Admin User | quiz | initialnumfeedbacks | 5 | 2 | + | Admin User | folder | maxsizetodownload | 2048 | 0 | + | Admin User | core | defaultcity | Perth | | + + @javascript + Scenario Outline: Search configuration changes report + When I navigate to "Reports > Config changes" in site administration + And I click on "Show more..." "link" + And I set the field "" to "" + And I click on "Search" "button" in the "#fitem_id_submitbutton" "css_element" + Then the following should exist in the "report-configlog-report-table" table: + | Plugin | Setting | New value | + | | | | + And I should not see "" in the "report-configlog-report-table" "table" + Examples: + | field | search | plugin | setting | value | excluded | + | Setting | initialnumfeedbacks | quiz | initialnumfeedbacks | 5 | maxsizetodownload | + | Setting | maxsizetodownload | folder | maxsizetodownload | 2048 | initialnumfeedbacks | + | Value | Perth | core | defaultcity | Perth | maxsizetodownload | + | User | Admin | core | defaultcity | Perth | zzzzzzzzz | \ No newline at end of file