diff --git a/keyControlPanel.php b/keyControlPanel.php index 67b61a3..b30b3bf 100755 --- a/keyControlPanel.php +++ b/keyControlPanel.php @@ -37,7 +37,7 @@ session_start(); $formState = STATE_ACTION_NEWCODE; $keyCode = ""; - $keyData = ReadKeyFile(KEYFILE); + $keyData = ReadKeys(KEYFILE); // if the variable is set, the form has been posted to itself // if the submission ids of the post and the form don't match, the @@ -55,8 +55,9 @@ $keyCode = $_POST["keyCode"]; //$keyData = ReadKeyFile(KEYFILE); // if no action was performed on the entered key, simply display its state - if (!isset($_POST["action"])) - $formState = KeyStateToFormState(GetKeyState($keyData, $_POST["keyCode"])); + if (!isset($_POST["action"])){ + $formState = KeyStateToFormState(GetKeyState(KEYFILE, $_POST["keyCode"])); + } else { // otherwise set the state of the form to the action that should be performed @@ -65,8 +66,8 @@ // on the key and if the action was successful save the key file if ($formState != STATE_ACTION_NEWCODE) { - if (SetKeyState($keyData, $keyCode, FormStateToKeyState($formState))) - WriteKeyFile(KEYFILE, $keyData); + if (SetKeyState(KEYFILE, $keyCode, FormStateToKeyState($formState))) + SetKeyState(KEYFILE, $keyCode, FormStateToKeyState($formState)); else $formState($STATE_ACTION_FAILED); $_POST["action"] = STATE_ACTION_NEWCODE; @@ -205,11 +206,11 @@ echo "
\n"; echo " \n"; //echo " exec("DROP TABLE answers;"); + $handle->exec("CREATE TABLE IF NOT EXISTS 'answers'(KeyId text primary key,Status text,Student int,Answer);"); + $keys = GenerateKeys($keyAmount); + $count = count($keys); + + for ($i = 0; $i < $count; $i++) + { + $stmt = $handle->prepare("INSERT INTO answers (KeyId, Status) VALUES (:key,:status);"); + if ($stmt) + { + $stmt->bindValue(':key', $keys[$i], SQLITE3_TEXT); + $stmt->bindValue(':status', KEYSTATE_UNISSUED, SQLITE3_TEXT); + $result = $stmt->execute(); + } + else + { + $handle->close(); + return false; + } + } + + $handle->close(); + return true; + } + + function AppendKeys($keyAmount, $fileName) { $handle = new SQLite3($fileName, SQLITE3_OPEN_READWRITE); if (!$handle) @@ -186,7 +216,7 @@ return false; //secure deleting by checking if key has been uesed already - $stmt = $handle->prepare("DELETE FROM answers WHERE KeyId=:keyid AND Answer!=NULL;"); + $stmt = $handle->prepare("DELETE FROM answers WHERE KeyId=:keyid;"); if ($stmt) { $stmt->bindValue(':keyid', $key, SQLITE3_TEXT);