diff --git a/src/main/java/controller/RegistrationServlet.java b/src/main/java/controller/RegistrationServlet.java index 4f5c071..1ee3ccc 100644 --- a/src/main/java/controller/RegistrationServlet.java +++ b/src/main/java/controller/RegistrationServlet.java @@ -31,7 +31,24 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) response.getWriter().write(String.valueOf(result)); if(result == 0) { setSessionAttributes(email, request); - response.sendRedirect("JSP/legal.jsp"); + response.sendRedirect("/JSP/legal.jsp"); + } + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + UserData ud = new UserData(); + HttpSession session = request.getSession(); + String parameter = request.getParameter("type"); + if(parameter.equals("analytics")) { + Boolean value = Boolean.parseBoolean(request.getParameter("accept")); + ud.updateAnalyticsPreference(String.valueOf(session.getAttribute("id")), value); + } + if(parameter.equals("emailTime")) { + String start = request.getParameter("startTime"); + String end = request.getParameter("endTime"); + String time = start + "|" + end; + ud.updateEmailTime(String.valueOf(session.getAttribute("id")), time); + response.sendRedirect("/JSP/welcome.jsp"); } } diff --git a/src/main/java/model/DAO/DAOUser.java b/src/main/java/model/DAO/DAOUser.java index ca2edf4..cfa5fa2 100644 --- a/src/main/java/model/DAO/DAOUser.java +++ b/src/main/java/model/DAO/DAOUser.java @@ -233,4 +233,102 @@ public boolean resetPassword(String email, String newPassword) { // Default to false if an exception occurs return false; } + + /** + * Updates the user's analytics choice in the User table. + * + * @param userId The id of the user. + * @param value The analytics choice value to set for the user. + * @return true if the choice was successfully updated; false otherwise. + */ + public boolean updateAnalyticsPreference(String userId, boolean value) { + Connection connection = null; + PreparedStatement preparedStatement = null; + + try { + // Get connection + connection = DAOConnection.getConnection(); + + // Query to update analytics choice for the given userId + String query = "UPDATE user SET Analytics = ? WHERE ID = ?"; + + // Prepare the statement + preparedStatement = connection.prepareStatement(query); + + // Set the parameters + preparedStatement.setBoolean(1, value); + preparedStatement.setString(2, userId); + + // Execute the update query + int rowsModified = preparedStatement.executeUpdate(); + + // If rowsModified is greater than 0, then a row has been updated. + // So, return true. If not, return false. + return rowsModified > 0; + } catch (SQLException e) { + // Handle the exception (e.g., log or throw) + e.printStackTrace(); + } finally { + try { + // Close everything properly + if (preparedStatement != null) preparedStatement.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + // Handle the exception (e.g., log or throw) + e.printStackTrace(); + } + } + + // Default to false if an exception occurs + return false; + } + + /** + * Updates the user's Email Time in the User table. + * + * @param id The id of the user. + * @param value The email time value to set for the user. + * @return true if the email time was successfully updated; false otherwise. + */ + public boolean updateEmailTime(String id, String value) { + Connection connection = null; + PreparedStatement preparedStatement = null; + + try { + // Get connection + connection = DAOConnection.getConnection(); + + // Query to update Email Time for the given id + String query = "UPDATE user SET NotificationTime = ? WHERE ID = ?"; + + // Prepare the statement + preparedStatement = connection.prepareStatement(query); + + // Set the parameters + preparedStatement.setString(1, value); + preparedStatement.setString(2, id); + + // Execute the update query + int rowsModified = preparedStatement.executeUpdate(); + + // If rowsModified is greater than 0, then a row has been updated. + // So, return true. If not, return false. + return rowsModified > 0; + } catch (SQLException e) { + // Handle the exception (e.g., log or throw) + e.printStackTrace(); + } finally { + try { + // Close everything properly + if (preparedStatement != null) preparedStatement.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + // Handle the exception (e.g., log or throw) + e.printStackTrace(); + } + } + + // Default to false if an exception occurs + return false; + } } diff --git a/src/main/java/model/service/user/UserData.java b/src/main/java/model/service/user/UserData.java index 363e8f1..0ec260a 100644 --- a/src/main/java/model/service/user/UserData.java +++ b/src/main/java/model/service/user/UserData.java @@ -21,4 +21,12 @@ public User getUserByIdOrEmail(Object idOrEmail) { public boolean isTherapist(User user){ return user.getIdTherapist() == 0; } + + public boolean updateAnalyticsPreference(String id, Boolean value) { + return daoUser.updateAnalyticsPreference(id, value); + } + + public boolean updateEmailTime(String id, String value) { + return daoUser.updateEmailTime(id, value); + } } diff --git a/src/main/webapp/JS/legal.js b/src/main/webapp/JS/legal.js new file mode 100644 index 0000000..13ea0c1 --- /dev/null +++ b/src/main/webapp/JS/legal.js @@ -0,0 +1,52 @@ +// Function to show the popup +function mostraPopup() { + document.getElementById("overlay").style.display = "block"; +} + +// Function called when the user accepts the sharing +function accettaCondivisione() { + var xhr = new XMLHttpRequest(); + xhr.open("GET", "../register?type=analytics&accept=true", true); + xhr.send(); + + document.getElementById("overlay").style.display = "none"; +} + +// Show the popup when the page is fully loaded +window.onload = mostraPopup; + +function impostaOrario() { + event.preventDefault(); + let start = document.getElementById('startTime').value = ''; + let end = document.getElementById('endTime').value = ''; + let time = start + "|" + end; + var xhr = new XMLHttpRequest(); + xhr.open("GET", "../register?type=emailTime&time="+time, true); + xhr.send(); +} + +function nonAccetto() { + var xhr = new XMLHttpRequest(); + xhr.open("GET", "../register?type=analytics&accept=false", true); + xhr.send(); + + document.getElementById("overlay").style.display = "none"; +} + +function validateTimes() { + var startTime = new Date(); + var endTime = new Date(); + + // Convert time strings into Date objects + startTime.setHours(document.getElementById('startTime').value.split(':')[0]); + startTime.setMinutes(document.getElementById('startTime').value.split(':')[1]); + endTime.setHours(document.getElementById('endTime').value.split(':')[0]); + endTime.setMinutes(document.getElementById('endTime').value.split(':')[1]); + + // Check if the start time is greater than the end time + if (startTime > endTime) { + alert("L'ora di inizio deve essere inferiore all'ora di fine"); + document.getElementById('startTime').value = ''; + document.getElementById('endTime').value = ''; + } +} \ No newline at end of file diff --git a/src/main/webapp/JSP/legal.jsp b/src/main/webapp/JSP/legal.jsp index cd3627a..ce56281 100644 --- a/src/main/webapp/JSP/legal.jsp +++ b/src/main/webapp/JSP/legal.jsp @@ -5,9 +5,9 @@ <%@page contentType="text/html;charset=UTF-8"%> <% - String userId = (String) session.getAttribute("id"); + Integer userId = (Integer) session.getAttribute("id"); if(userId == null) { - response.sendRedirect("www.google.it"); + response.sendRedirect("../errorPage/403.html"); } %> @@ -265,7 +265,8 @@ -
+ +

Seleziona una fascia oraria dove preferisci ricevere delle notifiche

@@ -279,51 +280,7 @@
- - + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 6666f4d..692ac15 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -13,8 +13,8 @@ /errorPage/404.html - + \ No newline at end of file