-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit.php
84 lines (66 loc) · 1.88 KB
/
submit.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
<?php
require_once('lib/common.php');
if(user_is_not_logged_in())
header('location:login.php') || die(); //user not logged in
if(user_has_completed_the_survey())
header('location:questions.php') || die(); //questionario già inviato
if(!isset($_POST))
header('location:questions.php');
$id_user = get_user_id();
$answers_list = array();
foreach($_POST as $id_question => $id_answer)
{
if(is_array($id_answer))
foreach($id_answer as $id)
$answers_list[] = $id;
else
$answers_list[] = $id_answer;
}
if(count($answers_list) == 0)
header('location:questions.php');
//filtering out invalid answers
$n = count($answers_list);
$question_marks_string = build_question_marks_string($n);
$query = "SELECT answers.id_answer
FROM answers, questions
WHERE answers.id_question = questions.id_question
AND answers.id_answer IN ({$question_marks_string})
AND ( questions.dependency IS NULL
OR questions.dependency IN ({$question_marks_string}))";
$types = str_repeat('i', $n * 2);
$args = array_merge(array($query, $types), $answers_list, $answers_list);
$result = call_user_func_array('exec_query_many_results', $args);
disable_autocommit();
$success = True;
foreach($result as $row)
{
$answer = $row->id_answer;
$query = 'INSERT INTO given_answers
(id_given_answer, id_user, id_answer)
VALUES
(DEFAULT, ?, ?)';
$result = exec_query($query, 'ii', $id_user, $answer);
if($result === FALSE)
$success = FALSE;
}
//user has completed the survey
$query = 'UPDATE users
SET completed=1
WHERE id_user=?';
$result = exec_query($query, 'i', $id_user);
if($result === FALSE)
$success = FALSE;
$newlocation = "";
if($success)
{
commit();
$newlocation = 'completed.php';
}
else
{
rollback();
$newlocation = 'questions.php';
}
enable_autocommit();
header("Location:$newlocation") || die();
?>