| 1567 | mathias | 1 | <?php
 | 
        
           |  |  | 2 | namespace TelaBotanica\Del\Commun;
 | 
        
           |  |  | 3 |   | 
        
           |  |  | 4 | class Stats {
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | 	static function updateStats($db, $id_image, $id_proto) {
 | 
        
           |  |  | 7 | 		// @TODO Attention aux situations de concurrence possible - nécessite une
 | 
        
           |  |  | 8 | 		// transaction mais la flemme de tout mettre en InnoDB
 | 
        
           |  |  | 9 | 		$id_image = intval($id_image);
 | 
        
           |  |  | 10 | 		$id_proto = intval($id_proto);
 | 
        
           | 1598 | jpm | 11 | 		if (!$id_image || !$id_proto) throw new Exception("Ne peut mettre à jour les statistiques de vote",
 | 
        
           |  |  | 12 | 			RestServeur::HTTP_CODE_ERREUR);
 | 
        
           |  |  | 13 |   | 
        
           | 1567 | mathias | 14 | 		// 1) choper tous les votes pour le protocole choisi
 | 
        
           |  |  | 15 | 		$votes = $db->requeter(sprintf(
 | 
        
           | 1598 | jpm | 16 | 			' SELECT ce_image, ce_protocole, valeur, ce_utilisateur'.
 | 
        
           |  |  | 17 | 			' FROM del_image_vote'.
 | 
        
           |  |  | 18 | 			' WHERE ce_image = %d AND ce_protocole = %d',
 | 
        
           |  |  | 19 | 			$id_image, $id_proto));
 | 
        
           |  |  | 20 |   | 
        
           | 1567 | mathias | 21 | 		// 2) calculer la moyenne pondérée, le nombre de points
 | 
        
           |  |  | 22 | 		// @ACHTUNG_ALARM attention à ce que cette méthode corresponde avec les
 | 
        
           |  |  | 23 | 		// calculs dans GWT, notamment l'échelle de points !!
 | 
        
           |  |  | 24 | 		$nbPoints = 0;
 | 
        
           |  |  | 25 | 		$nbVotes = 0;
 | 
        
           | 1582 | mathias | 26 | 		//$echelle = array(-1, 0, 1, 4, 20);
 | 
        
           |  |  | 27 | 		$echelle = array(1, 10, 100, 1000, 10000);
 | 
        
           | 1567 | mathias | 28 | 		$notesParOccurrences = array();
 | 
        
           | 1572 | mathias | 29 | 		$utilisateurs = array();
 | 
        
           | 1567 | mathias | 30 | 		foreach ($votes as $vote) {
 | 
        
           | 1572 | mathias | 31 | 			if (! in_array($vote['ce_utilisateur'], $utilisateurs)) {
 | 
        
           |  |  | 32 | 				// un seul vote par utilisateur sur un protocole donné !!
 | 
        
           |  |  | 33 | 				$utilisateurs[] = $vote['ce_utilisateur'];
 | 
        
           |  |  | 34 | 				$note = $vote['valeur'];
 | 
        
           |  |  | 35 | 				$nbPoints += $echelle[$note - 1];
 | 
        
           |  |  | 36 | 				if (array_key_exists($note, $notesParOccurrences)) {
 | 
        
           |  |  | 37 | 					$notesParOccurrences[$note]++;
 | 
        
           |  |  | 38 | 				} else {
 | 
        
           |  |  | 39 | 					$notesParOccurrences[$note] = 1;
 | 
        
           |  |  | 40 | 				}
 | 
        
           |  |  | 41 | 				$nbVotes++;
 | 
        
           | 1567 | mathias | 42 | 			}
 | 
        
           |  |  | 43 | 		}
 | 
        
           |  |  | 44 | 		$moyennePonderee = 0;
 | 
        
           |  |  | 45 | 		$diviseur = 0;
 | 
        
           |  |  | 46 | 		foreach ($notesParOccurrences as $n => $o) {
 | 
        
           |  |  | 47 | 			$moyennePonderee += ($n * $o * $o);
 | 
        
           |  |  | 48 | 			$diviseur += ($o * $o);
 | 
        
           |  |  | 49 | 		}
 | 
        
           |  |  | 50 | 		if ($diviseur > 0) {
 | 
        
           |  |  | 51 | 			$moyennePonderee = $moyennePonderee / $diviseur;
 | 
        
           |  |  | 52 | 		}
 | 
        
           | 1598 | jpm | 53 |   | 
        
           | 1567 | mathias | 54 | 		// 3) mise à jour de la table stats
 | 
        
           |  |  | 55 | 		$db->requeter(sprintf(
 | 
        
           | 1598 | jpm | 56 | 			' INSERT INTO del_image_stat (ce_image, ce_protocole, moyenne, nb_votes, nb_points)'.
 | 
        
           |  |  | 57 | 			' VALUES (%d, %d, %s, %d, %d)'.
 | 
        
           |  |  | 58 | 			' ON DUPLICATE KEY UPDATE moyenne = %s, nb_votes = %d, nb_points = %d',
 | 
        
           |  |  | 59 | 			$id_image, $id_proto, number_format($moyennePonderee, 3, '.', ''), $nbVotes, $nbPoints,
 | 
        
           |  |  | 60 | 			number_format($moyennePonderee, 3, '.', ''), $nbVotes, $nbPoints));
 | 
        
           |  |  | 61 |   | 
        
           | 1567 | mathias | 62 | 	}
 | 
        
           | 1796 | jpm | 63 | }
 |