<?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/>.

/**
 * User usage statistics page.
 *
 * @package    local_gamibot_manager
 * @copyright  2024 ISB Bayern
 * @author     Philipp Memmel
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

use local_gamibot_manager\local\tenant_config_output_utils;
use local_gamibot_manager\output\tenantnavbar;

require_once(dirname(__FILE__) . '/../../config.php');
require_login();

global $CFG, $DB, $OUTPUT, $PAGE, $SESSION, $USER;

tenant_config_output_utils::setup_tenant_config_page(new moodle_url('/local/gamibot_manager/user_statistics.php'));
// Make sure we do not have a purpose selected in the session when calling this page.
unset($SESSION->local_gamibot_manager_statistics_purpose);

$tenant = \core\di::get(\local_gamibot_manager\local\tenant::class);
require_capability('local/gamibot_manager:viewuserstatistics', $tenant->get_context());

echo $OUTPUT->header();
$tenantnavbar = new tenantnavbar('user_statistics.php');
echo $OUTPUT->render($tenantnavbar);


echo $OUTPUT->heading(get_string('userstatistics', 'local_gamibot_manager'), 2, 'text-center pb-3');
if (!empty($purpose)) {
    echo $OUTPUT->heading(get_string('purpose', 'local_gamibot_manager') . ': '
            . get_string('pluginname', 'gbpurpose_' . $purpose), 4, 'text-center');
}

$recordscount =
        $DB->count_records_sql("SELECT COUNT(*) FROM {local_gamibot_manager_request_log} WHERE tenant = :tenant",
                ['tenant' => $tenant->get_sql_identifier()]);

if ($recordscount !== 0) {
    $uniqid = 'statistics-table-users-all-purposes';

    $baseurl = new moodle_url('/local/gamibot_manager/user_statistics.php', ['tenant' => $tenant->get_sql_identifier()]);
    $table = new \local_gamibot_manager\table\userstats_table($uniqid);
    $table->out(30, false);
} else {
    echo html_writer::div(get_string('nodata', 'local_gamibot_manager'), 'alert alert-info');
}

echo $OUTPUT->footer();
