Search in sources :

Example 11 with SupplementationRequest

use of fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest in project muikku by otavanopisto.

the class RequestedAssessmentSupplementationsNotificationStrategy method sendNotifications.

@Override
public void sendNotifications() {
    Collection<Long> groups = getGroups();
    if (groups.isEmpty()) {
        return;
    }
    // Iterate through active students who belong to pre-configured groups (read: study programs)
    SearchResult searchResult = requestedAssessmentSupplementationsNotificationController.searchActiveStudentIds(groups, FIRST_RESULT + offset, MAX_RESULTS);
    logger.log(Level.INFO, String.format("%s processing %d/%d", getClass().getSimpleName(), offset, searchResult.getTotalHitCount()));
    if ((offset + MAX_RESULTS) > searchResult.getTotalHitCount()) {
        offset = 0;
    } else {
        offset += MAX_RESULTS;
    }
    for (Map<String, Object> result : searchResult.getResults()) {
        String studentId = (String) result.get("id");
        if (StringUtils.isBlank(studentId)) {
            logger.severe("Could not process user found from search index because it had a null id");
            continue;
        }
        String[] studentIdParts = studentId.split("/", 2);
        SchoolDataIdentifier studentIdentifier = studentIdParts.length == 2 ? new SchoolDataIdentifier(studentIdParts[0], studentIdParts[1]) : null;
        if (studentIdentifier == null) {
            logger.severe(String.format("Could not process user found from search index with id %s", studentId));
            continue;
        }
        UserEntity studentEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
        if (studentEntity == null) {
            logger.severe(String.format("UserEntity with identifier %s not found", studentIdentifier));
            continue;
        }
        // Iterate through the workspaces in which the student is currently active
        List<WorkspaceEntity> workspaceEntities = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserIdentifier(studentIdentifier);
        for (WorkspaceEntity workspaceEntity : workspaceEntities) {
            SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(workspaceEntity.getIdentifier(), workspaceEntity.getDataSource().getIdentifier());
            if (requestedAssessmentSupplementationsNotificationController.countByStudentIdentifierAndWorkspaceIdentifier(studentIdentifier, workspaceIdentifier) == 0) {
                // Skip if workspace doesn't have a supplementation request
                SupplementationRequest supplementationRequest = supplementationRequestDAO.findByStudentAndWorkspace(studentEntity.getId(), workspaceEntity.getId());
                if (supplementationRequest == null) {
                    continue;
                }
                // Skip if workspace assessment is newer than supplementation request
                WorkspaceAssessment workspaceAssessment = gradingController.findLatestWorkspaceAssessment(workspaceIdentifier, studentIdentifier);
                if (workspaceAssessment != null && workspaceAssessment.getDate().getTime() >= supplementationRequest.getRequestDate().getTime()) {
                    continue;
                }
                // Skip if assessment request is newer than supplementation request
                // TODO: At some point, refactor to simply fetch latest request by student + workspace
                WorkspaceAssessmentRequest latestAssesmentRequest = null;
                List<WorkspaceAssessmentRequest> studentAssesmentRequests = gradingController.listWorkspaceAssessmentRequests(workspaceIdentifier.getDataSource(), workspaceIdentifier.getIdentifier(), studentIdentifier.getIdentifier());
                for (WorkspaceAssessmentRequest assessmentRequest : studentAssesmentRequests) {
                    Date assessmentRequestDate = assessmentRequest.getDate();
                    if (assessmentRequestDate != null) {
                        if (latestAssesmentRequest == null || latestAssesmentRequest.getDate().before(assessmentRequestDate)) {
                            latestAssesmentRequest = assessmentRequest;
                        }
                    }
                }
                if (latestAssesmentRequest != null && latestAssesmentRequest.getDate().getTime() >= supplementationRequest.getRequestDate().getTime()) {
                    continue;
                }
                if (!supplementationRequest.getRequestDate().before(Date.from(OffsetDateTime.now().minusDays(NOTIFICATION_THRESHOLD_DAYS).toInstant()))) {
                    continue;
                }
                // If we haven't skipped so far, student needs to be notified
                Workspace workspace = workspaceController.findWorkspace(workspaceIdentifier);
                if (workspace != null) {
                    String workspaceName = StringUtils.isBlank(workspace.getNameExtension()) ? workspace.getName() : String.format("%s (%s)", workspace.getName(), workspace.getNameExtension());
                    Locale studentLocale = localeController.resolveLocale(LocaleUtils.toLocale(studentEntity.getLocale()));
                    Map<String, Object> templateModel = new HashMap<>();
                    templateModel.put("workspaceName", workspaceName);
                    templateModel.put("locale", studentLocale);
                    templateModel.put("localeHelper", jadeLocaleHelper);
                    String notificationContent = renderNotificationTemplate("requested-assessment-supplementation-notification", templateModel);
                    notificationController.sendNotification(localeController.getText(studentLocale, "plugin.timednotifications.notification.category"), localeController.getText(studentLocale, "plugin.timednotifications.notification.requestedassessmentsupplementation.subject"), notificationContent, studentEntity, studentIdentifier, "requestedassessmentsupplementation");
                    // Store notification to avoid duplicates in the future
                    requestedAssessmentSupplementationsNotificationController.createRequestedAssessmentSupplementationNotification(studentIdentifier, workspaceIdentifier);
                } else {
                    logger.log(Level.SEVERE, String.format("Cannot send notification to student with identifier %s because UserEntity or workspace was not found", studentIdentifier.toId()));
                }
            }
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) Locale(java.util.Locale) WorkspaceAssessment(fi.otavanopisto.muikku.schooldata.entity.WorkspaceAssessment) HashMap(java.util.HashMap) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Date(java.util.Date) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceAssessmentRequest(fi.otavanopisto.muikku.schooldata.entity.WorkspaceAssessmentRequest) SupplementationRequest(fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace)

Example 12 with SupplementationRequest

use of fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest in project muikku by otavanopisto.

the class SupplementationRequestDAO method findByStudentAndWorkspaceMaterialAndArchived.

public SupplementationRequest findByStudentAndWorkspaceMaterialAndArchived(Long studentEntityId, Long workspaceMaterialId, Boolean archived) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<SupplementationRequest> criteria = criteriaBuilder.createQuery(SupplementationRequest.class);
    Root<SupplementationRequest> root = criteria.from(SupplementationRequest.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(SupplementationRequest_.studentEntityId), studentEntityId), criteriaBuilder.equal(root.get(SupplementationRequest_.workspaceMaterialId), workspaceMaterialId), criteriaBuilder.equal(root.get(SupplementationRequest_.archived), archived)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) SupplementationRequest(fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest)

Example 13 with SupplementationRequest

use of fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest in project muikku by otavanopisto.

the class SupplementationRequestDAO method createSupplementationRequest.

public SupplementationRequest createSupplementationRequest(Long userEntityId, Long studentEntityId, Long workspaceEntityId, Long workspaceMaterialId, Date requestDate, String requestText) {
    SupplementationRequest supplementationRequest = new SupplementationRequest();
    supplementationRequest.setUserEntityId(userEntityId);
    supplementationRequest.setStudentEntityId(studentEntityId);
    supplementationRequest.setWorkspaceEntityId(workspaceEntityId);
    supplementationRequest.setWorkspaceMaterialId(workspaceMaterialId);
    supplementationRequest.setRequestDate(requestDate);
    supplementationRequest.setRequestText(requestText);
    supplementationRequest.setArchived(Boolean.FALSE);
    return persist(supplementationRequest);
}
Also used : SupplementationRequest(fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest)

Example 14 with SupplementationRequest

use of fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest in project muikku by otavanopisto.

the class SupplementationRequestDAO method findByStudentAndWorkspaceAndArchived.

public SupplementationRequest findByStudentAndWorkspaceAndArchived(Long studentEntityId, Long workspaceEntityId, Boolean archived) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<SupplementationRequest> criteria = criteriaBuilder.createQuery(SupplementationRequest.class);
    Root<SupplementationRequest> root = criteria.from(SupplementationRequest.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(SupplementationRequest_.studentEntityId), studentEntityId), criteriaBuilder.equal(root.get(SupplementationRequest_.workspaceEntityId), workspaceEntityId), criteriaBuilder.equal(root.get(SupplementationRequest_.archived), archived)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) SupplementationRequest(fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest)

Example 15 with SupplementationRequest

use of fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest in project muikku by otavanopisto.

the class Evaluation2RESTService method findWorkspaceMaterialSupplementationRequest.

@GET
@Path("/workspace/{WORKSPACEENTITYID}/user/{USERENTITYID}/workspacematerial/{WORKSPACEMATERIALID}/supplementationrequest")
@RESTPermit(handling = Handling.INLINE)
public Response findWorkspaceMaterialSupplementationRequest(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("USERENTITYID") Long userEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.ACCESS_EVALUATION)) {
        // Allow students to access their own supplementation requests
        if (!sessionController.getLoggedUserEntity().getId().equals(userEntityId)) {
            return Response.status(Status.FORBIDDEN).build();
        }
    }
    // User entity
    UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
    if (userEntity == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    // Workspace material
    WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
    if (workspaceMaterial == null) {
        return Response.status(Status.NOT_FOUND).entity("workspaceMaterial not found").build();
    }
    // Supplementation request
    SupplementationRequest supplementationRequest = evaluationController.findSupplementationRequestByStudentAndWorkspaceMaterial(userEntityId, workspaceMaterialId);
    if (supplementationRequest == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    // SupplementationRequest to RestSupplementationRequest
    RestSupplementationRequest restSupplementationRequest = new RestSupplementationRequest(supplementationRequest.getId(), supplementationRequest.getUserEntityId(), supplementationRequest.getStudentEntityId(), supplementationRequest.getWorkspaceEntityId(), supplementationRequest.getWorkspaceMaterialId(), supplementationRequest.getRequestDate(), supplementationRequest.getRequestText());
    return Response.ok(restSupplementationRequest).build();
}
Also used : RestSupplementationRequest(fi.otavanopisto.muikku.plugins.evaluation.rest.model.RestSupplementationRequest) SupplementationRequest(fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest) RestSupplementationRequest(fi.otavanopisto.muikku.plugins.evaluation.rest.model.RestSupplementationRequest) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

SupplementationRequest (fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest)16 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)10 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)9 RestSupplementationRequest (fi.otavanopisto.muikku.plugins.evaluation.rest.model.RestSupplementationRequest)9 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)8 Path (javax.ws.rs.Path)8 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)7 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)5 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)5 WorkspaceAssessment (fi.otavanopisto.muikku.schooldata.entity.WorkspaceAssessment)4 EntityManager (javax.persistence.EntityManager)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 POST (javax.ws.rs.POST)4 WorkspaceMaterialEvaluation (fi.otavanopisto.muikku.plugins.evaluation.model.WorkspaceMaterialEvaluation)3 WorkspaceGradingScale (fi.otavanopisto.muikku.plugins.evaluation.rest.model.WorkspaceGradingScale)3 CompositeGradingScale (fi.otavanopisto.muikku.schooldata.entity.CompositeGradingScale)3 GradingScale (fi.otavanopisto.muikku.schooldata.entity.GradingScale)3 GradingScaleItem (fi.otavanopisto.muikku.schooldata.entity.GradingScaleItem)3 Date (java.util.Date)3 GET (javax.ws.rs.GET)3