1
0
mirror of https://github.com/flarum/core.git synced 2025-08-24 00:53:27 +02:00

Initial commit

This commit is contained in:
Toby Zerner
2017-12-10 21:11:13 +10:30
commit a595d9e846
15 changed files with 712 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Statistics\Listener;
use Flarum\Event\ConfigureWebApp;
use Illuminate\Contracts\Events\Dispatcher;
class AddClientAssets
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureWebApp::class, [$this, 'addAssets']);
}
/**
* @param ConfigureClientView $event
*/
public function addAssets(ConfigureWebApp $event)
{
if ($event->isAdmin()) {
$event->addAssets([
__DIR__.'/../../js/admin/dist/extension.js',
__DIR__.'/../../less/admin/extension.less'
]);
$event->addBootstrapper('flarum/statistics/main');
}
}
}

View File

@@ -0,0 +1,59 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Statistics\Listener;
use DateTime;
use Flarum\Core\Discussion;
use Flarum\Core\Post;
use Flarum\Core\User;
use Flarum\Event\ConfigureWebApp;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Events\Dispatcher;
class AddStatisticsData
{
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureWebApp::class, [$this, 'addStatisticsData']);
}
public function addStatisticsData(ConfigureWebApp $event)
{
$event->view->setVariable('statistics', $this->getStatistics());
}
private function getStatistics()
{
$entities = [
'users' => [User::query(), 'join_time'],
'discussions' => [Discussion::query(), 'start_time'],
'posts' => [Post::where('type', 'comment'), 'time']
];
return array_map(function ($entity) {
return [
'total' => $entity[0]->count(),
'daily' => $this->getDailyCounts($entity[0], $entity[1])
];
}, $entities);
}
private function getDailyCounts(Builder $query, $column)
{
return $query
->selectRaw('DATE('.$column.') as date')
->selectRaw('COUNT(id) as count')
->where($column, '>', new DateTime('-24 months'))
->groupBy('date')
->lists('count', 'date');
}
}