Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read watched listened #66

Merged
merged 6 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controller/PlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function index($pid) {
$row = $db->query("SELECT * FROM sg_ledger WHERE pid = " . $player['pid'] .
" AND metadata LIKE '%gamecode:$completion_gamecode%'")->fetchObject();
if ($row->lid) {
$completed_classic = 'Yes, completed on ' . date('F j, Y', $row->timestamp);
$completed_classic = date('F j, Y', $row->timestamp);
}
else {
$completed_classic = FALSE;
Expand Down
105 changes: 79 additions & 26 deletions src/Form/SummerGamePlayerConsumeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,53 @@ public function getFormId() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $pid = 0, $bnum = 0) {
public function buildForm(array $form, FormStateInterface $form_state, $pid = 0) {
$player = summergame_player_load($pid);
$guzzle = \Drupal::httpClient();
$api_url = \Drupal::config('arborcat.settings')->get('api_url');
$db = \Drupal::database();

$finished_default = 0;

$form = [
'#attributes' => ['class' => 'form-width-exception'],
];

if ($bnum) {
if ($_GET['bnum']) {
// Get Bib Record from API
$json = json_decode($guzzle->get("$api_url/record/$bnum/harvest")->getBody()->getContents());
$bib = $json->bib;
$json = json_decode($guzzle->get($api_url . '/record/' . $_GET['bnum'] . '/harvest')->getBody()->getContents(), TRUE);
$bib = $json['bib'];

if ($bib) {
$form['bib'] = [
$form['bnum'] = [
'#type' => 'value',
'#value' => $bib,
'#value' => $bib['id'],
];
$title = title_case($bib['title']);
$title = $bib['title'];
if ($bib['title_medium']) {
$title .= ' ' . title_case($bib['title_medium']);
}
$finished_default = 1;
}
}

$mat_types = $guzzle->get("$api_url/mat-names")->getBody()->getContents();
$mat_names = json_decode($mat_types, TRUE);

$form['pid'] = [
'#type' => 'value',
'#value' => $pid,
];
$form['message'] = [
'#markup' => '<p>Logging points for ' .
($player['nickname'] ? $player['nickname'] : $player['name']) . '</p>' .
'<p>Earn 50 points for each day that you log something!</p>'
'#markup' => '<p>Logging points for player: <strong>' .
($player['nickname'] ? $player['nickname'] : $player['name']) . '</strong>. ' .
'Earn 50 points for each day that you log something!</p>'
];
$form['mat_code'] = [
$form['consume_type'] = [
'#type' => 'select',
'#title' => t("I've been enjoying this"),
'#default_value' => $bib['mat_code'],
'#options' => $mat_names,
'#prefix' => "<div class=\"container-inline\">",
'#suffix' => "</div>",
'#options' => [
'read' => 'I read something',
'watch' => 'I watched something',
'listen' => 'I listened to something',
],
'#required' => TRUE,
];
$form['title'] = [
'#type' => 'textfield',
Expand All @@ -72,18 +79,55 @@ public function buildForm(array $form, FormStateInterface $form_state, $pid = 0,
'#description' => t('Title of the Book/Movie/Music that you are reporting'),
'#required' => TRUE,
];
$form['finished'] = [
'#type' => 'checkbox',
'#title' => 'and I finished it!',
'#default_value' => $finished_default,
'#description' => 'Add this title to your offical Summer Game log',
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Score!'),
'#prefix' => '<div class="sg-form-actions">'
];
$form['cancel'] = [
'#type' => 'link',
'#title' => 'Cancel',
'#title' => 'Return to Player Page',
'#url' => \Drupal\Core\Url::fromRoute('summergame.player'),
'#suffix' => '</div>'
];

// Display Classic Reading Game Log
$log_rows = [];
$player_log = summergame_get_player_log($pid);
foreach ($player_log as $log_row) {
$log_rows[] = [++$row_number, $log_row->description, date('F j', $log_row->timestamp)];
}

// Determine Classic Reading Game status
$completion_gamecode = \Drupal::config('summergame.settings')->get('summergame_completion_gamecode');
$row = $db->query("SELECT * FROM sg_ledger WHERE pid = :pid AND metadata LIKE :metadata",
[':pid' => $pid, ':metadata' => "%gamecode:$completion_gamecode%"])->fetchObject();
if ($row->lid) {
$log_text = 'You completed the Classic Reading Game on ' . date('F j, Y', $row->timestamp);
}
else if (count($log_rows) >= 10) {
// Completed, display the completion code
$log_text = "You've completed the Classic Reading Game! " .
'<a href="/summergame/player/' . $pid . '/gamecode?text=' . $completion_gamecode . '">' .
"Enter code $completion_gamecode to receive the Badge</a>";
}
else {
$log_text = 'Read/Listen to 10 items and mark them finished to complete the Classic Reading Game!';
}

$form['log_listing'] = [
'#prefix' => "<h2>Summer Game Log</h2><p>$log_text</p>",
'#type' => 'table',
'#header' => ['', 'Title', 'Finished Date'],
'#rows' => $log_rows,
];

return $form;

}
Expand All @@ -100,22 +144,31 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$db = \Drupal::database();

// check for daily read watch listen bonus
$bnum = $form_state->getValue('bnum');
$pid = $form_state->getValue('pid');
$consume_type = $form_state->getValue('consume_type');
$title = $form_state->getValue('title');
$type = 'Read Watched Listened';
$finished = $form_state->getValue('finished');

$points = 0;
$game_term = \Drupal::config('summergame.settings')->get('summergame_current_game_term');
$type = 'Read Watched Listened';
$metadata = [
'consume_type' => $consume_type,
];

// check for daily read watch listen bonus
$res = $db->query("SELECT * FROM sg_ledger WHERE pid=:pid AND type='Read Watched Listened Daily Bonus' ORDER BY lid DESC LIMIT 1", [':pid' => $pid])->fetch();
if (date('mdY', $res->timestamp) != date('mdY', time())) {
$type .= ' Daily Bonus';
$points = 50;
}

$points = summergame_player_points($pid, $points, $type, $title);
drupal_set_message("Earned $points points for $title");
if ($finished && ($consume_type == 'read' || $consume_type == 'listen')) {
$metadata['logged'] = 1;
}

$form_state->setRedirect('summergame.player', ['pid' => $pid]);
$points = summergame_player_points($pid, $points, $type, $title, $metadata);
drupal_set_message("Earned $points points for $title");

return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/SummerGamePlayerRedeemForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $pid = 0)
];
$form['cancel'] = [
'#type' => 'link',
'#title' => 'Cancel',
'#title' => 'Return to Player Page',
'#url' => \Drupal\Core\Url::fromRoute('summergame.player'),
'#suffix' => '</div>'
];
Expand Down
24 changes: 24 additions & 0 deletions summergame.module
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,30 @@ function summergame_get_player_points($pid, $game_term = '', $type = '') {
return $player_points;
}

/**
* UTILITY: Get a player's reading log
*/
function summergame_get_player_log($pid, $game_term = '') {
$db = \Drupal::database();
$log = [];

// Set game_term
if (empty($game_term)) {
$game_term = \Drupal::config('summergame.settings')->get('summergame_current_game_term');
}

$res = $db->query("SELECT * FROM sg_ledger " .
"WHERE pid = :pid " .
"AND metadata LIKE '%logged:1%' " .
"AND game_term = :game_term", [':pid' => $pid, ':game_term' => $game_term]);

while ($row = $res->fetchObject()) {
$log[] = $row;
}

return $log;
}

/**
* UTILITY: Save a player record
*/
Expand Down
12 changes: 11 additions & 1 deletion templates/summergame-player-info.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,19 @@
{% endif %}
{% endif %}
{% if progress|length %}
<h2>Game Point Limit Progress</h2>
<h2>Game Point Limits</h2>
<table class="account-summary not-fixed-table">
<tr><td><strong>Point Type</strong></td><td><strong>Progress</strong></td></tr>
<tr>
<td>Classic Reading Game</td>
<td>
{% if completed_classic %}
Completed on {{ completed_classic }}
{% else %}
<a href="/summergame/player/{{ player.pid }}/consume">Log 10 items to complete!</a>
{% endif %}
</td>
</tr>
{% for row in progress %}
<tr><td>{{ row.type }}</td><td>{{ row.total }} / {{ row.limit }}</td></tr>
{% endfor %}
Expand Down