|
12 | 12 | import java.util.ArrayList;
|
13 | 13 | import java.util.Collection;
|
14 | 14 | import java.util.Collections;
|
| 15 | +import java.util.HashMap; |
15 | 16 | import java.util.HashSet;
|
16 | 17 | import java.util.Iterator;
|
17 | 18 | import java.util.List;
|
|
57 | 58 | import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleRestTemplateFactory;
|
58 | 59 | import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils;
|
59 | 60 | import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.CourseData;
|
| 61 | +import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.Courses; |
60 | 62 | import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.CoursesPlugin;
|
61 | 63 | import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.MoodleUserDetails;
|
62 | 64 | import io.micrometer.core.instrument.util.StringUtils;
|
@@ -425,26 +427,29 @@ private Collection<CourseData> fetchCoursesPage(
|
425 | 427 | try {
|
426 | 428 | // get course ids per page
|
427 | 429 | final long filterDate = Utils.toUnixTimeInSeconds(quizFromTime);
|
428 |
| - final long defaultCutOff = Utils.toUnixTimeInSeconds(DateTime.now(DateTimeZone.UTC).minusYears(this.cutoffTimeOffset)); |
| 430 | + final long defaultCutOff = Utils.toUnixTimeInSeconds( |
| 431 | + DateTime.now(DateTimeZone.UTC).minusYears(this.cutoffTimeOffset)); |
429 | 432 | final long cutoffDate = Math.min(filterDate, defaultCutOff);
|
430 |
| - |
431 |
| - final String sqlCondition = getSQLCondition(nameCondition, cutoffDate, filterDate); |
| 433 | + String sqlCondition = String.format( |
| 434 | + SQL_CONDITION_TEMPLATE, cutoffDate, filterDate); |
432 | 435 | final String fromElement = String.valueOf(page * size);
|
433 | 436 | final LinkedMultiValueMap<String, String> attributes = new LinkedMultiValueMap<>();
|
434 |
| - |
| 437 | + |
| 438 | + if (this.applyNameCriteria && StringUtils.isNotBlank(nameCondition)) { |
| 439 | + final String n = Utils.toSQLWildcard(nameCondition); |
| 440 | + sqlCondition = sqlCondition + " AND (" + SQL_QUIZ_NAME + " LIKE '" + n + "' OR " + SQL_COURSE_NAME + " LIKE '" + n + "')"; |
| 441 | + } |
| 442 | + |
435 | 443 | // Note: courseid[]=0 means all courses. Moodle don't like empty parameter
|
436 | 444 | attributes.add(PARAM_COURSE_ID_ARRAY, "0");
|
437 | 445 | attributes.add(PARAM_SQL_CONDITIONS, sqlCondition);
|
438 | 446 | attributes.add(PARAM_PAGE_START, fromElement);
|
439 | 447 | attributes.add(PARAM_PAGE_SIZE, String.valueOf(size));
|
440 | 448 |
|
441 | 449 | final String courseKeyPageJSON = this.protectedMoodlePageCall
|
442 |
| - .protectedRun(() -> getCoursePageFromMoodle( |
443 |
| - restTemplate, |
444 |
| - attributes, |
445 |
| - cutoffDate, |
446 |
| - filterDate |
447 |
| - )) |
| 450 | + .protectedRun(() -> restTemplate.callMoodleAPIFunction( |
| 451 | + COURSES_API_FUNCTION_NAME, |
| 452 | + attributes)) |
448 | 453 | .getOrThrow();
|
449 | 454 |
|
450 | 455 | MoodleUtils.checkJSONFormat(courseKeyPageJSON);
|
@@ -498,45 +503,6 @@ private Collection<CourseData> fetchCoursesPage(
|
498 | 503 | return Collections.emptyList();
|
499 | 504 | }
|
500 | 505 | }
|
501 |
| - |
502 |
| - private String getCoursePageFromMoodle( |
503 |
| - final MoodleAPIRestTemplate restTemplate, |
504 |
| - final LinkedMultiValueMap<String, String> attributes, |
505 |
| - final long cutoffDate, |
506 |
| - final long filterDate) { |
507 |
| - |
508 |
| - String responseBody = null; |
509 |
| - |
510 |
| - // SEBSERV-652 mitigation (can be removed when Moodle bug is fixed |
511 |
| - try { |
512 |
| - responseBody = restTemplate.callMoodleAPIFunction(COURSES_API_FUNCTION_NAME, attributes); |
513 |
| - } catch (final Exception e) { |
514 |
| - responseBody = null; |
515 |
| - } |
516 |
| - |
517 |
| - if (responseBody == null) { |
518 |
| - |
519 |
| - log.warn("*** Moodle respond with empty body on request with attributes: {}", attributes); |
520 |
| - log.info("*** Apply SEBSERV-652 mitigation..."); |
521 |
| - |
522 |
| - attributes.remove(PARAM_SQL_CONDITIONS); |
523 |
| - attributes.add(PARAM_SQL_CONDITIONS, getSQLCondition(null, cutoffDate, filterDate)); |
524 |
| - responseBody = restTemplate.callMoodleAPIFunction(COURSES_API_FUNCTION_NAME, attributes); |
525 |
| - } |
526 |
| - |
527 |
| - return responseBody; |
528 |
| - } |
529 |
| - |
530 |
| - private String getSQLCondition(final String nameCondition, final long cutoffDate, final long filterDate) { |
531 |
| - String sqlCondition = String.format(SQL_CONDITION_TEMPLATE, cutoffDate, filterDate); |
532 |
| - |
533 |
| - if (this.applyNameCriteria && StringUtils.isNotBlank(nameCondition)) { |
534 |
| - final String nc = Utils.toSQLWildcard(nameCondition); |
535 |
| - sqlCondition = sqlCondition + " AND (" + SQL_QUIZ_NAME + " LIKE '" + nc + "' OR " + SQL_COURSE_NAME + " LIKE '" + nc + "')"; |
536 |
| - } |
537 |
| - |
538 |
| - return sqlCondition; |
539 |
| - } |
540 | 506 |
|
541 | 507 | private List<QuizData> getQuizzesForIds(
|
542 | 508 | final MoodleAPIRestTemplate restTemplate,
|
|
0 commit comments