Skip to content
template_choice.go 4.41 KiB
Newer Older
Lo^2's avatar
wip
Lo^2 committed
package page_vote

import ()

type TemplateVars struct {
Lo^2's avatar
Lo^2 committed
	Position          int
	Name              string
	Title             string
	Sentence          string
	MinChoices        int
	MaxChoices        int
	VoteCount         int
	VoteOpen          bool
	VotedAlready      bool
	VoteError         string
	VoterPrivateToken string
Lo^2's avatar
Lo^2 committed
	UserUuid          string
Lo^2's avatar
Lo^2 committed
	VoterFullname     string
	Answers           []TemplateAnswer
	TotalVoteCount    int
Lo^2's avatar
wip
Lo^2 committed
}

type TemplateAnswer struct {
	Name      string
Lo^2's avatar
Lo^2 committed
	Checked   bool
Lo^2's avatar
wip
Lo^2 committed
	Sentence  string
	VoteCount int
}

const TEMPLATE_CHOICE_STR = `<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Vote Électrolab - {{.Title}}</title>
Lo^2's avatar
Lo^2 committed
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
Lo^2's avatar
wip
Lo^2 committed
		<script type="text/javascript">
			function checkForm() {
				{{ if not .VoteOpen }}
					return false;
Lo^2's avatar
Lo^2 committed
				{{ else }}
					var nbOfAnswers = 0;
					var answers = document.getElementsByName("answer");
					for (var i=0; i<answers.length; i++) {
						if (answers[i].checked) {
							nbOfAnswers++;
						}
Lo^2's avatar
wip
Lo^2 committed
					}

Lo^2's avatar
Lo^2 committed
					if (nbOfAnswers < {{.MinChoices}}) {
						alert("Vous devez sélectionner au minimum {{.MinChoices}} réponse(s)");
						return false;
					}
					if ({{.MaxChoices}} >=0 && (nbOfAnswers > {{.MaxChoices}})) {
						alert("Vous devez sélectionner au maximum {{.MinChoices}} réponse(s)");
						return false;
					}
Lo^2's avatar
wip
Lo^2 committed

Lo^2's avatar
Lo^2 committed
					return confirm("Tout vote est définitif. Cliquez sur OK pour valider le vote.");
				{{ end }}
Lo^2's avatar
wip
Lo^2 committed
			}
		</script>
Lo^2's avatar
Lo^2 committed
		<style type="text/css">
			.vote-page {
				padding: 1em;
				max-width:50em;
			}
		</style>
Lo^2's avatar
wip
Lo^2 committed
	</head>
	<body>
Lo^2's avatar
Lo^2 committed
		<div class="vote-page">
			<nav aria-label="breadcrumb">
			  <ol class="breadcrumb">
			    <li class="breadcrumb-item" aria-current="page"><a href="/?private_token={{.VoterPrivateToken}}">Événements</a></li>
			    <li class="breadcrumb-item" aria-current="page"><a href="/votes?private_token={{.VoterPrivateToken}}">AG Électrolab 2020</a></li>
			    <li class="breadcrumb-item active" aria-current="page">Vote nº{{.Position}} - {{.Title}}</li>
			  </ol>
			</nav>
Lo^2's avatar
wip
Lo^2 committed

Lo^2's avatar
Lo^2 committed
			<h1>Vote nº{{.Position}} - {{.Title}}</h1>
Lo^2's avatar
wip
Lo^2 committed

Lo^2's avatar
Lo^2 committed
			<h2>{{.Sentence}}</h2>
Lo^2's avatar
Lo^2 committed

Lo^2's avatar
Lo^2 committed
			{{ if .VoteError }}
				<p class="alert alert-danger">
					Votre vote n'a pas pu être pris en compte!<br/>
					Erreur lors de la procédure: {{.VoteError}}.
				</p>
			{{ end}}
Lo^2's avatar
wip
Lo^2 committed

Lo^2's avatar
Lo^2 committed
			{{ if .VotedAlready }}
				<p>Votre vote a été enregistré. Vous pourrez rafraichir la page au moment de la cloture pour voir les résultats.</p>
			{{ else }}

				{{ if not .VoteOpen }}
					<div class="alert alert-primary">Le vote n'est pas encore ouvert. Vous pourrez <a href="?private_token={{.VoterPrivateToken}}">rafraichir</a> la page au moment de l'ouverture pour voter.</div>
Lo^2's avatar
Lo^2 committed
				{{ end }}
Lo^2's avatar
wip
Lo^2 committed

Lo^2's avatar
Lo^2 committed
				<form id="answers_form" method="post" onsubmit="return checkForm();" autocomplete="off">
Lo^2's avatar
Lo^2 committed
					<label>
						{{ if (and (eq 1 $.MinChoices) (eq 1 $.MaxChoices)) }}
Lo^2's avatar
Lo^2 committed
							Liste des choix :
Lo^2's avatar
Lo^2 committed
						{{ else if (and (gt $.MinChoices 0) (ge $.MaxChoices 0)) }}
Lo^2's avatar
Lo^2 committed
							{{if .VoteOpen}}
								Cochez
							{{else}}
								Vous devrez cocher
							{{end}}
							entre {{$.MinChoices}} et {{$.MaxChoices}} choix) :
Lo^2's avatar
Lo^2 committed
						{{ else if gt $.MinChoices 0 }}
Lo^2's avatar
Lo^2 committed
							{{if .VoteOpen}}
								Cochez
							{{else}}
								Vous devrez cocher
							{{end}}
							au minimum {{$.MinChoices}} choix :
Lo^2's avatar
Lo^2 committed
						{{ else if ge $.MaxChoices 0 }}
Lo^2's avatar
Lo^2 committed
							{{if .VoteOpen}}
								Cochez
							{{else}}
								Vous devrez cocher
							{{end}}
							au maximum {{$.MaxChoices}} choix :
						{{ else }}
							Liste des choix :
Lo^2's avatar
Lo^2 committed
						{{ end }}
					</label>
					<div class="form-group">
						{{ range .Answers }}
							<div class="form-check">
Lo^2's avatar
Lo^2 committed
								<input
									class="form-check-input"
									{{if not $.VoteOpen}}
										disabled
									{{end}}
									{{if .Checked}}
										checked
									{{end}}
									type="{{ if (and (eq 1 $.MinChoices) (eq 1 $.MaxChoices)) }}radio{{else}}checkbox{{end}}"
									id="answer_{{.Name}}"
									name="answer"
									value="{{.Name}}"
								>
								<label
									class="form-check-label"
									for="answer_{{.Name}}"
								>{{.Sentence}}</label>
Lo^2's avatar
Lo^2 committed
							</div>
						{{ end }}
					</div>

					{{ if .VoteOpen }}
Lo^2's avatar
Lo^2 committed
						<button type="submit" class="btn btn-danger">Voter</button>
Lo^2's avatar
Lo^2 committed
					{{ end }}
				</form>
			{{ end }}
		</div>
Lo^2's avatar
wip
Lo^2 committed
	</body>
</html>
`