MDL-57892 report_outline: add date filter

This commit is contained in:
Davo Smith 2017-11-28 08:54:46 +00:00
parent 3fa531ea2c
commit eadd195151
2 changed files with 127 additions and 6 deletions

View File

@ -0,0 +1,74 @@
<?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/>.
/**
* Form to filter the outline report
*
* @package report_outline
* @copyright 2017 Davo Smith, Synergy Learning
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_outline;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/formslib.php');
/**
* Class filter_form form to filter the results by date
* @package report_outline
*/
class filter_form extends \moodleform {
/**
* Form definition
* @throws \HTML_QuickForm_Error
* @throws \coding_exception
*/
protected function definition() {
$mform = $this->_form;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('header', 'filterheader', get_string('filter'));
$opts = ['optional' => true];
$mform->addElement('date_selector', 'filterstartdate', get_string('from'), $opts);
$mform->addElement('date_selector', 'filterenddate', get_string('to'), $opts);
$mform->setExpanded('filterheader', false);
// Add the filter/cancel buttons (without 'closeHeaderBefore', so they collapse with the filter).
$buttonarray = [
$mform->createElement('submit', 'submitbutton', get_string('filter')),
$mform->createElement('cancel'),
];
$mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
}
/**
* Expand the form contents if the filter is in use.
* @throws \HTML_QuickForm_Error
*/
public function definition_after_data() {
$mform = $this->_form;
$filterstartdate = $mform->getElement('filterstartdate')->getValue();
$filterenddate = $mform->getElement('filterenddate')->getValue();
if (!empty($filterstartdate['enabled']) || !empty($filterenddate['enabled'])) {
$mform->setExpanded('filterheader', true);
}
}
}

View File

@ -27,16 +27,45 @@ require('../../config.php');
require_once($CFG->dirroot.'/report/outline/locallib.php');
$id = required_param('id',PARAM_INT); // course id
$startdate = optional_param('startdate', null, PARAM_INT);
$enddate = optional_param('enddate', null, PARAM_INT);
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
$PAGE->set_url('/report/outline/index.php', array('id'=>$id));
$pageparams = array('id' => $id);
if ($startdate) {
$pageparams['startdate'] = $startdate;
}
if ($enddate) {
$pageparams['enddate'] = $enddate;
}
$PAGE->set_url('/report/outline/index.php', $pageparams);
$PAGE->set_pagelayout('report');
require_login($course);
$context = context_course::instance($course->id);
require_capability('report/outline:view', $context);
// Handle form to filter access logs by date.
$filterform = new \report_outline\filter_form();
$filterform->set_data(['id' => $course->id, 'filterstartdate' => $startdate, 'filterenddate' => $enddate]);
if ($filterform->is_cancelled()) {
$redir = $PAGE->url;
$redir->remove_params(['startdate', 'enddate']);
redirect($redir);
}
if ($filter = $filterform->get_data()) {
$redir = $PAGE->url;
if ($filter->filterstartdate) {
$redir->param('startdate', $filter->filterstartdate);
}
if ($filter->filterenddate) {
$redir->param('enddate', $filter->filterenddate);
}
redirect($redir);
}
// Trigger an activity report viewed event.
$event = \report_outline\event\activity_report_viewed::create(array('context' => $context));
$event->trigger();
@ -85,6 +114,8 @@ if ($useinternalreader) {
}
}
$filterform->display();
echo $OUTPUT->container(get_string('computedfromlogs', 'admin', userdate($minlog)), 'loginfo');
$outlinetable = new html_table();
@ -107,9 +138,19 @@ $modinfo = get_fast_modinfo($course);
if ($uselegacyreader) {
// If we are going to use the internal (not legacy) log table, we should only get records
// from the legacy table that exist before we started adding logs to the new table.
$params = array('courseid' => $course->id, 'action' => 'view%', 'visible' => 1);
$limittime = '';
if (!empty($minloginternalreader)) {
$limittime = ' AND time < :timeto ';
$params['timeto'] = $minloginternalreader;
}
if ($startdate) {
$limittime .= ' AND time >= :startdate ';
$params['startdate'] = $startdate;
}
if ($enddate) {
$limittime .= ' AND time < :enddate ';
$params['enddate'] = $enddate;
}
// Check if we need to show the last access.
$sqllasttime = '';
@ -127,10 +168,6 @@ if ($uselegacyreader) {
AND $logactionlike
AND m.visible = :visible $limittime
GROUP BY cm.id";
$params = array('courseid' => $course->id, 'action' => 'view%', 'visible' => 1);
if (!empty($minloginternalreader)) {
$params['timeto'] = $minloginternalreader;
}
$views = $DB->get_records_sql($sql, $params);
}
@ -141,14 +178,24 @@ if ($useinternalreader) {
if ($showlastaccess) {
$sqllasttime = ", MAX(timecreated) AS lasttime";
}
$params = array('courseid' => $course->id, 'contextmodule' => CONTEXT_MODULE);
$limittime = '';
if ($startdate) {
$limittime .= ' AND timecreated >= :startdate ';
$params['startdate'] = $startdate;
}
if ($enddate) {
$limittime .= ' AND timecreated < :enddate ';
$params['enddate'] = $enddate;
}
$sql = "SELECT contextinstanceid as cmid, COUNT('x') AS numviews, COUNT(DISTINCT userid) AS distinctusers $sqllasttime
FROM {" . $logtable . "} l
WHERE courseid = :courseid
AND anonymous = 0
AND crud = 'r'
AND contextlevel = :contextmodule
$limittime
GROUP BY contextinstanceid";
$params = array('courseid' => $course->id, 'contextmodule' => CONTEXT_MODULE);
$v = $DB->get_records_sql($sql, $params);
if (empty($views)) {