From 4cb6c6d7d8855bf0054f22d5b318c82061092a9e Mon Sep 17 00:00:00 2001 From: anhefti Date: Mon, 21 Feb 2022 16:51:08 +0100 Subject: [PATCH] SEBSERV-153 fixes --- .../ch/ethz/seb/sebserver/gbl/api/API.java | 1 + .../sebserver/gui/content/exam/ExamForm.java | 17 +++++++- .../gui/service/ResourceService.java | 15 ++++--- .../api/exam/GetDefaultExamTemplate.java | 40 +++++++++++++++++++ .../weblayer/api/ExamTemplateController.java | 18 +++++++++ src/main/resources/messages.properties | 7 ++-- 6 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/exam/GetDefaultExamTemplate.java diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/api/API.java b/src/main/java/ch/ethz/seb/sebserver/gbl/api/API.java index 8ccc5728..2c5c715f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/api/API.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/api/API.java @@ -228,5 +228,6 @@ public final class API { public static final String EXAM_TEMPLATE_ENDPOINT = "/exam-template"; public static final String EXAM_TEMPLATE_INDICATOR_PATH_SEGMENT = "/indicator"; + public static final String EXAM_TEMPLATE_DEFAULT_PATH_SEGMENT = "/default"; } diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/exam/ExamForm.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/exam/ExamForm.java index 144c9fab..ad20887d 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/exam/ExamForm.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/exam/ExamForm.java @@ -63,6 +63,7 @@ import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.RestCallError; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.RestService; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.exam.CheckExamConsistency; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.exam.CheckSEBRestriction; +import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.exam.GetDefaultExamTemplate; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.exam.GetExam; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.exam.GetExamTemplate; import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.exam.GetProctoringSettings; @@ -351,7 +352,9 @@ public class ExamForm implements TemplateComposer { .addField(FormBuilder.singleSelection( Domain.EXAM.ATTR_EXAM_TEMPLATE_ID, FORM_EXAM_TEMPLATE_TEXT_KEY, - (exam.examTemplateId == null) ? null : String.valueOf(exam.examTemplateId), + (exam.examTemplateId == null) + ? getDefaultExamTemplateId() + : String.valueOf(exam.examTemplateId), this.resourceService::examTemplateResources) .withSelectionListener(form -> this.processTemplateSelection(form, formContext)) .withLabelSpan(2) @@ -382,6 +385,10 @@ public class ExamForm implements TemplateComposer { ? this.restService.getRestCall(ImportAsExam.class) : this.restService.getRestCall(SaveExam.class)); + if (importFromQuizData) { + this.processTemplateSelection(formHandle.getForm(), formContext); + } + final boolean proctoringEnabled = importFromQuizData ? false : this.restService .getBuilder(GetProctoringSettings.class) .withURIVariable(API.PARAM_MODEL_ID, entityKey.modelId) @@ -478,6 +485,14 @@ public class ExamForm implements TemplateComposer { } } + private String getDefaultExamTemplateId() { + return this.restService.getBuilder(GetDefaultExamTemplate.class) + .call() + .map(ExamTemplate::getId) + .map(Object::toString) + .getOr(StringUtils.EMPTY); + } + private void processTemplateSelection(final Form form, final PageContext context) { try { final String templateId = form.getFieldValue(Domain.EXAM.ATTR_EXAM_TEMPLATE_ID); diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/service/ResourceService.java b/src/main/java/ch/ethz/seb/sebserver/gui/service/ResourceService.java index 6db2e0f6..a085c914 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/service/ResourceService.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/service/ResourceService.java @@ -147,6 +147,7 @@ public class ResourceService { public static final String SEB_CONNECTION_STATUS_KEY_PREFIX = "sebserver.monitoring.exam.connection.status."; public static final LocTextKey ACTIVE_TEXT_KEY = new LocTextKey("sebserver.overall.status.active"); public static final LocTextKey INACTIVE_TEXT_KEY = new LocTextKey("sebserver.overall.status.inactive"); + public static final LocTextKey NO_SELECTION = new LocTextKey("sebserver.overall.action.select.none"); private final I18nSupport i18nSupport; private final RestService restService; @@ -310,7 +311,8 @@ public class ResourceService { public List> lmsSetupResource() { final boolean isSEBAdmin = this.currentUser.get().hasRole(UserRole.SEB_SERVER_ADMIN); - final String institutionId = (isSEBAdmin) ? "" : String.valueOf(this.currentUser.get().institutionId); + final String institutionId = + (isSEBAdmin) ? StringUtils.EMPTY : String.valueOf(this.currentUser.get().institutionId); return this.restService.getBuilder(GetLmsSetupNames.class) .withQueryParam(Entity.FILTER_ATTR_INSTITUTION, institutionId) .withQueryParam(Entity.FILTER_ATTR_ACTIVE, Constants.TRUE_STRING) @@ -324,7 +326,8 @@ public class ResourceService { public Function getLmsSetupNameFunction() { final boolean isSEBAdmin = this.currentUser.get().hasRole(UserRole.SEB_SERVER_ADMIN); - final String institutionId = (isSEBAdmin) ? "" : String.valueOf(this.currentUser.get().institutionId); + final String institutionId = + (isSEBAdmin) ? StringUtils.EMPTY : String.valueOf(this.currentUser.get().institutionId); final Map idNameMap = this.restService.getBuilder(GetLmsSetupNames.class) .withQueryParam(Entity.FILTER_ATTR_INSTITUTION, institutionId) .withQueryParam(Entity.FILTER_ATTR_ACTIVE, Constants.TRUE_STRING) @@ -732,7 +735,7 @@ public class ResourceService { .map(node -> new Tuple<>(node.getModelId(), node.name)) .sorted(RESOURCE_COMPARATOR) .collect(Collectors.toList()); - collect.add(0, new Tuple<>(null, "")); + collect.add(0, new Tuple<>(null, StringUtils.EMPTY)); return collect; } @@ -788,7 +791,7 @@ public class ResourceService { public List> identityCertificatesResources() { return Stream.concat( - Stream.of(new EntityName("", EntityType.CERTIFICATE, "")), + Stream.of(new EntityName(StringUtils.EMPTY, EntityType.CERTIFICATE, StringUtils.EMPTY)), this.restService.getBuilder(GetCertificateNames.class) .withQueryParam( CertificateInfo.FILTER_ATTR_TYPE, @@ -802,8 +805,10 @@ public class ResourceService { } public List> examTemplateResources() { + return Stream.concat( - Stream.of(new EntityName("", EntityType.EXAM_TEMPLATE, "")), + Stream.of(new EntityName(StringUtils.EMPTY, EntityType.EXAM_TEMPLATE, + this.i18nSupport.getText(NO_SELECTION))), this.restService.getBuilder(GetExamTemplateNames.class) .call() .onError(error -> log.warn("Failed to get exam template names: {}", error.getMessage())) diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/exam/GetDefaultExamTemplate.java b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/exam/GetDefaultExamTemplate.java new file mode 100644 index 00000000..58ec7584 --- /dev/null +++ b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/api/exam/GetDefaultExamTemplate.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package ch.ethz.seb.sebserver.gui.service.remote.webservice.api.exam; + +import org.springframework.context.annotation.Lazy; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.core.type.TypeReference; + +import ch.ethz.seb.sebserver.gbl.api.API; +import ch.ethz.seb.sebserver.gbl.api.EntityType; +import ch.ethz.seb.sebserver.gbl.model.exam.ExamTemplate; +import ch.ethz.seb.sebserver.gbl.profile.GuiProfile; +import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.RestCall; + +@Lazy +@Component +@GuiProfile +public class GetDefaultExamTemplate extends RestCall { + + public GetDefaultExamTemplate() { + super(new TypeKey<>( + CallType.GET_SINGLE, + EntityType.EXAM_TEMPLATE, + new TypeReference() { + }), + HttpMethod.GET, + MediaType.APPLICATION_FORM_URLENCODED, + API.EXAM_TEMPLATE_ENDPOINT + API.EXAM_TEMPLATE_DEFAULT_PATH_SEGMENT); + } + +} diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamTemplateController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamTemplateController.java index 32d5b0d0..dfefafe5 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamTemplateController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamTemplateController.java @@ -49,6 +49,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.Authorization import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.UserService; import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionService; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.EntityDAO; +import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ExamTemplateDAO; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ResourceNotFoundException; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO; import ch.ethz.seb.sebserver.webservice.servicelayer.validation.BeanValidationService; @@ -77,6 +78,23 @@ public class ExamTemplateController extends EntityController