Subversion Repositories eFlore/Applications.del

Rev

Rev 1573 | Rev 2000 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1573 Rev 1583
1
package org.tela_botanica.del.client.composants.votes.moyennevotes;
1
package org.tela_botanica.del.client.composants.votes.moyennevotes;
-
 
2
 
-
 
3
import java.util.ArrayList;
-
 
4
import java.util.Collections;
2
 
5
import java.util.Comparator;
3
import java.util.HashMap;
6
import java.util.HashMap;
-
 
7
import java.util.Iterator;
4
import java.util.Iterator;
8
import java.util.List;
5
 
9
 
6
import org.tela_botanica.del.client.cache.CacheClient;
10
import org.tela_botanica.del.client.cache.CacheClient;
7
import org.tela_botanica.del.client.composants.votefleur.VoteFleurPresenteur;
11
import org.tela_botanica.del.client.composants.votefleur.VoteFleurPresenteur;
8
import org.tela_botanica.del.client.composants.votefleur.VoteFleurVue;
12
import org.tela_botanica.del.client.composants.votefleur.VoteFleurVue;
9
import org.tela_botanica.del.client.modeles.Image;
13
import org.tela_botanica.del.client.modeles.Image;
10
import org.tela_botanica.del.client.modeles.Protocole;
14
import org.tela_botanica.del.client.modeles.Protocole;
11
import org.tela_botanica.del.client.modeles.VoteProtocole;
15
import org.tela_botanica.del.client.modeles.VoteProtocole;
12
import org.tela_botanica.del.client.navigation.evenement.BusEvenementiel;
16
import org.tela_botanica.del.client.navigation.evenement.BusEvenementiel;
13
import org.tela_botanica.del.client.navigation.evenement.changementEtatUtilisateur.EvenementChangementEtatUtilisateur;
17
import org.tela_botanica.del.client.navigation.evenement.changementEtatUtilisateur.EvenementChangementEtatUtilisateur;
14
import org.tela_botanica.del.client.navigation.evenement.changementEtatUtilisateur.GestionnaireEvenementChangementEtatUtilisateur;
18
import org.tela_botanica.del.client.navigation.evenement.changementEtatUtilisateur.GestionnaireEvenementChangementEtatUtilisateur;
15
import org.tela_botanica.del.client.navigation.evenement.suppressioncommentaire.EvenementSuppressionCommentaire;
19
import org.tela_botanica.del.client.navigation.evenement.suppressioncommentaire.EvenementSuppressionCommentaire;
16
import org.tela_botanica.del.client.navigation.evenement.suppressioncommentaire.GestionnaireEvenementSuppressionCommentaire;
20
import org.tela_botanica.del.client.navigation.evenement.suppressioncommentaire.GestionnaireEvenementSuppressionCommentaire;
17
import org.tela_botanica.del.client.services.rest.VoteProtocoleService;
21
import org.tela_botanica.del.client.services.rest.VoteProtocoleService;
18
import org.tela_botanica.del.client.services.rest.async.AjoutVoteProtocoleCallback;
22
import org.tela_botanica.del.client.services.rest.async.AjoutVoteProtocoleCallback;
19
import org.tela_botanica.del.client.services.rest.async.VoteProtocoleCallback;
23
import org.tela_botanica.del.client.services.rest.async.VoteProtocoleCallback;
20
 
24
 
21
import com.google.gwt.event.dom.client.ClickEvent;
25
import com.google.gwt.event.dom.client.ClickEvent;
22
import com.google.gwt.event.dom.client.ClickHandler;
26
import com.google.gwt.event.dom.client.ClickHandler;
23
import com.google.gwt.event.dom.client.HasClickHandlers;
27
import com.google.gwt.event.dom.client.HasClickHandlers;
24
import com.google.gwt.event.dom.client.HasMouseMoveHandlers;
28
import com.google.gwt.event.dom.client.HasMouseMoveHandlers;
25
import com.google.gwt.event.dom.client.MouseMoveEvent;
29
import com.google.gwt.event.dom.client.MouseMoveEvent;
26
import com.google.gwt.event.dom.client.MouseMoveHandler;
30
import com.google.gwt.event.dom.client.MouseMoveHandler;
27
import com.google.gwt.user.client.Window;
31
import com.google.gwt.user.client.Window;
28
import com.google.gwt.user.client.ui.HasText;
32
import com.google.gwt.user.client.ui.HasText;
29
import com.google.gwt.user.client.ui.HasWidgets;
33
import com.google.gwt.user.client.ui.HasWidgets;
30
import com.google.gwt.user.client.ui.IsWidget;
34
import com.google.gwt.user.client.ui.IsWidget;
-
 
35
import com.google.gwt.user.client.ui.Panel;
31
 
36
 
32
public class MoyenneVotePresenteur {
37
public class MoyenneVotePresenteur {
33
 
38
 
34
	public interface Vue extends IsWidget {
39
	public interface Vue extends IsWidget {
35
		public HasClickHandlers getBoutonAnnuler();
40
		public HasClickHandlers getBoutonAnnuler();
36
 
41
 
37
		public HasClickHandlers getVotes();
42
		public HasClickHandlers getVotes();
38
 
43
 
39
		public int getValeurVote();
44
		public int getValeurVote();
40
 
45
 
41
		public void afficherBoutonAnnuler();
46
		public void afficherBoutonAnnuler();
42
 
47
 
43
		public void masquerBoutonAnnuler();
48
		public void masquerBoutonAnnuler();
44
 
49
 
45
		public void afficherNbVotes();
50
		public void afficherNbVotes();
46
 
51
 
47
		public void masquerNbVotes();
52
		public void masquerNbVotes();
48
 
53
 
49
		public void afficherNbPoints();
54
		//public void afficherNbPoints();
50
 
55
 
51
		public void masquerNbPoints();
56
		//public void masquerNbPoints();
52
 
57
 
53
		public void reinitialiserVotes();
58
		public void reinitialiserVotes();
54
 
59
 
55
		public void rafraichir(int moyenneVote, int nbVotes, int nombrePoints);
60
		public void rafraichir(int moyenneVote, int nbVotes, int nombrePoints, double moyenneArithmetique, double mediane, HashMap<Integer,Integer> occurrencesParNote, double moyennePonderee);
56
 
61
 
57
		public void ajouterAuParent(HasWidgets composite);
62
		public void ajouterAuParent(HasWidgets composite);
58
 
63
 
59
		public void afficherVotePrisEnCompte();
64
		public void afficherVotePrisEnCompte();
60
 
65
 
61
		public HasWidgets getZoneFleur();
66
		public HasWidgets getZoneFleur();
62
 
67
 
63
		public HasText getZoneProtocole();
68
		public HasText getZoneProtocole();
-
 
69
 
-
 
70
		public HasText getNbVotes();
-
 
71
 
-
 
72
		//public HasText getNbPoints();
-
 
73
 
-
 
74
		public HasClickHandlers getLienNbVotes();
64
 
75
 
65
		public void setNoteGenerale(int note);
76
		public void setNoteGenerale(int note);
66
 
77
 
67
		public void afficherVoteModifie();
78
		public void afficherVoteModifie();
68
 
79
 
69
		public HasMouseMoveHandlers getZoneVoter();
80
		public HasMouseMoveHandlers getZoneVoter();
70
		public void masquerZoneVoter();
81
		public void masquerZoneVoter();
71
 
82
 
72
		public void setNoteGeneraleToolTip(double valeurVoteTotalPrecise);
83
		public void setNoteGeneraleToolTip(double valeurVoteTotalPrecise);
73
 
84
 
-
 
85
		public void desactiverInteractionVote();
-
 
86
 
-
 
87
		public void activerInteractionVote();
-
 
88
 
74
		void desactiverInteractionVote();
89
		public void afficherVoteSupprime();
75
 
90
 
-
 
91
		public Panel getPanneauDetailVotes();
-
 
92
 
-
 
93
		public void afficherPanneauDetailVotes();
-
 
94
 
76
		void activerInteractionVote();
95
		public void masquerPanneauDetailVotes();
77
 
96
 
78
		void afficherVoteSupprime();
97
		public HasClickHandlers getLienPanneauFermer();
79
	}
98
	}
80
 
99
 
81
	private Vue vue;
100
	private Vue vue;
82
	private Protocole protocole;
101
	private Protocole protocole;
83
	private Image image;
102
	private Image image;
84
 
103
 
85
	private int valeurVoteDefaut = -1;
104
	private int valeurVoteDefaut = -1;
86
	private int valeurVoteUtilisateur = -1;
105
	private int valeurVoteUtilisateur = -1;
87
	private int valeurVoteTotal = 0;
106
	private int valeurVoteTotal = 0;
-
 
107
	private double moyenneArithmetique = 0;
-
 
108
	private double mediane = 0;
88
	private int nombrePoints = 0;
109
	private int nombrePoints = 0;
89
	private double valeurVoteTotalPrecise = 0.0;
110
	private double valeurVoteTotalPrecise = 0.0;
-
 
111
	private HashMap<Integer,Integer> occurrencesParNote;
90
 
112
 
91
	// TODO: on devrait passer un conteneur qui permet d'accéder à ces services
113
	// TODO: on devrait passer un conteneur qui permet d'accéder à ces services
92
	private VoteProtocoleService voteProtocoleService;
114
	private VoteProtocoleService voteProtocoleService;
93
 
115
 
94
	public MoyenneVotePresenteur(Image image, Protocole protocole, Vue vue,
116
	public MoyenneVotePresenteur(Image image, Protocole protocole, Vue vue,
95
			VoteProtocoleService voteProtocoleService) {
117
			VoteProtocoleService voteProtocoleService) {
96
		this.vue = vue;
118
		this.vue = vue;
97
		this.protocole = protocole;
119
		this.protocole = protocole;
98
		this.image = image;
120
		this.image = image;
99
		this.voteProtocoleService = voteProtocoleService;
121
		this.voteProtocoleService = voteProtocoleService;
100
 
122
 
101
		String nomProtocole = protocole.getNom();
123
		String nomProtocole = protocole.getNom();
102
		nomProtocole = nomProtocole.substring(0, 1).toUpperCase()
124
		nomProtocole = nomProtocole.substring(0, 1).toUpperCase()
103
				+ nomProtocole.substring(1).toLowerCase();
125
				+ nomProtocole.substring(1).toLowerCase();
104
		vue.getZoneProtocole().setText(nomProtocole);
126
		vue.getZoneProtocole().setText(nomProtocole);
105
	}
127
	}
106
 
128
 
107
	public void go(HasWidgets composite) {
129
	public void go(HasWidgets composite) {
108
		vue.ajouterAuParent(composite);
130
		vue.ajouterAuParent(composite);
109
		gererEvenements();
131
		gererEvenements();
110
		rafraichirVue();
132
		rafraichirVue();
111
	}
133
	}
112
 
134
 
113
	public void gererEvenements() {
135
	public void gererEvenements() {
114
		
136
		
115
		vue.getZoneVoter().addMouseMoveHandler(new MouseMoveHandler() {
137
		vue.getZoneVoter().addMouseMoveHandler(new MouseMoveHandler() {
116
			
138
			
117
			@Override
139
			@Override
118
			public void onMouseMove(MouseMoveEvent event) {
140
			public void onMouseMove(MouseMoveEvent event) {
119
				vue.masquerZoneVoter();
141
				vue.masquerZoneVoter();
120
			}
142
			}
121
		});
143
		});
122
		
144
		
123
		vue.getVotes().addClickHandler(new ClickHandler() {
145
		vue.getVotes().addClickHandler(new ClickHandler() {
124
			@Override
146
			@Override
125
			public void onClick(ClickEvent event) {
147
			public void onClick(ClickEvent event) {
126
				valeurVoteUtilisateur = vue.getValeurVote();
148
				valeurVoteUtilisateur = vue.getValeurVote();
127
				enregistrerVote();
149
				enregistrerVote();
128
				vue.afficherBoutonAnnuler();
150
				vue.afficherBoutonAnnuler();
129
				vue.masquerNbVotes();
-
 
130
				vue.masquerNbPoints();
-
 
131
			}
151
			}
132
		});
152
		});
133
 
153
 
134
		vue.getBoutonAnnuler().addClickHandler(new ClickHandler() {
154
		vue.getBoutonAnnuler().addClickHandler(new ClickHandler() {
135
			@Override
155
			@Override
136
			public void onClick(ClickEvent event) {
156
			public void onClick(ClickEvent event) {
137
				valeurVoteUtilisateur = valeurVoteDefaut;
157
				valeurVoteUtilisateur = valeurVoteDefaut;
138
				vue.masquerBoutonAnnuler();
158
				vue.masquerBoutonAnnuler();
139
				supprimerVote();
159
				supprimerVote();
140
				vue.afficherNbVotes();
-
 
141
				vue.afficherNbPoints();
-
 
142
				vue.reinitialiserVotes();
160
				vue.reinitialiserVotes();
143
			}
161
			}
144
		});
162
		});
145
		
163
 
-
 
164
		// Affiche un chouette panneau qui montre le détail des votes
-
 
165
		vue.getLienNbVotes().addClickHandler(new ClickHandler() {
-
 
166
			@Override
-
 
167
			public void onClick(ClickEvent event) {
-
 
168
				if (vue.getPanneauDetailVotes().isVisible()) {
-
 
169
					vue.masquerPanneauDetailVotes();
-
 
170
				} else {
-
 
171
					vue.afficherPanneauDetailVotes();
-
 
172
				}
-
 
173
			}
-
 
174
		});
-
 
175
 
-
 
176
		vue.getLienPanneauFermer().addClickHandler(new ClickHandler() {
-
 
177
			@Override
-
 
178
			public void onClick(ClickEvent event) {
-
 
179
				vue.masquerPanneauDetailVotes();
-
 
180
			}
-
 
181
		});
-
 
182
 
146
		BusEvenementiel.getInstance().addHandler(EvenementChangementEtatUtilisateur.TYPE, new GestionnaireEvenementChangementEtatUtilisateur() {		
183
		BusEvenementiel.getInstance().addHandler(EvenementChangementEtatUtilisateur.TYPE, new GestionnaireEvenementChangementEtatUtilisateur() {		
147
			@Override
184
			@Override
148
			public void onModificationEtatUtilisateur(EvenementChangementEtatUtilisateur evenementChangementEtatUtilisateur) {
185
			public void onModificationEtatUtilisateur(EvenementChangementEtatUtilisateur evenementChangementEtatUtilisateur) {
149
				rafraichirVue();
186
				rafraichirVue();
150
			}
187
			}
151
		});
188
		});
152
	}
189
	}
153
 
190
 
154
	public void setValeurVoteUtilisateur(int valeurVoteUtilisateur) {
191
	public void setValeurVoteUtilisateur(int valeurVoteUtilisateur) {
155
		this.valeurVoteUtilisateur = valeurVoteUtilisateur;
192
		this.valeurVoteUtilisateur = valeurVoteUtilisateur;
156
	}
193
	}
157
 
194
 
158
	public void enregistrerVote() {
195
	public void enregistrerVote() {
159
		vue.desactiverInteractionVote();
196
		vue.desactiverInteractionVote();
160
		final VoteProtocole voteProtocole = new VoteProtocole();
197
		final VoteProtocole voteProtocole = new VoteProtocole();
161
		voteProtocole.setProtocole(this.protocole);
198
		voteProtocole.setProtocole(this.protocole);
162
		voteProtocole.setVote(valeurVoteUtilisateur);
199
		voteProtocole.setVote(valeurVoteUtilisateur);
163
		String idContributeur = CacheClient.getInstance().getUtilisateur()
200
		String idContributeur = CacheClient.getInstance().getUtilisateur()
164
				.getId();
201
				.getId();
165
		voteProtocole.setContributeur(idContributeur);
202
		voteProtocole.setContributeur(idContributeur);
166
		
203
		
167
		//TODO: ceci devrait pouvoir être factorisé
204
		//TODO: ceci devrait pouvoir être factorisé
168
		if (image.utilisateurAVotePourProtocole(this.protocole.getId() + "",
205
		if (image.utilisateurAVotePourProtocole(this.protocole.getId() + "",
169
				idContributeur)) {
206
				idContributeur)) {
170
			voteProtocole.setId(image.getVotesProtocoles(this.protocole.getId()+"").get(idContributeur).getId());
207
			voteProtocole.setId(image.getVotesProtocoles(this.protocole.getId()+"").get(idContributeur).getId());
171
			VoteProtocoleCallback vpc = new VoteProtocoleCallback() {
208
			VoteProtocoleCallback vpc = new VoteProtocoleCallback() {
172
				@Override
209
				@Override
173
				public void surRetour(Void objetRetour) {
210
				public void surRetour(Void objetRetour) {
174
					image.ajouterVoteProtocole(voteProtocole);
211
					image.ajouterVoteProtocole(voteProtocole);
175
					rafraichirVue();
212
					rafraichirVue();
176
					vue.afficherVoteModifie();
213
					vue.afficherVoteModifie();
177
					vue.activerInteractionVote();
214
					vue.activerInteractionVote();
178
				}
215
				}
179
			};
216
			};
180
			voteProtocoleService.modifierVote(image.getIdImage(),
217
			voteProtocoleService.modifierVote(image.getIdImage(),
181
					voteProtocole, vpc);
218
					voteProtocole, vpc);
182
		} else {
219
		} else {
183
			AjoutVoteProtocoleCallback vpc = new AjoutVoteProtocoleCallback() {
220
			AjoutVoteProtocoleCallback vpc = new AjoutVoteProtocoleCallback() {
184
				@Override
221
				@Override
185
				public void surRetour(String idVote) {
222
				public void surRetour(String idVote) {
186
					voteProtocole.setId(idVote);
223
					voteProtocole.setId(idVote);
187
					image.ajouterVoteProtocole(voteProtocole);
224
					image.ajouterVoteProtocole(voteProtocole);
188
					rafraichirVue();
225
					rafraichirVue();
189
					vue.afficherVotePrisEnCompte();
226
					vue.afficherVotePrisEnCompte();
190
					vue.activerInteractionVote();
227
					vue.activerInteractionVote();
191
				}
228
				}
192
			};
229
			};
193
			voteProtocoleService.ajouterVote(image.getIdImage(), voteProtocole,
230
			voteProtocoleService.ajouterVote(image.getIdImage(), voteProtocole,
194
					vpc);
231
					vpc);
195
		}
232
		}
196
	}
233
	}
197
	
234
	
198
	public void supprimerVote() {
235
	public void supprimerVote() {
199
		vue.desactiverInteractionVote();
236
		vue.desactiverInteractionVote();
200
		final String idContributeur = CacheClient.getInstance().getUtilisateur()
237
		final String idContributeur = CacheClient.getInstance().getUtilisateur()
201
				.getId();
238
				.getId();
202
		final VoteProtocole voteProtocole = image.getVotesProtocoles(protocole.getId()).get(idContributeur);
239
		final VoteProtocole voteProtocole = image.getVotesProtocoles(protocole.getId()).get(idContributeur);
203
		VoteProtocoleCallback vpc = new VoteProtocoleCallback() {
240
		VoteProtocoleCallback vpc = new VoteProtocoleCallback() {
204
			@Override
241
			@Override
205
			public void surRetour(Void objetRetour) {
242
			public void surRetour(Void objetRetour) {
206
				// TODO: voir si l'on affiche un message en cas de succès ?
243
				// TODO: voir si l'on affiche un message en cas de succès ?
207
				image.supprimerVoteProtocole(protocole.getId() + "", idContributeur);
244
				image.supprimerVoteProtocole(protocole.getId() + "", idContributeur);
208
				rafraichirVue();
245
				rafraichirVue();
209
				vue.afficherVoteSupprime();
246
				vue.afficherVoteSupprime();
210
				vue.activerInteractionVote();
247
				vue.activerInteractionVote();
211
			}
248
			}
212
 
249
 
213
		};
250
		};
214
		voteProtocoleService.supprimerVote(image.getIdImage(), voteProtocole, vpc);
251
		voteProtocoleService.supprimerVote(image.getIdImage(), voteProtocole, vpc);
215
	}
252
	}
216
 
253
 
217
	public int getValeurVoteTotal() {
254
	public int getValeurVoteTotal() {
218
		return valeurVoteTotal;
255
		return valeurVoteTotal;
219
	}
256
	}
220
 
257
 
221
	public IsWidget getVue() {
258
	public IsWidget getVue() {
222
		return vue;
259
		return vue;
223
	}
260
	}
224
 
261
 
225
	private void rafraichirVue() {
262
	private void rafraichirVue() {
226
		valeurVoteTotal = calculerMoyenneVotesArrondie();
263
		valeurVoteTotal = calculerMoyenneVotesArrondie();
227
		//valeurVoteTotalPrecise = calculerMoyenneVotes();
264
		moyenneArithmetique = calculerMoyenneVotes();
-
 
265
		mediane = calculerMedianeVotes();
228
		valeurVoteTotalPrecise = calculerMoyennePondereeVotes();
266
		valeurVoteTotalPrecise = calculerMoyennePondereeVotes();
229
		nombrePoints = calculerNombrePointsEchelleArbitraire();
267
		nombrePoints = calculerNombrePointsEchelleArbitraire();
230
		VoteProtocole voteProtocole = image.getVotesProtocoles(
268
		VoteProtocole voteProtocole = image.getVotesProtocoles(
231
				protocole.getId()).get(
269
				protocole.getId()).get(
232
				CacheClient.getInstance().getUtilisateur().getId());
270
				CacheClient.getInstance().getUtilisateur().getId());
233
		int voteUtilisateur = 0;
271
		int voteUtilisateur = 0;
234
		if (voteProtocole != null) {
272
		if (voteProtocole != null) {
235
			voteUtilisateur = voteProtocole.getVote();
273
			voteUtilisateur = voteProtocole.getVote();
236
			vue.masquerZoneVoter();
274
			vue.masquerZoneVoter();
237
		}
275
		}
238
		
276
		
239
		if(image.utilisateurAVotePourProtocole(protocole.getId()+"", CacheClient.getInstance().getUtilisateur().getId())) {
277
		if(image.utilisateurAVotePourProtocole(protocole.getId()+"", CacheClient.getInstance().getUtilisateur().getId())) {
240
			vue.afficherBoutonAnnuler();
278
			vue.afficherBoutonAnnuler();
241
		}
279
		}
242
 
280
 
243
		VoteFleurPresenteur presenteurFleur = new VoteFleurPresenteur(
281
		VoteFleurPresenteur presenteurFleur = new VoteFleurPresenteur(
244
				new VoteFleurVue());
282
				new VoteFleurVue());
245
		presenteurFleur.setNote(valeurVoteTotal);
283
		presenteurFleur.setNote(valeurVoteTotal);
246
		vue.setNoteGenerale(valeurVoteTotal);
284
		vue.setNoteGenerale(valeurVoteTotal);
247
		vue.setNoteGeneraleToolTip(valeurVoteTotalPrecise);
285
		vue.setNoteGeneraleToolTip(valeurVoteTotalPrecise);
248
		presenteurFleur.go(vue.getZoneFleur());
286
		presenteurFleur.go(vue.getZoneFleur());
-
 
287
		vue.rafraichir(voteUtilisateur, image.getVotesProtocoles(protocole.getId()).size(), nombrePoints,
-
 
288
					   moyenneArithmetique, mediane, occurrencesParNote, valeurVoteTotalPrecise);
-
 
289
	}
-
 
290
 
-
 
291
	public double calculerMedianeVotes() {
-
 
292
		double med = 0;
-
 
293
		// comparateur de votes (le bulldozer Java™ pour écraser une mouche)
-
 
294
		class Comparatisateur3000 implements Comparator<VoteProtocole> {
-
 
295
			@Override
-
 
296
			public int compare(VoteProtocole v1, VoteProtocole v2) {
-
 
297
				if (v1.getVote() > v2.getVote()) {
-
 
298
					return 1;
-
 
299
				} else if (v1.getVote() > v2.getVote()) {
-
 
300
					return -1;
-
 
301
				} else {
-
 
302
					return 0;
-
 
303
				}
-
 
304
			}
-
 
305
		}
249
		vue.rafraichir(voteUtilisateur,
306
		// conversion en liste triable
-
 
307
		List<VoteProtocole> liste = new ArrayList<VoteProtocole>(image.getVotesProtocoles(protocole.getId()).values());
-
 
308
		Collections.sort(liste, new Comparatisateur3000());
-
 
309
		int longueur = liste.size();
-
 
310
		if (longueur > 0) {
-
 
311
			if (longueur % 2 == 0) {
-
 
312
				// moyenne des éléments centraux
-
 
313
				med = (liste.get(longueur / 2 - 1).getVote() + liste.get(longueur / 2).getVote()) / 2;
-
 
314
			} else {
-
 
315
				// élément central
-
 
316
				med = liste.get((int) (longueur / 2)).getVote(); 
-
 
317
			}
-
 
318
		}
-
 
319
		return med;
-
 
320
	}
-
 
321
 
-
 
322
	// calcule la moyenne arithmétique
-
 
323
	public double calculerMoyenneVotes() {
-
 
324
		double valeurVote = 0;
-
 
325
		double nbVote = 0;
-
 
326
		for (Iterator<String> iterator = image
250
				image.getVotesProtocoles(protocole.getId()).size(), nombrePoints);
327
				.getVotesProtocoles(protocole.getId()).keySet().iterator(); iterator
-
 
328
				.hasNext();) {
-
 
329
			VoteProtocole imageCelValidationData = image.getVotesProtocoles(
-
 
330
					protocole.getId()).get(iterator.next());
-
 
331
			valeurVote += (double) imageCelValidationData.getVote() / 5;
-
 
332
			nbVote++;
-
 
333
		}
-
 
334
 
-
 
335
		if (nbVote > 0) {
-
 
336
			valeurVote /= nbVote;
-
 
337
			valeurVote *= 5;
-
 
338
		}
-
 
339
 
-
 
340
		return valeurVote;
251
	}
341
	}
252
 
342
 
253
	// Calcule une moyenne, où chaque note est pondérée par son nombre d'occurrences au carré.
343
	// Calcule une moyenne, où chaque note est pondérée par son nombre d'occurrences au carré.
254
	// voir http://www.tela-botanica.org/wikini/DevInformatiques/wakka.php?wiki=AppliDelCalculVotes
344
	// voir http://www.tela-botanica.org/wikini/DevInformatiques/wakka.php?wiki=AppliDelCalculVotes
255
	public double calculerMoyennePondereeVotes() {
345
	public double calculerMoyennePondereeVotes() {
256
 
346
 
257
		double score = 0;
347
		double score = 0;
258
		double diviseur = 0;
348
		double diviseur = 0;
259
		int nbOccurrences;
349
		int nbOccurrences;
-
 
350
		// mise à jour des occurrences par note, utile pour le panneau de détails
260
		HashMap<Integer,Integer> occurrencesParNote = new HashMap<Integer,Integer>();
351
		occurrencesParNote = new HashMap<Integer,Integer>();
261
 
352
 
262
		// rangement par note => occurrences
353
		// rangement par note => occurrences
263
		for (String clef : image.getVotesProtocoles(protocole.getId()).keySet()) {
354
		for (String clef : image.getVotesProtocoles(protocole.getId()).keySet()) {
264
			VoteProtocole imageCelValidationData = image.getVotesProtocoles(protocole.getId()).get(clef);
355
			VoteProtocole imageCelValidationData = image.getVotesProtocoles(protocole.getId()).get(clef);
265
			nbOccurrences = 1;
356
			nbOccurrences = 1;
266
			if (occurrencesParNote.containsKey(imageCelValidationData.getVote())) {
357
			if (occurrencesParNote.containsKey(imageCelValidationData.getVote())) {
267
				nbOccurrences = occurrencesParNote.get(imageCelValidationData.getVote()) + 1;
358
				nbOccurrences = occurrencesParNote.get(imageCelValidationData.getVote()) + 1;
268
			}
359
			}
269
			occurrencesParNote.put(imageCelValidationData.getVote(), nbOccurrences);
360
			occurrencesParNote.put(imageCelValidationData.getVote(), nbOccurrences);
270
		}
361
		}
271
 
362
 
272
		// calcul pondéré
363
		// calcul pondéré
273
		for (Integer clef : occurrencesParNote.keySet()) {
364
		for (Integer clef : occurrencesParNote.keySet()) {
274
			score += clef * occurrencesParNote.get(clef) * occurrencesParNote.get(clef);
365
			score += clef * occurrencesParNote.get(clef) * occurrencesParNote.get(clef);
275
			diviseur += occurrencesParNote.get(clef) * occurrencesParNote.get(clef);
366
			diviseur += occurrencesParNote.get(clef) * occurrencesParNote.get(clef);
276
		}
367
		}
277
 
368
 
278
		if (diviseur > 0) {
369
		if (diviseur > 0) {
279
			score /= diviseur;
370
			score /= diviseur;
280
		}
371
		}
281
 
372
 
282
		return score;
373
		return score;
283
	}
374
	}
284
 
375
 
285
	// remplace chaque note par un nombre de points noté dans "echelle" afin de favoriser
376
	// remplace chaque note par un nombre de points noté dans "echelle" afin de favoriser
286
	// les note fortes (5 et 4), pour le défi photo notamment
377
	// les note fortes (5 et 4), pour le défi photo notamment
287
	public int calculerNombrePointsEchelleArbitraire() {
378
	public int calculerNombrePointsEchelleArbitraire() {
288
		int points = 0;
379
		int points = 0;
289
		int[] echelle = {-1,0,1,4,20};
380
		//int[] echelle = {-1,0,1,4,20};
-
 
381
		int[] echelle = {1,10,100,1000,10000};
290
 
382
 
291
		for (String clef : image.getVotesProtocoles(protocole.getId()).keySet()) {
383
		for (String clef : image.getVotesProtocoles(protocole.getId()).keySet()) {
292
			VoteProtocole imageCelValidationData = image.getVotesProtocoles(protocole.getId()).get(clef);
384
			VoteProtocole imageCelValidationData = image.getVotesProtocoles(protocole.getId()).get(clef);
293
			points += echelle[imageCelValidationData.getVote() - 1];
385
			points += echelle[imageCelValidationData.getVote() - 1];
294
		}
386
		}
295
 
387
 
296
		return points;
388
		return points;
297
	}
389
	}
298
 
390
 
299
	public int calculerMoyenneVotesArrondie() {
391
	public int calculerMoyenneVotesArrondie() {
300
		//double valeurVote = calculerMoyenneVotes();
392
		//double valeurVote = calculerMoyenneVotes();
301
		double valeurVote = calculerMoyennePondereeVotes();
393
		double valeurVote = calculerMoyennePondereeVotes();
302
		return (int) Math.round(valeurVote);
394
		return (int) Math.round(valeurVote);
303
	}
395
	}
304
}
396
}