Skip to content
template.go 5.34 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
Lo^2's avatar
Lo^2 committed
	User      *model.User
Lo^2's avatar
Lo^2 committed
	Error     string
Lo^2's avatar
Lo^2 committed
	VoteError string
Lo^2's avatar
wip
Lo^2 committed
}

Lo^2's avatar
Lo^2 committed
const TEMPLATE_STR = `
Lo^2's avatar
Lo^2 committed
	<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 < {{.Question.MinChoices}}) {
					alert("Vous devez sélectionner au minimum {{.Question.MinChoices}} réponse(s)");
Lo^2's avatar
Lo^2 committed
					return false;
				}
Lo^2's avatar
Lo^2 committed
				if ({{.Question.MaxChoices}} >=0 && (nbOfAnswers > {{.Question.MaxChoices}})) {
					alert("Vous devez sélectionner au maximum {{.Question.MinChoices}} réponse(s)");
Lo^2's avatar
Lo^2 committed
					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">
Lo^2's avatar
Lo^2 committed
		    <li class="breadcrumb-item" aria-current="page"><a href="../votes?private_token={{.User.PrivateToken}}">AG Électrolab 2020</a></li>
Lo^2's avatar
Lo^2 committed
		    <li class="breadcrumb-item active" aria-current="page">
		    	<a href="?private_token={{.User.PrivateToken}}">
		    		Vote nº{{.Question.Position}} - {{.Question.Title}}

					{{ if .Question.VotedAlready }}
						<span class="badge badge-info">a voté</span>
					{{ end }}

					{{ if .Question.VoteOpen }}
						<span class="badge badge-success">en cours</span>
					{{ else if and (gt .Question.VoteCount 0) (not .Question.VotedAlready) }}
						<span class="badge badge-danger">clos</span>
					{{ end }}

		    	</a></li>
Lo^2's avatar
Lo^2 committed
		  </ol>
		</nav>
Lo^2's avatar
wip
Lo^2 committed

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

Lo^2's avatar
Lo^2 committed
		<p><em>{{.Question.Sentence}}</em></p>
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 .Error }}
			<p class="alert alert-danger">
				Erreur: {{.VoteError}}.
			</p>
		{{ end}}

		{{ if .User.Admin }}
			<form id="openclose" method="post" autocomplete="off">
				<input type="hidden" name="action" value="openclose"/>
				{{ if .Question.VoteOpen }}
					<input type="hidden" name="open" value="false"/>
					<button type="submit" class="btn btn-primary">Clore le vote</button>
				{{ else }}
					<input type="hidden" name="open" value="true"/>
					<button type="submit" class="btn btn-primary">Ouvrir le vote</button>
				{{ end }}
			</form>
		{{ end }}
Lo^2's avatar
wip
Lo^2 committed

Lo^2's avatar
Lo^2 committed

Lo^2's avatar
Lo^2 committed
		{{ if or .Question.VoteOpen (eq .Question.VoteCount 0) }}

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

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

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

Lo^2's avatar
Lo^2 committed
		{{ else }}
Lo^2's avatar
Lo^2 committed
			<h6>Le vote est clos. Résultats:</h6>
Lo^2's avatar
Lo^2 committed
		<ul>
			<li><strong>Votants</strong>: {{.Question.VoteCount}}</li>
		</ul>
Lo^2's avatar
Lo^2 committed
		<ul>
Lo^2's avatar
Lo^2 committed
			{{ range .Question.Answers }}
				{{ if gt .VoteCount 1 }}
Lo^2's avatar
Lo^2 committed
					<li><strong>{{.Sentence}}</strong>: {{.VoteCount}} votes</li>
Lo^2's avatar
Lo^2 committed
				{{ else }}
Lo^2's avatar
Lo^2 committed
					<li><strong>{{.Sentence}}</strong>: {{.VoteCount}} vote</li>
Lo^2's avatar
Lo^2 committed
				{{ end }}
Lo^2's avatar
Lo^2 committed
			{{ end }}
Lo^2's avatar
Lo^2 committed
		</ul>
Lo^2's avatar
Lo^2 committed
		{{ end }}
	</div>
Lo^2's avatar
wip
Lo^2 committed
`