Commit e91fc287 authored by William Paul Liggett's avatar William Paul Liggett

Admin Page: The Ajax backend now works to edit a test. The front-end DOM will...

Admin Page: The Ajax backend now works to edit a test. The front-end DOM will be enhanced later to auto-update the new changes.
parent c3a8080f
<?php
// Enables user sessions.
session_start();
?>
<!DOCTYPE html>
<html>
<head>
......@@ -60,6 +64,11 @@
// Login successful!
else {
// Maintains the authenticated user session across different admin pages.
if(!isset($_SESSION['ov_admin_user'])) {
$_SESSION['ov_admin_user'] = $submitted_username;
}
// Logs into the OpenVigilance Task tests database to control and alter user tests.
// `$pdo' is defined as the database connection.
require_once "../../../../../protected_site_configs/junktext.com/openvigilance_db_connection.php";
......@@ -124,12 +133,13 @@
"$tab$tab$tab<p class=\"validateTips\">All form fields are required.</p>" . PHP_EOL .
"$tab$tab$tab<form id=\"EditTest\">" . PHP_EOL .
"$tab$tab$tab$tab<fieldset>" . PHP_EOL .
"$tab$tab$tab$tab$tab<input type=\"hidden\" name=\"edit_test_sk\" id=\"edit_test_sk\" value=\"\" />" . PHP_EOL .
"$tab$tab$tab$tab$tab<label for=\"edit_test_subject_id\">Subject ID</label>" . PHP_EOL .
"$tab$tab$tab$tab$tab<input type=\"text\" name=\"edit_test_subject_id\" id=\"edit_test_subject_id\" value=\"\" class=\"text ui-widget-content ui-corner-all\">" . PHP_EOL .
"$tab$tab$tab$tab$tab<input type=\"text\" name=\"edit_test_subject_id\" id=\"edit_test_subject_id\" value=\"\" class=\"text ui-widget-content ui-corner-all\" />" . PHP_EOL .
"$tab$tab$tab$tab$tab<label for=\"edit_test_condition\">Test Condition</label>" . PHP_EOL .
"$tab$tab$tab$tab$tab<input type=\"text\" name=\"edit_test_condition\" id=\"edit_test_condition\" value=\"\" class=\"text ui-widget-content ui-corner-all\">" . PHP_EOL .
"$tab$tab$tab$tab$tab<input type=\"text\" name=\"edit_test_condition\" id=\"edit_test_condition\" value=\"\" class=\"text ui-widget-content ui-corner-all\" />" . PHP_EOL .
"$tab$tab$tab$tab$tab<label for=\"edit_login_code\">Login Code</label>" . PHP_EOL .
"$tab$tab$tab$tab$tab<input type=\"text\" name=\"edit_login_code\" id=\"edit_login_code\" value=\"\" disabled=\"disabled\" title=\"Cannot edit as the code is auto-generated.\" class=\"text ui-widget-content ui-corner-all\">" . PHP_EOL .
"$tab$tab$tab$tab$tab<input type=\"text\" name=\"edit_login_code\" id=\"edit_login_code\" value=\"\" disabled=\"disabled\" title=\"Cannot edit as the code is auto-generated.\" class=\"text ui-widget-content ui-corner-all\" />" . PHP_EOL .
// Allow form submission with keyboard without duplicating the dialog button
"$tab$tab$tab$tab$tab<input type=\"submit\" tabindex=\"-1\" style=\"position:absolute; top:-1000px\">" . PHP_EOL .
......
<?php
/* --------------------------------------------------------------------------------------------------------------
* Copyright (C) 2018 by William Paul Liggett (junktext@junktext.com)
* This Source Code Form is subject to the terms of the Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Filename: output_json_response.php
*
* Purpose: Function to standardise the output of various Ajax requests using JSON as the data storage.
* -------------------------------------------------------------------------------------------------------------- */
function output_json_response($successful, $error_message) {
// Ensures the variables must be set correctly.
if(!isset($successful) || !is_bool($successful)) {
$successful = false;
$error_message = "output_json_response($successful, $error_message): 1st argument, $successful, must be a boolean value.";
}
if(!isset($error_message) || !is_string($error_message)) {
$successful = false;
$error_message = "output_json_response($successful, $error_message): 2nd argument, $error_message, must be a string.";
}
// Gets rid of potentially harmful injection characters.
$error_message = htmlspecialchars($error_message);
// JSON array to output as a web service.
$json_data = [
'successful' => $successful,
'error_message' => $error_message,
];
echo json_encode($json_data);
return;
}
<?php
/* --------------------------------------------------------------------------------------------------------------
* Copyright (C) 2018 by William Paul Liggett (junktext@junktext.com)
* This Source Code Form is subject to the terms of the Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Filename: scheduled_test_edit.php
*
* Purpose: Modify an existing OpenVigilance Task test that was previously scheduled in the database.
* For example, the subject's ID or the test condition might need to be changed.
* The page is meant to be used as a simple web service, such as with Ajax or the like.
*
* Output: JSON array as defined in ``output_json_response.php''.
* -------------------------------------------------------------------------------------------------------------- */
// Maintains the authenticated user session across different admin pages.
// Ensures only valid administrators can do anything.
session_start();
if(!isset($_SESSION['ov_admin_user'])) {
output_json_response(false, "Not logged on as an administrator.");
return;
}
// Function: output_json_response($successful, $error_message)
require_once "output_json_response.php";
// ------------------------------------------------------------------------------------
// POST input variables sent by the web browser.
// Note: The login_code cannot be modified by an OV admin. The code is auto-generated.
// ------------------------------------------------------------------------------------
$sk = intval($_POST['edit_test_sk']); // Surrogate Key identifier of the scheduled test in the database.
$subject_id = intval($_POST['edit_test_subject_id']); // The ID of the person meant to take the test.
$test_condition = intval($_POST['edit_test_condition']); // The kind of test being taken.
// Validate the data.
if($sk <= 0 || $subject_id <= 0 || $test_condition <= 0) {
output_json_response(false, "The surrogate key (sk), subject_id, nor the test_condition can be <= 0.");
return;
}
else if($test_condition > 4) {
output_json_response(false, "The test_condition cannot be greater than 4.");
}
// Logs into the OpenVigilance Task tests database to control and alter user tests.
// `$pdo' is defined as the database connection.
require_once "../../../../../protected_site_configs/junktext.com/openvigilance_db_connection.php";
// Test Control: Edit a test in the database.
$sql = "UPDATE test_control SET subject_id=:subject_id, test_condition=:test_condition WHERE sk=:sk";
$statement = $pdo->prepare($sql);
$statement->bindValue(":subject_id", $subject_id, PDO::PARAM_INT);
$statement->bindValue(":test_condition", $test_condition, PDO::PARAM_INT);
$statement->bindValue(":sk", $sk, PDO::PARAM_INT);
$statement->execute();
// Inform the web browser that all went well.
output_json_response(true, "");
......@@ -5,6 +5,7 @@
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Filename: validate_login.php
*
* Purpose: Ensure that only administrators can see or change any OpenVigilance Task settings.
* -------------------------------------------------------------------------------------------------------------- */
......
......@@ -9,11 +9,11 @@
;(function(window, $) {
$(document).ready(function() {
// Edit Test
var dialog, form,
subject_id = $("#edit_test_subject_id"),
test_condition = $("#edit_test_condition"),
test_pin = $("#edit_test_pin"),
allFields = $([]).add(subject_id).add(test_condition).add(test_pin),
var edit_dialog, edit_form,
edit_subject_id = $("#edit_test_subject_id"),
edit_test_condition = $("#edit_test_condition"),
edit_login_code = $("#edit_login_code"),
edit_allFields = $([]).add(edit_subject_id).add(edit_test_condition).add(edit_login_code),
tips = $(".validateTips");
function updateTips(t) {
......@@ -47,7 +47,7 @@
function editTest() {
var valid = true;
allFields.removeClass("ui-state-error");
edit_allFields.removeClass("ui-state-error");
/*
valid = valid && checkLength(name, "username", 3, 16);
......@@ -67,10 +67,26 @@
dialog.dialog("close");
}
*/
if(valid) {
// The data sent to the server is what the admin wanted modified via the pop-up dialog defined further below.
// Specifically, see the Getters and Setters near the bottom.
$.ajax({
type: 'POST',
url: "../admin/scheduled_test_edit.php",
dataType: "json",
data: $("form#EditTest").serialize(),
success: function(response) {
alert(response.successful + "\n" + response.error_message);
}
});
edit_dialog.dialog("close");
}
return valid;
}
dialog = $("#dialog_form_edit_test").dialog({
edit_dialog = $("#dialog_form_edit_test").dialog({
autoOpen: false,
height: 400,
width: 350,
......@@ -78,16 +94,16 @@
buttons: {
"Submit Changes": editTest,
Cancel: function() {
dialog.dialog("close");
edit_dialog.dialog("close");
}
},
close: function() {
document.getElementById("EditTest").reset();
allFields.removeClass("ui-state-error");
edit_allFields.removeClass("ui-state-error");
}
});
form = dialog.find("form#dialog_form_edit_test").on("submit", function(event) {
edit_form = edit_dialog.find("form#dialog_form_edit_test").on("submit", function(event) {
event.preventDefault();
editTest();
});
......@@ -109,19 +125,22 @@
});
}
// Shows the pop-up dialog.
// Shows the pop-up dialog with the data filled in from the record that was selected by the user.
else {
// Getters. Grabs the record data of the row selected.
var scheduled_sk = $("input[type='radio']:checked").val();
var scheduled_subject_id = $("input[type='radio']:checked").attr("data-ov-subject-id");
var scheduled_test_condition = $("input[type='radio']:checked").attr("data-ov-test-condition");
var scheduled_login_code = $("input[type='radio']:checked").attr("data-ov-login-code");
// Setters. Puts the data into the pop-up dialog for the user to edit.
// Note: The 'sk' field isn't shown, but we need the data to be sent to the server nonetheless.
$("#edit_test_sk").val(scheduled_sk);
$("#edit_test_subject_id").val(scheduled_subject_id);
$("#edit_test_condition").val(scheduled_test_condition);
$("#edit_login_code").val(scheduled_login_code);
dialog.dialog("open");
edit_dialog.dialog("open");
}
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment