/lenasys/0.1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/0.1

« back to all changes in this revision

Viewing changes to trunk/DuggaSys/quizAjax/getQuiz.php

  • Committer: Henrik G.
  • Date: 2013-03-26 23:22:55 UTC
  • Revision ID: henrik.gustavsson@his.se-20130326232255-ik6snyatlbkf3zs1
First seed of Lenasys ... Needs to be Organized Further

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
//// Returns a quiz variant including quiz data (If the student is registered for the course and the quiz is open)
3
3
//// If no quiz variant already assigned to the student a variant will be randomly selected and assigned
4
4
//// Parameters: (POST) login, courseName, courseOccasion, quizNr
5
 
//// Returns:   quizNr 
6
 
////                    quizCourseName 
7
 
////                    quizData 
8
 
////                    quizObjectIDs 
 
5
//// Returns:  quizNr 
 
6
////           quizCourseName 
 
7
////               quizData 
 
8
////               quizObjectIDs 
9
9
 
10
10
session_start();
11
11
//Check if the sent login name is the same as the one stored in the session
12
 
if($_POST['loginName']==$_SESSION['loginName'] && $_POST['courseName']==$_SESSION['courseName'] && $_POST['quizNr']==$_SESSION['quizNr']) { 
 
12
if($_POST['loginName']==$_SESSION['loginName'] && $_POST['courseName']==$_SESSION['courseName'] && $_POST['quizNr']==$_SESSION['quizNr']){ 
13
13
 
14
14
//Prevents browsers (IE) from caching the response
15
15
header('Cache-Control: no-cache, must-revalidate');
16
16
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
17
17
//header('Content-type: application/json');
18
18
header('Content-type: application/json; charset=utf-8'); 
19
 
include "../../php/dbconnect.php";
 
19
 
20
20
/* Function used to fetch quiz variant data */
21
 
function fetchQuizVariant($courseName, $quizNr, $quizVariantNr, $pdo) {
 
21
function fetchQuizVariant($courseName, $quizNr, $quizVariantNr, $pdo){                                  
22
22
        $queryString=" SELECT QuizVariant.quizNr, QuizVariant.quizCourseName, QuizVariant.quizObjectIDs, Quiz.quizData 
23
23
                                        FROM QuizVariant, Quiz
24
24
                                        WHERE QuizVariant.quizNr=Quiz.nr 
36
36
        return $quizVariantData;
37
37
}
38
38
 
39
 
 
 
39
include "dbconnect.php";
40
40
 
41
41
//Check if the student is a praticipant of the course
42
42
$queryString="SELECT COUNT(*) 
53
53
$stmt->bindParam(':COCCASION', $_POST['courseOccasion']);
54
54
$stmt->execute();
55
55
 
56
 
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) 
 
56
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) 
57
57
        
58
58
        //Check if quiz is open
59
59
        $queryString="SELECT Quiz.opening, Quiz.closing, Quiz.allowMultipleReplies
65
65
        $stmt->bindParam(':CNAME', $_POST['courseName']);
66
66
        $stmt->execute();
67
67
        $quizData=$stmt->fetch(PDO::FETCH_ASSOC); 
68
 
        if($quizData) {
 
68
        if($quizData){
69
69
                $now = new DateTime();
70
70
                $opening = new DateTime($quizData['opening']);
71
71
                $closing = new DateTime($quizData['closing']);
100
100
        $result=$stmt->fetch(PDO::FETCH_ASSOC);
101
101
        if($result){ //If this quiz is assigned to the student
102
102
        //if($stmt->rowCount() >= 1){ //If this quiz is assigned to the student
103
 
 
104
 
                if($result['answerHash']!=NULL && $quizData['allowMultipleReplies']!='1'){ //Student has already answered the quiz
105
 
                        echo json_encode(array('Error' => 'This student has already answered this quiz', 'answerHash'=>$result['answerHash']));
106
 
                        exit();
107
 
                } else { //Return quiz variant data assigned to student
108
 
                        $quizVariant=$result['qVarNr'];
109
 
                        $quizVariantData=fetchQuizVariant($_POST['courseName'], $_POST['quizNr'], $quizVariant, $pdo);
110
 
                        $quizVariantData['storedAnswer']=$result['answer'];
111
 
                        echo json_encode($quizVariantData);
112
 
                        exit();
113
 
                }
 
103
                        
 
104
                        if($result['answerHash']!=NULL && $quizData['allowMultipleReplies']!='1'){ //Student has already answered the quiz
 
105
                                echo json_encode(array('Error' => 'This student has already answered this quiz', 'answerHash'=>$result['answerHash']));
 
106
                                exit();
 
107
                        } else { //Return quiz variant data assigned to student
 
108
                                $quizVariant=$result['qVarNr'];
 
109
                                $quizVariantData=fetchQuizVariant($_POST['courseName'], $_POST['quizNr'], $quizVariant, $pdo);
 
110
                                $quizVariantData['storedAnswer']=$result['answer'];
 
111
                                echo json_encode($quizVariantData);
 
112
                                exit();
 
113
                        }
114
114
        } else { //This quiz has not been assigned to the student
115
115
                //Select random variant for requested quiz nr (for a particular course)
116
116
                //$queryString="SELECT COUNT(QuizVariant.quizNr) 
121
121
                $queryString="SELECT QuizVariant.qVarNr 
122
122
                                          FROM QuizVariant
123
123
                                          WHERE QuizVariant.quizNr=:QNR 
124
 
                                          AND QuizVariant.quizCourseName=:CNAME;";
 
124
                                                AND QuizVariant.quizCourseName=:CNAME;";
125
125
                
126
126
                $stmt = $pdo->prepare($queryString);
 
127
                
127
128
                $stmt->bindParam(':QNR', $_POST['quizNr']);
128
129
                $stmt->bindParam(':CNAME', $_POST['courseName']);
129
130
                $stmt->execute();
130
131
                $quizVariants=$stmt->fetchAll(PDO::FETCH_NUM);
131
 
                if(count($quizVariants)>0) {
 
132
                if(count($quizVariants)>0){
132
133
                        $randomIndex=rand(0,count($quizVariants)-1);
133
134
                        $quizVariant=$quizVariants[$randomIndex][0];
134
135
                        $quizVariantData=fetchQuizVariant($_POST['courseName'], $_POST['quizNr'], $quizVariant, $pdo);
135
 
 
 
136
                        
136
137
                        //Store selected variant
137
138
                        $insertString="INSERT INTO AssignedQuizzes(ssn, quizNr, qVarNr, quizCourseName, courseOccasion) 
138
139
                                                   VALUES((SELECT Student.ssn FROM Student WHERE Student.loginName=:LOGIN), :QNR, :QVARNR, :CNAME, :COCCASION);";
143
144
                        $insertStmt->bindParam(':QVARNR', $quizVariant);  
144
145
                        $insertStmt->bindParam(':COCCASION', $_SESSION['courseOccasion']);
145
146
                        $insertStmt->execute();
146
 
 
 
147
                        
147
148
                        echo json_encode($quizVariantData);     
148
149
                        exit();
149
150
                } else {