<commit_before>package com.liferay.lms.portlet.inscriptioncommunity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletURL;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;

import com.liferay.lms.course.inscriptiontype.InscriptionType;
import com.liferay.lms.course.inscriptiontype.InscriptionTypeRegistry;
import com.liferay.lms.model.Course;
import com.liferay.lms.model.CourseCompetence;
import com.liferay.lms.model.CourseResult;
import com.liferay.lms.model.Schedule;
import com.liferay.lms.service.CourseCompetenceLocalServiceUtil;
import com.liferay.lms.service.CourseLocalServiceUtil;
import com.liferay.lms.service.CourseResultLocalServiceUtil;
import com.liferay.lms.service.ScheduleLocalServiceUtil;
import com.liferay.lms.util.LmsConstant;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.GroupConstants;
import com.liferay.portal.model.MembershipRequestConstants;
import com.liferay.portal.model.Team;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.service.TeamLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;


/** Portlet implementation class CommunityInscription */
public class CommunityInscription extends MVCPortlet {
    private static Log log = LogFactoryUtil.getLog(CommunityInscription.class);

    @Override
	public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
        ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        try {
            Course course = CourseLocalServiceUtil.getCourseByGroupCreatedId(themeDisplay.getScopeGroupId());

            InscriptionTypeRegistry inscriptionTypeRegistry = new InscriptionTypeRegistry();
            InscriptionType inscriptionType = inscriptionTypeRegistry.getInscriptionType(course.getInscriptionType());
            renderRequest.setAttribute("inscriptionType", inscriptionType);
			renderRequest.setAttribute("inscriptionAllowedTime", inscriptionType.getAllowedTime(course.getCourseId(), themeDisplay.getUserId(), themeDisplay.getLocale()));

            log.debug("inscriptionTypeFactory: " + inscriptionType.getTypeId() + " - " + inscriptionType.getPortletId());
            if (!Validator.isNull(inscriptionType.getPortletId())) {
                StringBundler sb = new StringBundler();
                sb.append("<portlet-preferences >");
                sb.append("<preference>");
                sb.append("<name>");
                sb.append("portletSetupShowBorders");
                sb.append("</name>");
                sb.append("<value>");
                sb.append("false");
                sb.append("</value>");
                sb.append("</preference>");
                sb.append("</portlet-preferences>");
                renderRequest.setAttribute("inscriptionPortletName", inscriptionType.getPortletId());
                renderRequest.setAttribute("defaultPreferences", sb);
            } else if (themeDisplay.isSignedIn()) {
                log.debug("usuario logado");
                HttpServletRequest renderServletRequest = PortalUtil.getHttpServletRequest(renderRequest);
                HttpServletRequest servletRequest = PortalUtil.getOriginalServletRequest(renderServletRequest);

                String inscriptionParam = servletRequest.getParameter("inscriptionOk");
                log.debug("inscriptionParam: " + inscriptionParam);
                if (Validator.isNotNull(inscriptionParam)) {
                    try {
                        SessionMessages.add(renderRequest, "inscription-ok");
                    } catch (Exception e) {
						if(log.isDebugEnabled())e.printStackTrace();
                    }
                }

                PortletURL unsubscribeURL = renderResponse.createActionURL();
                unsubscribeURL.setParameter("javax.portlet.action", "unsubscribe");
                renderRequest.setAttribute("unsubscribeURL", unsubscribeURL);

                boolean canUnsubscribeLocal = true;
                boolean unsubscribeIfFinished =
                    !Boolean.parseBoolean(renderRequest.getPreferences().getValue("unsubscribeIfFinished", "true"));
                
                boolean checkExecutionDate = false;

                try {
                    checkExecutionDate =
                        PrefsPropsUtil.getBoolean(themeDisplay.getCompanyId(), LmsConstant.CHECK_EXECUTION_DATE);
                } catch (SystemException e) {
                    log.error("Error buscando la propiedad " + LmsConstant.CHECK_EXECUTION_DATE);
                }

                // Comprobamos si estoy inscrita en el curso o en alguna convocatoria
                if (UserLocalServiceUtil.hasGroupUser(course.getGroupCreatedId(), themeDisplay.getUserId())) {
                    // Ya estoy inscrito, mando el curso y que estoy inscrito
                    log.debug("usuario registrado: " + course.getCourseId());

                    if (unsubscribeIfFinished) {
                        // comprobamos si puede desinscribirse del curso
                        CourseResult cr =
                            CourseResultLocalServiceUtil.getCourseResultByCourseAndUser(course.getCourseId(),
                                themeDisplay.getUserId());
                        Date now = new Date();
                        boolean finishedCourse = ((cr != null) ? cr.getPassedDate() == null : true);
                        boolean outOfTime = ((cr != null) ? now.after(cr.getAllowFinishDate()) : true);
                        
                        canUnsubscribeLocal = finishedCourse && outOfTime;
                        
                    }
                    renderRequest.setAttribute("registredUser", true);
                    renderRequest.setAttribute("course", course);
                } else {
                    // Comprobamos si estoy inscrita en alguna de las hijas
                    long courseParentId = course.getCourseId();
                    log.debug("courseParentId: " + courseParentId);
                    if (course.getParentCourseId() != LmsConstant.DEFAULT_PARENT_COURSE_ID) {<commit_msg>[FIXED] #168915 Corrección para la condición del allowedEndDate para ocultar el boton de desinscribir<commit_after>package com.liferay.lms.portlet.inscriptioncommunity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletURL;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;

import com.liferay.lms.course.inscriptiontype.InscriptionType;
import com.liferay.lms.course.inscriptiontype.InscriptionTypeRegistry;
import com.liferay.lms.model.Course;
import com.liferay.lms.model.CourseCompetence;
import com.liferay.lms.model.CourseResult;
import com.liferay.lms.model.Schedule;
import com.liferay.lms.service.CourseCompetenceLocalServiceUtil;
import com.liferay.lms.service.CourseLocalServiceUtil;
import com.liferay.lms.service.CourseResultLocalServiceUtil;
import com.liferay.lms.service.ScheduleLocalServiceUtil;
import com.liferay.lms.util.LmsConstant;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.GroupConstants;
import com.liferay.portal.model.MembershipRequestConstants;
import com.liferay.portal.model.Team;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.service.TeamLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;


/** Portlet implementation class CommunityInscription */
public class CommunityInscription extends MVCPortlet {
    private static Log log = LogFactoryUtil.getLog(CommunityInscription.class);

    @Override
	public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
        ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        try {
            Course course = CourseLocalServiceUtil.getCourseByGroupCreatedId(themeDisplay.getScopeGroupId());

            InscriptionTypeRegistry inscriptionTypeRegistry = new InscriptionTypeRegistry();
            InscriptionType inscriptionType = inscriptionTypeRegistry.getInscriptionType(course.getInscriptionType());
            renderRequest.setAttribute("inscriptionType", inscriptionType);
			renderRequest.setAttribute("inscriptionAllowedTime", inscriptionType.getAllowedTime(course.getCourseId(), themeDisplay.getUserId(), themeDisplay.getLocale()));

            log.debug("inscriptionTypeFactory: " + inscriptionType.getTypeId() + " - " + inscriptionType.getPortletId());
            if (!Validator.isNull(inscriptionType.getPortletId())) {
                StringBundler sb = new StringBundler();
                sb.append("<portlet-preferences >");
                sb.append("<preference>");
                sb.append("<name>");
                sb.append("portletSetupShowBorders");
                sb.append("</name>");
                sb.append("<value>");
                sb.append("false");
                sb.append("</value>");
                sb.append("</preference>");
                sb.append("</portlet-preferences>");
                renderRequest.setAttribute("inscriptionPortletName", inscriptionType.getPortletId());
                renderRequest.setAttribute("defaultPreferences", sb);
            } else if (themeDisplay.isSignedIn()) {
                log.debug("usuario logado");
                HttpServletRequest renderServletRequest = PortalUtil.getHttpServletRequest(renderRequest);
                HttpServletRequest servletRequest = PortalUtil.getOriginalServletRequest(renderServletRequest);

                String inscriptionParam = servletRequest.getParameter("inscriptionOk");
                log.debug("inscriptionParam: " + inscriptionParam);
                if (Validator.isNotNull(inscriptionParam)) {
                    try {
                        SessionMessages.add(renderRequest, "inscription-ok");
                    } catch (Exception e) {
						if(log.isDebugEnabled())e.printStackTrace();
                    }
                }

                PortletURL unsubscribeURL = renderResponse.createActionURL();
                unsubscribeURL.setParameter("javax.portlet.action", "unsubscribe");
                renderRequest.setAttribute("unsubscribeURL", unsubscribeURL);

                boolean canUnsubscribeLocal = true;
                boolean unsubscribeIfFinished =
                    !Boolean.parseBoolean(renderRequest.getPreferences().getValue("unsubscribeIfFinished", "true"));
                
                boolean checkExecutionDate = false;

                try {
                    checkExecutionDate =
                        PrefsPropsUtil.getBoolean(themeDisplay.getCompanyId(), LmsConstant.CHECK_EXECUTION_DATE);
                } catch (SystemException e) {
                    log.error("Error buscando la propiedad " + LmsConstant.CHECK_EXECUTION_DATE);
                }

                // Comprobamos si estoy inscrita en el curso o en alguna convocatoria
                if (UserLocalServiceUtil.hasGroupUser(course.getGroupCreatedId(), themeDisplay.getUserId())) {
                    // Ya estoy inscrito, mando el curso y que estoy inscrito
                    log.debug("usuario registrado: " + course.getCourseId());

                    if (unsubscribeIfFinished) {
                        // comprobamos si puede desinscribirse del curso
                        CourseResult cr =
                            CourseResultLocalServiceUtil.getCourseResultByCourseAndUser(course.getCourseId(),
                                themeDisplay.getUserId());
                        Date now = new Date();
                        boolean finishedCourse = ((cr != null) ? cr.getPassedDate() == null : true);
                        boolean outOfTime = ((cr != null) ? now.after(cr.getAllowFinishDate()) : true);
                        
                        canUnsubscribeLocal = finishedCourse && !outOfTime;
                        
                    }
                    renderRequest.setAttribute("registredUser", true);
                    renderRequest.setAttribute("course", course);
                } else {
                    // Comprobamos si estoy inscrita en alguna de las hijas
                    long courseParentId = course.getCourseId();
                    log.debug("courseParentId: " + courseParentId);
                    if (course.getParentCourseId() != LmsConstant.DEFAULT_PARENT_COURSE_ID) {
{'fraction_non_alphanumeric': 0.09530358247954342, 'fraction_numerical': 0.0004125696211235646, 'mean_word_length': 2.7875, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '684328', 'n_tokens_mistral': 3743, 'n_tokens_neox': 3746, 'n_words': 603}