// ------------------------------------------------------------------ // Throws Update Errors. // In this case, the model is implemented using an MySQL database. // If survey_id is absent from the database, exit with failure. $matching_row = DB::queryFirstRow("SELECT * FROM credentials_stu WHERE survey_id = %i0", $survey_id); if (is_null($matching_row)) { die(bake_with_debug($return_value, 'Given survey_id does not yet exist in the model.')); } // Update survey_tag. if ($change_survey_tag) { // If new_survey_tag is already associated with a different survey id, exit with failure. // Note that if previous duplicates exist, this will always fail. $query = "SELECT * FROM credentials_stu WHERE survey_tag = %s AND survey_id <> %i"; $other_matching_survey_ids = DB::queryOneColumn('survey_id', $query, $new_survey_tag, $survey_id); if (!empty($other_matching_survey_ids)) { die(bake_with_debug($return_value, 'Duplicate survey_tag.')); } DB::query("UPDATE credentials_stu SET survey_tag = %s1 WHERE survey_id = %i0", $survey_id, $new_survey_tag); } // Update survey_start_date and survey_end_date. if ($change_survey_time) { // All integers are valid Unix timestamps. $new_survey_start_datetime = (new DateTime())->setTimestamp($new_survey_start); $new_survey_end_datetime = (new DateTime())->setTimestamp($new_survey_end); var_dump($new_survey_start_datetime, $new_survey_end_datetime); DB::query("UPDATE credentials_stu SET start_date = %t1, end_date = %t2 WHERE survey_id=%i0", $survey_id, $new_survey_start_datetime, $new_survey_end_datetime); } // ------------------------------------------------------------------ // Script exit with success. // ------------------------------------------------------------------ $return_value->data->success = TRUE;
// Issues: Timezone is not considered yet; as of now, system manipulates pure UTC. // ------------------------------------------------------------------ // Initialize return value to the failure message state. $return_value = json_decode('{"debug": null, "data": {"survey_id": null, "success": false} }'); // Change debug member of return value, then get string rep of return value. //TODO: move to a utilities file. function bake_with_debug($mutable_return_value, $debug_string) { $mutable_return_value->debug = $debug_string; return json_encode($mutable_return_value); } // ------------------------------------------------------------------ // Defines $server, $user, $pass, $dbname, and $port. include 'connectionData.txt'; // Connect to SQL database. $conn = mysqli_connect($server, $user, $pass, $dbname, $port) or die(bake_with_debug($return_value, 'Error connecting to MySQL server.')); // ------------------------------------------------------------------ // Read _POST parameter. $survey_tag = $_POST['survey_tag']; // Fetch survey_id, and start and end times for survey with given survey_tag. $query = "SELECT survey_id, start_date, end_date FROM credentials_stu WHERE survey_tag = '{$survey_tag}'"; // (The mysqli way of querying the database.) if ($stmt = $conn->prepare($query)) { $stmt->execute(); $stmt->bind_result($survey_id, $start_date, $end_date); // Populate variables with query results, as bound above. $stmt->fetch(); // Check if the SQL results are nonempty--i.e., user entered a valid survey_tag. if (!is_null($survey_id)) { // Credentials ok. $return_value->data->survey_id = $survey_id;