Skip to content

Commit

Permalink
imnprove logging in case reporting a new result to LTI consumer fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Krusche committed Oct 5, 2020
1 parent ac2b13b commit 6d923d9
Showing 1 changed file with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -443,37 +442,32 @@ private String httpServletRequestToString(HttpServletRequest request) {
public void onNewResult(ProgrammingExerciseStudentParticipation participation) {
if (this.OAUTH_KEY.isPresent() && this.OAUTH_SECRET.isPresent()) {
// Get the LTI outcome URL
participation.getStudents().forEach(student -> {
ltiOutcomeUrlRepository.findByUserAndExercise(student, participation.getExercise()).ifPresent(ltiOutcomeUrl -> {
participation.getStudents().forEach(student -> ltiOutcomeUrlRepository.findByUserAndExercise(student, participation.getExercise()).ifPresent(ltiOutcomeUrl -> {

String score = "0.00";
String score = "0.00";

// Get the latest result
Optional<Result> latestResult = resultRepository.findFirstByParticipationIdOrderByCompletionDateDesc(participation.getId());
// Get the latest result
Optional<Result> latestResult = resultRepository.findFirstByParticipationIdOrderByCompletionDateDesc(participation.getId());

if (latestResult.isPresent() && latestResult.get().getScore() != null) {
// LTI scores needs to be formatted as String between "0.00" and "1.00"
score = String.format(Locale.ROOT, "%.2f", latestResult.get().getScore().floatValue() / 100);
}
if (latestResult.isPresent() && latestResult.get().getScore() != null) {
// LTI scores needs to be formatted as String between "0.00" and "1.00"
score = String.format(Locale.ROOT, "%.2f", latestResult.get().getScore().floatValue() / 100);
}

try {
log.info("Reporting score {} for participation {} to LTI consumer with outcome URL {} using the source id {}", score, participation, ltiOutcomeUrl.getUrl(),
ltiOutcomeUrl.getSourcedId());
HttpPost request = IMSPOXRequest.buildReplaceResult(ltiOutcomeUrl.getUrl(), OAUTH_KEY.get(), OAUTH_SECRET.get(), ltiOutcomeUrl.getSourcedId(), score, null,
false);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
String responseString = new BasicResponseHandler().handleResponse(response);
log.info("Response from LTI consumer: {}", responseString);
if (response.getStatusLine().getStatusCode() >= 400) {
throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
}
}
catch (Exception e) {
log.error("Reporting to LTI consumer failed: {}", e, e);
}
});
});
try {
log.info("Reporting score {} for participation {} to LTI consumer with outcome URL {} using the source id {}", score, participation, ltiOutcomeUrl.getUrl(),
ltiOutcomeUrl.getSourcedId());
HttpPost request = IMSPOXRequest.buildReplaceResult(ltiOutcomeUrl.getUrl(), OAUTH_KEY.get(), OAUTH_SECRET.get(), ltiOutcomeUrl.getSourcedId(), score, null,
false);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
String responseString = new BasicResponseHandler().handleResponse(response);
log.info("Response from LTI consumer: {}", responseString);
}
catch (Exception ex) {
log.error("Reporting to LTI consumer failed: " + ex.getMessage(), ex);
}
}));
}
}

Expand Down

0 comments on commit 6d923d9

Please sign in to comment.