Skip to content

Commit

Permalink
Merge branch 'release/1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewNatoli committed Nov 9, 2016
2 parents 7e5d4c5 + 9ef1044 commit c6bbeb8
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 264 deletions.
5 changes: 5 additions & 0 deletions app/Household.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ protected static function boot()
}
}

public function scopeDraft ($query)
{
return $query->where('draft', '=', 'Y');
}

public function child() {
return $this->hasMany("\App\Child");
}
Expand Down
259 changes: 8 additions & 251 deletions app/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Http\Controllers\Admin;

use App\Base\Controllers\AdminController;
use App\Household;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use LaravelAnalytics;
Expand Down Expand Up @@ -56,256 +58,11 @@ public function __construct()

public function getIndex()
{
/*
$statistics = [
'keywords' => LaravelAnalytics::getTopKeywords($this->period, $this->limit),
'referrers' => LaravelAnalytics::getTopReferrers($this->period, $this->limit),
'browsers' => LaravelAnalytics::getTopBrowsers($this->period, $this->limit),
'pages' => LaravelAnalytics::getMostVisitedPages($this->period, $this->limit),
'users' => LaravelAnalytics::getActiveUsers(),
'total_visits' => $this->getTotalVisits(),
'landings' => $this->getLandings(),
'exits' => $this->getExits(),
'times' => $this->getTimeOnPages(),
'sources' => $this->getSources(),
'ops' => $this->getOperatingSystems(),
'browsers' => $this->getBrowsers(),
'countries' => $this->getCountries(),
'visits' => $this->getDailyVisits(),
'regions' => $this->getRegions(),
'averages' => $this->getAverages()
];
*/
$statistics = array();
return view('admin.dashboard.index', compact('statistics'));
}

/**
* Simplify the query
*
* @param array $options
* @param string $metrics
* @return mixed
*/
private function query($options = [], $metrics = 'ga:visits')
{
return LaravelAnalytics::performQuery($this->start, $this->end, $metrics, $options)->rows;
}

/**
* Transform analytics array into a collection
*
* @param $data
* @param $fields
* @param int $offset
* @return Collection
*/
private function makeCollection($data, $fields, $offset = 1)
{
if (is_null($data)) {
return new Collection([]);
} else {
foreach ($data as $pageRow) {
$keywordData[] = [$fields[0] => $pageRow[0], $fields[1] => $pageRow[$offset]];
}
return new Collection($keywordData);
}
}

/**
* Total visits
*
* @return mixed
*/
private function getTotalVisits()
{
$options = [
'dimensions' => 'ga:year',
];
return $this->query($options)[0][1];
}

/**
* Landing pages
*
* @return mixed
*/
private function getLandings()
{
$options = [
'dimensions' => 'ga:landingPagePath',
'sort' => '-ga:entrances',
'max-results' => $this->limit
];
$data = $this->query($options, 'ga:entrances');
return $this->makeCollection($data, ['0' => 'path', '1' => 'visits']);
}

/**
* Exit pages
*
* @return mixed
*/
private function getExits()
{
$options = [
'dimensions' => 'ga:exitPagePath',
'sort' => '-ga:exits',
'max-results' => $this->limit
];
$data = $this->query($options, 'ga:exits');
return $this->makeCollection($data, ['0' => 'path', '1' => 'visits']);
}

/**
* Time spent on pages
*
* @return Collection
*/
public function getTimeOnPages()
{
$options = [
'dimensions' => 'ga:pagePath',
'sort' => '-ga:timeOnPage',
'max-results' => $this->limit
];
$data = $this->query($options, 'ga:timeOnPage');
return $this->makeCollection($data, ['0' => 'path', '1' => 'time']);
}

/**
* Traffic sources
*
* @return mixed
*/
private function getSources()
{
$options = [
'dimensions' => 'ga:source, ga:medium',
'sort' => '-ga:visits',
'max-results' => $this->limit
];
$data = $this->query($options);
return $this->makeCollection($data, ['0' => 'path', '1' => 'visits'], 2);
}

/**
* Operating systems
*
* @return mixed
*/
public function getOperatingSystems()
{
$options = [
'dimensions' => 'ga:operatingSystem',
'sort' => '-ga:visits',
'max-results' => $this->limit
];
$data = $this->query($options);
return $this->makeCollection($data, ['0' => 'os', '1' => 'visits']);
}

/**
* Browsers
*
* @return mixed
*/
public function getBrowsers()
{
$options = [
'dimensions' => 'ga:browser',
'sort' => '-ga:visits',
'max-results' => $this->limit
];
$data = $this->query($options);
return $this->makeCollection($data, ['0' => 'browser', '1' => 'visits']);
}

/**
* Country distribution
*
* @return string
*/
private function getCountries()
{
$options = [
'dimensions' => 'ga:country',
'sort' => '-ga:visits'
];
$array = $this->query($options);
$visits = [];
if (count($array)) {
foreach ($array as $k => $v) {
$visits[$k] = [$v[0], (int) $v[1]];
}
}
return json_encode($visits);
}

/**
* Daily visits
*
* @return string
*/
private function getDailyVisits()
{
$options = [
'dimensions' => 'ga:date'
];
$array = $this->query($options);
$visits = [];
foreach ($array as $k => $v) {
$visits[$k]['date'] = Carbon::parse($v['0'])->format('Y-m-d');
$visits[$k]['visits'] = $v['1'];
}
return json_encode($visits);
}

/**
* Region distribution for a specific country
*
* @return string
*/
private function getRegions()
{
$options = [
'dimensions' => 'ga:country, ga:region',
'sort' => '-ga:visits',
'filters' => 'ga:country==' . $this->country . ''
];
$array = $this->query($options);
$visits = [];
if (count($array)) {
foreach ($array as $k => $v) {
$visits[$k] = [str_replace(" Province", "", $v[1]), (int) $v[2]];
}
}
return json_encode($visits);
}

/**
* Average time on pages, bounce rate and page views per visits
*
* @return array
*/
private function getAverages()
{
$options = [
'dimensions' => 'ga:pagePath'
];
$array = $this->query($options, 'ga:avgTimeOnPage, ga:entranceBounceRate, ga:pageviewsPerVisit');
$count = count($array);
$average = ['time' => 0, 'bounce' => 0, 'visit' => 0];
if (count($array)) {
foreach ($array as $v) {
$average['time'] += $v['1'];
$average['bounce'] += $v['2'];
$average['visit'] += $v['3'];
}
$average['time'] = ($average['time'] ? floor($average['time'] / $count) : 0);
$average['bounce'] = ($average['bounce'] ? round($average['bounce'] / $count, 2) : 0);
$average['visit'] = ($average['visit'] ? round($average['visit'] / $count, 2) : 0);
}
return $average;
$accounts_pending_approval = User::query()->pending()->count();
$draftCount = Household::query()->draft()->count();
return view('admin.dashboard.index', [
'accounts_pending_approval' => $accounts_pending_approval,
'drafts' => $draftCount
]);
}
}
11 changes: 8 additions & 3 deletions app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
class UserController extends AdminController
{

/**
public function __construct()
{
$this->middleware('admins_only');
parent::__construct();
}

/**
* Display a listing of the users.
* @return Response
*/
Expand Down Expand Up @@ -169,8 +175,7 @@ public function searchPending(Request $request)
->select ("users.*", "affiliation.type", "affiliation.name")
->join ("affiliation", "affiliation.id", "=", "users.affiliation_id")
->where ("name_last", "LIKE", "$search%")
->where ("confirmed_email", "=", "Y")
->where("approved", "=", "N")
->pending()
->orderBy ($columns[$order[0]["column"]]["name"], $order[0]["dir"]);

$count = $users->count ();
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/HouseholdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public function upload_attachment(Request $request) {
public function sendNotification($id) {
Mail::queue("email.nomination_submitted", [ "id" => $id], function($message) use($id) {
$message->from(env("MAIL_FROM_ADDRESS"));
$message->to(env("NOMINATION_NOTICE_ADDRESS"));
$message->subject(env("NOMINATION_SUBJECT"));
$message->to(env("MAIL_ADMIN_ADDRESS"));
$message->subject("New nomination submitted");
});
}
}
3 changes: 2 additions & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Kernel extends HttpKernel
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'admins_only' => \App\Http\Middleware\Custom\AdminsOnly::class
];
}
24 changes: 24 additions & 0 deletions app/Http/Middleware/Custom/AdminsOnly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Middleware\Custom;

use Closure;
use Illuminate\Support\Facades\Auth;

class AdminsOnly
{
public function __construct()
{
//
}

public function handle($request, Closure $next)
{
$user = Auth::user();
if (!$user->hasRole("admin"))
{
abort(403, 'Unauthorized');
}
return $next($request);
}
}
26 changes: 26 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ protected static function boot()
});
}

/**
* For an account to be in considered pending the
* user must have confirmed their email address.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePending ($query)
{
return $query->where('approved', '=', 'N')->where('confirmed_email', '=', 'Y');
}

/**
* Set the ip address attribute.
*
Expand All @@ -74,8 +86,22 @@ public function roles() {
return $this->belongsToMany('App\Role');
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeThisYear ($query)
{
// TODO: Query current year...
return $query;
}

/**
* Whether or not the user has reached their limit of nominations for the year
*
* TODO: Rejected nominations should not count toward the limit
* TODO: Need to use current year for limit
*
* @return bool
*/
public function getMaxNominationsReachedAttribute()
Expand Down
Loading

0 comments on commit c6bbeb8

Please sign in to comment.