A simple system for creating multiple-choice questions for an audience to answer on their phones. Responses are tallied and displayed in real-time. https://openquestion.netsyms.com

add.php 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. require_once __DIR__ . "/required.php";
  3. $question = $VARS['question'];
  4. $answers = $VARS['answers'];
  5. if (trim($question) == "" || trim($answers) == "") {
  6. header("Location: ./create/error");
  7. }
  8. /**
  9. * Generate question access codes.
  10. * https://stackoverflow.com/a/4356295
  11. * @param int $length
  12. * @return string
  13. */
  14. function generateRandomString($length = 4) {
  15. $characters = 'ABCEFGHJKLMNPQRSTWXYZ';
  16. $charactersLength = strlen($characters);
  17. $randomString = '';
  18. for ($i = 0; $i < $length; $i++) {
  19. $randomString .= $characters[rand(0, $charactersLength - 1)];
  20. }
  21. return $randomString;
  22. }
  23. $length = 4;
  24. $code = generateRandomString($length);
  25. $tries = 0;
  26. while ($database->has("questions", ['qcode' => $code])) {
  27. if ($tries > 10) {
  28. $length++;
  29. }
  30. $code = generateRandomString($length);
  31. $tries++;
  32. }
  33. $database->insert("questions", ['qtext' => $question, 'qcode' => $code]);
  34. $qid = $database->id();
  35. $answers = explode("\n", $answers);
  36. foreach ($answers as $ans) {
  37. if (trim($ans) != "") {
  38. $database->insert("answers", ['atext' => trim($ans), 'qid' => $qid]);
  39. }
  40. }
  41. header("Location: ./q/$code");