Skip to content

Commit

Permalink
Merge pull request #55 from krlxfm/supporting-views
Browse files Browse the repository at this point in the history
"Lincoln" Supporting Views. Yes, we're ignoring Code Climate again. Will be fixed later, see #45, #46, #47, and #48 for ongoing issues to be resolved later. (Famous last words.)
  • Loading branch information
tatebosler authored Jul 22, 2018
2 parents c8a7b7a + 7fe018c commit 0735d0c
Show file tree
Hide file tree
Showing 25 changed files with 1,070 additions and 50 deletions.
14 changes: 12 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace KRLX\Http\Controllers;

use KRLX\Term;
use Illuminate\Http\Request;

class HomeController extends Controller
{
/**
Expand All @@ -17,10 +20,17 @@ public function __construct()
/**
* Show the application dashboard.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Request $request)
{
return view('home');
$terms = Term::orderByDesc('on_air')->get();
$term = $terms->first();

$user = $request->user();
$shows = $user->shows()->where('term_id', $term->id)->get();

return view('home', compact('user', 'shows', 'term'));
}
}
57 changes: 57 additions & 0 deletions app/Http/Controllers/ShowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,61 @@ public function review(Show $show)
{
return view('shows.review', compact('show'));
}

/**
* Display the master list of shows.
*
* @param Illuminate\Http\Request $request
* @param KRLX\Term|null $term
* @return Illuminate\Http\Response
*/
public function all(Request $request, Term $term = null)
{
$terms = Term::orderByDesc('on_air')->get();
if ($term == null) {
$term = $terms->first();
}

$shows = $term->shows->sort(function ($a, $b) {
$boost_diff = ($b->boost == 'S') <=> ($a->boost == 'S');
$track_diff = $a->track->order <=> $b->track->order;
$priority_diff = $a->priority <=> $b->priority;
$updated_at_diff = $a->updated_at <=> $b->updated_at;
$id_diff = $a->id <=> $b->id;

$diffs = [$boost_diff, $track_diff, $priority_diff, $updated_at_diff, $id_diff];

foreach ($diffs as $diff) {
if ($diff != 0) {
return $diff;
}
}
});

return view('shows.all', compact('shows', 'terms', 'term'));
}

/**
* Display the list of all DJs involved in at least one show.
*
* @param Illuminate\Http\Request $request
* @param KRLX\Term|null $term
* @return Illuminate\Http\Response
*/
public function djs(Request $request, Term $term = null)
{
$terms = Term::orderByDesc('on_air')->get();
if ($term == null) {
$term = $terms->first();
}

$hosts = $term->shows->pluck('hosts')->flatten();
$invitees = $term->shows->pluck('invitees')->flatten();

$users = $hosts->concat($invitees)->unique(function ($user) {
return $user['id'];
})->sortBy('email');

return view('shows.djs', compact('term', 'terms', 'users'));
}
}
1 change: 1 addition & 0 deletions app/Listeners/FillUserDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ public function handle(UserCreating $event)
$user->first_name = $user->first_name ?? $names->first() ?? 'User';
$user->title = $user->title ?? config('defaults.title', 'KRLX Community');
$user->photo = $user->photo ?? $icon->getImageDataUri();
$user->xp = array_wrap($user->xp) ?? [];
}
}
55 changes: 55 additions & 0 deletions app/Priority.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace KRLX;

class Priority
{
public $terms = 0;
public $year = 0;

/**
* Get the string representation of this priority.
*
* @return string
*/
public function display()
{
$terms = '';
if ($this->terms >= count(config('defaults.priority.terms'))) {
$terms = config('defaults.priority.default');
} else {
$terms = config('defaults.priority.terms')[$this->terms];
}

$year = '';
if ($this->year >= count(config('defaults.status_codes'))) {
$year = $this->year;
} else {
$year = config('defaults.status_codes')[$this->year];
}

return $terms.' | '.$year;
}

/**
* Get the HTML representation of this priority.
*
* @return string
*/
public function html()
{
return str_replace(' | ', ' &bull; ', $this->display());
}

/**
* Get the zone letter corresponding to this priority's terms.
*
* @return string
*/
public function zone()
{
$letters = range('J', 'A');

return $this->terms < count($letters) ? $letters[$this->terms] : 'A';
}
}
79 changes: 79 additions & 0 deletions app/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,83 @@ public function getBoostedAttribute()

return false;
}

/**
* Compute how much of a priority boost the show should get. Returns "Z+N"
* if the show is entitled to an N-zone boost, "S" if the show is titled to
* skipping track order, or "A" if the priority should be set to A1 within
* the track.
*
* @return string|null
*/
public function getBoostAttribute()
{
if (! $this->boosted) {
return;
}

$string = '';
foreach ($this->hosts as $host) {
if ($host->membership->boost == 'S') {
$string = 'S';
break;
} elseif ($host->membership->boost == 'A1') {
$string = 'A1';
} elseif ($host->membership->boost[0] == '+' and $string != 'A1') {
$zones = intval(substr($host->membership->boost));
$current_zones = ($string[0] == 'Z' ? intval(substr($string, 2)) : 0);
if ($zones > $current_zones) {
$string = 'Z+'.$zones;
}
}
}

return $string;
}

/**
* Generate the show's priority string.
*
* @return string
*/
public function getPriorityAttribute()
{
$priorities = $this->hosts->pluck('priority');
$zone = '';
$group = '';

$terms = $priorities->max->terms ?? 0;
if ($this->boosted and $this->boost[0] == 'Z') {
$terms += intval(substr($this->boost, 2));
} elseif ($this->boosted and $this->boost == 'A1') {
return 'A1';
}

if ($this->track->zone) {
$zone = $this->track->zone;
} elseif ($terms >= count(config('defaults.priority.terms'))) {
$zone = config('defaults.priority.default');
} else {
$zone = config('defaults.priority.terms')[$terms];
}

$year = $priorities->min->year;
if ($this->track->group !== null) {
$group = $this->track->group;
} elseif ($year == 0) {
$zone = config('defaults.priority.none');
} elseif ($year >= count(config('defaults.status_codes')) and $year < 1000) {
$zone = config('defaults.priority.default');
} elseif (strlen($zone) == 2) {
$group = '';
} else {
$group = $year - $this->term->year + ($this->term->boosted ? 1 : 0);
if ($group <= 0) {
$group = '';
$zone = config('defaults.priority.default');
}
}

return $zone.$group;
}
}
20 changes: 20 additions & 0 deletions app/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,24 @@ public function getNameAttribute()
return str_replace('_', ' ', title_case($components[1])).' '.$components[0];
}
}

/**
* Get the shows attached to this term.
*
* @return Eloquent\Collection<KRLX\Show>
*/
public function shows()
{
return $this->hasMany('KRLX\Show');
}

/**
* Get the year attached to the term.
*
* @return int
*/
public function getYearAttribute()
{
return intval(explode('-', $this->id)[0]);
}
}
58 changes: 58 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class User extends Authenticatable
'password', 'remember_token', 'created_at', 'updated_at',
];

/**
* The attributes that should be added to arrays.
*
* @var array
*/
protected $appends = [
'full_name',
];

/**
* The events that should be dispatched.
*
Expand All @@ -39,6 +48,15 @@ class User extends Authenticatable
'creating' => UserCreating::class,
];

/**
* The attributes that should be type-cast.
*
* @var array
*/
protected $casts = [
'xp' => 'array',
];

/**
* Returns the shows that the user is a member of.
*
Expand Down Expand Up @@ -67,6 +85,46 @@ public function invitations()
->as('membership');
}

/**
* Generate the Priority object corresponding to this user.
*
* @return KRLX\Priority
*/
public function getPriorityAttribute()
{
$priority = new Priority;
$priority->terms = collect($this->xp)->unique()->count();
$priority->year = $this->year;

return $priority;
}

/**
* Gets the user's "full name". For Carls and alumni, this appends the class
* name onto the end of the user's full name.
*
* @return string
*/
public function getFullNameAttribute()
{
if ($this->year >= 1000) {
return $this->name." '".substr($this->year, -2);
} else {
return $this->name;
}
}

/**
* Gets the user's "public name", seen by folks who aren't logged in.
* TODO: Implement last-name splits.
*
* @return string
*/
public function getPublicNameAttribute()
{
return $this->nickname ?? $this->name;
}

/**
* Send a password reset notification.
*
Expand Down
Loading

0 comments on commit 0735d0c

Please sign in to comment.