Skip to content

Commit

Permalink
Completed the manipolation of the exercise.
Browse files Browse the repository at this point in the history
TODO:
-Dynamic CSS loading based on the exercise
-CSS of every exercise
  • Loading branch information
r-monti authored and ms@Nicro committed Jan 2, 2024
1 parent 4ee9f0a commit 6860ce3
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 46 deletions.
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<name>Maven Repository Switchboard</name>
<url>https://repo1.maven.org/maven2/</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

Expand Down Expand Up @@ -76,6 +80,11 @@
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.3</version>
</dependency>

</dependencies>
<build>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/controller/ExerciseController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controller;

import model.entity.ExerciseGlossary;
import model.service.exercise.ExerciseManager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.apache.commons.text.StringEscapeUtils;

@WebServlet("/exerciseController")
public class ExerciseController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ExerciseManager em = new ExerciseManager();
String id = request.getParameter("exerciseID");
ExerciseGlossary ex = em.getExercise(Integer.parseInt(id));
request.getSession().setAttribute("exercise", ex);
response.sendRedirect(request.getContextPath() + "/JSP/exercise.jsp");
}
}
10 changes: 4 additions & 6 deletions src/main/java/model/DAO/DAOExerciseGlossary.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import model.entity.ExerciseGlossary;

import com.google.gson.Gson;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -21,16 +20,15 @@ public class DAOExerciseGlossary {
* @throws SQLException if there is any SQL related error
*/
private static ExerciseGlossary extractExerciseFromResultSet(ResultSet resultSet) throws SQLException {
Gson g = new Gson();
ExerciseGlossary exercise = new ExerciseGlossary();
exercise.setIdExercise(resultSet.getInt("ID_exercise"));
exercise.setExerciseName(resultSet.getString("ExerciseName"));
exercise.setExerciseDescription(resultSet.getString("ExerciseDescription"));
exercise.setType(resultSet.getString("Type"));
exercise.setDifficulty(resultSet.getInt("Difficulty"));
exercise.setTarget(resultSet.getString("Target"));
exercise.setInitialState(g.fromJson(resultSet.getString("InitialState"), Object.class));
exercise.setSolution(g.fromJson(resultSet.getString("Solution"), Object.class));
exercise.setInitialState(resultSet.getString("InitialState"));
exercise.setSolution(resultSet.getString("Solution"));

return exercise;
}
Expand All @@ -41,7 +39,7 @@ private static ExerciseGlossary extractExerciseFromResultSet(ResultSet resultSet
* @param code the ExerciseID code to search for.
* @return the Exercise if it is found, else null.
*/
public ExerciseGlossary getExerciseByCode(String code) {
public ExerciseGlossary getExerciseByCode(int code) {
String query = "SELECT * FROM exercise_glossary WHERE ID_exercise = ?";
Connection connection = null;
PreparedStatement preparedStatement = null;
Expand All @@ -50,7 +48,7 @@ public ExerciseGlossary getExerciseByCode(String code) {
try {
connection = DAOConnection.getConnection();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, code);
preparedStatement.setInt(1, code);
resultSet = preparedStatement.executeQuery();

if (resultSet.next()) {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/model/entity/ExerciseGlossary.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class ExerciseGlossary {
private String exerciseDescription;
private String type;
private int difficulty;
private Object initialState;
private Object solution;
private String initialState;
private String solution;
private String target;

// Getter and Setter methods
Expand Down Expand Up @@ -53,13 +53,13 @@ public void setDifficulty(int difficulty) {
this.difficulty = difficulty;
}

public Object getInitialState() { return initialState; }
public String getInitialState() { return initialState; }

public void setInitialState(Object initialState) { this.initialState = initialState; }
public void setInitialState(String initialState) { this.initialState = initialState; }

public Object getSolution() { return solution; }
public String getSolution() { return solution; }

public void setSolution(Object solution) { this.solution = solution; }
public void setSolution(String solution) { this.solution = solution; }

public String getTarget() {
return target;
Expand All @@ -68,5 +68,7 @@ public String getTarget() {
public void setTarget(String target) {
this.target = target;
}


}

21 changes: 21 additions & 0 deletions src/main/java/model/service/exercise/ExerciseManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package model.service.exercise;

import model.DAO.DAOExerciseGlossary;
import model.entity.ExerciseGlossary;

import java.sql.Blob;

public class ExerciseManager implements ExerciseManagerInterface {
private DAOExerciseGlossary dao = new DAOExerciseGlossary();
public ExerciseGlossary getExercise(int exerciseID) {
return dao.getExerciseByCode(exerciseID);
}

public Blob getExecution(int exerciseID, int userID) {
return null;
}

public void saveExecution(int userID) {

}
}
29 changes: 29 additions & 0 deletions src/main/java/model/service/exercise/ExerciseManagerInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package model.service.exercise;

import model.entity.ExerciseGlossary;

import java.sql.Blob;

public interface ExerciseManagerInterface {

/**
* Preleva un esercizio dal Database
* @param exerciseID l'id dell'esercizio
* @return l'oggetto esercizio
*/
ExerciseGlossary getExercise(int exerciseID);

/**
* Preleva un'esecuzione di un esercizio
* @param exerciseID l'id dell'esercizio
* @param userID l'id dell'utente che ha svolto l'esercizio
* @return un BLOB contenente l'esecuzione dell'esercizio
*/
Blob getExecution(int exerciseID, int userID);

/**
* Salva l'esecuzione di un esercizio
* @param userID l'id dell'utente che ha eseguito l'esercizio
*/
void saveExecution(int userID);
}
114 changes: 83 additions & 31 deletions src/main/webapp/JS/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,35 @@ const CROSSWORD = "CROSSWORD"
const COMPLETETEXT = "COMPLETETEXT"
const RIGHTTEXT = "RIGHTTEXT"
const IMAGESINAROW = 2
const exercise = $("#exerciseInfo")

const EXERCISETYPE = exercise.data("type");

const exerciseDiv = $("#exerciseDiv");

$(document).ready(()=>{
$("#backDiv").click(() => redirect("home"));
$("#notificationDiv").click(() => redirect("message.jsp")); //TODO: mettere i redirect giusti
loadExercise(/*TODO: mettere qui la tipologia di esercizio*/);
})
function startUp(exerciseIS){
$(document).ready(()=>{
const EXERCISEINITIALSTATE = parseJSON(exerciseIS);
$("#backDiv").click(() => redirect("home"));
$("#notificationDiv").click(() => redirect("message.jsp")); //TODO: mettere i redirect giusti
loadExercise(EXERCISETYPE, EXERCISEINITIALSTATE);
})
}

function parseJSON(json) {
try {
const jsonData = JSON.parse(json);
return jsonData;
} catch (jsonError) {
try {
const jsObject = eval('(' + json + ')');
return jsObject;
} catch (objectError) {
return json;
}
}
}


function redirect(where){
if (where === "home"){
Expand All @@ -29,37 +50,55 @@ function redirect(where){
}
}

function loadExercise(type){
switch (type) {
function loadExercise(type, initialState){
switch (type.toUpperCase()) {
case READTEXT:
loadReadText();
loadReadText(initialState);
//loadCSS("path/al/tuo/readtext.css");
break;
case READIMAGES:
loadReadImages();
loadReadImages(initialState);
//loadCSS("path/al/tuo/readtext.css");
break;
case IMAGESTOTEXT:
loadImagesToText();
loadImagesToText(initialState);
//loadCSS("path/al/tuo/readtext.css");
break;
case TEXTTOIMAGES:
loadTextToImages();
loadTextToImages(initialState);
//loadCSS("path/al/tuo/readtext.css");
break;
case CROSSWORD:
loadCrossword();
loadCrossword(initialState);
//loadCSS("path/al/tuo/readtext.css");
break;
case COMPLETETEXT:
loadCompleteText();
loadCompleteText(initialState);
//loadCSS("path/al/tuo/readtext.css");
break;
case RIGHTTEXT:
loadRightText();
loadRightText(initialState);
//loadCSS("path/al/tuo/readtext.css");
break;
default:
//TODO: Errore e torna indietro
setTimeout(()=> {
alert("Ci scusiamo del disagio! Le consigliamo di riprovare tra pochi minuti");
window.location.href = "homepagepatient" //TODO:mettere la homepage del paziente
}, 3000);
}
}

function loadReadText(){
function loadCSS(cssPath) {
let link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = cssPath;
document.head.appendChild(link);
}

function loadReadText(initialState){

let text = $("<p>").text(/*TODO: inserire il testo*/);
let text = $("<p>").text(initialState);
let textDiv = $("<div>").attr("id", "textDiv").append(text);

let button = $("<button>").click(/*TODO: inizia la registrazione*/);
Expand All @@ -70,8 +109,8 @@ function loadReadText(){
exerciseDiv.append(div);
}

function loadReadImages(){
let images = []; //TODO: prendere le varie immagini
function loadReadImages(initialState){
let images = initialState;
let index = 0;
let number = 0
let div = $("<div>").addClass("row");
Expand Down Expand Up @@ -100,9 +139,8 @@ function loadReadImages(){
exerciseDiv.append(buttonDiv);
}

function loadImagesToText(){
let images = []; //TODO: prendere le varie immagini
let textInput = $("<input>").attr("type", "text");
function loadImagesToText(initialState){
let images = initialState;
let index = 0;
let number = 0
let div = $("<div>").addClass("row");
Expand All @@ -119,7 +157,10 @@ function loadImagesToText(){
id: "image "+number
});
div.append(i);
div.append(textInput.attr("id", "textInput"+number));
div.append($("<input>").attr({
type: "text",
id: "textInput "+number
}))
if (index === IMAGESINAROW-1){
exerciseDiv.append(div);
//Create a new Div
Expand All @@ -129,9 +170,9 @@ function loadImagesToText(){
});
}

function loadTextToImages(){
let images = []; //TODO: prendere le varie immagini
let texts = []; //TODO: Prendere i vari testi
function loadTextToImages(initialState){
let images = initialState[0]
let texts = initialState[1]
let index = 0;
let number = 0
let div = $("<div>").addClass("row");
Expand Down Expand Up @@ -163,8 +204,8 @@ function loadTextToImages(){
});
}

function loadCrossword(){
let matrix = [[]]; //TODO: Prendere la matrice
function loadCrossword(initialState){
let matrix = initialState;
let vertical = [];
let horizontal = [];
let crosswordDiv = $("<div>");
Expand Down Expand Up @@ -205,9 +246,15 @@ function loadCrossword(){

}

function loadCompleteText(){
let words = [];
function loadCompleteText(initialState){
let words = initialState;
words.forEach((word, index)=>{
//TODO: Rimuovere, quando si inserisce nel db si tolgono le lettere
const randomIndex = Math.floor(Math.random() * word.length);
tempWordArray = word.split('');
tempWordArray[randomIndex] = "#";
word = tempWordArray.join('');

let div=$("<div>")
div.attr("id", "word"+index);
for (let i=0; i < word.length; i++){
Expand All @@ -224,9 +271,13 @@ function loadCompleteText(){
})
}

function loadRightText(){
function loadRightText(initialState){
let firstSetOfWords = [];
let secondSetOfWords = [];
initialState.forEach((pair)=>{
firstSetOfWords.push(pair[0]);
secondSetOfWords.push(pair[1]);
});

for(let i=0; i<firstSetOfWords.length; i++){
let div = $("<div>");
Expand All @@ -240,5 +291,6 @@ function loadRightText(){
value: secondSetOfWords[i]
});
div.append($("<p>").text(firstSetOfWords[i]), radio1, radio2, $("<p>").text(secondSetOfWords[i]));
exerciseDiv.append(div);
}
}
Loading

0 comments on commit 6860ce3

Please sign in to comment.