/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: gustav.hartvigsson at gmail
  • Date: 2013-04-02 13:00:31 UTC
  • mfrom: (4.7.8 GammaBear)
  • Revision ID: gustav.hartvigsson@gmail.com-20130402130031-442y89s0cfzmw3r2
Merged stuff.

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');
18
18
header('Content-type: application/json; charset=utf-8'); 
19
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 
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
 
                
128
127
                $stmt->bindParam(':QNR', $_POST['quizNr']);
129
128
                $stmt->bindParam(':CNAME', $_POST['courseName']);
130
129
                $stmt->execute();
131
130
                $quizVariants=$stmt->fetchAll(PDO::FETCH_NUM);
132
 
                if(count($quizVariants)>0){
 
131
                if(count($quizVariants)>0) {
133
132
                        $randomIndex=rand(0,count($quizVariants)-1);
134
133
                        $quizVariant=$quizVariants[$randomIndex][0];
135
134
                        $quizVariantData=fetchQuizVariant($_POST['courseName'], $_POST['quizNr'], $quizVariant, $pdo);
136
 
                        
 
135
 
137
136
                        //Store selected variant
138
137
                        $insertString="INSERT INTO AssignedQuizzes(ssn, quizNr, qVarNr, quizCourseName, courseOccasion) 
139
138
                                                   VALUES((SELECT Student.ssn FROM Student WHERE Student.loginName=:LOGIN), :QNR, :QVARNR, :CNAME, :COCCASION);";
144
143
                        $insertStmt->bindParam(':QVARNR', $quizVariant);  
145
144
                        $insertStmt->bindParam(':COCCASION', $_SESSION['courseOccasion']);
146
145
                        $insertStmt->execute();
147
 
                        
 
146
 
148
147
                        echo json_encode($quizVariantData);     
149
148
                        exit();
150
149
                } else {