Skip to content

Commit

Permalink
Merge pull request #164 from Arquisoft/develop
Browse files Browse the repository at this point in the history
merging for deploy
  • Loading branch information
Toto-hitori authored Apr 4, 2024
2 parents 250f5e7 + 0b47f15 commit 2bbfca1
Show file tree
Hide file tree
Showing 33 changed files with 843 additions and 280 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lab.en2b.quizapi.questions.answer;

public enum AnswerCategory {
OTHER, CAPITAL_CITY, COUNTRY, SONG, STADIUM, DATE, PERSON, EVENT
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR
}

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package lab.en2b.quizapi.questions.answer;

import lab.en2b.quizapi.questions.question.Question;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface AnswerRepository extends JpaRepository<Answer,Long> {

@Query(value = "SELECT * FROM answers WHERE category=?1 AND language=?2 ORDER BY RANDOM() LIMIT ?3", nativeQuery = true)
List<Answer> findDistractors(String answerCategory, String lang, int numDistractors);
@Query(value = "SELECT MAX(id) AS id, text, category, language FROM answers WHERE category = ?1 AND language = ?2 AND text <> ?3 GROUP BY text, category, language ORDER BY RANDOM() LIMIT ?4", nativeQuery = true)
List<Answer> findDistractors(String answerCategory, String lang, String answerTest, int numDistractors);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lab.en2b.quizapi.questions.question;

public enum QuestionCategory {
HISTORY, GEOGRAPHY, SCIENCE, MATH, LITERATURE, ART, SPORTS, MUSIC, MOVIES, TV, POLITICS, OTHER
//HISTORY, GEOGRAPHY, SCIENCE, MATH, LITERATURE, ART, SPORTS, MUSIC, MOVIES, TV, POLITICS, OTHER
GEOGRAPHY, SPORTS, MUSIC
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@

@Component
public class QuestionHelper {

private static int MAX_DISTRACTORS = 3;

public static List<Answer> getDistractors(AnswerRepository answerRepository, Question question){
List<Answer> distractors = new ArrayList<>();

AnswerCategory cat = question.getAnswerCategory();

switch (cat){ // Write the case only for the exceptions
case COUNTRY:
// Implement more cases
break;
default:
distractors = answerRepository.findDistractors(cat.toString(), question.getLanguage(), 1);
distractors = answerRepository.findDistractors(question.getAnswerCategory().toString(), question.getLanguage(), question.getCorrectAnswer().getText(), MAX_DISTRACTORS);
}

return distractors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package lab.en2b.quizapi.questions.question;

import jakarta.annotation.PostConstruct;
import lab.en2b.quizapi.questions.answer.Answer;
import lab.en2b.quizapi.questions.answer.AnswerCategory;
import lab.en2b.quizapi.questions.answer.AnswerRepository;
import lab.en2b.quizapi.questions.answer.dtos.AnswerDto;
import lab.en2b.quizapi.questions.question.dtos.AnswerCheckResponseDto;
Expand All @@ -12,6 +10,7 @@
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
Expand Down Expand Up @@ -57,11 +56,13 @@ public QuestionResponseDto getQuestionById(Long id) {
public void loadAnswers(Question question) {
// Create the new answers list with the distractors
List<Answer> answers = new ArrayList<>(QuestionHelper.getDistractors(answerRepository, question));
// Add the correct
answers.add(question.getCorrectAnswer());

// Add the correct answer in a random position
int randomIndex = (int) (Math.random() * (answers.size() + 1));
answers.add(randomIndex, question.getCorrectAnswer());
// Shuffle the answers
Collections.shuffle(answers);

question.setAnswers(answers);
questionRepository.save(question);
}
}
31 changes: 23 additions & 8 deletions questiongenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import model.AnswerCategory;
import repositories.GeneralRepositoryStorer;
import templates.BallonDOrQuestion;
import templates.CountryCapitalQuestion;
import templates.SongQuestion;
import templates.StadiumQuestion;

public class Main {
public static void main(String[] args) {
new CountryCapitalQuestion("en");
new CountryCapitalQuestion("es");

//new SongQuestion("en");
//new SongQuestion("es");
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.CAPITAL_CITY)) {
new CountryCapitalQuestion("en");
new CountryCapitalQuestion("es");
}

/*
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.SONG.toString())) {
new SongQuestion("en");
new SongQuestion("es");
}
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.STADIUM.toString())) {
new StadiumQuestion("en");
new StadiumQuestion("es");
}
*/

if (GeneralRepositoryStorer.doesntExist(AnswerCategory.BALLON_DOR)) {
new BallonDOrQuestion(""); // No need to specify language code as it is not used
}

//new StadiumQuestion("en");
//new StadiumQuestion("es");
}
}
20 changes: 2 additions & 18 deletions questiongenerator/src/main/java/model/AnswerCategory.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
package model;

public enum AnswerCategory {
OTHER(1),
CAPITAL_CITY(2),
COUNTRY(3),
SONG(4),
STADIUM(5),
DATE(6),
PERSON(7),
EVENT(8);

private final int value;

AnswerCategory(int value) {
this.value = value;
}

public int getValue() {
return value;
}
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR
}

3 changes: 2 additions & 1 deletion questiongenerator/src/main/java/model/QuestionCategory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package model;

public enum QuestionCategory {
HISTORY, GEOGRAPHY, SCIENCE, MATH, LITERATURE, ART, SPORTS, MUSIC, MOVIES, TV, POLITICS, OTHER
//HISTORY, GEOGRAPHY, SCIENCE, MATH, LITERATURE, ART, SPORTS, MUSIC, MOVIES, TV, POLITICS, OTHER
GEOGRAPHY, SPORTS, MUSIC
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
package repositories;

import model.Answer;

import model.AnswerCategory;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import java.util.List;

/**
* Class for storing entries in the Question and Answer DB.
* They implement the Storable interface.
*/
public class GeneralRepositoryStorer {
public void save(Storable s){
EntityManagerFactory emf = Jpa.getEntityManagerFactory();

EntityManager entityManager = emf.createEntityManager();
entityManager.getTransaction().begin();

entityManager.persist(s);

entityManager.getTransaction().commit();
entityManager.close();

Jpa.close();
}
public void saveAll(List<Storable> storableList) {
EntityManagerFactory emf = Jpa.getEntityManagerFactory();

EntityManager entityManager = emf.createEntityManager();

for (Storable s : storableList) {
Expand All @@ -36,7 +25,22 @@ public void saveAll(List<Storable> storableList) {
}

entityManager.close();
Jpa.close();
}

public static boolean doesntExist(AnswerCategory category) {
EntityManagerFactory emf = Jpa.getEntityManagerFactory();
EntityManager entityManager = emf.createEntityManager();

Query query = entityManager.createQuery("SELECT COUNT(a) FROM Answer a WHERE a.category = :category");
query.setParameter("category", category);
Long count = (Long) query.getSingleResult();

entityManager.close();
Jpa.close();

return count == 0;


}
}
65 changes: 65 additions & 0 deletions questiongenerator/src/main/java/templates/BallonDOrQuestion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package templates;

import model.*;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class BallonDOrQuestion extends QuestionTemplate {

/**
* It is not necessary to specify the language code for this question
* @param langCode IGNORED, spanish and english languages are generated at the same time
*/
public BallonDOrQuestion(String langCode) {
super(langCode);
}

@Override
protected void setQuery() {
this.sparqlQuery =
"SELECT ?playerLabel ?year " +
"WHERE { " +
" wd:Q166177 p:P1346 ?statement. " +
" ?statement ps:P1346 ?player. " +
" ?statement pq:P585 ?year. " +
" SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\". } " +
"} " +
"ORDER BY ?year";
}

@Override
protected void processResults() {
List<Question> questions = new ArrayList<>();
List<Answer> answers = new ArrayList<>();
for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
String playerLabel = result.getJSONObject("playerLabel").getString("value");
String year = result.getJSONObject("year").getString("value").substring(0, 4);

if (needToSkip(playerLabel, year))
continue;

// EXCEPTION FOR THIS TYPE OF QUESTION
// English
Answer a = new Answer(playerLabel, AnswerCategory.BALLON_DOR, "en");
answers.add(a);
questions.add(new Question(a, "Who won the Ballon d'Or in " + year + "?", QuestionCategory.SPORTS, QuestionType.TEXT));

// Spanish
a = new Answer(playerLabel, AnswerCategory.BALLON_DOR, "es");
answers.add(a);
questions.add(new Question(a, "¿Quién ganó el Balón de Oro en " + year + "?", QuestionCategory.SPORTS, QuestionType.TEXT));

}

repository.saveAll(new ArrayList<>(answers));
repository.saveAll(new ArrayList<>(questions));
}

// Method to check if the player name or year is missing
private boolean needToSkip(String playerLabel, String year) {
return playerLabel.isEmpty() || year.isEmpty();
}
}
15 changes: 14 additions & 1 deletion webapp/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"rules": "Rules",
"dashboard": "User's Dashboard",
"goBack": "Go Back",
"finish": "Finish"
"finish": "Finish",
"english": "English",
"spanish": "Spanish",
"language": "Language:"
},
"session": {
"username": "Username",
Expand Down Expand Up @@ -68,5 +71,15 @@
"personalWrong": "{{wrong, number}} wrong answers",
"personalRate": "{{rate, number}} %"
}
},
"game": {
"round": "Round",
"next": "Next"
},
"about": {
"title": "About",
"description1": "We are the WIQ_EN2B group and we are delighted that you like 🥝KIWIQ🥝. Enjoy!!!",
"table1": "Developers🍌🍌",
"table2": "Contact"
}
}
15 changes: 14 additions & 1 deletion webapp/public/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"rules": "Reglas",
"dashboard": "Panel de Usuario",
"goBack": "Volver",
"finish": "Finalizar"
"finish": "Finalizar",
"english": "Inglés",
"spanish": "Español",
"language": "Idioma:"
},
"session": {
"username": "Nombre de usuario",
Expand Down Expand Up @@ -67,5 +70,15 @@
"personalWrong": "{{wrong, number}} respuestas incorrectas",
"personalRate": "{{rate, number}} %"
}
},
"game": {
"round": "Ronda",
"next": "Siguiente"
},
"about": {
"title": "Sobre nosotros",
"description1": "Somos el grupo WIQ_EN2B y estamos encantados de que te guste 🥝KIWIQ🥝. Disfruta!!!",
"table1": "Desarrolladores🍌🍌",
"table2": "Contacto"
}
}
Loading

0 comments on commit 2bbfca1

Please sign in to comment.