123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
-
- require_once __DIR__ . "/required.php";
-
- $question = $VARS['question'];
- $answers = $VARS['answers'];
-
- if (trim($question) == "" || trim($answers) == "") {
- header("Location: ./create/error");
- }
-
- /**
- * Generate question access codes.
- * https://stackoverflow.com/a/4356295
- * @param int $length
- * @return string
- */
- function generateRandomString($length = 4) {
- $characters = 'ABCEFGHJKLMNPQRSTWXYZ';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- return $randomString;
- }
-
- $length = 4;
- $code = generateRandomString($length);
- $tries = 0;
- while ($database->has("questions", ['qcode' => $code])) {
- if ($tries > 10) {
- $length++;
- }
- $code = generateRandomString($length);
- $tries++;
- }
-
- $database->insert("questions", ['qtext' => $question, 'qcode' => $code]);
-
- $qid = $database->id();
-
- $answers = explode("\n", $answers);
-
- foreach ($answers as $ans) {
- if (trim($ans) != "") {
- $database->insert("answers", ['atext' => trim($ans), 'qid' => $qid]);
- }
- }
-
- header("Location: ./q/$code");
|