001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.minisurvey;
017
018import java.time.LocalDate;
019import java.time.temporal.ChronoUnit;
020import java.util.Calendar;
021import java.util.Date;
022import java.util.GregorianCalendar;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.avalon.framework.parameters.Parameters;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.cocoon.acting.ServiceableAction;
031import org.apache.cocoon.environment.ObjectModelHelper;
032import org.apache.cocoon.environment.Redirector;
033import org.apache.cocoon.environment.Request;
034import org.apache.cocoon.environment.SourceResolver;
035
036import org.ametys.core.cocoon.JSonReader;
037import org.ametys.core.user.CurrentUserProvider;
038import org.ametys.core.util.DateUtils;
039import org.ametys.plugins.repository.AmetysObjectResolver;
040import org.ametys.plugins.survey.dao.SurveyDAO;
041import org.ametys.plugins.survey.data.SurveyAnswerDao;
042import org.ametys.plugins.survey.data.SurveySession;
043import org.ametys.plugins.survey.repository.Survey;
044import org.ametys.plugins.survey.repository.SurveyAccessHelper;
045
046/**
047 * Action to get dynamic information about a survey
048 *
049 */
050public class GetDynamicInformationAction extends ServiceableAction
051{
052    private AmetysObjectResolver _resolver;
053    private SurveyAnswerDao _surveyAnswerDAO;
054    private SurveyDAO _surveyDAO;
055    private SurveyAccessHelper _accessHelper;
056    private CurrentUserProvider _currentUserProvider;
057
058    @Override
059    public void service(ServiceManager smanager) throws ServiceException
060    {
061        super.service(smanager);
062        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
063        _surveyAnswerDAO = (SurveyAnswerDao) smanager.lookup(SurveyAnswerDao.ROLE);
064        _surveyDAO = (SurveyDAO) smanager.lookup(SurveyDAO.ROLE);
065        _accessHelper = (SurveyAccessHelper) smanager.lookup(SurveyAccessHelper.ROLE);
066        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
067    }
068    
069    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
070    {
071        Map<String, Object> result = new HashMap<>();
072        
073        Request request = ObjectModelHelper.getRequest(objectModel);
074        
075        String surveyId = parameters.getParameter("id", request.getParameter("id"));
076        Survey survey = _resolver.resolveById(surveyId);
077        
078        // Days left
079        Date endDate = survey.getEndDate();
080        if (endDate != null)
081        {
082            LocalDate today = LocalDate.now();
083            long daysLeft = today.until(DateUtils.asLocalDate(endDate), ChronoUnit.DAYS);
084            
085            result.put("daysLeft", daysLeft);
086        }
087
088        // Number of votes
089        List<SurveySession> sessions = _surveyAnswerDAO.getSessionsWithAnswers(surveyId);
090        result.put("votes", sessions.size());
091        
092        // Status
093        String status = _getStatus(survey.getStartDate(), survey.getEndDate());
094        result.put("status", status);
095
096        // Did the user answer to the survey?
097        boolean answered = _hasAnswered(survey, request);
098        result.put("answered", answered);
099
100        // Results
101        if (answered || "over".equals(status))
102        {
103            result.put("results", _surveyDAO.getStatistics(surveyId));
104        }
105        
106        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
107        
108        return EMPTY_MAP;
109    }
110        
111    private String _getStatus (Date startDate, Date endDate)
112    {
113        Calendar today = new GregorianCalendar();
114        today.set(Calendar.HOUR_OF_DAY, 0);
115        today.set(Calendar.MINUTE, 0);
116        today.set(Calendar.SECOND, 0);
117        today.set(Calendar.MILLISECOND, 0);
118        
119        if (startDate != null && startDate.after(today.getTime()))
120        {
121            return "coming";
122        }
123        
124        if (endDate != null && endDate.before(today.getTime()))
125        {
126            return "over";
127        }
128        
129        return "open";
130    }
131    
132    private boolean _hasAnswered(Survey survey, Request request)
133    {
134        Date submissionDate = _accessHelper.getSubmissionDate(survey.getId(), _currentUserProvider.getUser());
135        
136        if (submissionDate != null)
137        {
138            // The authenticated user has already answered to the survey
139            return true;
140        }
141        
142        // Finally check cookies
143        if (_currentUserProvider.getUser() == null && _accessHelper.getCookieName(request, survey) != null)
144        {
145            // Anonymous user has already answered to the survey
146            return true;
147        }
148        
149        return false;
150    }
151    
152}