bzr branch
http://gegoxaren.bato24.eu/bzr/lenasys/0.1
1
by Henrik G.
First seed of Lenasys ... Needs to be Organized Further |
1 |
<?php
|
2 |
////Handels quiz answers
|
|
3 |
////Parameters: loginName, password, courseName, courseOccasion, quizNr, qVarNr, quizAnswer
|
|
4 |
////If autoCorrection==false the answer is stored without checking
|
|
5 |
////Else the quiz answer is checked for correctness,
|
|
6 |
//// if incorrect the answer is not stored, else stored
|
|
7 |
||
8 |
//Store answer and loginName+answer hash
|
|
4.10.1
by Daniel Johansson
Uppdaterat DuggaSys till att följa kodstandard utseendemässigt. Namnkonventioner kvar. |
9 |
function storeAnswer($loginName,$password,$courseName,$courseOccasion,$quizNr,$qVarNr,$quizAnswer,$grade,$gradeComment,$ip,$userAgent,$pdo) { |
1
by Henrik G.
First seed of Lenasys ... Needs to be Organized Further |
10 |
$updateQuery="UPDATE AssignedQuizzes |
11 |
SET AssignedQuizzes.answer=:ANSWER,
|
|
12 |
AssignedQuizzes.answerHash=:ANSWERHASH,
|
|
13 |
AssignedQuizzes.grade=:GRADE,
|
|
14 |
AssignedQuizzes.gradeComment=:GRADECOMMENT,
|
|
15 |
AssignedQuizzes.answeredDateTime=:DATETIME,
|
|
16 |
AssignedQuizzes.userAgent=:AGENT,
|
|
17 |
AssignedQuizzes.userIP=:IP
|
|
18 |
WHERE AssignedQuizzes.ssn=(SELECT Student.ssn FROM Student WHERE Student.loginName=:LOGIN AND Student.passw=:PASSW)
|
|
19 |
AND AssignedQuizzes.quizNr=:QNR
|
|
20 |
AND AssignedQuizzes.qVarNr=:QVNR
|
|
21 |
AND AssignedQuizzes.quizCourseName=:CNAME
|
|
22 |
AND AssignedQuizzes.courseOccasion=:COCCASION;"; |
|
23 |
$updateStmt = $pdo->prepare($updateQuery); |
|
24 |
$updateStmt->bindParam(':LOGIN', $loginName); |
|
25 |
$updateStmt->bindParam(':CNAME', $courseName); |
|
26 |
$updateStmt->bindParam(':QNR', $quizNr); |
|
27 |
$updateStmt->bindParam(':QVNR', $qVarNr); |
|
28 |
$updateStmt->bindParam(':COCCASION', $courseOccasion); |
|
29 |
$updateStmt->bindParam(':ANSWER', $quizAnswer); |
|
30 |
$hashedAnswer= md5($loginName.$quizAnswer); |
|
31 |
$updateStmt->bindParam(':ANSWERHASH', $hashedAnswer); |
|
32 |
$updateStmt->bindParam(':GRADE', $grade); |
|
33 |
$updateStmt->bindParam(':GRADECOMMENT', $gradeComment); |
|
34 |
$now=new DateTime(); |
|
35 |
$dateString=$now->format('Y-m-d H:i:s'); |
|
36 |
//$updateStmt->bindParam(':DATETIME', new DateTime()->format('Y-m-d H:i:s')); // date and time formated to string e.g. "2012-08-23 08:59:00"
|
|
37 |
$updateStmt->bindParam(':DATETIME', $dateString); // date and time formated to string e.g. "2012-08-23 08:59:00" |
|
38 |
$updateStmt->bindParam(':PASSW',$password); |
|
39 |
$updateStmt->bindParam(':IP',$ip); |
|
40 |
$updateStmt->bindParam(':AGENT',$userAgent); |
|
41 |
return $updateStmt->execute(); |
|
42 |
}
|
|
43 |
||
44 |
||
45 |
//Prevents browsers (IE) from caching the response
|
|
46 |
header('Cache-Control: no-cache, must-revalidate'); |
|
47 |
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
|
48 |
header('Content-type: application/json; charset=utf-8'); |
|
49 |
||
50 |
include "dbconnect.php"; |
|
51 |
||
52 |
//Check if the student is a praticipant of the course
|
|
53 |
$queryString="SELECT COUNT(*) |
|
54 |
FROM Student, StudentCourseRegistration
|
|
55 |
WHERE Student.ssn=StudentCourseRegistration.studentSsn
|
|
56 |
AND Student.loginName=:LOGIN
|
|
57 |
AND Student.passw=:PASSW
|
|
58 |
AND courseName=:CNAME
|
|
59 |
AND courseOccasion=:COCCASION;"; |
|
60 |
$stmt = $pdo->prepare($queryString); |
|
61 |
$stmt->bindParam(':LOGIN', $_POST['loginName']); |
|
62 |
$stmt->bindParam(':PASSW', $_POST['password']); |
|
63 |
$stmt->bindParam(':CNAME', $_POST['courseName']); |
|
64 |
$stmt->bindParam(':COCCASION', $_POST['courseOccasion']); |
|
65 |
$stmt->execute(); |
|
66 |
||
4.10.1
by Daniel Johansson
Uppdaterat DuggaSys till att följa kodstandard utseendemässigt. Namnkonventioner kvar. |
67 |
if($stmt->fetchColumn()==1) { //Student is registered for the course - COUNT result read from the first column of the next unread row (i.e. the first row) |
1
by Henrik G.
First seed of Lenasys ... Needs to be Organized Further |
68 |
//Check if quiz is open (and fetch auto correction setting)
|
69 |
$queryString="SELECT Quiz.opening, Quiz.closing, Quiz.autoCorrected |
|
70 |
FROM Quiz
|
|
71 |
WHERE Quiz.nr=:QNR
|
|
72 |
AND Quiz.courseName=:CNAME;"; |
|
73 |
$stmt = $pdo->prepare($queryString); |
|
74 |
$stmt->bindParam(':QNR', $_POST['quizNr']); |
|
75 |
$stmt->bindParam(':CNAME', $_POST['courseName']); |
|
76 |
$stmt->execute(); |
|
77 |
$quizData=$stmt->fetch(PDO::FETCH_ASSOC); |
|
4.10.1
by Daniel Johansson
Uppdaterat DuggaSys till att följa kodstandard utseendemässigt. Namnkonventioner kvar. |
78 |
if($quizData) { |
1
by Henrik G.
First seed of Lenasys ... Needs to be Organized Further |
79 |
|
80 |
$now = new DateTime(); |
|
81 |
$opening = new DateTime($quizData['opening']); |
|
82 |
$closing = new DateTime($quizData['closing']); |
|
83 |
||
84 |
if($now<$opening) { //Quiz is not open yet |
|
85 |
echo json_encode(array('Error' => 'Requested quiz is not open yet')); |
|
86 |
exit(); |
|
87 |
} else if($now>$closing) { //Quiz is closed |
|
88 |
echo json_encode(array('Error' => 'Requested quiz is closed')); |
|
89 |
exit(); |
|
90 |
} // else continue (Not the best coding practice...) |
|
91 |
||
92 |
} else { //Quiz does not exist |
|
93 |
echo json_encode(array('Error' => 'Requested quiz does not exist')); |
|
94 |
exit(); |
|
95 |
}
|
|
96 |
/*
|
|
97 |
AssignedQuizzes(
|
|
98 |
ssn CHAR(11), //YYMMDD-XXXX
|
|
99 |
quizNr INTEGER,
|
|
100 |
qVarNr INTEGER,
|
|
101 |
quizCourseName VARCHAR(200),
|
|
102 |
courseOccasion VARCHAR(25) NOT NULL,
|
|
103 |
answerHash VARCHAR(255), //Hash of Student login name + answer
|
|
104 |
answer TEXT,
|
|
105 |
grade VARCHAR(10),
|
|
106 |
gradeComment TEXT,
|
|
107 |
answeredDateTime TIMESTAMP,
|
|
108 |
userAgent VARCHAR(1024),
|
|
109 |
userIP VARCHAR(20), //$_SERVER['REMOTE_ADDR']
|
|
110 |
PRIMARY KEY(ssn, qVarNr, quizNr, quizCourseName),
|
|
111 |
*/
|
|
112 |
||
113 |
//Check if student already has answered the assigned quiz variant
|
|
114 |
$queryString="SELECT AssignedQuizzes.answerHash |
|
115 |
FROM AssignedQuizzes, Student
|
|
116 |
WHERE Student.ssn=AssignedQuizzes.ssn
|
|
117 |
AND AssignedQuizzes.quizNr=:QNR
|
|
118 |
AND AssignedQuizzes.qVarNr=:QVARNR
|
|
119 |
AND Student.loginName=:LOGIN
|
|
120 |
AND AssignedQuizzes.quizCourseName=:CNAME
|
|
121 |
AND AssignedQuizzes.courseOccasion=:COCCASION;"; |
|
122 |
$stmt = $pdo->prepare($queryString); |
|
123 |
$stmt->bindParam(':QNR', $_POST['quizNr']); |
|
124 |
$stmt->bindParam(':QVARNR', $_POST['qVarNr']); |
|
125 |
$stmt->bindParam(':LOGIN', $_POST['loginName']); |
|
126 |
$stmt->bindParam(':CNAME', $_POST['courseName']); |
|
127 |
$stmt->bindParam(':COCCASION', $_POST['courseOccasion']); |
|
128 |
$stmt->execute(); |
|
129 |
$result=$stmt->fetch(PDO::FETCH_ASSOC); |
|
4.10.1
by Daniel Johansson
Uppdaterat DuggaSys till att följa kodstandard utseendemässigt. Namnkonventioner kvar. |
130 |
if($result) { //If there is a quiz variant assigned to the student |
1
by Henrik G.
First seed of Lenasys ... Needs to be Organized Further |
131 |
if($result['answerHash']!=NULL){ //Student has already answered the quiz |
132 |
echo json_encode(array('Error' => 'This quiz has already been answered', 'answerHash'=>$result['answerHash'])); |
|
133 |
exit(); |
|
134 |
} else { //check if autoCorrection |
|
135 |
||
4.10.1
by Daniel Johansson
Uppdaterat DuggaSys till att följa kodstandard utseendemässigt. Namnkonventioner kvar. |
136 |
if($quizData['autoCorrected']=='0') { //Not auto corrected |
1
by Henrik G.
First seed of Lenasys ... Needs to be Organized Further |
137 |
if(storeAnswer($_POST['loginName'], |
138 |
$_POST['password'], |
|
139 |
$_POST['courseName'], |
|
140 |
$_POST['courseOccasion'], |
|
141 |
$_POST['quizNr'], |
|
142 |
$_POST['qVarNr'], |
|
143 |
$_POST['quizAnswer'], |
|
144 |
"ungraded", |
|
145 |
"-", |
|
146 |
$_SERVER['REMOTE_ADDR'], |
|
147 |
$_SERVER['HTTP_USER_AGENT'], |
|
148 |
$pdo)){ |
|
149 |
//Stored answer
|
|
150 |
echo json_encode(array('Success' => 'true')); |
|
151 |
} else { |
|
152 |
//Failed to store answer
|
|
153 |
echo json_encode(array('Success' => 'false')); |
|
154 |
}
|
|
155 |
exit(); |
|
156 |
} else { //Is auto corrected - Check if the correct answer was given |
|
157 |
/*QuizVariant(
|
|
158 |
qVarNr INTEGER,
|
|
159 |
quizNr INTEGER,
|
|
160 |
quizCourseName VARCHAR(200),
|
|
161 |
correctAnswer VARCHAR(255),
|
|
162 |
quizObjectIDs TEXT,
|
|
163 |
PRIMARY KEY(qVarNr, quizNr, quizCourseName),
|
|
164 |
*/
|
|
165 |
$queryString = "SELECT QuizVariant.correctAnswer |
|
166 |
FROM QuizVariant
|
|
167 |
WHERE QuizVariant.qVarNr=:QVNR
|
|
168 |
AND QuizVariant.quizNr=:QNR
|
|
169 |
AND QuizVariant.quizCourseName=:CNAME"; |
|
170 |
$stmt = $pdo->prepare($queryString); |
|
171 |
$stmt->bindParam(':QNR', $_POST['quizNr']); |
|
172 |
$stmt->bindParam(':QVNR', $_POST['qVarNr']); |
|
173 |
$stmt->bindParam(':CNAME', $_POST['courseName']); |
|
174 |
$stmt->execute(); |
|
175 |
$qVarData=$stmt->fetch(PDO::FETCH_ASSOC); |
|
176 |
$correctAnswer=$qVarData['correctAnswer']; |
|
177 |
if($_POST['quizAnswer']==$correctAnswer){ //Correct answer was given |
|
178 |
$hashedAnswer= md5($_POST['loginName'].$_POST['quizAnswer']); |
|
179 |
||
180 |
if(storeAnswer($_POST['loginName'], |
|
181 |
$_POST['password'], |
|
182 |
$_POST['courseName'], |
|
183 |
$_POST['courseOccasion'], |
|
184 |
$_POST['quizNr'], |
|
185 |
$_POST['qVarNr'], |
|
186 |
$_POST['quizAnswer'], |
|
187 |
"Correct", |
|
188 |
"Quiz was corrected automatically", |
|
189 |
$_SERVER['REMOTE_ADDR'], |
|
190 |
$_SERVER['HTTP_USER_AGENT'], |
|
191 |
$pdo)){ |
|
192 |
//Stored answer
|
|
193 |
echo json_encode(array('Success' => 'true', 'isCorrect' => 'true', 'hashedAnswer' => $hashedAnswer)); |
|
194 |
} else { |
|
195 |
//Failed to store answer
|
|
196 |
echo json_encode(array('Success' => 'false', 'isCorrect' => 'true', 'hashedAnswer' => $hashedAnswer)); |
|
197 |
}
|
|
198 |
exit(); |
|
199 |
} else { //Answer is incorrect |
|
200 |
echo json_encode(array('isCorrect' => 'false')); |
|
201 |
}
|
|
202 |
exit(); |
|
203 |
}
|
|
204 |
}
|
|
205 |
} else { |
|
206 |
echo json_encode(array('Error' => 'This student has not been assigned the quiz')); |
|
207 |
exit(); |
|
208 |
}
|
|
209 |
||
210 |
} else { |
|
211 |
echo json_encode(array('Error' => 'Student not registered for this course (or incorrect password was sent)')); |
|
212 |
exit(); |
|
213 |
}
|
|
214 |
||
215 |
?>
|