b_b

Revision 1f35e70e661d () - Diff

Link to this snippet: https://friendpaste.com/1QLP6pPcGBdVJblmON9xv2
Embed:
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php

if (!defined("_ECRIRE_INC_VERSION")) return;

include_spip('inc/notation_util');
include_spip('inc/vieilles_defs');
include_spip('balise/notation_balises');
include_spip('base/abstract_sql');

function formulaires_notation_charger_dist($type, $id_objet, $retour='', $row=array(), $hidden=''){

spip_log("********** faites tourner le bouzin ***********","notation193");

// définition des valeurs de base du formulaire
$valeurs = array(
'type'=>$type,
'id_objet'=>$id_objet,
'editable'=>true
);
// on recupère l'id de l'auteur connecté sinon on le fixe à 0 et on récupère l'IP du visiteur
if ($GLOBALS["auteur_session"]) {
$id_auteur = $GLOBALS['auteur_session']['id_auteur'];
$statut = $GLOBALS['auteur_session']['statut'];
}else{
$id_auteur = 0;
$ip = $_SERVER['REMOTE_ADDR'];
}
spip_log("id_auteur : $id_auteur","notation193");

$acces = notation_get_acces();
spip_log("acces : $acces","notation193");

$id_table_objet = id_table_objet($type);
spip_log("type : $type","notation193");
spip_log("id_table_objet : $id_table_objet","notation193");
spip_log("id_objet : $id_objet","notation193");

// Si pas inscrit : recuperer la note de l'article sur l'IP
if ($id_auteur == 0){
$res = sql_select(
'spip_notations.id_notation,spip_notations.id_auteur,spip_notations.note',
'spip_notations',
$id_table_objet . '=' . sql_quote($id_objet) . ' AND ip=' . sql_quote($ip)
);
// Sinon rechercher la note de l'auteur
}else{
$res = sql_select(
'spip_notations.id_notation,spip_notations.id_auteur,spip_notations.note',
'spip_notations',
$id_table_objet . '=' . sql_quote($id_objet) . ' AND id_auteur=' . sql_quote($id_auteur)
);
}
// on récupère la note de l'auteur pour l'élément en cours
$note=0;
if ($row = sql_fetch($res)){
$note = $row['note'];
}
// et on l'ajoute au contexte du formulaire html
$valeurs["note"] = $note;
spip_log("note de l'auteur : $note","notation193");
spip_log("IP du visiteur : $ip","notation193");

// est-on autorise a voter ?
$isauteur = ($statut=="0minirezo" || $statut=="1comite");
if ($acces=='all'){
return $valeurs;
}else{
if (($acces=='ide' && $statut!='') || ($acces=='aut' && $isauteur) || ($acces=='adm' && $statut=="0minirezo")){
return $valeurs;
}else{
return array(false,$valeurs);
}
}
}

function formulaires_notation_traiter_dist($type, $id_objet, $retour='', $row=array(), $hidden=''){
return noter_objet_traiter($type, $id_objet, $retour, $row, $hidden);
}

function noter_objet_traiter($type, $id_objet){

spip_log("%%%%%%% ça va noter sec %%%%%%%%%%","notation193");

$id_table_objet = id_table_objet($type);
if ($GLOBALS["auteur_session"]) {
$id_auteur = $GLOBALS['auteur_session']['id_auteur'];
}else{
$id_auteur = 0;
}

spip_log("qui veut noter : id_auteur = $id_auteur","notation193");

$ip = $_SERVER['REMOTE_ADDR'];
//recuperation des champs
$note = intval(_request('note'));
$robot = _request('content');
$id_donnees = _request('id_donnees');
$acces = notation_get_acces();

spip_log("champ note : $note","notation193");
spip_log("champ robot : $robot","notation193");
spip_log("champ id_donnees : $id_donnees","notation193");
spip_log("valeur acces : $acces","notation193");
$erreur = '';

// s'assurer que l'objet existe bien
if (sql_countsel("spip_$type", "$id_table_objet=" . sql_quote($id_objet))){
// On est en train de voter
if (($id_donnees==$id_objet) && $robot==''){ // Note correcte ?
if($note<1 || $note>notation_get_nb_notes()){
$erreur = _T('notation:note_hors_plage');
}else{
include_spip('ecrire/inc_connect');
// Si pas inscrit : recuperer la note de l'objet sur l'IP
if ($id_auteur == 0){
$res = sql_select(
"spip_notations.id_notation,spip_notations.id_auteur,spip_notations.note",
"spip_notations",
"$id_table_objet=" . sql_quote($id_objet) . " AND ip=" . sql_quote($ip)
);
// Sinon rechercher la note de l'auteur
}else{
$res = sql_select(
"spip_notations.id_notation,spip_notations.id_auteur,spip_notations.note",
"spip_notations",
"$id_table_objet=" . sql_quote($id_objet) . " AND id_auteur=" . sql_quote($id_auteur)
);
}
// Premier vote
if (sql_count($res) == 0){ // Remplir la table de notation
sql_insertq("spip_notations", array(
"objet" => $type,
"$id_table_objet" => $id_objet,
"id_auteur" => $id_auteur,
"ip" => $ip,
"note" => $note
));
$duchangement = true;
spip_log("vote pris en compte","notation193");
}else{ // Modifier la note
$row = sql_fetch($res);
// Seulement si elle a changee ou que l'auteur a change
if ($row['note'] != $note || ($row['id_auteur'] != $id_auteur)){ // Un auteur non reference ne remplace pas la note d'un auteur reference
if ($row['id_auteur'] == 0 || $id_auteur != 0){
sql_update("spip_notations", array(
"note" => $note,
"id_auteur" => $id_auteur),
"id_notation=" . sql_quote($row["id_notation"])
);
$duchangement = true;
}
}
}
// Calculer la nouvelle note de l'article
if ($duchangement){
$res = sql_select(
"spip_notations.$id_table_objet,spip_notations.note",
"spip_notations",
"$id_table_objet=" . sql_quote($id_objet)
);
$lanote = 0;
$total = 0;
while ($row =sql_fetch($res)){
$lanote += $row['note'];
$total++;
}
$lanote = $lanote/$total;
$lanote = intval($lanote*100)/100;
$note = round($lanote);
$note_ponderee = notation_ponderee ($lanote, $total);
// Mise à jour ou insertion ?
if (!sql_countsel("spip_notations_objets", "$id_table_objet=" . sql_quote($id_objet))){
// Remplir la table de notation des objets
sql_insertq("spip_notations_objets", array(
"objet" => $type,
"$id_table_objet" => $id_objet,
"note" => $note,
"note_ponderee" => $note_ponderee,
"nombre_votes" => $total
));
}else{
// Mettre ajour dans les autres cas
sql_update("spip_notations_objets", array(
"note" => $lanote,
"note_ponderee" => $note_ponderee,
"nombre_votes" => $total),
"$id_table_objet=" . sql_quote($id_objet)
);
}
}
}
}
$res = sql_select(
"spip_notations_objets.$id_table_objet,spip_notations_objets.note_ponderee,spip_notations_objets.nombre_votes",
"spip_notations_objets",
"$id_table_objet=" . sql_quote($id_objet)
);
$lanote=0;
$total=0;
if ($row = sql_fetch($res)){
$lanote = $row['note_ponderee'];
$total = $row['nombre_votes'];
}
$note = round($lanote);
}

return array(
"note"=>$note
);

}


?>