-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
123 lines (98 loc) · 4.4 KB
/
ajax.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
include("php_functions/mysql.php");
include("php_functions/connect.php");
$isWebsiteOpen;
call_user_func(function(){
global $db, $isWebsiteOpen;
$lastPromo = $db->query('SELECT UNIX_TIMESTAMP(`dateOpen`) as dateOpen,UNIX_TIMESTAMP(`dateClose`) as dateClose FROM `Promo` ORDER BY idPromo DESC LIMIT 0,1')->fetch_array();
$dStart = intval($lastPromo["dateOpen"]);
$dEnd = intval($lastPromo["dateClose"]);
$isWebsiteOpen = (time()>$dStart && time()<$dEnd);
});
function getPeopleInGroup($gId){
global $db;
return $db->query('SELECT idEtu as id, (idGrEtu>0) as agree, CONCAT(prenomEtu, " ", nomEtu) as name FROM V_EtudiantPromo WHERE idGrEtu='.$gId.' OR idGrEtu='.(-intval($gId)));
}
$ajaxFunctions = array();
include("php_functions/ajax/updateOrder.php");
include("php_functions/ajax/opinionNeededChoices.php");
include("php_functions/ajax/decisionWithChoices.php");
include("php_functions/ajax/confirmChoices.php");
if(isset($_POST['order']) && @$_POST['action']=='update-order' && getGroupId() && $isWebsiteOpen && getUserType()==ELEVE){
$ajaxFunctions['update-order'](getGroupFromGroupId(getGroupId()), $_POST['order']);
}else if(@$_GET['action']=='opinion-needed-choices' && getGroupId()){
$ajaxFunctions['opinion-needed-choices']();
}else if(@$_GET['action']=='enumerate-students' && getUserType()==ADMIN){
$res = $db->query('SELECT * FROM V_EtudiantPromo');
$result = array();
while($row = $res->fetch_assoc())
$result[] = $row;
echo json_encode($result);
}else if(@$_GET['action']=='decision-with-choices' && $isWebsiteOpen && @$_GET['agree'] && getGroupId()){
$ajaxFunctions['decision-with-choices'](intval($_GET['agree']=='true')+1);
}else if(@$_POST['action']=='confirm-choices' && $isWebsiteOpen && getGroupId()){
$ajaxFunctions['confirm-choices']();
}else if(@$_GET['action']=='getState' && getUserType()==ELEVE){
$result = array(
'groupId' => getGroupId(),
'inGroup' => array(),
'listStudents' => array(),
'myName' => getUserName()
);
$res = $db->query('SELECT idEtu as id, idGrEtu, CONCAT(`nomEtu`, " ", `prenomEtu`) as name FROM V_EtudiantPromo WHERE idEtu!='.getUserId()) or die(mysqli_error($db));
while($row = $res->fetch_array())
$result['listStudents'][] = array(
'id' => intval($row['id']),
'available'=> intval($row['idGrEtu']==0),
'name' => $row['name']
);
if(getGroupId()!=0){
$listInGroupSQL = getPeopleInGroup(getGroupId());
while($user = $listInGroupSQL->fetch_array())
if(intval($user['id'])!=getUserId())
$result['inGroup'][] = array(
'id' => intval($user['id']),
'agree' => intval($user['agree']),
'name' => $user['name'],
);
}
echo json_encode($result);
}else if(@$_GET['action']=='switchVisility' && @$_GET['id'] && getUserType()==ADMIN){
$db->query('UPDATE Projet SET estValide=(1-estValide) WHERE idProj='.intval($_GET['id']));
$res = $db->query('SELECT estValide FROM Projet WHERE idProj='.intval($_GET['id']))->fetch_array();
echo (intval($res['estValide'])==1)?'true':'false';
}else if(@$_GET['action']=='manageInvitation' && $isWebsiteOpen && getUserType()==ELEVE){
if(getGroupId()>=0)
die('Aucune invitation trouvée.');
$finalIdG = (intval($_GET['accept'])==1)?(-getGroupId()):'NULL';
$db->query('UPDATE Etudiant SET idGrEtu='.$finalIdG.' WHERE idEtu='.getUserId());
}else if(
isset($_GET['action']) &&
isset($_GET['id']) &&
$_GET['action']=='addToGroup' &&
$_GET['id']!='' &&
getUserType()==ELEVE && $isWebsiteOpen
){
$listInGroupSQL = getPeopleInGroup(getGroupId());
$count = 0;
while($row = $listInGroupSQL->fetch_array())
$count++;
if($count>=4) //3+le créateur
die('Erreur : il ne peut y avoir que 3 personnes maximum par groupes.');
if(!doesTheseUsersExistAndHaveNoGroup(array($_GET['id'])))
die('[#'.$_GET['id'].'] student was not found.');
$db->query('UPDATE Etudiant SET idGrEtu='.(-getGroupId()).' WHERE idEtu='.intval($_GET['id']));
}else if(
isset($_GET['action']) &&
$_GET['action']=='createGroup' &&
getUserType()==ELEVE && $isWebsiteOpen
){
if(getGroupId()!=null)
die('Error: the student ['.getUserName().'] can\'t create a group while he is already in another one.');
$db->query('INSERT INTO Groupe(idEtuCreator) VALUES ('.intval(getUserId()).')');
$idG = $db->insert_id;
$db->query('UPDATE Etudiant SET idGrEtu='.$idG.' WHERE idEtu='.intval(getUserId()));
}else{
echo 'invalid request';
}
?>