Search in sources :

Example 1 with GradeDTO

use of de.tum.in.www1.artemis.web.rest.dto.GradeDTO in project ArTEMiS by ls1intum.

the class GradeStepIntegrationTest method testGetGradeStepByPercentageForExam.

/**
 * Test get request for a single grade by grade percentage
 *
 * @throws Exception
 */
@Test
@WithMockUser(username = "student1", roles = "USER")
public void testGetGradeStepByPercentageForExam() throws Exception {
    GradeStep gradeStep = new GradeStep();
    gradeStep.setGradeName("Test grade");
    gradeStep.setLowerBoundPercentage(0);
    gradeStep.setUpperBoundPercentage(40);
    gradeStep.setIsPassingGrade(false);
    gradeStep.setGradingScale(examGradingScale);
    gradeSteps = Set.of(gradeStep);
    examGradingScale.setGradeSteps(gradeSteps);
    examGradingScale.setGradeType(GradeType.BONUS);
    gradingScaleRepository.save(examGradingScale);
    exam.setPublishResultsDate(ZonedDateTime.now());
    examRepository.save(exam);
    GradeDTO foundGrade = request.get("/api/courses/" + course.getId() + "/exams/" + exam.getId() + "/grading-scale/match-grade-step?gradePercentage=35", HttpStatus.OK, GradeDTO.class);
    assertThat(foundGrade.gradeName).isEqualTo("Test grade");
    assertThat(foundGrade.gradeType).isEqualTo(GradeType.BONUS);
    assertThat(foundGrade.isPassingGrade).isFalse();
}
Also used : GradeDTO(de.tum.in.www1.artemis.web.rest.dto.GradeDTO) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 2 with GradeDTO

use of de.tum.in.www1.artemis.web.rest.dto.GradeDTO in project ArTEMiS by ls1intum.

the class GradeStepResource method getGradeStepByPercentageForExam.

/**
 * GET /courses/{courseId}/exams/{examId}/grading-scale/grade-steps/match-grade-step : Find a grade step for the grading scale of a course by grade percentage
 *
 * @param courseId the course to which the exam belongs
 * @param examId the exam to which the grading scale belongs
 * @param gradePercentage the grade percentage the has to be mapped to a grade step
 * @return ResponseEntity with status 200 (Ok) with body the grade if the grading scale and grade step exist and 404 (Not found) otherwise
 */
@GetMapping("/courses/{courseId}/exams/{examId}/grading-scale/match-grade-step")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<GradeDTO> getGradeStepByPercentageForExam(@PathVariable Long courseId, @PathVariable Long examId, @RequestParam Double gradePercentage) {
    log.debug("REST request to get grade step for grade percentage {} for exam: {}", gradePercentage, examId);
    User user = userRepository.getUserWithGroupsAndAuthorities();
    Course course = courseRepository.findByIdElseThrow(courseId);
    Exam exam = examRepository.findByIdElseThrow(examId);
    Optional<GradingScale> gradingScale = gradingScaleRepository.findByExamId(examId);
    authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.STUDENT, course, user);
    boolean isInstructor = authCheckService.isAtLeastInstructorInCourse(course, user);
    if (gradingScale.isEmpty()) {
        return ResponseEntity.ok(null);
    } else if (!isInstructor && !exam.resultsPublished()) {
        throw new AccessForbiddenException();
    }
    GradeStep gradeStep = gradingScaleRepository.matchPercentageToGradeStep(gradePercentage, gradingScale.get().getId());
    GradeDTO gradeDTO = new GradeDTO(gradeStep.getGradeName(), gradeStep.getIsPassingGrade(), gradeStep.getGradingScale().getGradeType());
    return ResponseEntity.ok(gradeDTO);
}
Also used : GradingScale(de.tum.in.www1.artemis.domain.GradingScale) User(de.tum.in.www1.artemis.domain.User) GradeDTO(de.tum.in.www1.artemis.web.rest.dto.GradeDTO) GradeStep(de.tum.in.www1.artemis.domain.GradeStep) Course(de.tum.in.www1.artemis.domain.Course) Exam(de.tum.in.www1.artemis.domain.exam.Exam) AccessForbiddenException(de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with GradeDTO

use of de.tum.in.www1.artemis.web.rest.dto.GradeDTO in project Artemis by ls1intum.

the class GradeStepResource method getGradeStepByPercentageForExam.

/**
 * GET /courses/{courseId}/exams/{examId}/grading-scale/grade-steps/match-grade-step : Find a grade step for the grading scale of a course by grade percentage
 *
 * @param courseId the course to which the exam belongs
 * @param examId the exam to which the grading scale belongs
 * @param gradePercentage the grade percentage the has to be mapped to a grade step
 * @return ResponseEntity with status 200 (Ok) with body the grade if the grading scale and grade step exist and 404 (Not found) otherwise
 */
@GetMapping("/courses/{courseId}/exams/{examId}/grading-scale/match-grade-step")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<GradeDTO> getGradeStepByPercentageForExam(@PathVariable Long courseId, @PathVariable Long examId, @RequestParam Double gradePercentage) {
    log.debug("REST request to get grade step for grade percentage {} for exam: {}", gradePercentage, examId);
    User user = userRepository.getUserWithGroupsAndAuthorities();
    Course course = courseRepository.findByIdElseThrow(courseId);
    Exam exam = examRepository.findByIdElseThrow(examId);
    Optional<GradingScale> gradingScale = gradingScaleRepository.findByExamId(examId);
    authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.STUDENT, course, user);
    boolean isInstructor = authCheckService.isAtLeastInstructorInCourse(course, user);
    if (gradingScale.isEmpty()) {
        return ResponseEntity.ok(null);
    } else if (!isInstructor && !exam.resultsPublished()) {
        throw new AccessForbiddenException();
    }
    GradeStep gradeStep = gradingScaleRepository.matchPercentageToGradeStep(gradePercentage, gradingScale.get().getId());
    GradeDTO gradeDTO = new GradeDTO(gradeStep.getGradeName(), gradeStep.getIsPassingGrade(), gradeStep.getGradingScale().getGradeType());
    return ResponseEntity.ok(gradeDTO);
}
Also used : GradingScale(de.tum.in.www1.artemis.domain.GradingScale) User(de.tum.in.www1.artemis.domain.User) GradeDTO(de.tum.in.www1.artemis.web.rest.dto.GradeDTO) GradeStep(de.tum.in.www1.artemis.domain.GradeStep) Course(de.tum.in.www1.artemis.domain.Course) Exam(de.tum.in.www1.artemis.domain.exam.Exam) AccessForbiddenException(de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with GradeDTO

use of de.tum.in.www1.artemis.web.rest.dto.GradeDTO in project Artemis by ls1intum.

the class GradeStepIntegrationTest method testGetGradeStepByPercentageForExam.

/**
 * Test get request for a single grade by grade percentage
 *
 * @throws Exception
 */
@Test
@WithMockUser(username = "student1", roles = "USER")
public void testGetGradeStepByPercentageForExam() throws Exception {
    GradeStep gradeStep = new GradeStep();
    gradeStep.setGradeName("Test grade");
    gradeStep.setLowerBoundPercentage(0);
    gradeStep.setUpperBoundPercentage(40);
    gradeStep.setIsPassingGrade(false);
    gradeStep.setGradingScale(examGradingScale);
    gradeSteps = Set.of(gradeStep);
    examGradingScale.setGradeSteps(gradeSteps);
    examGradingScale.setGradeType(GradeType.BONUS);
    gradingScaleRepository.save(examGradingScale);
    exam.setPublishResultsDate(ZonedDateTime.now());
    examRepository.save(exam);
    GradeDTO foundGrade = request.get("/api/courses/" + course.getId() + "/exams/" + exam.getId() + "/grading-scale/match-grade-step?gradePercentage=35", HttpStatus.OK, GradeDTO.class);
    assertThat(foundGrade.gradeName).isEqualTo("Test grade");
    assertThat(foundGrade.gradeType).isEqualTo(GradeType.BONUS);
    assertThat(foundGrade.isPassingGrade).isFalse();
}
Also used : GradeDTO(de.tum.in.www1.artemis.web.rest.dto.GradeDTO) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 5 with GradeDTO

use of de.tum.in.www1.artemis.web.rest.dto.GradeDTO in project ArTEMiS by ls1intum.

the class GradeStepResource method getGradeStepByPercentageForCourse.

/**
 * GET /courses/{courseId}/grading-scale/grade-steps/match-grade-step : Find a grade step for the grading scale of a course by grade percentage
 *
 * @param courseId the course to which the grading scale belongs
 * @param gradePercentage the grade percentage the has to be mapped to a grade step
 * @return ResponseEntity with status 200 (Ok) with body the grade if the grading scale and grade step exist and 404 (Not found) otherwise
 */
@GetMapping("/courses/{courseId}/grading-scale/match-grade-step")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<GradeDTO> getGradeStepByPercentageForCourse(@PathVariable Long courseId, @RequestParam Double gradePercentage) {
    log.debug("REST request to get grade step for grade percentage {} for course: {}", gradePercentage, courseId);
    Course course = courseRepository.findByIdElseThrow(courseId);
    Optional<GradingScale> gradingScale = gradingScaleRepository.findByCourseId(courseId);
    authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.STUDENT, course, null);
    if (gradingScale.isEmpty()) {
        return ResponseEntity.ok(null);
    }
    GradeStep gradeStep = gradingScaleRepository.matchPercentageToGradeStep(gradePercentage, gradingScale.get().getId());
    GradeDTO gradeDTO = new GradeDTO(gradeStep.getGradeName(), gradeStep.getIsPassingGrade(), gradeStep.getGradingScale().getGradeType());
    return ResponseEntity.ok(gradeDTO);
}
Also used : GradingScale(de.tum.in.www1.artemis.domain.GradingScale) GradeDTO(de.tum.in.www1.artemis.web.rest.dto.GradeDTO) GradeStep(de.tum.in.www1.artemis.domain.GradeStep) Course(de.tum.in.www1.artemis.domain.Course) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

GradeDTO (de.tum.in.www1.artemis.web.rest.dto.GradeDTO)8 Course (de.tum.in.www1.artemis.domain.Course)4 GradeStep (de.tum.in.www1.artemis.domain.GradeStep)4 GradingScale (de.tum.in.www1.artemis.domain.GradingScale)4 Test (org.junit.jupiter.api.Test)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 WithMockUser (org.springframework.security.test.context.support.WithMockUser)4 User (de.tum.in.www1.artemis.domain.User)2 Exam (de.tum.in.www1.artemis.domain.exam.Exam)2 AccessForbiddenException (de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException)2