Skip to content

Commit 1d62d4e

Browse files
committed
update import for non sheets
1 parent 8c66c14 commit 1d62d4e

1 file changed

Lines changed: 101 additions & 91 deletions

File tree

src/main/java/fr/istic/service/ExamService.java

Lines changed: 101 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void delete(Long id) {
8989
StudentResponse.getAll4ExamIdEvenOrphan(id).list().forEach(sr -> sr.delete());
9090
FinalResult.getAll4ExamId(id).list().forEach(f -> f.delete());
9191
Exam e = Exam.findById(id);
92-
HybridGradedComment.deleteByQIds(e.questions.stream().map(q-> q.id).collect(Collectors.toSet()));
92+
HybridGradedComment.deleteByQIds(e.questions.stream().map(q -> q.id).collect(Collectors.toSet()));
9393

9494
if (e.scanfile != null && this.fichierS3Service.isObjectExist("scan/" + e.scanfile.id + ".pdf")) {
9595
try {
@@ -117,7 +117,7 @@ public void delete(Long id) {
117117
}
118118

119119
@Transactional
120-
protected void cleanStudentRssponse(long id){
120+
protected void cleanStudentRssponse(long id) {
121121
List<StudentResponse> srs = StudentResponse.getAll4ExamIdEvenOrphan(id).list();
122122
srs.forEach(sr -> {
123123
Set<TextComment> tcs = new HashSet<TextComment>(sr.textcomments);
@@ -139,25 +139,21 @@ protected void cleanStudentRssponse(long id){
139139
sr.persistOrUpdate();
140140
});
141141

142-
// srs.forEach(sr -> sr.delete());
142+
// srs.forEach(sr -> sr.delete());
143143

144144
}
145145

146146
@Transactional
147-
protected Set<Long> cleanExamZone(long id){
147+
protected Set<Long> cleanExamZone(long id) {
148148
Exam exam = Exam.findById(id);
149149
Set<Long> zoneids = new HashSet<>();
150-
if (exam.namezone != null ){
150+
if (exam.namezone != null) {
151151
zoneids.add(exam.namezone.id);
152-
}
153-
else if (exam.firstnamezone != null ){
152+
} else if (exam.firstnamezone != null) {
154153
zoneids.add(exam.firstnamezone.id);
155-
}
156-
else if (exam.idzone != null ){
154+
} else if (exam.idzone != null) {
157155
zoneids.add(exam.idzone.id);
158-
}
159-
else if (exam.notezone != null )
160-
{
156+
} else if (exam.notezone != null) {
161157
zoneids.add(exam.notezone.id);
162158
}
163159
Exam.removeAllZone(exam);
@@ -166,46 +162,41 @@ else if (exam.notezone != null )
166162
}
167163

168164
@Transactional
169-
protected void cleanExamZone(Set<Long> zoneids){
165+
protected void cleanExamZone(Set<Long> zoneids) {
170166
Zone.deleteAllZonesIds(zoneids);
171167

172168
}
173169

174-
175-
176170
@Transactional
177-
protected void cleanQuestion(long examId,Set<Long> qids ){
178-
StudentResponse.deleteByQIds( qids);
171+
protected void cleanQuestion(long examId, Set<Long> qids) {
172+
StudentResponse.deleteByQIds(qids);
179173
TextComment.deleteByQIds(qids);
180174
GradedComment.deleteByQIds(qids);
181175
Question.deleteAllExamId(examId);
182176

183177
}
184178

185-
186-
187179
@Transactional
188-
protected void cleanFinalResult(long id){
180+
protected void cleanFinalResult(long id) {
189181
FinalResult.deleteAllByExamId(id);
190182

191183
}
192-
/**
184+
185+
/**
193186
* Delete the Exam by id.
194187
*
195188
* @param id the id of the entity.
196189
*/
197190
@Transactional
198191
public void deleteQuestionCommentAndZone(Long id) {
199192
log.debug("Request to delete Exam : {}", id);
200-
Set<Long> qids = Question.findQuestionbyExamId(id).list().stream().map(ex -> ex.id).collect(Collectors.toSet());
201-
202-
this.cleanFinalResult(id);
203-
Set<Long> zonesids = this.cleanExamZone(id);
204-
this.cleanExamZone(zonesids);
205-
// this.cleanStudentRssponse(id);
206-
this.cleanQuestion(id,qids);
207-
193+
Set<Long> qids = Question.findQuestionbyExamId(id).list().stream().map(ex -> ex.id).collect(Collectors.toSet());
208194

195+
this.cleanFinalResult(id);
196+
Set<Long> zonesids = this.cleanExamZone(id);
197+
this.cleanExamZone(zonesids);
198+
// this.cleanStudentRssponse(id);
199+
this.cleanQuestion(id, qids);
209200

210201
}
211202

@@ -249,15 +240,16 @@ public Paged<ExamDTO> findExambyScanId(Page page, long scanId) {
249240
return new Paged<>(Exam.findExambyScanId(scanId).page(page))
250241
.map(exam -> examMapper.toDto((Exam) exam));
251242
}
243+
252244
private static final Map<Double, Integer> knownFractions = new HashMap<>();
253245
static {
254-
knownFractions.put(1.0 / 2,2);
246+
knownFractions.put(1.0 / 2, 2);
255247
knownFractions.put(1.0 / 3, 3);
256248
knownFractions.put(0.33, 3);
257249
knownFractions.put(0.66, 3);
258250
knownFractions.put(2.0 / 3, 3);
259-
knownFractions.put(1.0 / 4,4);
260-
knownFractions.put(3.0 / 4,4);
251+
knownFractions.put(1.0 / 4, 4);
252+
knownFractions.put(3.0 / 4, 4);
261253
knownFractions.put(1.0 / 5, 5);
262254
knownFractions.put(2.0 / 5, 5);
263255
knownFractions.put(3.0 / 5, 5);
@@ -297,13 +289,13 @@ public Paged<ExamDTO> findExambyScanId(Page page, long scanId) {
297289

298290
private static final Map<Double, Integer> knownFractionsNum = new HashMap<>();
299291
static {
300-
knownFractionsNum.put(1.0 / 2,1);
292+
knownFractionsNum.put(1.0 / 2, 1);
301293
knownFractionsNum.put(1.0 / 3, 1);
302294
knownFractionsNum.put(0.33, 1);
303295
knownFractionsNum.put(0.66, 2);
304296
knownFractionsNum.put(2.0 / 3, 2);
305-
knownFractionsNum.put(1.0 / 4,1);
306-
knownFractionsNum.put(3.0 / 4,3);
297+
knownFractionsNum.put(1.0 / 4, 1);
298+
knownFractionsNum.put(3.0 / 4, 3);
307299
knownFractionsNum.put(1.0 / 5, 1);
308300
knownFractionsNum.put(2.0 / 5, 2);
309301
knownFractionsNum.put(3.0 / 5, 3);
@@ -341,8 +333,6 @@ public Paged<ExamDTO> findExambyScanId(Page page, long scanId) {
341333
knownFractionsNum.put(0.889, 8);
342334
}
343335

344-
345-
346336
public static int decimalToFractionDenominateur(double value) {
347337
for (Map.Entry<Double, Integer> entry : knownFractions.entrySet()) {
348338
if (Math.abs(value - entry.getKey()) < 0.001) {
@@ -363,7 +353,8 @@ public static int decimalToFractionDenominateur(double value) {
363353
bestNumerator = numer;
364354
bestDenominator = denom;
365355
minError = error;
366-
if (error < 0.0001) break;
356+
if (error < 0.0001)
357+
break;
367358
}
368359
}
369360

@@ -391,46 +382,50 @@ public static int decimalToFractionNumerateur(double value) {
391382
bestNumerator = numer;
392383
bestDenominator = denom;
393384
minError = error;
394-
if (error < 0.0001) break;
385+
if (error < 0.0001)
386+
break;
395387
}
396388
}
397389

398390
int gcd = gcd(bestNumerator, bestDenominator);
399391
return (bestNumerator / gcd);
400392
}
401393

402-
403394
private static int gcd(int a, int b) {
404395
return b == 0 ? a : gcd(b, a % b);
405396
}
397+
406398
@Transactional
407399
public void createNoteBookExamStructure(List<AnswersNoteBook> answersNoteBook, User u) {
408-
Exam e =Exam.findById(answersNoteBook.get(0).getExamId());
400+
Exam e = Exam.findById(answersNoteBook.get(0).getExamId());
409401

410-
Integer maxLength = answersNoteBook.stream().mapToInt(answersNoteBook1 -> answersNoteBook1.getQuestions().size()).max().orElse(0);
402+
Integer maxLength = answersNoteBook.stream()
403+
.mapToInt(answersNoteBook1 -> answersNoteBook1.getQuestions().size()).max().orElse(0);
411404
AnswersNoteBook questionMaxLength = null;
412405
for (AnswersNoteBook answersNoteBook1 : answersNoteBook) {
413406
if (answersNoteBook1.getQuestions().size() == maxLength) {
414407
questionMaxLength = answersNoteBook1;
415408
break;
416409
}
417410
}
411+
Set<Integer> qsnumero = questionMaxLength.getQuestions().stream().map(e1 -> e1.getNumero())
412+
.collect(Collectors.toSet());
418413

419414
Scan scan = new Scan();
420-
scan.name= e.name +"Scan";
415+
scan.name = e.name + "Scan";
421416
scan.contentContentType = "application/zip";
422-
e.scanfile =scan;
417+
e.scanfile = scan;
423418
Scan.persistOrUpdate(scan);
424419
e.persistOrUpdate();
425-
Map<Integer,Question> questionCaches = new HashMap<>();
426-
Map<Integer,HybridGradedComment> hcCaches = new HashMap<>();
427-
int qIndex= 0;
420+
Map<Integer, Question> questionCaches = new HashMap<>();
421+
Map<Integer, HybridGradedComment> hcCaches = new HashMap<>();
422+
int qIndex = 0;
428423
for (QuestionNoteBook qnb : questionMaxLength.getQuestions()) {
429424
Question q = new Question();
430425
q.exam = e;
431426
q.gradeType = GradeType.HYBRID;
432427
q.type = QuestionType.findQuestionTypebyAlgoName("manual").firstResult();
433-
q.numero = qIndex+1;
428+
q.numero = qIndex + 1;
434429
q.randomHorizontalCorrection = false;
435430
q.canBeNegative = false;
436431
q.canExceedTheMax = false;
@@ -440,10 +435,10 @@ public void createNoteBookExamStructure(List<AnswersNoteBook> answersNoteBook, U
440435

441436
Zone z = new Zone();
442437
z.pageNumber = qIndex;
443-
z.xInit =- 1;
444-
z.yInit =- 1;
445-
z.height =- 1;
446-
z.width =- 1;
438+
z.xInit = -1;
439+
z.yInit = -1;
440+
z.height = -1;
441+
z.width = -1;
447442

448443
Zone.persistOrUpdate(z);
449444

@@ -460,70 +455,85 @@ public void createNoteBookExamStructure(List<AnswersNoteBook> answersNoteBook, U
460455
hybridGradedComment.description = "Nbgrader automatic evaluation";
461456
hybridGradedComment.question = q;
462457
q.hybridcomments.add(hybridGradedComment);
463-
hybridGradedComment.relative= true;
458+
hybridGradedComment.relative = true;
464459
// Compute minimum step
465460
Integer maxStep = 1;
466-
for(AnswersNoteBook answerNoteBook : answersNoteBook){
467-
if (qIndex < answerNoteBook.getQuestions().size()){
468-
QuestionNoteBook q1= answerNoteBook.getQuestions().get(qIndex);
469-
if (q1.getNotemax() >0.0 && q1.getNote() >0.0){
470-
Integer step = decimalToFractionDenominateur(q1.getNote()/q1.getNotemax());
471-
if (step> maxStep){
472-
maxStep = step;
461+
for (AnswersNoteBook answerNoteBook : answersNoteBook) {
462+
if (qIndex < answerNoteBook.getQuestions().size()) {
463+
QuestionNoteBook q1 = answerNoteBook.getQuestions().get(qIndex);
464+
if (q1.getNotemax() > 0.0 && q1.getNote() > 0.0) {
465+
Integer step = decimalToFractionDenominateur(q1.getNote() / q1.getNotemax());
466+
if (step > maxStep) {
467+
maxStep = step;
468+
}
473469
}
474470
}
475471
}
476-
}
477472

478-
hybridGradedComment.step = maxStep;
473+
hybridGradedComment.step = maxStep;
479474
hybridGradedComment.grade = 400;
480475
HybridGradedComment.persistOrUpdate(hybridGradedComment);
481-
hcCaches.put(qIndex, hybridGradedComment);
476+
hcCaches.put(q.numero, hybridGradedComment);
482477

483-
qIndex = qIndex+1;
478+
qIndex = qIndex + 1;
484479

485480
}
486481

487482
Integer studentIndex = 0;
488483
for (AnswersNoteBook answerNoteBook : answersNoteBook) {
489484

490-
491485
ExamSheet es = new ExamSheet();
492-
es.pagemin = (studentIndex* maxLength) ;
493-
es.pagemax = (studentIndex* maxLength) + maxLength-1;
486+
es.pagemin = (studentIndex * maxLength);
487+
es.pagemax = (studentIndex * maxLength) + maxLength - 1;
494488
es.name = answerNoteBook.getSheetName();
495489
scan.sheets.add(es);
496490
es.scan = scan;
497491
ExamSheet.persistOrUpdate(es);
498-
qIndex= 0;
499-
500-
for (QuestionNoteBook qnb : answerNoteBook.getQuestions()) {
501-
StudentResponse sr = new StudentResponse();
502-
sr.sheet = es;
503-
sr.lastModifiedDate =Instant.now();
504-
sr.correctedBy = u;
505-
sr.question = questionCaches.get(qnb.getNumero());
506-
sr.worststar = false;
507-
sr.star = false;
508-
StudentResponse.persistOrUpdate(sr);
509-
Answer2HybridGradedComment answer2HybridGradedComment = new Answer2HybridGradedComment();
510-
answer2HybridGradedComment.hybridcomments =hcCaches.get(qIndex);
511-
hcCaches.get(qIndex).valueAnswers.add(answer2HybridGradedComment);
512-
HybridGradedComment.persistOrUpdate(hcCaches.get(qIndex));
513-
answer2HybridGradedComment.studentResponse = sr;
514-
if (qnb.getNotemax() >0.0 && qnb.getNote() >0.0){
515-
answer2HybridGradedComment.stepValue = decimalToFractionNumerateur(qnb.getNote()/ qnb.getNotemax());
516-
} else {
517-
answer2HybridGradedComment.stepValue = 0;
492+
493+
for (int numero : qsnumero) {
494+
Optional<QuestionNoteBook> q1 = answerNoteBook.getQuestions().stream()
495+
.filter(qnb -> qnb.getNumero() == numero).findFirst();
496+
q1.ifPresent(qnb -> {
497+
// QuestionNoteBook qnb : answerNoteBook.getQuestions()) {
498+
StudentResponse sr = new StudentResponse();
499+
sr.sheet = es;
500+
sr.lastModifiedDate = Instant.now();
501+
sr.correctedBy = u;
502+
sr.question = questionCaches.get(qnb.getNumero());
503+
sr.worststar = false;
504+
sr.star = false;
505+
StudentResponse.persistOrUpdate(sr);
506+
Answer2HybridGradedComment answer2HybridGradedComment = new Answer2HybridGradedComment();
507+
answer2HybridGradedComment.hybridcomments = hcCaches.get(numero);
508+
hcCaches.get(numero).valueAnswers.add(answer2HybridGradedComment);
509+
HybridGradedComment.persistOrUpdate(hcCaches.get(numero));
510+
answer2HybridGradedComment.studentResponse = sr;
511+
if (qnb.getNotemax() > 0.0 && qnb.getNote() > 0.0) {
512+
answer2HybridGradedComment.stepValue = decimalToFractionNumerateur(
513+
qnb.getNote() / qnb.getNotemax());
514+
} else {
515+
answer2HybridGradedComment.stepValue = 0;
516+
}
517+
Answer2HybridGradedComment.persistOrUpdate(answer2HybridGradedComment);
518+
StudentResponse.persistOrUpdate(sr);
519+
});
520+
if (q1.isEmpty()) {
521+
StudentResponse sr = new StudentResponse();
522+
sr.sheet = es;
523+
sr.lastModifiedDate = Instant.now();
524+
sr.correctedBy = u;
525+
sr.question = questionCaches.get(numero);
526+
sr.worststar = false;
527+
sr.star = false;
528+
sr.quarternote = 0;
529+
StudentResponse.persistOrUpdate(sr);
530+
518531
}
519-
Answer2HybridGradedComment.persistOrUpdate(answer2HybridGradedComment);
520-
StudentResponse.persistOrUpdate(sr);
521-
qIndex = qIndex+1;
532+
522533
}
523534
studentIndex = studentIndex + 1;
524535
}
525536

526-
527537
}
528538

529539
}

0 commit comments

Comments
 (0)