Browse Source

Add named responses

master
Skylar Ittner 1 year ago
parent
commit
200762399d
7 changed files with 128 additions and 64 deletions
  1. BIN
      database.mwb
  2. 2
    0
      database.sql
  3. 14
    0
      getresults.php
  4. 9
    0
      login.php
  5. 73
    26
      pages/question.php
  6. 29
    37
      pages/respond.php
  7. 1
    1
      vote.php

BIN
database.mwb View File


+ 2
- 0
database.sql View File

@@ -1,5 +1,5 @@
-- MySQL Script generated by MySQL Workbench
-- Mon 05 Feb 2018 11:09:37 PM MST
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering

@@ -55,6 +55,7 @@ CREATE TABLE IF NOT EXISTS `openquestion`.`responses` (
`aid` INT NOT NULL,
`qid` INT NOT NULL,
`timestamp` DATETIME NOT NULL,
`name` VARCHAR(100) NULL,
PRIMARY KEY (`rid`, `aid`, `qid`),
UNIQUE INDEX `rid_UNIQUE` (`rid` ASC),
INDEX `fk_responses_answers1_idx` (`aid` ASC, `qid` ASC),

+ 14
- 0
getresults.php View File

@@ -2,6 +2,8 @@

require_once __DIR__ . "/required.php";

header('Content-Type: application/json');

$qid = $VARS['qid'];

if (!$database->has('questions', ['qid' => $qid])) {
@@ -22,4 +24,16 @@ foreach ($answers as $a) {
];
}

$responses = $database->select('responses', ['name', 'aid'], ['qid' => $qid]);
$data['responses'] = [];
foreach ($responses as $r) {
if ($r['name'] == null) {
$r['name'] = "Anonymous";
}
$data['responses'][] = [
"name" => $r['name'],
"aid" => $r['aid']
];
}

echo json_encode($data);

+ 9
- 0
login.php View File

@@ -0,0 +1,9 @@
<?php

require __DIR__ . "/required.php";

if (isset($VARS['name'])) {
$_SESSION['name'] = $VARS['name'];
}

header('Location: ' . SITE_PATH . 'r/' . $VARS['code']);

+ 73
- 26
pages/question.php View File

@@ -18,7 +18,7 @@ include __DIR__ . "/../bits/navbar.php";
$qid = $database->get('questions', 'qid', ['qcode' => $arg1]);
?>

<div class="row my-4">
<div class="row mt-4">
<div class="col-12 col-md-6">
<h1><?php echo $database->get('questions', 'qtext', ['qid' => $qid]); ?></h1>
</div>
@@ -36,33 +36,74 @@ include __DIR__ . "/../bits/navbar.php";
</div>
</div>
</div>
<div class="list-group">
<?php
$total = $database->count('responses', ['qid' => $qid]);
$answers = $database->select('answers', ['aid', 'atext'], ['qid' => $qid]);
$colors = ["primary", "success", "danger", "info", "warning"];
$color = -1;
foreach ($answers as $a) {
$color++;
if ($color > count($colors) - 1) {
$color = 0;

<div class="nav nav-tabs">
<a class="nav-item nav-link active" data-toggle="tab" href="#totals" role="tab">Totals</a>
<a class="nav-item nav-link" data-toggle="tab" href="#results" role="tab">Results</a>
</div>

<div class="tab-content">
<div class="list-group tab-pane fade show active" id="totals" role="tabpanel">
<?php
$total = $database->count('responses', ['qid' => $qid]);
$answers = $database->select('answers', ['aid', 'atext'], ['qid' => $qid]);
$colors = ["primary", "success", "danger", "info", "warning"];
$color = -1;
foreach ($answers as $a) {
$color++;
if ($color > count($colors) - 1) {
$color = 0;
}
$votes = $database->count('responses', ['aid' => $a['aid']]);
?>
<div class="list-group-item">
<h3 class="d-flex"><?php echo $a['atext']; ?> <span class="small ml-auto" id="votes_<?php echo $a['aid']; ?>"><?php echo $votes; ?> votes</span></h3>
<style nonce="<?php echo $SECURE_NONCE; ?>">
#progress_<?php echo $a['aid']; ?> {
width: <?php echo ($votes / $total) * 100.0; ?>%;
}
</style>
<div class="progress">
<div id="progress_<?php echo $a['aid']; ?>" class="progress-bar bg-<?php echo $colors[$color]; ?>" role="progressbar"></div>
</div>
</div>
<?php
}
$votes = $database->count('responses', ['aid' => $a['aid']]);
?>
<div class="list-group-item">
<h3 class="d-flex"><?php echo $a['atext']; ?> <span class="small ml-auto" id="votes_<?php echo $a['aid']; ?>"><?php echo $votes; ?> votes</span></h3>
<style nonce="<?php echo $SECURE_NONCE; ?>">
#progress_<?php echo $a['aid']; ?> {
width: <?php echo ($votes / $total) * 100.0; ?>%;
}
</style>
<div class="progress">
<div id="progress_<?php echo $a['aid']; ?>" class="progress-bar bg-<?php echo $colors[$color]; ?>" role="progressbar"></div>
</div>
</div>
</div>
<div class="tab-pane fade card-columns pt-3" id="results" role="tabpanel">
<?php
}
?>
$color = -1;
foreach ($answers as $a) {
$color++;
if ($color > count($colors) - 1) {
$color = 0;
}
?>
<div class="card border-<?php echo $colors[$color]; ?>">
<div class="card-body">
<h4 class="card-title text-<?php echo $colors[$color]; ?>"><?php echo $a['atext']; ?></h4>
<div class="list-group" id="answer_<?php echo $a['aid']; ?>_list">
<?php
$users = $database->select('responses', 'name', ['aid' => $a['aid']]);
foreach ($users as $u) {
if ($u == null) {
$u = "Anonymous";
}
?>
<div class="list-group-item">
<?php echo $u; ?>
</div>
<?php
}
?>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
<script nonce="<?php echo $SECURE_NONCE; ?>">
@@ -70,12 +111,18 @@ include __DIR__ . "/../bits/navbar.php";
$.getJSON('<?php echo SITE_PATH; ?>getresults.php', {
qid: <?php echo $qid; ?>
}, function (data) {
var total = data['total'];
var answers = data['answers'];
var responses = data['responses'];
for (var i = 0; i < answers.length; i++) {
$('#progress_' + answers[i]['id']).css('width', answers[i]['percent']);
$('#votes_' + answers[i]['id']).text(answers[i]['votes'] + " votes");
}
for (var i = 0; i < responses.length; i++) {
$('#answer_' + responses[i]['aid'] + "_list").html("");
}
for (var i = 0; i < responses.length; i++) {
$('#answer_' + responses[i]['aid'] + "_list").append("<div class=\"list-group-item\">" + responses[i]['name'] + "</div>");
}
});
}, 1000);
</script>

+ 29
- 37
pages/respond.php View File

@@ -18,18 +18,16 @@
<div class="row justify-content-center mt-4">
<div class="col-12 col-md-6 col-lg-4">
<div class="card border-primary">
<div class="card-body">
<form class="card-body" action="<?php echo SITE_PATH; ?>login.php" method="POST">
<h5 class="card-title">Enter question code:</h5>
<input type="text" class="form-control my-2" id="code" placeholder="XXXX" />
<button class="btn btn-primary btn-block" id="gobtn">
<?php if (!isset($_SESSION['name'])) { ?>
<input type="text" class="form-control my-2" name="name" placeholder="Name" required />
<?php } ?>
<input type="text" class="form-control my-2" name="code" placeholder="XXXX" required />
<button class="btn btn-primary btn-block" type="submit">
Go
</button>
<script nonce="<?php echo $SECURE_NONCE; ?>">
$("#gobtn").click(function () {
document.location.href = "<?php echo SITE_PATH; ?>r/" + $("#code").val();
});
</script>
</div>
</form>
</div>
</div>
</div>
@@ -67,20 +65,18 @@
</div>
</div>
</div>
<div class="col-12 col-md-6 col-lg-4">
<div class="col-12 col-md-6 col-lg-4 mt-2 mt-md-0">
<div class="card border-primary">
<div class="card-body">
<form class="card-body" action="<?php echo SITE_PATH; ?>login.php" method="POST">
<h5 class="card-title">Enter question code:</h5>
<input type="text" class="form-control my-2" id="code" placeholder="XXXX" />
<button class="btn btn-primary btn-block" id="gobtn">
<?php if (!isset($_SESSION['name'])) { ?>
<input type="text" class="form-control my-2" name="name" placeholder="Name" required />
<?php } ?>
<input type="text" class="form-control my-2" name="code" placeholder="XXXX" required />
<button class="btn btn-primary btn-block" type="submit">
Go
</button>
<script nonce="<?php echo $SECURE_NONCE; ?>">
$("#gobtn").click(function () {
document.location.href = "<?php echo SITE_PATH; ?>r/" + $("#code").val();
});
</script>
</div>
</form>
</div>
</div>
</div>
@@ -91,18 +87,16 @@
<div class="row justify-content-center mt-4">
<div class="col-12 col-md-6 col-lg-4">
<div class="card border-primary">
<div class="card-body">
<form class="card-body" action="<?php echo SITE_PATH; ?>login.php" method="POST">
<h5 class="card-title">Enter question code:</h5>
<input type="text" class="form-control my-2" id="code" placeholder="XXXX" />
<button class="btn btn-primary btn-block" id="gobtn">
<?php if (!isset($_SESSION['name'])) { ?>
<input type="text" class="form-control my-2" name="name" placeholder="Name" required />
<?php } ?>
<input type="text" class="form-control my-2" name="code" placeholder="XXXX" required />
<button class="btn btn-primary btn-block" type="submit">
Go
</button>
<script nonce="<?php echo $SECURE_NONCE; ?>">
$("#gobtn").click(function () {
document.location.href = "<?php echo SITE_PATH; ?>r/" + $("#code").val();
});
</script>
</div>
</form>
</div>
</div>
</div>
@@ -113,18 +107,16 @@
<div class="row justify-content-center mt-4">
<div class="col-12 col-md-6 col-lg-4">
<div class="card border-primary">
<div class="card-body">
<form class="card-body" action="<?php echo SITE_PATH; ?>login.php" method="POST">
<h5 class="card-title">Enter question code:</h5>
<input type="text" class="form-control my-2" id="code" placeholder="XXXX" />
<button class="btn btn-primary btn-block" id="gobtn">
<?php if (!isset($_SESSION['name'])) { ?>
<input type="text" class="form-control my-2" name="name" placeholder="Name" required />
<?php } ?>
<input type="text" class="form-control my-2" name="code" placeholder="XXXX" required />
<button class="btn btn-primary btn-block" type="submit">
Go
</button>
<script nonce="<?php echo $SECURE_NONCE; ?>">
$("#gobtn").click(function () {
document.location.href = "<?php echo SITE_PATH; ?>r/" + $("#code").val();
});
</script>
</div>
</form>
</div>
</div>
</div>

+ 1
- 1
vote.php View File

@@ -17,7 +17,7 @@ if ($_SESSION['voted'][$qid] === true) {
die();
}

$database->insert("responses", ['qid' => $qid, 'aid' => $aid, 'timestamp' => date('Y-m-d H:i:s')]);
$database->insert("responses", ['qid' => $qid, 'aid' => $aid, 'timestamp' => date('Y-m-d H:i:s'), 'name' => $_SESSION['name']]);

$_SESSION['voted'][$qid] = true;


Loading…
Cancel
Save