Skip to content

Commit

Permalink
MDL-44950 logstore_database: Make all settings positive for usability
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitagarwal committed Apr 22, 2014
1 parent 23a9a56 commit a3cfbcc
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 17 deletions.
20 changes: 10 additions & 10 deletions admin/tool/log/store/database/classes/log/store.php
Expand Up @@ -38,11 +38,11 @@ class store implements \tool_log\log\writer, \core\log\sql_select_reader {
/** @var bool $logguests true if logging guest access */
protected $logguests;

/** @var array $excludelevels An array of education levels to exclude */
protected $excludelevels = array();
/** @var array $includelevels An array of education levels to include */
protected $includelevels = array();

/** @var array $excludeactions An array of actions types to exclude */
protected $excludeactions = array();
/** @var array $includeactions An array of actions types to include */
protected $includeactions = array();

/**
* Construct
Expand All @@ -53,10 +53,10 @@ public function __construct(\tool_log\log\manager $manager) {
$this->helper_setup($manager);
$this->buffersize = $this->get_config('buffersize', 50);
$this->logguests = $this->get_config('logguests', 1);
$actions = $this->get_config('excludeactions', '');
$levels = $this->get_config('excludelevels', '');
$this->excludeactions = $actions === '' ? array() : explode(',', $actions);
$this->excludelevels = $levels === '' ? array() : explode(',', $levels);
$actions = $this->get_config('includeactions', '');
$levels = $this->get_config('includelevels', '');
$this->includeactions = $actions === '' ? array() : explode(',', $actions);
$this->includelevels = $levels === '' ? array() : explode(',', $levels);
}

/**
Expand Down Expand Up @@ -113,8 +113,8 @@ protected function init() {
* @return bool
*/
protected function is_event_ignored(\core\event\base $event) {
if (in_array($event->crud, $this->excludeactions) ||
in_array($event->edulevel, $this->excludelevels)
if (!in_array($event->crud, $this->includeactions) &&
!in_array($event->edulevel, $this->includelevels)
) {
// Ignore event if the store settings do not want to store it.
return true;
Expand Down
37 changes: 37 additions & 0 deletions admin/tool/log/store/database/db/upgrade.php
@@ -0,0 +1,37 @@
<?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/>.

/**
* Database log store upgrade.
*
* @package logstore_database
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

function xmldb_logstore_database_upgrade($oldversion) {

if ($oldversion < 2014041700) {
// Clean up old config.
unset_config('excludelevels', 'logstore_database');
unset_config('excludeactions', 'logstore_database');

// Savepoint reached.
upgrade_plugin_savepoint(true, 2014041700, 'logstore', 'database');
}

return true;
}
4 changes: 2 additions & 2 deletions admin/tool/log/store/database/lang/en/logstore_database.php
Expand Up @@ -33,8 +33,8 @@
$string['databasecollation'] = 'Database collation';
$string['databasetable'] = 'Database table';
$string['databasetable_help'] = 'Name of the table where logs will be stored. This table should have a structure identical to the one used by logstore_standard (mdl_logstore_standard_log).';
$string['excludeactions'] = 'Exclude actions of these types';
$string['excludelevels'] = 'Exclude actions with these educational levels';
$string['includeactions'] = 'Include actions of these types';
$string['includelevels'] = 'Include actions with these educational levels';
$string['filters'] = 'Filter logs';
$string['filters_help'] = 'Enable filters that exclude some actions from being logged.';
$string['logguests'] = 'Log guest actions';
Expand Down
8 changes: 4 additions & 4 deletions admin/tool/log/store/database/settings.php
Expand Up @@ -63,9 +63,9 @@
$settings->add(new admin_setting_configcheckbox('logstore_database/logguests', get_string('logguests',
'logstore_database'), '', '0'));
$levels = \logstore_database\helper::get_level_options();
$settings->add(new admin_setting_configmulticheckbox('logstore_database/excludelevels', get_string('excludelevels',
'logstore_database'), '', array(), $levels));
$settings->add(new admin_setting_configmulticheckbox('logstore_database/includelevels', get_string('includelevels',
'logstore_database'), '', $levels, $levels));
$actions = \logstore_database\helper::get_action_options();
$settings->add(new admin_setting_configmulticheckbox('logstore_database/excludeactions', get_string('excludeactions',
'logstore_database'), '', array(), $actions));
$settings->add(new admin_setting_configmulticheckbox('logstore_database/includeactions', get_string('includeactions',
'logstore_database'), '', $actions, $actions));
}
40 changes: 40 additions & 0 deletions admin/tool/log/store/database/tests/fixtures/store.php
@@ -0,0 +1,40 @@
<?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/>.

/**
* Fixtures for database log storage testing.
*
* @package logstore_database
* @copyright 2014 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace logstore_database\test;

defined('MOODLE_INTERNAL') || die();

class store extends \logstore_database\log\store {
/**
* Public wrapper for testing.
*
* @param \core\event\base $event
*
* @return bool
*/
public function is_event_ignored(\core\event\base $event) {
return parent::is_event_ignored($event);
}
}
44 changes: 44 additions & 0 deletions admin/tool/log/store/database/tests/store_test.php
Expand Up @@ -25,6 +25,7 @@
defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/fixtures/event.php');
require_once(__DIR__ . '/fixtures/store.php');

class logstore_database_store_testcase extends advanced_testcase {
public function test_log_writing() {
Expand Down Expand Up @@ -225,6 +226,49 @@ public function test_log_writing() {
get_log_manager(true);
}

/**
* Test method is_event_ignored.
*/
public function test_is_event_ignored() {
$this->resetAfterTest();

// Test guest filtering.
set_config('logguests', 0, 'logstore_database');
$this->setGuestUser();
$event = \logstore_database\event\unittest_executed::create(
array('context' => context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10)));
$logmanager = get_log_manager();
$store = new \logstore_database\test\store($logmanager);
$this->assertTrue($store->is_event_ignored($event));

set_config('logguests', 1, 'logstore_database');
$store = new \logstore_database\test\store($logmanager); // Reload.
$this->assertFalse($store->is_event_ignored($event));

// Test action/level filtering.
set_config('includelevels', '', 'logstore_database');
set_config('includeactions', '', 'logstore_database');
$store = new \logstore_database\test\store($logmanager); // Reload.
$this->assertTrue($store->is_event_ignored($event));

set_config('includelevels', '0,1', 'logstore_database');
$store = new \logstore_database\test\store($logmanager); // Reload.
$this->assertTrue($store->is_event_ignored($event));

set_config('includelevels', '0,1,2', 'logstore_database');
$store = new \logstore_database\test\store($logmanager); // Reload.
$this->assertFalse($store->is_event_ignored($event));

set_config('includelevels', '', 'logstore_database');
set_config('includeactions', 'c,r,d', 'logstore_database');
$store = new \logstore_database\test\store($logmanager); // Reload.
$this->assertTrue($store->is_event_ignored($event));

set_config('includeactions', 'c,r,u,d', 'logstore_database');
$store = new \logstore_database\test\store($logmanager); // Reload.
$this->assertFalse($store->is_event_ignored($event));
}

/**
* Test logmanager::get_supported_reports returns all reports that require this store.
*/
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/log/store/database/version.php
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2014011900; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2014041700; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2014011000; // Requires this Moodle version.
$plugin->component = 'logstore_database'; // Full name of the plugin (used for diagnostics).

0 comments on commit a3cfbcc

Please sign in to comment.