<%@ page import="java.text.DateFormat" %> <%@ page import="java.text.SimpleDateFormat" %> <%@ page import="java.util.Date" %> <%@ page import="java.util.Calendar" %> <%@ page import="java.util.List" %> <%@ page import="java.util.Collections" %> <%@ page import="java.util.Comparator" %> <%@page import="com.insightguru.giraffe.common.constant.UserProfileBitFlags"%> <%@page import="com.insightguru.giraffe.server.Configuration"%> <%@page import="com.insightguru.giraffe.server.htmlserviceimpl.HtmlServiceImpl"%> <%@page import="com.insightguru.giraffe.server.domain.DailyQuiz"%> <%@page import="com.insightguru.giraffe.server.domain.Company"%> <%@page import="com.insightguru.giraffe.server.domain.ModelId"%> <%@page import="com.insightguru.giraffe.server.domain.User"%> <%@page import="com.insightguru.giraffe.server.dao.DailyQuizDAO"%> <%@page import="com.insightguru.giraffe.server.dao.CompanyDAO"%> <%@page import="com.insightguru.giraffe.server.dao.UserDAO"%> <%@page import="com.insightguru.giraffe.server.utils.ShareUtil"%> <%@page import="com.insightguru.giraffe.server.controller.LoginController"%> <%@ page import="org.json.JSONArray" %> <%@ page import="org.owasp.esapi.ESAPI" %> <%@ page import="org.owasp.esapi.Encoder" %> <%! DateFormat dateFormat = SimpleDateFormat.getDateInstance(DateFormat.SHORT); %> <%@page import="org.json.JSONObject"%> <%@page import="com.insightguru.giraffe.server.domain.DailyQuizAnswer"%> <%@page import="java.util.ArrayList"%> <%@page import="com.insightguru.giraffe.server.utils.TextUtil"%> <%@page import="com.insightguru.giraffe.server.utils.DailyQuizUtil"%> Add a Quiz-of-the-Day <%@ include file="components/commonHead.jsp" %>
<% User loggedInUser = UserDAO.getCurrentUser(); if(loggedInUser == null || !UserProfileBitFlags.isAdmin(loggedInUser)) { %>

Admin Access Only

Logout and try again <% } else { %> <% if("submit".equals(request.getParameter("do"))) { boolean editing = request.getParameter("id") != null; DailyQuiz quiz; if(editing) quiz = DailyQuizDAO.get(Integer.parseInt(request.getParameter("id"))); else quiz = new DailyQuiz(); quiz.setHtmlText(request.getParameter("text")); quiz.setDay(dateFormat.parse(request.getParameter("day"))); quiz.setImageURL(TextUtil.NullOrEmpty(request.getParameter("image")) ? null : request.getParameter("image")); quiz.setConfirmationText(request.getParameter("confirmationText")); quiz.setSkipConfirmation(ServletUtil.getBooleanParameter(request, "skipConfirmation")); quiz.setFooterText(request.getParameter("footer")); String answerText[] = request.getParameterValues("answerText"); String answerLink[] = request.getParameterValues("answerLink"); String correctAnswerString = request.getParameter("correctAnswer"); Integer correctAnswerIndex = correctAnswerString == null || "null".equalsIgnoreCase(correctAnswerString) ?null :Integer.parseInt(correctAnswerString); if (answerText != null && answerLink != null && answerText.length == answerLink.length && answerText.length > 0) { quiz.getAnswers().clear(); for (int i = 0; i < answerText.length; i++) { DailyQuizAnswer answer = new DailyQuizAnswer(); answer.setDailyQuiz(quiz); answer.setHtmlText(answerText[i]); answer.setLinkText(answerLink[i]); answer.setAnswerIndex(i+1); quiz.getAnswers().add(answer); if (correctAnswerIndex != null && answer.getAnswerIndex() == correctAnswerIndex) quiz.setCorrectAnswer(answer); } } else { quiz.setAnswers(null); quiz.setCorrectAnswer(null); } if(!editing) DailyQuizDAO.addQuiz(quiz); %>

Quiz <%= editing ? "edited" : "added" %>!

<% } %> <% if("delete".equals(request.getParameter("do"))) { DailyQuizDAO.deleteQuiz(Integer.parseInt(request.getParameter("id"))); %>

Quiz deleted!

<% } %> <% if("add".equals(request.getParameter("do")) || "edit".equals(request.getParameter("do"))) { DailyQuiz quizToEdit = null; if("edit".equals(request.getParameter("do"))) quizToEdit = DailyQuizDAO.get(Integer.parseInt(request.getParameter("id"))); %>
Make Link:
Date:
HTML Text:
Image (Optional): "/>
Answers (Optional)
<% if (quizToEdit != null && quizToEdit.getAnswers() != null) { for (DailyQuizAnswer answer : quizToEdit.getAnswers()) { %> <% } } %>
AnswerAction
<%= DailyQuizUtil.getLetterOfAlphabet(answer.getAnswerIndex()) %>
Text:
Delete
Correct Answer: <% if ((quizToEdit == null) || (quizToEdit.getAnswers()==null) || (quizToEdit.getAnswers().size()==0)) { %> <% } else { %> <% } %>
Footer Text (Optional):
Confirmation:
/>
" <%= quizToEdit != null && quizToEdit.getSkipConfirmation() ? "disabled":"" %> >
Cancel
<% if(quizToEdit != null) { %> <% } %>
<% List companies = CompanyDAO.getCompanies(false); Collections.sort(companies, new Comparator() { public int compare(Company company1, Company company2) { return company1.getSymbol().compareToIgnoreCase(company2.getSymbol()); } }); JSONArray companyList = new JSONArray(); JSONObject linksMap = new JSONObject(); for(Company company : CompanyDAO.getCompanies(false)) { linksMap.put(company.getSymbol(), ShareUtil.createShareModelURLWithoutShrTag(ModelId.getTrefisModelId(company.getSymbol()))); companyList.put(company.getSymbol()); } %> <%@ include file="/components/commonJS.jsp" %>

Intended usage

Ordinary quiz

  1. Use the "Make Link" section to generate any ad-hoc links to insert in the "HTML Text" region.
  2. Set the date to be the date at which the quiz should be shown. Note that the text "from=quiz" will get replaced with the appropriate quiz id at quiz generation time for tracking purposes.
  3. Introductory text goes in the "HTML Text" area.
  4. Click "Add new answer" to add as many answers as you want. Use "Link" pulldown to select a company page to link to.
  5. Add any footer text
  6. The Confirmation refers to a banner shown on the company page after the user clicks. By default they will get
    1. "Congratulations, the correct answer was B, XXXX" or "Sorry, the correct answer was B, XXXX"
    2. "The majority of respondents picked A, XXXX"
    3. Confirmation text

Factoid

  1. Use the "Make Link" section to generate any ad-hoc links to insert in the "HTML Text" region.
  2. Set the date to be the date at which the quiz should be shown.
  3. Factoid goes in the HTML Text area. Note that the text "from=quiz" will get replaced with the appropriate quiz id at quiz generation time for tracking purposes.
  4. Delete all answers if any. If there are no answers, the first two confirmation options go away
  5. Leave footer blank
  6. Either fill in the confirmation text or click "skip confirmation" depending whether you want a banner when they click through.
<% } else {%> Add a new quiz | Show recent and future quizzes | Show all quizzes <% final List quizzes; if("1".equals(request.getParameter("show"))) { quizzes = DailyQuizDAO.getAllQuizzes(); %>

All Quizzes

<% } else { // defaults to show=0 Calendar fiveDaysAgo = Calendar.getInstance(); fiveDaysAgo.add(Calendar.DATE, -5); quizzes = DailyQuizDAO.getQuizzesAfter(fiveDaysAgo.getTime()); %>

Recent and Future Quizzes

<% } %> <% for(DailyQuiz quiz : quizzes) { %> <% } %>
<%= dateFormat.format(quiz.getDay()) %> <% /* float the roundedBox so that it will contain the image, which is floated, w/o using a clear */ %>
<%@ include file="components/allCornersIE.jsp" %>
— QUIZ OF THE DAY (ID <%= quiz.getId() %>) —
Edit Delete
<% } %> <% } %>