Search in sources :

Example 56 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.

the class NoPassedCoursesNotificationStrategy method sendNotifications.

@Override
public void sendNotifications() {
    List<SchoolDataIdentifier> studentsToNotify = getStudentsToNotify();
    for (SchoolDataIdentifier studentIdentifier : studentsToNotify) {
        UserEntity studentEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
        if (studentEntity != null) {
            Locale studentLocale = localeController.resolveLocale(LocaleUtils.toLocale(studentEntity.getLocale()));
            Map<String, Object> templateModel = new HashMap<>();
            templateModel.put("locale", studentLocale);
            templateModel.put("localeHelper", jadeLocaleHelper);
            String notificationContent = renderNotificationTemplate("no-passed-courses-notification", templateModel);
            notificationController.sendNotification(localeController.getText(studentLocale, "plugin.timednotifications.notification.category"), localeController.getText(studentLocale, "plugin.timednotifications.notification.nopassedcourses.subject"), notificationContent, studentEntity, studentIdentifier, "nopassedcourses");
            noPassedCoursesNotificationController.createNoPassedCoursesNotification(studentIdentifier);
        } else {
            logger.log(Level.SEVERE, String.format("Cannot send notification to student with identifier %s because UserEntity was not found", studentIdentifier.toId()));
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) Locale(java.util.Locale) HashMap(java.util.HashMap) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity)

Example 57 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier 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 58 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.

the class StudyTimeNotificationStrategy method getStudentIdentifiers.

private List<SchoolDataIdentifier> getStudentIdentifiers(SearchResult searchResult) {
    List<SchoolDataIdentifier> studentIdentifiers = new ArrayList<>();
    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;
        }
        studentIdentifiers.add(studentIdentifier);
    }
    return studentIdentifiers;
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) ArrayList(java.util.ArrayList)

Example 59 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.

the class StudyTimeNotificationStrategy method sendNotifications.

@Override
public void sendNotifications() {
    Collection<Long> groups = getGroups();
    if (groups.isEmpty()) {
        return;
    }
    OffsetDateTime studyTimeEndsOdt = OffsetDateTime.now().plusDays(NOTIFICATION_THRESHOLD_DAYS_LEFT);
    OffsetDateTime sendNotificationIfStudentStartedBefore = OffsetDateTime.now().minusDays(DAYS_UNTIL_FIRST_NOTIFICATION);
    Date studyTimeEnds = Date.from(studyTimeEndsOdt.toInstant());
    Date lastNotifiedThresholdDate = Date.from(OffsetDateTime.now().minusDays(NOTIFICATION_THRESHOLD_DAYS_LEFT + 1).toInstant());
    List<SchoolDataIdentifier> studentIdentifierAlreadyNotified = studyTimeLeftNotificationController.listNotifiedSchoolDataIdentifiersAfter(lastNotifiedThresholdDate);
    SearchResult searchResult = studyTimeLeftNotificationController.searchActiveStudentIds(groups, FIRST_RESULT + offset, MAX_RESULTS, studentIdentifierAlreadyNotified, studyTimeEnds);
    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 (SchoolDataIdentifier studentIdentifier : getStudentIdentifiers(searchResult)) {
        UserEntity studentEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
        if (studentEntity != null) {
            User student = userController.findUserByIdentifier(studentIdentifier);
            if (student.getStudyStartDate() == null || student.getStudyStartDate().isAfter(sendNotificationIfStudentStartedBefore)) {
                continue;
            }
            if (student.getStudyTimeEnd() == null || student.getStudyTimeEnd().isAfter(studyTimeEndsOdt) || student.getStudyTimeEnd().isBefore(OffsetDateTime.now())) {
                continue;
            }
            Locale studentLocale = localeController.resolveLocale(LocaleUtils.toLocale(studentEntity.getLocale()));
            Map<String, Object> templateModel = new HashMap<>();
            templateModel.put("internetixStudent", student.hasEvaluationFees());
            templateModel.put("locale", studentLocale);
            templateModel.put("localeHelper", jadeLocaleHelper);
            String notificationContent = renderNotificationTemplate("study-time-notification", templateModel);
            notificationController.sendNotification(localeController.getText(studentLocale, "plugin.timednotifications.notification.category"), localeController.getText(studentLocale, "plugin.timednotifications.notification.studytime.subject"), notificationContent, studentEntity, studentIdentifier, "studytime");
            studyTimeLeftNotificationController.createStudyTimeNotification(studentIdentifier);
        } else {
            logger.log(Level.SEVERE, String.format("Cannot send notification to student with identifier %s because UserEntity was not found", studentIdentifier.toId()));
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) Locale(java.util.Locale) User(fi.otavanopisto.muikku.schooldata.entity.User) HashMap(java.util.HashMap) SearchResult(fi.otavanopisto.muikku.search.SearchResult) Date(java.util.Date) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) OffsetDateTime(java.time.OffsetDateTime)

Example 60 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.

the class AssessmentRequestController method setCommunicatorMessageId.

public void setCommunicatorMessageId(WorkspaceAssessmentRequest assessmentRequest, CommunicatorMessageId communicatorMessageId) {
    SchoolDataIdentifier workspaceUserIdentifier = new SchoolDataIdentifier(assessmentRequest.getWorkspaceUserIdentifier(), assessmentRequest.getWorkspaceUserSchoolDataSource());
    WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifier(workspaceUserIdentifier);
    AssessmentRequestMessageId requestMessageId = assessmentRequestMessageIdDAO.findByWorkspaceUser(workspaceUserEntity);
    if (requestMessageId == null)
        assessmentRequestMessageIdDAO.create(workspaceUserEntity, communicatorMessageId);
    else
        assessmentRequestMessageIdDAO.updateMessageId(requestMessageId, communicatorMessageId);
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)

Aggregations

SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)130 Path (javax.ws.rs.Path)63 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)59 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)58 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)53 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)50 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)48 ArrayList (java.util.ArrayList)38 GET (javax.ws.rs.GET)36 User (fi.otavanopisto.muikku.schooldata.entity.User)30 HashMap (java.util.HashMap)19 Workspace (fi.otavanopisto.muikku.schooldata.entity.Workspace)13 SearchResult (fi.otavanopisto.muikku.search.SearchResult)13 WorkspaceUser (fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser)12 Date (java.util.Date)11 POST (javax.ws.rs.POST)11 GradingScaleItem (fi.otavanopisto.muikku.schooldata.entity.GradingScaleItem)10 WorkspaceAssessment (fi.otavanopisto.muikku.schooldata.entity.WorkspaceAssessment)10 HashSet (java.util.HashSet)9 PUT (javax.ws.rs.PUT)9