Search in sources :

Example 71 with SchoolDataIdentifier

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

the class TranscriptofRecordsRESTService method retrieveForm.

@GET
@Path("/hops/{USERIDENTIFIER}")
@RESTPermit(handling = Handling.INLINE)
public Response retrieveForm(@PathParam("USERIDENTIFIER") String userIdentifierString) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).entity("Must be logged in").build();
    }
    SchoolDataIdentifier userIdentifier = SchoolDataIdentifier.fromId(userIdentifierString);
    if (userIdentifier == null) {
        return Response.status(Status.BAD_REQUEST).entity("Malformed identifier").build();
    }
    UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(userIdentifier);
    if (userEntity == null) {
        return Response.status(Status.NOT_FOUND).entity("User not found").build();
    }
    User user = userController.findUserByIdentifier(userIdentifier);
    if (!vopsController.shouldShowStudies(user)) {
        return Response.ok(HopsRESTModel.nonOptedInHopsRESTModel()).build();
    }
    if (!sessionController.hasEnvironmentPermission(TranscriptofRecordsPermissions.TRANSCRIPT_OF_RECORDS_VIEW_ANY_STUDENT_HOPS_FORM) && !Objects.equals(sessionController.getLoggedUser(), userIdentifier)) {
        return Response.status(Status.FORBIDDEN).entity("Can only look at own information").build();
    }
    HopsRESTModel response = createHopsRESTModelForStudent(userIdentifier);
    if (response == null) {
        return Response.status(Status.NOT_FOUND).entity("No HOPS form for non-students").build();
    }
    return Response.ok(response).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 72 with SchoolDataIdentifier

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

the class TranscriptofRecordsRESTService method planCourse.

@POST
@Path("/plannedCourses/")
@RESTPermit(handling = Handling.INLINE)
public Response planCourse(VopsPlannedCourseRESTModel model) {
    SchoolDataIdentifier loggedUserIdentifier = sessionController.getLoggedUser();
    boolean hasPermission = Objects.equals(loggedUserIdentifier.toId(), model.getStudentIdentifier());
    if (!hasPermission) {
        return Response.status(Status.FORBIDDEN).entity("You don't have the permission to access this").build();
    }
    StudiesViewCourseChoice choice = studiesViewCourseChoiceController.find(model.getSubjectIdentifier(), model.getCourseNumber(), model.getStudentIdentifier());
    if (choice == null) {
        studiesViewCourseChoiceController.create(model.getSubjectIdentifier(), model.getCourseNumber(), model.getStudentIdentifier());
    }
    return Response.ok(model).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) StudiesViewCourseChoice(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.StudiesViewCourseChoice) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 73 with SchoolDataIdentifier

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

the class VopsLister method performListing.

public void performListing() {
    for (Subject subject : subjects) {
        boolean subjectHasCourses = false;
        if (vopsController.subjectAppliesToStudent(student, subject)) {
            List<VopsRESTModel.VopsEntry> entries = new ArrayList<>();
            for (int courseNumber = 1; courseNumber <= MAX_COURSE_NUMBER; courseNumber++) {
                VopsRESTModel.VopsEntry entry = processCourse(subject, courseNumber);
                entries.add(entry);
                if (!(entry instanceof VopsRESTModel.VopsPlaceholder)) {
                    subjectHasCourses = true;
                }
            }
            if (subjectHasCourses) {
                rows.add(new VopsRESTModel.VopsRow(subject.getCode(), new SchoolDataIdentifier(subject.getIdentifier(), subject.getSchoolDataSource()).toId(), entries));
            }
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) VopsRow(fi.otavanopisto.muikku.plugins.transcriptofrecords.rest.VopsRESTModel.VopsRow) ArrayList(java.util.ArrayList) VopsRESTModel(fi.otavanopisto.muikku.plugins.transcriptofrecords.rest.VopsRESTModel) Subject(fi.otavanopisto.muikku.schooldata.entity.Subject)

Example 74 with SchoolDataIdentifier

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

the class CoursePickerRESTService method listWorkspaces.

@GET
@Path("/workspaces/")
@RESTPermitUnimplemented
public Response listWorkspaces(@QueryParam("search") String searchString, @QueryParam("subjects") List<String> subjects, @QueryParam("educationTypes") List<String> educationTypeIds, @QueryParam("curriculums") List<String> curriculumIds, @QueryParam("minVisits") Long minVisits, @QueryParam("includeUnpublished") @DefaultValue("false") Boolean includeUnpublished, @QueryParam("myWorkspaces") @DefaultValue("false") Boolean myWorkspaces, @QueryParam("orderBy") List<String> orderBy, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("50") Integer maxResults, @Context Request request) {
    List<CoursePickerWorkspace> workspaces = new ArrayList<>();
    boolean doMinVisitFilter = minVisits != null;
    UserEntity userEntity = myWorkspaces ? sessionController.getLoggedUserEntity() : null;
    List<WorkspaceEntity> workspaceEntities = null;
    String schoolDataSourceFilter = null;
    List<String> workspaceIdentifierFilters = null;
    if (doMinVisitFilter) {
        if (userEntity != null) {
            workspaceEntities = workspaceVisitController.listWorkspaceEntitiesByMinVisitsOrderByLastVisit(userEntity, minVisits);
        } else {
            workspaceEntities = workspaceVisitController.listWorkspaceEntitiesByMinVisitsOrderByLastVisit(sessionController.getLoggedUserEntity(), minVisits);
        }
    } else {
        if (userEntity != null) {
            workspaceEntities = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserEntity(userEntity);
        }
    }
    Iterator<SearchProvider> searchProviderIterator = searchProviders.iterator();
    if (searchProviderIterator.hasNext()) {
        SearchProvider searchProvider = searchProviderIterator.next();
        SearchResult searchResult = null;
        if (workspaceEntities != null) {
            workspaceIdentifierFilters = new ArrayList<>();
            for (WorkspaceEntity workspaceEntity : workspaceEntities) {
                if (schoolDataSourceFilter == null) {
                    schoolDataSourceFilter = workspaceEntity.getDataSource().getIdentifier();
                }
                workspaceIdentifierFilters.add(workspaceEntity.getIdentifier());
            }
        }
        List<WorkspaceAccess> accesses = new ArrayList<>(Arrays.asList(WorkspaceAccess.ANYONE));
        if (sessionController.isLoggedIn()) {
            accesses.add(WorkspaceAccess.LOGGED_IN);
            accesses.add(WorkspaceAccess.MEMBERS_ONLY);
        }
        List<Sort> sorts = null;
        if (orderBy != null && orderBy.contains("alphabet")) {
            sorts = new ArrayList<>();
            sorts.add(new Sort("name.untouched", Sort.Order.ASC));
        }
        List<SchoolDataIdentifier> educationTypes = null;
        if (educationTypeIds != null) {
            educationTypes = new ArrayList<>(educationTypeIds.size());
            for (String educationTypeId : educationTypeIds) {
                SchoolDataIdentifier educationTypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
                if (educationTypeIdentifier != null) {
                    educationTypes.add(educationTypeIdentifier);
                } else {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed education type identifier", educationTypeId)).build();
                }
            }
        }
        List<SchoolDataIdentifier> curriculumIdentifiers = null;
        if (curriculumIds != null) {
            curriculumIdentifiers = new ArrayList<>(curriculumIds.size());
            for (String curriculumId : curriculumIds) {
                SchoolDataIdentifier curriculumIdentifier = SchoolDataIdentifier.fromId(curriculumId);
                if (curriculumIdentifier != null) {
                    curriculumIdentifiers.add(curriculumIdentifier);
                } else {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed curriculum identifier", curriculumId)).build();
                }
            }
        }
        searchResult = searchProvider.searchWorkspaces(schoolDataSourceFilter, subjects, workspaceIdentifierFilters, educationTypes, curriculumIdentifiers, searchString, accesses, sessionController.getLoggedUser(), includeUnpublished, firstResult, maxResults, sorts);
        schoolDataBridgeSessionController.startSystemSession();
        try {
            List<Map<String, Object>> results = searchResult.getResults();
            for (Map<String, Object> result : results) {
                String searchId = (String) result.get("id");
                if (StringUtils.isNotBlank(searchId)) {
                    String[] id = searchId.split("/", 2);
                    if (id.length == 2) {
                        String dataSource = id[1];
                        String identifier = id[0];
                        SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(identifier, dataSource);
                        WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByDataSourceAndIdentifier(workspaceIdentifier.getDataSource(), workspaceIdentifier.getIdentifier());
                        if (workspaceEntity != null) {
                            String name = (String) result.get("name");
                            String nameExtension = (String) result.get("nameExtension");
                            String description = (String) result.get("description");
                            boolean canSignup = getCanSignup(workspaceEntity);
                            boolean isCourseMember = getIsAlreadyOnWorkspace(workspaceEntity);
                            String educationTypeId = (String) result.get("educationTypeIdentifier");
                            String educationTypeName = null;
                            if (StringUtils.isNotBlank(educationTypeId)) {
                                EducationType educationType = null;
                                SchoolDataIdentifier educationTypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
                                if (educationTypeIdentifier == null) {
                                    logger.severe(String.format("Malformatted educationTypeIdentifier %s", educationTypeId));
                                } else {
                                    educationType = courseMetaController.findEducationType(educationTypeIdentifier.getDataSource(), educationTypeIdentifier.getIdentifier());
                                }
                                if (educationType != null) {
                                    educationTypeName = educationType.getName();
                                }
                            }
                            if (StringUtils.isNotBlank(name)) {
                                workspaces.add(createRestModel(workspaceEntity, name, nameExtension, description, educationTypeName, canSignup, isCourseMember));
                            } else {
                                logger.severe(String.format("Search index contains workspace %s that does not have a name", workspaceIdentifier));
                            }
                        } else {
                            logger.severe(String.format("Search index contains workspace %s that does not exits on the school data system", workspaceIdentifier));
                        }
                    }
                }
            }
        } finally {
            schoolDataBridgeSessionController.endSystemSession();
        }
    } else {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
    if (workspaces.isEmpty()) {
        return Response.noContent().build();
    }
    if (orderBy.contains("lastVisit")) {
        Collections.sort(workspaces, new Comparator<CoursePickerWorkspace>() {

            @Override
            public int compare(CoursePickerWorkspace workspace1, CoursePickerWorkspace workspace2) {
                if (workspace1.getLastVisit() == null || workspace2.getLastVisit() == null) {
                    return 0;
                }
                if (workspace1.getLastVisit().before(workspace2.getLastVisit())) {
                    return 1;
                }
                if (workspace1.getLastVisit().after(workspace2.getLastVisit())) {
                    return -1;
                }
                return 0;
            }
        });
    }
    return Response.ok(workspaces).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) EducationType(fi.otavanopisto.muikku.schooldata.entity.EducationType) ArrayList(java.util.ArrayList) Sort(fi.otavanopisto.muikku.search.SearchProvider.Sort) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceAccess(fi.otavanopisto.muikku.model.workspace.WorkspaceAccess) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) Map(java.util.Map) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 75 with SchoolDataIdentifier

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

the class CoursePickerRESTService method createWorkspaceUser.

@POST
@Path("/workspaces/{ID}/signup")
@RESTPermit(handling = Handling.INLINE)
public Response createWorkspaceUser(@PathParam("ID") Long workspaceEntityId, fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceUserSignup entity) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.WORKSPACE_SIGNUP, workspaceEntity)) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    User user = userController.findUserByDataSourceAndIdentifier(sessionController.getLoggedUserSchoolDataSource(), sessionController.getLoggedUserIdentifier());
    Long workspaceStudentRoleId = getWorkspaceStudentRoleId();
    WorkspaceRoleEntity workspaceRole = roleController.findWorkspaceRoleEntityById(workspaceStudentRoleId);
    Workspace workspace = workspaceController.findWorkspace(workspaceEntity);
    Role role = roleController.findRoleByDataSourceAndRoleEntity(user.getSchoolDataSource(), workspaceRole);
    SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(workspace.getIdentifier(), workspace.getSchoolDataSource());
    SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
    WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserIdentifierIncludeArchived(workspaceEntity, userIdentifier);
    if (workspaceUserEntity != null && Boolean.TRUE.equals(workspaceUserEntity.getArchived())) {
        workspaceUserEntityController.unarchiveWorkspaceUserEntity(workspaceUserEntity);
    }
    if (workspaceUserEntity != null && Boolean.FALSE.equals(workspaceUserEntity.getActive())) {
        workspaceUserEntityController.updateActive(workspaceUserEntity, Boolean.TRUE);
        userIndexer.indexUser(workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity());
    }
    fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser workspaceUser = workspaceController.findWorkspaceUserByWorkspaceAndUser(workspaceIdentifier, userIdentifier);
    if (workspaceUser == null) {
        workspaceUser = workspaceController.createWorkspaceUser(workspace, user, role);
        waitForWorkspaceUserEntity(workspaceEntity, userIdentifier);
    } else {
        workspaceController.updateWorkspaceStudentActivity(workspaceUser, true);
    }
    // TODO: should this work based on permission? Permission -> Roles -> Recipients
    // TODO: Messaging should be moved into a CDI event listener
    List<WorkspaceUserEntity> workspaceTeachers = workspaceUserEntityController.listActiveWorkspaceStaffMembers(workspaceEntity);
    List<UserEntity> teachers = new ArrayList<UserEntity>();
    String workspaceName = workspace.getName();
    if (!StringUtils.isBlank(workspace.getNameExtension())) {
        workspaceName += String.format(" (%s)", workspace.getNameExtension());
    }
    String userName = user.getNickName() == null ? user.getDisplayName() : String.format("%s \"%s\" %s (%s)", user.getFirstName(), user.getNickName(), user.getLastName(), user.getStudyProgrammeName());
    for (WorkspaceUserEntity workspaceTeacher : workspaceTeachers) {
        teachers.add(workspaceTeacher.getUserSchoolDataIdentifier().getUserEntity());
    }
    UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierBySchoolDataIdentifier(userIdentifier);
    workspaceController.createWorkspaceUserSignup(workspaceEntity, userSchoolDataIdentifier.getUserEntity(), new Date(), entity.getMessage());
    String caption = localeController.getText(sessionController.getLocale(), "rest.workspace.joinWorkspace.joinNotification.caption");
    caption = MessageFormat.format(caption, workspaceName);
    String workspaceLink = String.format("<a href=\"%s/workspace/%s\" >%s</a>", baseUrl, workspaceEntity.getUrlName(), workspaceName);
    SchoolDataIdentifier studentIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
    String studentLink = String.format("<a href=\"%s/guider#userprofile/%s\" >%s</a>", baseUrl, studentIdentifier.toId(), userName);
    String content;
    if (StringUtils.isEmpty(entity.getMessage())) {
        content = localeController.getText(sessionController.getLocale(), "rest.workspace.joinWorkspace.joinNotification.content");
        content = MessageFormat.format(content, studentLink, workspaceLink);
    } else {
        content = localeController.getText(sessionController.getLocale(), "rest.workspace.joinWorkspace.joinNotification.contentwmessage");
        String blockquoteMessage = String.format("<blockquote>%s</blockquote>", entity.getMessage());
        content = MessageFormat.format(content, studentLink, workspaceLink, blockquoteMessage);
    }
    for (MessagingWidget messagingWidget : messagingWidgets) {
        // TODO: Category?
        messagingWidget.postMessage(userSchoolDataIdentifier.getUserEntity(), "message", caption, content, teachers);
    }
    List<String> teacherEmails = new ArrayList<>(teachers.size());
    for (UserEntity teacher : teachers) {
        String teacherEmail = userEmailEntityController.getUserDefaultEmailAddress(teacher, false);
        if (StringUtils.isNotBlank(teacherEmail)) {
            teacherEmails.add(teacherEmail);
        }
    }
    if (!teacherEmails.isEmpty()) {
        mailer.sendMail(MailType.HTML, teacherEmails, caption, content);
    }
    return Response.noContent().build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) ArrayList(java.util.ArrayList) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Date(java.util.Date) Role(fi.otavanopisto.muikku.schooldata.entity.Role) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceRoleEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity) MessagingWidget(fi.otavanopisto.muikku.controller.messaging.MessagingWidget) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

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