manually picked changes from pull-request: Integrate SEB Server in OLAT again #121

This commit is contained in:
anhefti 2024-06-04 12:09:31 +02:00
parent c8ff644e7d
commit d6e65eb8ad
3 changed files with 33 additions and 27 deletions

View file

@ -275,7 +275,7 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm
final DateTime quizFromTime = (filterMap != null) ? filterMap.getQuizFromTime() : null; final DateTime quizFromTime = (filterMap != null) ? filterMap.getQuizFromTime() : null;
final long fromCutTime = (quizFromTime != null) ? Utils.toUnixTimeInSeconds(quizFromTime) : -1; final long fromCutTime = (quizFromTime != null) ? Utils.toUnixTimeInSeconds(quizFromTime) : -1;
String url = "/restapi/assessment_modes/seb?"; String url = "/restapi/repo/assessmentmodes?";
if (fromCutTime != -1) { if (fromCutTime != -1) {
url = String.format("%sdateFrom=%s&", url, fromCutTime); url = String.format("%sdateFrom=%s&", url, fromCutTime);
} }
@ -295,8 +295,8 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm
lmsSetup.getLmsType(), lmsSetup.getLmsType(),
a.name, a.name,
a.description, a.description,
Utils.toDateTimeUTC(a.dateFrom), Utils.toDateTimeUTC(a.begin - a.leadTime * 1000 * 60),
Utils.toDateTimeUTC(a.dateTo), Utils.toDateTimeUTC(a.end + a.followupTime * 1000 * 60),
examUrl(a.repositoryEntryKey), examUrl(a.repositoryEntryKey),
new HashMap<String, String>()); new HashMap<String, String>());
}) })
@ -316,7 +316,7 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm
private QuizData quizById(final OlatLmsRestTemplate restTemplate, final String id) { private QuizData quizById(final OlatLmsRestTemplate restTemplate, final String id) {
final LmsSetup lmsSetup = this.apiTemplateDataSupplier.getLmsSetup(); final LmsSetup lmsSetup = this.apiTemplateDataSupplier.getLmsSetup();
final String url = String.format("/restapi/assessment_modes/%s", id); final String url = String.format("/restapi/repo/assessmentmodes/%s", id);
final AssessmentData a = this.apiGet(restTemplate, url, AssessmentData.class); final AssessmentData a = this.apiGet(restTemplate, url, AssessmentData.class);
return new QuizData( return new QuizData(
String.format("%d", a.key), String.format("%d", a.key),
@ -325,26 +325,26 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm
lmsSetup.getLmsType(), lmsSetup.getLmsType(),
a.name, a.name,
a.description, a.description,
Utils.toDateTimeUTC(a.dateFrom), Utils.toDateTimeUTC(a.begin - a.leadTime * 1000 * 60),
Utils.toDateTimeUTC(a.dateTo), Utils.toDateTimeUTC(a.end + a.followupTime * 1000 * 60),
examUrl(a.repositoryEntryKey), examUrl(a.repositoryEntryKey),
new HashMap<String, String>()); new HashMap<String, String>());
} }
private ExamineeAccountDetails getExamineeById(final RestTemplate restTemplate, final String id) { private ExamineeAccountDetails getExamineeById(final RestTemplate restTemplate, final String id) {
final String url = String.format("/restapi/users/%s/name_username", id); final String url = String.format("/restapi/users/%s", id);
final UserData u = this.apiGet(restTemplate, url, UserData.class); final UserData u = this.apiGet(restTemplate, url, UserData.class);
final Map<String, String> attrs = new HashMap<>(); final Map<String, String> attrs = new HashMap<>();
return new ExamineeAccountDetails( return new ExamineeAccountDetails(
String.valueOf(u.key), String.valueOf(u.key),
u.lastName + ", " + u.firstName, u.lastName + ", " + u.firstName,
u.username, u.login,
"OLAT API does not provide email addresses", u.email,
attrs); attrs);
} }
private SEBRestriction getRestrictionForAssignmentId(final RestTemplate restTemplate, final String id) { private SEBRestriction getRestrictionForAssignmentId(final RestTemplate restTemplate, final String id) {
final String url = String.format("/restapi/assessment_modes/%s/seb_restriction", id); final String url = String.format("/restapi/repo/assessmentmodes/%s/seb", id);
final RestrictionData r = this.apiGet(restTemplate, url, RestrictionData.class); final RestrictionData r = this.apiGet(restTemplate, url, RestrictionData.class);
final HashMap<String, String> additionalAttributes = new HashMap<>(); final HashMap<String, String> additionalAttributes = new HashMap<>();
if (StringUtils.isNotBlank(r.quitLink)) { if (StringUtils.isNotBlank(r.quitLink)) {
@ -362,7 +362,7 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm
final String id, final String id,
final SEBRestriction restriction) { final SEBRestriction restriction) {
final String url = String.format("/restapi/assessment_modes/%s/seb_restriction", id); final String url = String.format("/restapi/repo/assessmentmodes/%s/seb", id);
final RestrictionDataPost post = new RestrictionDataPost(); final RestrictionDataPost post = new RestrictionDataPost();
post.browserExamKeys = new ArrayList<>(restriction.browserExamKeys); post.browserExamKeys = new ArrayList<>(restriction.browserExamKeys);
post.configKeys = new ArrayList<>(restriction.configKeys); post.configKeys = new ArrayList<>(restriction.configKeys);
@ -377,7 +377,7 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm
} }
private SEBRestriction deleteRestrictionForAssignmentId(final RestTemplate restTemplate, final String id) { private SEBRestriction deleteRestrictionForAssignmentId(final RestTemplate restTemplate, final String id) {
final String url = String.format("/restapi/assessment_modes/%s/seb_restriction", id); final String url = String.format("/restapi/repo/assessmentmodes/%s/seb", id);
final RestrictionData r = this.apiDelete(restTemplate, url, RestrictionData.class); final RestrictionData r = this.apiDelete(restTemplate, url, RestrictionData.class);
// OLAT returns RestrictionData with null values upon deletion. // OLAT returns RestrictionData with null values upon deletion.
// We return it here for consistency, even though SEB server does not need it // We return it here for consistency, even though SEB server does not need it
@ -473,7 +473,7 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm
.getOrThrow(); .getOrThrow();
final ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails(); final ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
details.setAccessTokenUri(lmsSetup.lmsApiUrl + "/restapi/auth/"); details.setAccessTokenUri(lmsSetup.lmsApiUrl + "/restapi/auth/{username}?password={password}");
details.setClientId(plainClientId.toString()); details.setClientId(plainClientId.toString());
details.setClientSecret(plainClientSecret.toString()); details.setClientSecret(plainClientSecret.toString());
@ -489,4 +489,5 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm
}); });
} }
} }

View file

@ -20,22 +20,24 @@ public final class OlatLmsData {
/* /*
* OLAT API example: * OLAT API example:
* { * {
* "courseName": "course 1", * "begin": 1624420800000,
* "dateFrom": 1624420800000, * "end": 1624658400000,
* "dateTo": 1624658400000,
* "description": "", * "description": "",
* "key": 6356992, * "key": 6356992,
* repositoryEntryKey: 462324, * repositoryEntryKey: 462324,
* "name": "SEB test" * "name": "SEB test",
* "leadTime": 15,
* "followupTime", 5
* } * }
*/ */
public long key; public long key;
public long repositoryEntryKey; public long repositoryEntryKey;
public String name; public String name;
public String description; public String description;
public String courseName; public Long begin;
public long dateFrom; public Long end;
public long dateTo; public long leadTime;
public long followupTime;
} }
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
@ -46,13 +48,15 @@ public final class OlatLmsData {
* "firstName": "OpenOLAT", * "firstName": "OpenOLAT",
* "key": 360448, * "key": 360448,
* "lastName": "Administrator", * "lastName": "Administrator",
* "username": "administrator" * "login": "administrator",
* "email": "admin@example.org"
* } * }
*/ */
public long key; public long key;
public String firstName; public String firstName;
public String lastName; public String lastName;
public String username; public String login;
public String email;
} }
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)

View file

@ -16,6 +16,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest; import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -116,14 +117,14 @@ public class OlatLmsRestTemplate extends RestTemplate {
// Authenticate with OLAT and store the received X-OLAT-TOKEN // Authenticate with OLAT and store the received X-OLAT-TOKEN
this.token = "authenticating"; this.token = "authenticating";
final String authUrl = this.details.getAccessTokenUri(); final String authUrl = this.details.getAccessTokenUri();
final Map<String, String> credentials = new HashMap<>(); final Map<String, String> parameters = new HashMap<>();
credentials.put("username", this.details.getClientId()); parameters.put("username", this.details.getClientId());
credentials.put("password", this.details.getClientSecret()); parameters.put("password", this.details.getClientSecret());
final HttpHeaders httpHeaders = new HttpHeaders(); final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("content-type", "application/json"); httpHeaders.set("content-type", "application/json");
final HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(credentials, httpHeaders); final HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(httpHeaders);
try { try {
final ResponseEntity<String> response = this.postForEntity(authUrl, requestEntity, String.class); final ResponseEntity<String> response = this.exchange(authUrl, HttpMethod.GET, requestEntity, String.class, parameters);
final HttpHeaders responseHeaders = response.getHeaders(); final HttpHeaders responseHeaders = response.getHeaders();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {