Browse Source

Add named responses

Skylar Ittner 10 months 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 @@
1 1
 -- MySQL Script generated by MySQL Workbench
2
+-- Mon 05 Feb 2018 11:09:37 PM MST
2 3
 -- Model: New Model    Version: 1.0
3 4
 -- MySQL Workbench Forward Engineering
4 5
 
@@ -55,6 +55,7 @@ CREATE TABLE IF NOT EXISTS `openquestion`.`responses` (
55 55
   `aid` INT NOT NULL,
56 56
   `qid` INT NOT NULL,
57 57
   `timestamp` DATETIME NOT NULL,
58
+  `name` VARCHAR(100) NULL,
58 59
   PRIMARY KEY (`rid`, `aid`, `qid`),
59 60
   UNIQUE INDEX `rid_UNIQUE` (`rid` ASC),
60 61
   INDEX `fk_responses_answers1_idx` (`aid` ASC, `qid` ASC),

+ 14
- 0
getresults.php View File

@@ -2,6 +2,8 @@
2 2
 
3 3
 require_once __DIR__ . "/required.php";
4 4
 
5
+header('Content-Type: application/json');
6
+
5 7
 $qid = $VARS['qid'];
6 8
 
7 9
 if (!$database->has('questions', ['qid' => $qid])) {
@@ -22,4 +24,16 @@ foreach ($answers as $a) {
22 24
     ];
23 25
 }
24 26
 
27
+$responses = $database->select('responses', ['name', 'aid'], ['qid' => $qid]);
28
+$data['responses'] = [];
29
+foreach ($responses as $r) {
30
+    if ($r['name'] == null) {
31
+        $r['name'] = "Anonymous";
32
+    }
33
+    $data['responses'][] = [
34
+        "name" => $r['name'],
35
+        "aid" => $r['aid']
36
+    ];
37
+}
38
+
25 39
 echo json_encode($data);

+ 9
- 0
login.php View File

@@ -0,0 +1,9 @@
1
+<?php
2
+
3
+require __DIR__ . "/required.php";
4
+
5
+if (isset($VARS['name'])) {
6
+    $_SESSION['name'] = $VARS['name'];
7
+}
8
+
9
+header('Location: ' . SITE_PATH . 'r/' . $VARS['code']);

+ 73
- 26
pages/question.php View File

@@ -18,7 +18,7 @@ include __DIR__ . "/../bits/navbar.php";
18 18
     $qid = $database->get('questions', 'qid', ['qcode' => $arg1]);
19 19
     ?>
20 20
 
21
-    <div class="row my-4">
21
+    <div class="row mt-4">
22 22
         <div class="col-12 col-md-6">
23 23
             <h1><?php echo $database->get('questions', 'qtext', ['qid' => $qid]); ?></h1>
24 24
         </div>
@@ -36,33 +36,74 @@ include __DIR__ . "/../bits/navbar.php";
36 36
             </div>
37 37
         </div>
38 38
     </div>
39
-    <div class="list-group">
40
-        <?php
41
-        $total = $database->count('responses', ['qid' => $qid]);
42
-        $answers = $database->select('answers', ['aid', 'atext'], ['qid' => $qid]);
43
-        $colors = ["primary", "success", "danger", "info", "warning"];
44
-        $color = -1;
45
-        foreach ($answers as $a) {
46
-            $color++;
47
-            if ($color > count($colors) - 1) {
48
-                $color = 0;
39
+
40
+    <div class="nav nav-tabs">
41
+        <a class="nav-item nav-link active" data-toggle="tab" href="#totals" role="tab">Totals</a>
42
+        <a class="nav-item nav-link" data-toggle="tab" href="#results" role="tab">Results</a>
43
+    </div>
44
+
45
+    <div class="tab-content">
46
+        <div class="list-group tab-pane fade show active" id="totals" role="tabpanel">
47
+            <?php
48
+            $total = $database->count('responses', ['qid' => $qid]);
49
+            $answers = $database->select('answers', ['aid', 'atext'], ['qid' => $qid]);
50
+            $colors = ["primary", "success", "danger", "info", "warning"];
51
+            $color = -1;
52
+            foreach ($answers as $a) {
53
+                $color++;
54
+                if ($color > count($colors) - 1) {
55
+                    $color = 0;
56
+                }
57
+                $votes = $database->count('responses', ['aid' => $a['aid']]);
58
+                ?>
59
+                <div class="list-group-item">
60
+                    <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>
61
+                    <style nonce="<?php echo $SECURE_NONCE; ?>">
62
+                        #progress_<?php echo $a['aid']; ?> {
63
+                            width: <?php echo ($votes / $total) * 100.0; ?>%;
64
+                        }
65
+                    </style>
66
+                    <div class="progress">
67
+                        <div id="progress_<?php echo $a['aid']; ?>" class="progress-bar bg-<?php echo $colors[$color]; ?>" role="progressbar"></div>
68
+                    </div>
69
+                </div>
70
+                <?php
49 71
             }
50
-            $votes = $database->count('responses', ['aid' => $a['aid']]);
51 72
             ?>
52
-            <div class="list-group-item">
53
-                <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>
54
-                <style nonce="<?php echo $SECURE_NONCE; ?>">
55
-                    #progress_<?php echo $a['aid']; ?> {
56
-                        width: <?php echo ($votes / $total) * 100.0; ?>%;
57
-                    }
58
-                </style>
59
-                <div class="progress">
60
-                    <div id="progress_<?php echo $a['aid']; ?>" class="progress-bar bg-<?php echo $colors[$color]; ?>" role="progressbar"></div>
61
-                </div>
62
-            </div>
73
+        </div>
74
+        <div class="tab-pane fade card-columns pt-3" id="results" role="tabpanel">
63 75
             <?php
64
-        }
65
-        ?>
76
+            $color = -1;
77
+            foreach ($answers as $a) {
78
+                $color++;
79
+                if ($color > count($colors) - 1) {
80
+                    $color = 0;
81
+                }
82
+                ?>
83
+                <div class="card border-<?php echo $colors[$color]; ?>">
84
+                    <div class="card-body">
85
+                        <h4 class="card-title text-<?php echo $colors[$color]; ?>"><?php echo $a['atext']; ?></h4>
86
+                        <div class="list-group" id="answer_<?php echo $a['aid']; ?>_list">
87
+                            <?php
88
+                            $users = $database->select('responses', 'name', ['aid' => $a['aid']]);
89
+                            foreach ($users as $u) {
90
+                                if ($u == null) {
91
+                                    $u = "Anonymous";
92
+                                }
93
+                                ?>
94
+                                <div class="list-group-item">
95
+                                    <?php echo $u; ?>
96
+                                </div>
97
+                                <?php
98
+                            }
99
+                            ?>
100
+                        </div>
101
+                    </div>
102
+                </div>
103
+                <?php
104
+            }
105
+            ?>
106
+        </div>
66 107
     </div>
67 108
 </div>
68 109
 <script nonce="<?php echo $SECURE_NONCE; ?>">
@@ -70,12 +111,18 @@ include __DIR__ . "/../bits/navbar.php";
70 111
         $.getJSON('<?php echo SITE_PATH; ?>getresults.php', {
71 112
             qid: <?php echo $qid; ?>
72 113
         }, function (data) {
73
-            var total = data['total'];
74 114
             var answers = data['answers'];
115
+            var responses = data['responses'];
75 116
             for (var i = 0; i < answers.length; i++) {
76 117
                 $('#progress_' + answers[i]['id']).css('width', answers[i]['percent']);
77 118
                 $('#votes_' + answers[i]['id']).text(answers[i]['votes'] + " votes");
78 119
             }
120
+            for (var i = 0; i < responses.length; i++) {
121
+                $('#answer_' + responses[i]['aid'] + "_list").html("");
122
+            }
123
+            for (var i = 0; i < responses.length; i++) {
124
+                $('#answer_' + responses[i]['aid'] + "_list").append("<div class=\"list-group-item\">" + responses[i]['name'] + "</div>");
125
+            }
79 126
         });
80 127
     }, 1000);
81 128
 </script>

+ 29
- 37
pages/respond.php View File

@@ -18,18 +18,16 @@
18 18
             <div class="row justify-content-center mt-4">
19 19
                 <div class="col-12 col-md-6 col-lg-4">
20 20
                     <div class="card border-primary">
21
-                        <div class="card-body">
21
+                        <form class="card-body" action="<?php echo SITE_PATH; ?>login.php" method="POST">
22 22
                             <h5 class="card-title">Enter question code:</h5>
23
-                            <input type="text" class="form-control my-2" id="code" placeholder="XXXX" />
24
-                            <button class="btn btn-primary btn-block" id="gobtn">
23
+                            <?php if (!isset($_SESSION['name'])) { ?>
24
+                                <input type="text" class="form-control my-2" name="name" placeholder="Name" required />
25
+                            <?php } ?>
26
+                            <input type="text" class="form-control my-2" name="code" placeholder="XXXX" required />
27
+                            <button class="btn btn-primary btn-block" type="submit">
25 28
                                 Go
26 29
                             </button>
27
-                            <script nonce="<?php echo $SECURE_NONCE; ?>">
28
-                                $("#gobtn").click(function () {
29
-                                    document.location.href = "<?php echo SITE_PATH; ?>r/" + $("#code").val();
30
-                                });
31
-                            </script>
32
-                        </div>
30
+                        </form>
33 31
                     </div>
34 32
                 </div>
35 33
             </div>
@@ -67,20 +65,18 @@
67 65
                     </div>
68 66
                 </div>
69 67
             </div>
70
-            <div class="col-12 col-md-6 col-lg-4">
68
+            <div class="col-12 col-md-6 col-lg-4 mt-2 mt-md-0">
71 69
                 <div class="card border-primary">
72
-                    <div class="card-body">
70
+                    <form class="card-body" action="<?php echo SITE_PATH; ?>login.php" method="POST">
73 71
                         <h5 class="card-title">Enter question code:</h5>
74
-                        <input type="text" class="form-control my-2" id="code" placeholder="XXXX" />
75
-                        <button class="btn btn-primary btn-block" id="gobtn">
72
+                        <?php if (!isset($_SESSION['name'])) { ?>
73
+                            <input type="text" class="form-control my-2" name="name" placeholder="Name" required />
74
+                        <?php } ?>
75
+                        <input type="text" class="form-control my-2" name="code" placeholder="XXXX" required />
76
+                        <button class="btn btn-primary btn-block" type="submit">
76 77
                             Go
77 78
                         </button>
78
-                        <script nonce="<?php echo $SECURE_NONCE; ?>">
79
-                            $("#gobtn").click(function () {
80
-                                document.location.href = "<?php echo SITE_PATH; ?>r/" + $("#code").val();
81
-                            });
82
-                        </script>
83
-                    </div>
79
+                    </form>
84 80
                 </div>
85 81
             </div>
86 82
         </div>
@@ -91,18 +87,16 @@
91 87
         <div class="row justify-content-center mt-4">
92 88
             <div class="col-12 col-md-6 col-lg-4">
93 89
                 <div class="card border-primary">
94
-                    <div class="card-body">
90
+                    <form class="card-body" action="<?php echo SITE_PATH; ?>login.php" method="POST">
95 91
                         <h5 class="card-title">Enter question code:</h5>
96
-                        <input type="text" class="form-control my-2" id="code" placeholder="XXXX" />
97
-                        <button class="btn btn-primary btn-block" id="gobtn">
92
+                        <?php if (!isset($_SESSION['name'])) { ?>
93
+                            <input type="text" class="form-control my-2" name="name" placeholder="Name" required />
94
+                        <?php } ?>
95
+                        <input type="text" class="form-control my-2" name="code" placeholder="XXXX" required />
96
+                        <button class="btn btn-primary btn-block" type="submit">
98 97
                             Go
99 98
                         </button>
100
-                        <script nonce="<?php echo $SECURE_NONCE; ?>">
101
-                            $("#gobtn").click(function () {
102
-                                document.location.href = "<?php echo SITE_PATH; ?>r/" + $("#code").val();
103
-                            });
104
-                        </script>
105
-                    </div>
99
+                    </form>
106 100
                 </div>
107 101
             </div>
108 102
         </div>
@@ -113,18 +107,16 @@
113 107
         <div class="row justify-content-center mt-4">
114 108
             <div class="col-12 col-md-6 col-lg-4">
115 109
                 <div class="card border-primary">
116
-                    <div class="card-body">
110
+                    <form class="card-body" action="<?php echo SITE_PATH; ?>login.php" method="POST">
117 111
                         <h5 class="card-title">Enter question code:</h5>
118
-                        <input type="text" class="form-control my-2" id="code" placeholder="XXXX" />
119
-                        <button class="btn btn-primary btn-block" id="gobtn">
112
+                        <?php if (!isset($_SESSION['name'])) { ?>
113
+                            <input type="text" class="form-control my-2" name="name" placeholder="Name" required />
114
+                        <?php } ?>
115
+                        <input type="text" class="form-control my-2" name="code" placeholder="XXXX" required />
116
+                        <button class="btn btn-primary btn-block" type="submit">
120 117
                             Go
121 118
                         </button>
122
-                        <script nonce="<?php echo $SECURE_NONCE; ?>">
123
-                            $("#gobtn").click(function () {
124
-                                document.location.href = "<?php echo SITE_PATH; ?>r/" + $("#code").val();
125
-                            });
126
-                        </script>
127
-                    </div>
119
+                    </form>
128 120
                 </div>
129 121
             </div>
130 122
         </div>

+ 1
- 1
vote.php View File

@@ -17,7 +17,7 @@ if ($_SESSION['voted'][$qid] === true) {
17 17
     die();
18 18
 }
19 19
 
20
-$database->insert("responses", ['qid' => $qid, 'aid' => $aid, 'timestamp' => date('Y-m-d H:i:s')]);
20
+$database->insert("responses", ['qid' => $qid, 'aid' => $aid, 'timestamp' => date('Y-m-d H:i:s'), 'name' => $_SESSION['name']]);
21 21
 
22 22
 $_SESSION['voted'][$qid] = true;
23 23