Permalink
Browse files

[Evo] Add a polls page.

  • Loading branch information...
1 parent 40dd840 commit ecfc2d2f85175db1f6c2dad655c7a7d3abc834c1 @dboissin dboissin committed Sep 6, 2011
View
@@ -16,4 +16,6 @@ tmp/**/*
.classpath
.settings/
.loadpath
+nbproject/
+nbproject/
@@ -3,9 +3,11 @@
import java.io.File;
import java.util.List;
+import java.util.Set;
import models.Event;
import models.EventPartner;
import models.News;
+import models.Poll;
import models.Speaker;
import models.YearPartner;
import play.mvc.Before;
@@ -15,29 +17,29 @@
@With(Filters.class)
public class Application extends Controller {
-
- @Before
- public static void addDefaults() {
- renderArgs.put("yearpartners", YearPartner.getCurrent());
- }
-
+
+ @Before
+ public static void addDefaults() {
+ renderArgs.put("yearpartners", YearPartner.getCurrent());
+ }
+
public static void index() {
Event event = Event.next();
if (event == null) {
event = Event.last();
}
render(event);
}
-
+
public static void event(Long id) {
Event event = Event.findById(id);
String[] attachments = Event.attachments(id);
if (event != null && event.description != null){
- event.description = Textile.toHTML(event.description);
+ event.description = Textile.toHTML(event.description);
}
render(event, attachments);
}
-
+
public static void about() {
if (renderArgs.get("nextEventId") != null) {
Event event = Event.findById(renderArgs.get("nextEventId"));
@@ -46,11 +48,11 @@ public static void about() {
render();
}
}
-
+
public static void view() {
render();
}
-
+
public static void members() {
List<Speaker> members = Speaker.getMembers();
if (renderArgs.get("nextEventId") != null) {
@@ -60,12 +62,12 @@ public static void members() {
render(members);
}
}
-
+
public static void member(Long id) {
Speaker member = Speaker.findById(id);
List<Event> memberEvent = Speaker.getSpeakerEvents(id);
if (member != null && member.description != null){
- member.description = Textile.toHTML(member.description);
+ member.description = Textile.toHTML(member.description);
}
if (renderArgs.get("nextEventId") != null) {
Event event = Event.findById(renderArgs.get("nextEventId"));
@@ -86,18 +88,18 @@ public static void downloadEventAttachment(Long eventId, String attachment) {
/**
* List all the talks tagged with the given tag name
- *
+ *
* @param tag
*/
public static void listTagged(String tag) {
- // TODO
- index();
+ // TODO
+ index();
}
-
+
/* News Section */
public static void news() {
- List<News> news = News.allByDate();
+ List<News> news = News.allByDate();
if (renderArgs.get("nextEventId") != null) {
Event event = Event.findById(renderArgs.get("nextEventId"));
render(news, event);
@@ -106,46 +108,46 @@ public static void news() {
}
}
- public static void newsDetail(Long id) {
- News news = News.findById(id);
- if (renderArgs.get("nextEventId") != null) {
- Event event = Event.findById(renderArgs.get("nextEventId"));
- render(news, event);
- } else {
- render(news);
- }
- }
+ public static void newsDetail(Long id) {
+ News news = News.findById(id);
+ if (renderArgs.get("nextEventId") != null) {
+ Event event = Event.findById(renderArgs.get("nextEventId"));
+ render(news, event);
+ } else {
+ render(news);
+ }
+ }
public static void newsFeed() {
- request.format = "rss";
- List<News> news = News.allByDate();
- response.setContentTypeIfNotSet("application/rss+xml");
- render(news);
+ request.format = "rss";
+ List<News> news = News.allByDate();
+ response.setContentTypeIfNotSet("application/rss+xml");
+ render(news);
}
public static void partners() {
- List<YearPartner> partners = YearPartner.getCurrent();
- List<EventPartner> eventPartners = EventPartner.all().fetch();
- render(partners, eventPartners);
+ List<YearPartner> partners = YearPartner.getCurrent();
+ List<EventPartner> eventPartners = EventPartner.all().fetch();
+ render(partners, eventPartners);
}
public static void partner(Long id) {
- YearPartner partner = YearPartner.findById(id);
- render(partner);
+ YearPartner partner = YearPartner.findById(id);
+ render(partner);
}
public static void oldPartners() {
- List<YearPartner> partners = YearPartner.getOldies();
- render(partners);
+ List<YearPartner> partners = YearPartner.getOldies();
+ render(partners);
}
public static void eventPartner(Long id) {
- EventPartner partner = EventPartner.findById(id);
- render(partner);
+ EventPartner partner = EventPartner.findById(id);
+ render(partner);
}
public static void speakers() {
- List<Speaker> speakers = Speaker.getSpeakers();
+ List<Speaker> speakers = Speaker.getSpeakers();
if (renderArgs.get("nextEventId") != null) {
Event event = Event.findById(renderArgs.get("nextEventId"));
render(speakers, event);
@@ -155,4 +157,15 @@ public static void speakers() {
}
+ public static void polls() {
+ List<Poll> polls = Poll.getVisibles();
+ Set<Long> closedPolls = Poll.closedPolls(request.cookies.get("polls"));
+ if (renderArgs.get("nextEventId") != null) {
+ Event event = Event.findById(renderArgs.get("nextEventId"));
+ render(event, polls, closedPolls);
+ } else {
+ render(polls, closedPolls);
+ }
+ }
+
}
@@ -0,0 +1,23 @@
+package controllers;
+
+import models.Answer;
+import models.Poll;
+import play.mvc.Controller;
+import utils.RenderJsonExclusion;
+
+/**
+ *
+ * @author dboissin
+ */
+public class PollController extends Controller {
+
+ public static void vote(Long answerId) {
+ notFoundIfNull(answerId);
+ Poll poll = Answer.vote(answerId, request, response);
+ renderJSONExclusion(poll);
+ }
+
+ private static void renderJSONExclusion(Object o) {
+ throw new RenderJsonExclusion(o);
+ }
+}
View
@@ -0,0 +1,57 @@
+package models;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.persistence.Entity;
+import javax.persistence.ManyToOne;
+
+import play.data.validation.Required;
+import play.db.jpa.Model;
+import play.mvc.Http.Cookie;
+import play.mvc.Http.Request;
+import play.mvc.Http.Response;
+import utils.JsonExclude;
+
+@Entity
+public class Answer extends Model {
+
+ @JsonExclude
+ @ManyToOne
+ public Poll poll;
+
+ @Required
+ public String answer;
+
+ public int votes;
+
+ @Override
+ public String toString() {
+ return answer;
+ }
+
+ public static Poll vote(Long answerId, Request request, Response response) {
+ Cookie cookie = request.cookies.get("polls");
+ Answer answer = Answer.findById(answerId);
+ Poll poll = answer.poll;
+
+ String pollsIds = "";
+ if (cookie != null) {
+ pollsIds = cookie.value;
+
+ String [] ids = pollsIds.substring(1).split(",");
+ List<Long> polls = new ArrayList<Long>();
+ for (String id: ids) {
+ polls.add(Long.valueOf(id));
+ }
+
+ if (polls.contains(poll.id)) {
+ return poll;
+ }
+ }
+ response.setCookie("polls", "," + poll.id + pollsIds, "365d");
+ answer.votes++;
+ answer.save();
+ return poll;
+ }
+
+}
View
@@ -0,0 +1,61 @@
+package models;
+
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.OneToMany;
+
+import play.data.binding.As;
+import play.data.validation.MaxSize;
+import play.data.validation.Required;
+import play.db.jpa.Model;
+import play.mvc.Http.Cookie;
+
+@Entity
+public class Poll extends Model {
+
+ @Required
+ @MaxSize(1000)
+ @Column(length = 1000)
+ public String question;
+
+ @OneToMany(mappedBy = "poll", cascade = CascadeType.ALL)
+ public Set<Answer> answers;
+
+ @As("yyyy-MM-dd")
+ public Date expiryDate;
+
+ public boolean visible;
+
+ @Override
+ public String toString() {
+ return question;
+ }
+
+ public static Set<Long> closedPolls(Cookie userPollsCookie) {
+ Set<Long> closedPolls = new HashSet<Long>();
+ if (userPollsCookie != null) {
+ String voted = userPollsCookie.value.substring(1);
+ for (String id: voted.split(",")) {
+ closedPolls.add(Long.valueOf(id));
+ }
+ }
+ closedPolls.addAll(visiblesExpiredIds());
+ return closedPolls;
+ }
+
+ public static List<Long> visiblesExpiredIds() {
+ return Poll.find("select poll.id from Poll poll " +
+ "where poll.visible = ? and poll.expiryDate < ?",
+ true, new Date()).fetch();
+ }
+
+ public static List<Poll> getVisibles() {
+ return Poll.find("visible = ?", true).fetch();
+ }
+}
@@ -0,0 +1,18 @@
+#{extends 'main.html' /}
+#{set title:'Sondages' /}
+
+#{if event}
+#{include 'Application/_eventHeader.html' /}
+#{/if}
+
+<section id="members">
+ <h2>&{'navigation.polls'}</h2>
+ #{list items: polls, as: 'poll'}
+ <article>
+ <div>
+ %{ type = closedPolls.contains(poll.id) ? 'ro' : 'rw' }%
+ #{poll poll: poll, as: type /}
+ </div>
+ </article>
+ #{/list}
+</section>
View
@@ -22,6 +22,7 @@
<!-- <li><a href="#">Publications</a></li>-->
<li ${request.actionMethod == 'about' ? 'class=active' : ''}><a href="@{Application.about()}">A propos</a></li>
<li ${request.actionMethod.startsWith('member') ? 'class=active' : ''}><a href="@{Application.members()}">&{'navigation.members'}</a></li>
+ <li ${request.actionMethod.startsWith('polls') ? 'class=active' : ''}><a href="@{Application.polls()}">&{'navigation.polls'}</a></li>
</ul>
</nav>
#{doLayout /}
Oops, something went wrong.

0 comments on commit ecfc2d2

Please sign in to comment.