You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
PHTML
51 lines
1.1 KiB
PHTML
6 years ago
|
<?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");
|