github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/vote/rapport.go (about)

     1  package vote
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/benoitkugler/goACVE/server/core/utils/pdf"
     9  )
    10  
    11  const (
    12  	nbCandidatPerRow   = 3
    13  	padding            = 3
    14  	limitLabelCandidat = 69 / nbCandidatPerRow // ajusté manuellement
    15  	widthResult        = 10                    // ajusté manuellement
    16  )
    17  
    18  // Edite un bilan du vote :
    19  //	- un bilan global (admin)
    20  //	- un résumé personnel
    21  
    22  type voteParticipants struct {
    23  	VoteAdmin
    24  
    25  	nbParticipants int
    26  	nbTotal        int
    27  }
    28  
    29  func limit(s string) string {
    30  	runes := []rune(s)
    31  	if len(runes) > limitLabelCandidat {
    32  		return string(runes[:limitLabelCandidat]) + "."
    33  	}
    34  	return s
    35  }
    36  
    37  // renvoie un pdf résumant la participant et les résultats
    38  func genereBilanVotes(votes []voteParticipants) ([]byte, error) {
    39  	f := pdf.InitPdfDetails(true, true)
    40  	le := "le " + time.Now().Format("02/01/2006")
    41  	f.DrawHeader([]string{le}, 0, 0)
    42  	f.SetFont("Arial", "B", 16)
    43  	f.CellFormat(0, 20, "Bilan des votes", "", "MC", false)
    44  	f.Ln(-1)
    45  	f.SetFontSize(12)
    46  	lh := f.LineHeight()
    47  	h := f.HTMLBasicNew()
    48  	mL, _, _, _ := f.GetMargins()
    49  	for _, vote := range votes {
    50  		// on affiche le nom
    51  		h.Write(lh, f.Tr(fmt.Sprintf("<b>%s</b> - <i>Voix exprimées : </i>%d / %d ", vote.Nom, vote.nbParticipants, vote.nbTotal)))
    52  		f.Ln(0.3 * lh)
    53  		f.SetLeftMargin(mL + padding)
    54  		f.SetFontSize(10)
    55  		// on affiche les voix
    56  		for index, candidat := range vote.Candidats {
    57  			if index%nbCandidatPerRow == 0 {
    58  				f.Ln(-1)
    59  			}
    60  			cellWidth := f.AreaWidth() / nbCandidatPerRow
    61  			beginCell := mL + padding + float64(index%nbCandidatPerRow)*cellWidth
    62  			f.SetX(beginCell)
    63  			h.Write(lh, f.Tr(fmt.Sprintf("%s:", limit(candidat.Label))))
    64  			f.SetX(beginCell + cellWidth - widthResult)
    65  			h.Write(lh, f.Tr(fmt.Sprintf("<b>%d</b>", vote.Voix[candidat.Id])))
    66  		}
    67  		f.SetLeftMargin(mL)
    68  		f.SetFontSize(12)
    69  		f.Ln(2.5 * lh)
    70  	}
    71  
    72  	var out bytes.Buffer
    73  	err := f.Output(&out)
    74  	return out.Bytes(), err
    75  }
    76  
    77  // renvoie un pdf résumant les votes d'une personne
    78  func genereBilanPersonne(membre MetaPersonne, votes []VotePersonneComplet) ([]byte, error) {
    79  	f := pdf.InitPdfDetails(true, true)
    80  	le := "le " + time.Now().Format("02/01/2006")
    81  	f.DrawHeader([]string{le}, 0, 0)
    82  	f.SetFont("Arial", "B", 16)
    83  	f.CellFormat(0, 20, "Votes de "+membre.PrenomNom, "", "MC", false)
    84  	f.Ln(-1)
    85  	f.SetFontSize(12)
    86  	lh := f.LineHeight()
    87  	h := f.HTMLBasicNew()
    88  	mL, _, _, _ := f.GetMargins()
    89  	for _, vote := range votes {
    90  		if vote.Time.IsZero() {
    91  			// on affiche uniquement les votes réellement effectués
    92  			continue
    93  		}
    94  		// on affiche le nom
    95  		h.Write(lh, f.Tr(fmt.Sprintf("<b>%s</b>", vote.Nom)))
    96  		f.Ln(0.3 * lh)
    97  		f.SetLeftMargin(mL + padding)
    98  
    99  		// on affiche les choix cochés
   100  		candidatsMap := make(map[int64]Candidat)
   101  		for _, c := range vote.Candidats {
   102  			candidatsMap[c.Id] = c
   103  		}
   104  		for index, choix := range vote.Choix {
   105  			candidat := candidatsMap[choix]
   106  			if index%nbCandidatPerRow == 0 {
   107  				f.Ln(-1)
   108  			}
   109  			offset := float64(index%nbCandidatPerRow) * f.AreaWidth() / nbCandidatPerRow
   110  			f.SetX(mL + padding + offset)
   111  			h.Write(lh, f.Tr(limit(candidat.Label)))
   112  		}
   113  		f.SetLeftMargin(mL)
   114  		f.Ln(2.5 * lh)
   115  	}
   116  
   117  	var out bytes.Buffer
   118  	err := f.Output(&out)
   119  	return out.Bytes(), err
   120  }