PUNKT-62 kolo ratunkowe

This commit is contained in:
unknown 2020-01-16 19:15:47 +01:00
parent cb4d85fd89
commit c03d1da842
5 changed files with 89 additions and 13 deletions

View File

@ -1,10 +1,7 @@
package studycave.studycaverestservice.controller;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
@ -81,18 +78,51 @@ public class TestController {
@Autowired
private UserActivityRepository userActivityRepository;
Random generator = new Random();
@GetMapping("/{id}")
public Optional<Test> getTest(@PathVariable(required = true) Long id) {
Optional<Test> test = testRepository.findById(id);
for (Question question : test.get().getQuestions()) {
Test testToReturn = test.get();
for (Question question : testToReturn.getQuestions()) {
if(question instanceof QuestionGaps) {
List<AnswerGaps> answers = ((QuestionGaps)question).getAnswers();
Collections.sort(answers,
(o1, o2) -> o1.getId().compareTo(o2.getId()));
}
}
test.get().setGroup(null);
test.get().setActivity(null);
if (testToReturn.getHelp()){
for (Question question : testToReturn.getQuestions()) {
if (question instanceof QuestionChoices){
QuestionChoices questionChoices = (QuestionChoices) question;
List<AnswerChoices> answers = questionChoices.getAnswers();
if (answers.size() > 2) {
for (AnswerChoices answerChoices : answers){
if (answerChoices.getGood()) {
questionChoices.getAnswers_help().add(answerChoices);
break;
}
}
if (answers.size() % 2 == 0 && questionChoices.getAnswers_help().size() == 1){
while (questionChoices.getAnswers_help().size() < answers.size() / 2){
AnswerChoices answerChoices = answers.get(generator.nextInt(answers.size()));
if (!answerChoices.getGood() && !questionChoices.getAnswers_help().contains(answerChoices))
questionChoices.getAnswers_help().add(answerChoices);
}
}
else if (questionChoices.getAnswers_help().size() == 1){
while (questionChoices.getAnswers_help().size() < (answers.size() / 2) + 1){
AnswerChoices answerChoices = answers.get(generator.nextInt(answers.size()));
if (!answerChoices.getGood() && !questionChoices.getAnswers_help().contains(answerChoices))
questionChoices.getAnswers_help().add(answerChoices);
}
}
}
}
}
}
testToReturn.setGroup(null);
testToReturn.setActivity(null);
return test;
}
@ -104,6 +134,8 @@ public class TestController {
Optional<Test> test = testRepository.findById(id);
for (Question question : test.get().getQuestions()) {
if (question.getType().equals("true-false") || question.getType().equals("single-choice")
|| question.getType().equals("multiple-choice")) {
@ -112,6 +144,38 @@ public class TestController {
AnswerChoicesSolveDTO answerDTO = modelMapper.map(answer, AnswerChoicesSolveDTO.class);
answersDTOs.add(answerDTO);
}
List<AnswerChoices> helpers = new ArrayList<>();
if (question.getType().equals("single-choice")) {
if (test.get().getHelp()){
List<AnswerChoices> answers = answersDTOs;
if (answers.size() > 2) {
for (AnswerChoices answerChoices : answers){
if (answerChoices.getGood()) {
helpers.add(answerChoices);
break;
}
}
if (answers.size() % 2 == 0 && helpers.size() == 1){
while (helpers.size() < answers.size() / 2){
AnswerChoices answerChoices = answers.get(generator.nextInt(answers.size()));
if (!answerChoices.getGood() && !helpers.contains(answerChoices))
helpers.add(answerChoices);
}
}
else if (helpers.size() == 1){
while (helpers.size() < (answers.size() / 2) + 1){
AnswerChoices answerChoices = answers.get(generator.nextInt(answers.size()));
if (!answerChoices.getGood() && !helpers.contains(answerChoices))
helpers.add(answerChoices);
}
}
}
}
}
if (helpers.size() > 0) {
((QuestionChoices) question).setAnswers_help(helpers);
}
((QuestionChoices) question).setAnswers(answersDTOs);
}
if (question.getType().equals("puzzle")) {
@ -304,6 +368,8 @@ public class TestController {
test.setAddDate();
test.setEditDate();
test.setGrade();
if (testDTO.getHelp() != null)
test.setHelp(testDTO.getHelp());
// Badge for creating first test
if(userBadgeRepository.findByIdAndUser((long)4, user.getId()).isEmpty()) {
@ -484,7 +550,8 @@ public class TestController {
// System.out.println("usuwam "+oldquestion.getId());
}
}
if (testDTO.getHelp() != null)
test.setHelp(testDTO.getHelp());
testRepository.save(test);
for (Long a : deletea)

View File

@ -66,6 +66,9 @@ public class Test {
@Nullable
private Long availableTo = null;
@Column(columnDefinition = "TINYINT(1)")
private Boolean help = false;
public void setAddDate() {
java.util.Date utilDate = new java.util.Date();

View File

@ -31,6 +31,8 @@ public class TestCreateDTO {
@ApiModelProperty(value = "Default value for note", required = true,example = "public")
private String permission;
private Boolean help = false;
@JsonIgnore
private Long grade;
@JsonProperty("body")

View File

@ -37,4 +37,6 @@ public class TestEditDTO {
@JsonProperty("body")
List<Question> questions;
private Boolean help;
}

View File

@ -6,10 +6,7 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@ -17,10 +14,15 @@ import java.util.List;
@AllArgsConstructor
public class QuestionChoices extends Question {
@OneToMany(fetch = FetchType.LAZY,mappedBy="question",cascade = CascadeType.ALL)
@OneToMany(fetch = FetchType.LAZY, mappedBy="question", cascade = CascadeType.ALL)
@JsonManagedReference
List<AnswerChoices> answers = new ArrayList<>();
@Transient
@Getter
@Setter
List<AnswerChoices> answers_help = new ArrayList<>();
public List<AnswerChoices> getAnswers() {
return answers;
}