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

Lo^2's avatar
Lo^2 committed
import (
	"code.electrolab.fr/it/vote.electrolab.fr/model"
)
Lo^2's avatar
wip
Lo^2 committed

type TemplateVars struct {
Lo^2's avatar
Lo^2 committed
	PageTitle string
	Question  *model.Question

	// QuestionPosition int
	// QuestionName     string // unused
	// QuestionTitle string
	// Sentence     string
	// VoteOpen     bool
	// VoteCount    int
	VotedAlready bool

Lo^2's avatar
Lo^2 committed
	MinChoices        int
	MaxChoices        int
	VoteError         string
	VoterPrivateToken string
	VoterFullname     string
	Answers           []TemplateAnswer
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
}

Lo^2's avatar
Lo^2 committed
const TEMPLATE_CHOICE_STR = `
	<script type="text/javascript">
		function checkForm() {
			{{ if not .Question.VoteOpen }}
				return false;
			{{ 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
Lo^2 committed
					}
Lo^2's avatar
Lo^2 committed
				}
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 }}
		}
	</script>
	<div class="vote-page">
		<nav aria-label="breadcrumb">
		  <ol class="breadcrumb">
		    <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º{{.Question.Position}} - {{.Question.Title}}</li>
		  </ol>
		</nav>
Lo^2's avatar
wip
Lo^2 committed

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

Lo^2's avatar
Lo^2 committed
		<h2>{{.Question.Sentence}}</h2>
Lo^2's avatar
wip
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
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 }}
Lo^2's avatar
wip
Lo^2 committed

Lo^2's avatar
Lo^2 committed
			{{ if not .Question.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>
			{{ end }}
Lo^2's avatar
Lo^2 committed

Lo^2's avatar
Lo^2 committed
			<form id="answers_form" method="post" onsubmit="return checkForm();" autocomplete="off">
				<label>
					{{ if (and (eq 1 $.MinChoices) (eq 1 $.MaxChoices)) }}
						Liste des choix :
					{{ else if (and (gt $.MinChoices 0) (ge $.MaxChoices 0)) }}
						{{if .Question.VoteOpen}}
							Cochez
						{{else}}
							Vous devrez cocher
						{{end}}
						entre {{$.MinChoices}} et {{$.MaxChoices}} choix) :
					{{ else if gt $.MinChoices 0 }}
						{{if .Question.VoteOpen}}
							Cochez
						{{else}}
							Vous devrez cocher
						{{end}}
						au minimum {{$.MinChoices}} choix :
					{{ else if ge $.MaxChoices 0 }}
						{{if .Question.VoteOpen}}
							Cochez
						{{else}}
							Vous devrez cocher
						{{end}}
						au maximum {{$.MaxChoices}} choix :
					{{ else }}
						Liste des choix :
Lo^2's avatar
Lo^2 committed
					{{ end }}
Lo^2's avatar
Lo^2 committed
				</label>
				<div class="form-group">
					{{ range .Answers }}
						<div class="form-check">
							<input
								class="form-check-input"
								{{if not $.Question.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>
						</div>
					{{ end }}
				</div>

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