Skip to content

Commit

Permalink
Release v2.11.0
Browse files Browse the repository at this point in the history
Commissioners can modify Player Picks
  • Loading branch information
Wilson Wise committed Sep 15, 2014
2 parents 3a267f1 + 5c0367b commit 37a83b8
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 6 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.10.0
v2.11.0
2 changes: 1 addition & 1 deletion controllers/AdminChallengeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static function challenge_add() {
);
$temp = new StdClass;
$temp->cid = $db->insert_id;
$temp->sid = 999;
$temp->sid = FC_DEFAULT_VALUE;
$user_challenges[] = $temp;
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static function find_missing_challenges($arr, $winners) {
// If it's not in TEMP, put it there with a bad pick
$missing = new StdClass;
$missing->cid = $key;
$missing->sid = 9999;
$missing->sid = FC_DEFAULT_VALUE;

// Add it to their arr
$arr[] = $missing;
Expand Down
268 changes: 268 additions & 0 deletions controllers/AdminPlayerPicksController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
<?php

class AdminPlayerPicksController extends AdminController
{
static function players() {
if (self::checkPerms()) {
$db = option('db');
$log = option('log');

$players = array();
$query = 'SELECT uid, username FROM {{users}} ORDER BY username';

if ($result = $db->qry($query)) {
while ($obj = $result->fetch_object()) {
$players[] = $obj;
}
}

return self::template('admin/player-picks-list.html.twig', array(
'page_name' => 'Pick Player to Edit Picks',
'players' => $players,
'back_url' => '..',
));
}
else {
halt(FORBIDDEN);
}
}

static function player_picks($uid=FALSE) {
if (self::checkPerms()) {
$db = option('db');
$log = option('log');

$username = self::get_username($uid);

if (!$username) {
halt(NOT_FOUND);
}

// Week[] = {num: 1, status: true|false}
$week_limit = (int) option('challenge_week');
$weeks = array();
for ($i = 1; $i <= $week_limit; $i++) {
$status = self::has_submission_for_week($username, $i);
$weeks[] = array(
'num' => $i,
'status' => $status,
);
}

return self::template('admin/player-picks-list.html.twig', array(
'page_name' => ': Pick the Week to Edit',
'username' => $username,
'weeks' => $weeks,
'back_url' => option('base_uri') . 'commissioner/players/picks',
));
}
else {
halt(FORBIDDEN);
}
}

static function player_picks_week($uid=FALSE, $week=FALSE) {
if (self::checkPerms()) {
$week = (int) $week;
$week_limit = (int) option('challenge_week');

if ($week && $week <= $week_limit) {
$db = option('db');
$log = option('log');

$username = self::get_username($uid);
$subkey = self::get_subkey($username, $week);

if (!$username) {
halt(NOT_FOUND);
}

// Get the submission object
$submission = NULL;
$submission_query = 'SELECT subvalue FROM {{submissions}} WHERE subkey = "%s"';
if ($result = $db->qry($submission_query, $subkey)) {
while ($obj = $result->fetch_object()) {
$submission = unserialize($obj->subvalue);
}
}
else {
$log->log('error', 'Could not fetch user submission for '. $subkey, $db->error);
}

// Challenges
// {home: {sid, name, selected}, away: {sid, name, selected}, none: {selected}}
$challenges = array();
$challenge_query = 'SELECT
c.cid, c.home_sid AS home_sid, hs.school AS home_school, hs.conference AS home_conf, c.away_sid AS away_sid,
vs.school AS away_school, vs.conference AS away_conf, c.closetime, c.winner_sid AS winner_sid, c.gametime
FROM {{challenges}} c, {{schools}} hs, {{schools}} vs
WHERE c.home_sid = hs.sid AND c.away_sid = vs.sid AND c.year = %d AND c.week = %d';

if ($result = $db->qry($challenge_query, FC_YEAR, $week)) {
while ($obj = $result->fetch_object()) {
$challenges[] = $obj;
}
}

// Sync player submission data to challenges
foreach ($challenges as $challenge) {
if ($submission) {
$challenge->user_selection = self::get_user_selection($challenge->cid, $submission->challenges);
}
else {
$challenge->user_selection = FC_DEFAULT_VALUE;
}
}

return self::template('admin/player-edit-picks.html.twig', array(
'page_name' => sprintf('Edit %s\'s Picks', $username),
'challenges' => $challenges,
'action' => $submission ? 'Update' : 'Create',
));
}
else {
halt(NOT_FOUND);
}
}
else {
halt(FORBIDDEN);
}
}

static function save_player_picks_week($uid=FALSE, $week=FALSE) {
if (self::checkPerms()) {
$week = (int) $week;
$week_limit = (int) option('challenge_week');

if ($week && $week <= $week_limit) {
$db = option('db');
$log = option('log');

$username = self::get_username($uid);
$subkey = self::get_subkey($username, $week);

// Get all the challenge id so we can get the post info
$challenges = array();
$query = 'SELECT cid FROM {{challenges}} WHERE year = %s AND week = %s';
if ($result = $db->qry($query, FC_YEAR, $week)) {
while ($obj = $result->fetch_object()) {
$temp = new StdClass;
$temp->cid = $obj->cid;
$temp->sid = get_post('challenge-' . $obj->cid);
$challenges[] = $temp;
}
}
else {
$log->log('error', 'Unable to get challenges to update player picks.', $db->error);
}

$submission = new StdClass;
$submission->user = $username;
$submission->week = $week;
$submission->challenges = $challenges;
$submission = serialize($submission);

// Insert or Update?
$is_create = (get_post('submit') === 'Create Picks');
if ($is_create) {
// s-subkey, s-name, i-uid, s-week, s-year, s-subvalue
$db->setQuery(
'submission',
'INSERT INTO {{submissions}} VALUES ("%s", "%s", %s, "%s", "%s", "%s")',
$db->escape_string($subkey),
$db->escape_string($username),
$uid,
$db->escape_string($week),
FC_YEAR,
$db->escape_string($submission)
);
}
else {
$db->setQuery(
'submission',
'UPDATE {{submissions}} SET subvalue = "%s" WHERE subkey = "%s"',
$db->escape_string($submission),
$db->escape_string($subkey)
);
}

$action = $is_create ? 'created' : 'updated';
if ($result = $db->useQuery('submission')) {
$message = sprintf('%s\'s picks were %s for week %s', $username, $action, $week);
flash('message', $message);
}
else {
$log->log('error', 'Issue creating/updating user picks.', $db->error, $db->getLastQuery());
$message = sprintf('%s\'s picks were NOT %s for week %s', $username, $action, $week);
flash('error', $message);
}
$url = sprintf('/commissioner/player/%s/picks', $uid);
redirect_to($url);
}
else {
halt(NOT_FOUND);
}
}
else {
halt(FORBIDDEN);
}
}

/* Helpers */

static function has_submission_for_week($username, $week) {
$db = option('db');
$log = option('log');

$subkey = self::get_subkey($username, $week);
$query = 'SELECT subvalue FROM {{submissions}} WHERE subkey = "%s"';
$has_submission = FALSE;

if ($result = $db->qry($query, $subkey)) {
if ($result->num_rows === 1) {
$has_submission = TRUE;
}
}
else {
$log->log('error', 'Failed to get submission', $db->error);
}

return $has_submission;
}

static function get_user_selection($cid, $challenges) {
$sid = FC_DEFAULT_VALUE;

foreach ($challenges as $challenge) {
if ($challenge->cid === $cid) {
$sid = $challenge->sid;
}
}

return $sid;
}

static function get_subkey($username, $week) {
return sprintf('%s-%s-%s', $username, FC_YEAR, $week);
}

static function get_username($uid) {
$db = option('db');
$log = option('log');

$uid = $db->escape_string($uid);
$username = NULL;

$player_query = 'SELECT username FROM {{users}} WHERE uid = %s';
if ($result = $db->qry($player_query, $uid)) {
while ($obj = $result->fetch_object()) {
$username = $obj->username;
}
}

return $username;
}

}

?>
8 changes: 5 additions & 3 deletions controllers/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ static public function autoload($class) {

// Template method
static function template($template, $extra_params = array()) {
$twig = option('twig');
$params = array_merge(option(), $extra_params);
$tmpl = $twig->loadTemplate($template);
$twig = option('twig');
$constants = get_defined_constants(TRUE);
$params = array_merge(option(), $constants['user'], $extra_params);
$tmpl = $twig->loadTemplate($template);

return $tmpl->render($params);
}

Expand Down
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
define('FC_COOKIE', 'football-challenge');
define('FC_YEAR', 2014);
define('FC_NUM_WEEKS', 15);
define('FC_DEFAULT_VALUE', 9999);
define('FC_NUM_CHALLENGES', 10);

// How much to truncate the Messages when a new challenge is created.
Expand Down
8 changes: 8 additions & 0 deletions public/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,11 @@ table td.padded {
background-color: #fee;
color: #900;
}
hr {
border: 0;
height: 1px;
background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -moz-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -ms-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -o-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
}
5 changes: 5 additions & 0 deletions routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
dispatch_put('/commissioner/player/:id/edit', 'AdminPlayerController::player_do_edit');
dispatch('/commissioner/player/:id/delete', 'AdminPlayerController::player_show_delete');
dispatch_delete('/commissioner/player/:id/delete', 'AdminPlayerController::player_do_delete');
// Player's Picks
dispatch('/commissioner/players/picks', 'AdminPlayerPicksController::players');
dispatch('/commissioner/player/:uid/picks', 'AdminPlayerPicksController::player_picks');
dispatch('/commissioner/player/:uid/picks/:week', 'AdminPlayerPicksController::player_picks_week');
dispatch_post('/commissioner/player/:uid/picks/:week', 'AdminPlayerPicksController::save_player_picks_week');

/*
* == UserController ==
Expand Down
24 changes: 24 additions & 0 deletions views/templates/admin/home.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{% extends "admin/admin.html.twig" %}

{% block head %}
{{ parent() }}
<style type="text/css">
p {
font-size: 10px;
color: gray;
}
</style>
{% endblock %}

{% block content %}
{{ parent() }}

Expand All @@ -15,18 +25,32 @@
</table>

<h3>Weekly Challenge Tasks</h3>
<p>These tasks allow a Commissioner to Create or Modify a Challenge. Additionally, they allow the Commissioner to create standing and check the status of games.</p>
<ul>
<li>{{ _self.challenge_link(challenge_state, base_admin, macros) }}</li>
<li>{{ macros.link("/standings", "Re-run Standings", base_admin) }}</li>
<li>{{ macros.link("/challenge/check", "Check Games", base_admin) }} <span style="color: #999; font-size: smaller;">(This is automated)</span></li>
</ul>

<hr />

<h3>Player Tasks</h3>
<p>These tasks allow a Commissioner to Create, Modify or Delete a Player.
<ul>
<li>{{ macros.link("/player/new", "Create New Player", base_admin) }}</li>
<li>{{ macros.link("/players", "View All Players", base_admin) }}</li>
</ul>

<hr />

<h3>Player Challenge Tasks</h3>
<p>These tasks allow a Commissioner to modify a Player's Picks. Use with caution.</p>
<ul>
<li>{{ macros.link("/players/picks", "Edit a Player's Picks", base_admin) }}</li>
</ul>

<hr />

<h3>Other Tasks</h3>
<ul>
<li>{{ macros.link("/logs", "View Logs", base_admin) }}</li>
Expand Down
Loading

0 comments on commit 37a83b8

Please sign in to comment.