/lenasys/trunk

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

« back to all changes in this revision

Viewing changes to DuggaSys/quizAjax/getQuizObject.php

  • Committer: gustav.hartvigsson at gmail
  • Date: 2013-04-02 13:58:01 UTC
  • mfrom: (7.1.2 pvp)
  • Revision ID: gustav.hartvigsson@gmail.com-20130402135801-jo6now4rir3kyln3
Mergerd fix for the widget library.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
////Returns a quiz object 
3
 
////Parameters: (POST) objectID, quizNr, qVarNr, courseName, courseOccasion, login, password
4
 
////Returns: objectData
5
 
 
6
 
session_start();
7
 
//Check if the sent login name is the same as the one stored in the session
8
 
if($_POST['loginName']==$_SESSION['loginName'] && $_POST['courseName']==$_SESSION['courseName'] && $_POST['quizNr']==$_SESSION['quizNr']){ 
9
 
 
10
 
        //Prevents browsers (IE) from caching the response
11
 
        header('Cache-Control: no-cache, must-revalidate');
12
 
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
13
 
        //header('Content-type: application/json');
14
 
        header('Content-type: application/json; charset=utf-8'); 
15
 
 
16
 
        include "../../php/dbconnect.php";
17
 
 
18
 
        //Check if the student is a praticipant of the course
19
 
        $queryString="SELECT COUNT(*) 
20
 
                                   FROM Student, StudentCourseRegistration 
21
 
                                   WHERE Student.ssn=StudentCourseRegistration.studentSsn
22
 
                                   AND Student.loginName=:LOGIN
23
 
                                   AND courseName=:CNAME 
24
 
                                   AND courseOccasion=:COCCASION;";
25
 
        $stmt = $pdo->prepare($queryString);
26
 
        $stmt->bindParam(':LOGIN', $_POST['loginName']);
27
 
        $stmt->bindParam(':CNAME', $_POST['courseName']);
28
 
        $stmt->bindParam(':COCCASION', $_POST['courseOccasion']);
29
 
        $stmt->execute();
30
 
 
31
 
        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) 
32
 
                
33
 
                //Check if quiz is open
34
 
                $queryString="SELECT Quiz.opening, Quiz.closing
35
 
                                           FROM Quiz
36
 
                                           WHERE Quiz.nr=:QNR
37
 
                                           AND Quiz.courseName=:CNAME;";
38
 
                $stmt = $pdo->prepare($queryString);
39
 
                $stmt->bindParam(':QNR', $_POST['quizNr']);
40
 
                $stmt->bindParam(':CNAME', $_POST['courseName']);
41
 
                $stmt->execute();
42
 
                $quizDateTimes=$stmt->fetch(PDO::FETCH_ASSOC);
43
 
                if($quizDateTimes){
44
 
                        $now = new DateTime();
45
 
                        $opening = new DateTime($quizDateTimes['opening']);
46
 
                        $closing = new DateTime($quizDateTimes['closing']);
47
 
 
48
 
                        if($now<$opening) { //Quiz is not open yet
49
 
                                echo json_encode(array('Error' => 'Requested quiz is not open yet'));
50
 
                                exit();
51
 
                        } else if($now>$closing) { //Quiz is closed
52
 
                                echo json_encode(array('Error' => 'Requested quiz is closed'));
53
 
                                exit();
54
 
                        } // else continue (Not the best coding practice...)
55
 
                        
56
 
                } else { //Quiz does not exist
57
 
                        echo json_encode(array('Error' => 'Requested quiz does not exist'));
58
 
                        exit();
59
 
                }
60
 
        
61
 
                //Fetch quiz variant nr
62
 
                $queryString="SELECT AssignedQuizzes.qVarNr 
63
 
                                          FROM AssignedQuizzes 
64
 
                                          WHERE AssignedQuizzes.ssn=(SELECT Student.ssn FROM Student WHERE Student.ssn=AssignedQuizzes.ssn AND Student.loginName=:LOGIN) 
65
 
                                                AND AssignedQuizzes.quizNr=:QNR 
66
 
                                                AND AssignedQuizzes.quizCourseName=:CNAME
67
 
                                                AND AssignedQuizzes.courseOccasion=:COCCASION;";
68
 
                $stmt = $pdo->prepare($queryString);
69
 
                $stmt->bindParam(':LOGIN', $_POST['loginName']);
70
 
                $stmt->bindParam(':QNR', $_POST['quizNr']);
71
 
                $stmt->bindParam(':CNAME', $_POST['courseName']);
72
 
                $stmt->bindParam(':COCCASION', $_POST['courseOccasion']);
73
 
                $stmt->execute();
74
 
 
75
 
                $quizAssignmentData=$stmt->fetch(PDO::FETCH_ASSOC);
76
 
                $qVarNr=$quizAssignmentData['qVarNr'];
77
 
                
78
 
                $queryString="SELECT QuizVariantObject.objectData 
79
 
                                          FROM QuizVariantObject 
80
 
                                          WHERE QuizVariantObject.id=:OID AND QuizVariantObject.quizNr=:QNR AND QuizVariantObject.qVarNr=:QVNR AND QuizVariantObject.quizCourseName=:CNAME;";
81
 
                $stmt = $pdo->prepare($queryString);
82
 
                $stmt->bindParam(':OID', $_POST['objectID']);
83
 
                $stmt->bindParam(':QNR', $_POST['quizNr']);
84
 
                $stmt->bindParam(':QVNR', $qVarNr);
85
 
                $stmt->bindParam(':CNAME', $_POST['courseName']);
86
 
                $stmt->execute();
87
 
 
88
 
                $quizVariantObject=$stmt->fetch(PDO::FETCH_ASSOC);
89
 
 
90
 
                if(count($quizVariantObject)>0) {
91
 
                        $quizVariantObject['objectData']=htmlspecialchars_decode($quizVariantObject['objectData']);
92
 
                        echo json_encode($quizVariantObject);
93
 
                        exit();
94
 
                } else {
95
 
                        echo json_encode(array('Error' => 'Object not found'));
96
 
                        exit();
97
 
                }
98
 
        } else {
99
 
                echo json_encode(array('Error' => 'Student not registered for this course (or incorrect password was sent)'));
100
 
                exit();
101
 
        }
102
 
} else { //Sent login name does not match the login name stored in the session
103
 
        echo json_encode(array('Error' => 'Sent login name does not match stored login name'));
104
 
}
105
 
?>
 
 
b'\\ No newline at end of file'