-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecordTestResult.php
147 lines (125 loc) · 5.53 KB
/
recordTestResult.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// Degbugging only
// ini_set('display_errors', 'On');
// error_reporting(E_ALL);
require_once("./genMappingArray.php");
// Data = test detail results
$data = $_POST['data'];
if (is_null($data) || empty($data))
{
echo "Error Calculating IQ";
}
else
{
// Parse to create object
$data = stripslashes($data);
$data = substr($data,1,-1);
// $data = '{"listNumber":1,"numWrong":2,"numRight":2,"questions":[{"personName":"Olivia Wells","personId":1234,"userAnswer":"old","correctAnswer":"old","timeTilAnswer":null,"didTimeout":"false"},{"personName":"Emma Tucker","personId":1234,"userAnswer":"new","correctAnswer":"old","timeTilAnswer":79,"didTimeout":"false"},{"personName":"Elyse Dawson","personId":1234,"userAnswer":"old","correctAnswer":"old","timeTilAnswer":792,"didTimeout":"false"},{"personName":"Erin Craig","personId":1234,"userAnswer":"new","correctAnswer":"old","timeTilAnswer":59,"didTimeout":"false"}],"time":1338777648307}';
$data = json_decode($data);
// Some values passed as individual post params
$listNumber = $data->listNumber;
// Num Wrong and Right sent
$totalNumWrong = $data->numWrong;
$totalNumRight = $data->numRight;
$testDay = $_POST['testDay'];
$testDay = substr(stripslashes($testDay), 1, -1);
// Total correct identifications of old names
$rightId = $_POST['rightId'];
$rightId = substr(stripslashes($rightId), 1, -1);
// Total old questions
$oldTotal = $_POST['oldTotal'];
$oldTotal = substr(stripslashes($oldTotal), 1, -1);
$oldHitRate = floatval($rightId) / floatval($oldTotal);
// Total correct rejections of new names
$wrongRej = $_POST['wrongRej'];
$wrongRej = substr(stripslashes($wrongRej), 1, -1);
// Total new questions asked
$newTotal = $_POST['newTotal'];
$newTotal = substr(stripslashes($newTotal), 1, -1);
$newHitRate = floatval($wrongRej) / floatval($newTotal);
$performance = $totalNumRight-$totalNumWrong;
$time = time();
$userId = $_POST['userid'];
$userId = substr(stripslashes($userId), 1, -1);
$dbh = new PDO('mysql:host=localhost;dbname=psych', 'root', 'root');
// Record overall performance for test
$testSummary = "";
if ((int)$testDay == 1)
{
// Record short term test
$testSummary = "INSERT INTO `psych`.`testOverviewShortTerm` (`uid`, `hits`, `falseAlarms`, `performance`, `oldHitRate`, `newHitRate`, `date`, `testNumber`) VALUES (".$userId.", ".$totalNumRight." , ".$totalNumWrong.", ".$performance." ,".$oldHitRate.", ".$newHitRate.", ".$time.", ".$listNumber.");";
}
else
{
// Record long term test result
$select = "SELECT date FROM testOverviewShortTerm WHERE uid = ".$userId;
$timeSinceFirstTest = 0;
// Calculate time since previous user test
foreach ($dbh->query($select) as $entry)
{
$timeSinceFirstTest = $time - $entry['date'];
if (is_null($timeSinceFirstTest) || empty($timeSinceFirstTest))
{
// Prevent sql call from failing
$timeSinceFirstTest = -1;
}
}
$testSummary = "INSERT INTO `psych`.`testOverviewLongTerm` (`uid`, `hits`, `falseAlarms`, `performance`, `oldHitRate`, `newHitRate`, `timeSinceFirstTest`, `testNumber`) VALUES (".$userId.", ".$totalNumRight." , ".$totalNumWrong.", ".$performance." , ".$oldHitRate.", ".$newHitRate.", ".$timeSinceFirstTest.", ".$listNumber.");";
}
$dbh->exec($testSummary);
// Record test result details per question
foreach ($data->questions as $questionSet)
{
$userAnswer = $questionSet->userAnswer;
$correctAnswer = $questionSet->correctAnswer;
// Get pair numerical id stored as mapping in included php file : genMappingArray.php
$pairId = $mapping[$questionSet->personId];
$timeUntilAnswer = $questionSet->timeTilAnswer;
if (is_null($timeUntilAnswer))
{
$timeUntilAnswer = 'NULL';
}
$string = "INSERT INTO `psych`.`detailTestLog` (`id`, `uid`, `testNumber`, `pairId`, `userAnswer`, `correctAnswer`, `timeUntilAnswer`,`testDay`) VALUES ('NULL', ".$userId.", ".$listNumber.", ".$pairId.",'".$userAnswer."' , '".$correctAnswer."', ".$timeUntilAnswer." , ".$testDay.");";
$dbh->exec($string);
}
// Calculate and return memory iq
$count = 0;
$sum = 0;
// [sum(sample mean - subj mean)^2] /total num over all subjects
// add or subtract std * 16 from 100
$better = 0;
$select = "";
if ((int)$testDay == 1)
{
$select = "SELECT * FROM testOverviewShortTerm";
}
else
{
$select = "SELECT * FROM testOverviewLongTerm";
}
// STDEV = [ (sum over all people {(person score - mean) ^2} )/ (number of people -1)]^0.5
// Calculate the mean and the user percentile
foreach ($dbh->query($select) as $entry) {
$count++;
$sum += $entry['performance'];
if ((int)$entry['performance'] < (int)$performance)
{
$better++;
}
}
$mean = $sum/$count;
// Calculate the sum of (each person's score - mean )^2
$sumOfSquares = 0;
foreach ($dbh->query($select) as $entry)
{
$sumOfSquares += (($entry['performance'] - $mean) * ($entry['performance'] - $mean));
}
$avgMinus1 = (floatval($sumOfSquares) / floatval($count-1));
$stdev = sqrt($avgMinus1);
$dif = floatval(($mean - $performance)/$stdev);
$iq = intval(100 - 16*$dif);
$level = (int)(100*$better/$count);
$result = '{"score":'.$iq.',"level":'.$level.',"totalTesters":'.$count.'}';
echo $result;
}
?>