Search in sources :

Example 61 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.

the class WorkspaceRESTService method getFileAnswer.

@GET
@Path("/fileanswer/{FILEID}")
@RESTPermit(handling = Handling.INLINE)
public Response getFileAnswer(@PathParam("FILEID") String fileId) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    WorkspaceMaterialFileFieldAnswerFile answerFile = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerFileByFileId(fileId);
    if (answerFile == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply workspaceMaterialReply = answerFile.getFieldAnswer().getReply();
    if (workspaceMaterialReply == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find reply from answer file %d", answerFile.getId())).build();
    }
    WorkspaceMaterial workspaceMaterial = workspaceMaterialReply.getWorkspaceMaterial();
    if (workspaceMaterial == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace material from reply %d", workspaceMaterialReply.getId())).build();
    }
    WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
    if (workspaceRootFolder == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace root folder for material %d", workspaceMaterial.getId())).build();
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceRootFolder.getWorkspaceEntityId());
    if (workspaceEntity == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace entity for root folder %d", workspaceRootFolder.getId())).build();
    }
    if (!workspaceMaterialReply.getUserEntityId().equals(sessionController.getLoggedUserEntity().getId())) {
        if (!sessionController.hasWorkspacePermission(MuikkuPermissions.ACCESS_STUDENT_ANSWERS, workspaceEntity)) {
            return Response.status(Status.FORBIDDEN).build();
        }
    }
    return Response.ok(answerFile.getContent()).type(answerFile.getContentType()).header("Content-Disposition", "attachment; filename=\"" + answerFile.getFileName().replaceAll("\"", "\\\"") + "\"").build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 62 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.

the class WorkspaceRESTService method getWorkspaceMaterial.

@GET
@Path("/workspaces/{WORKSPACEENTITYID}/materials/{ID}")
@RESTPermitUnimplemented
public Response getWorkspaceMaterial(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("ID") Long workspaceMaterialId) {
    // TODO: Security
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
    if (workspaceMaterial == null) {
        return Response.status(Status.NOT_FOUND).entity("workspaceMaterial not found").build();
    }
    WorkspaceRootFolder rootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
    if (rootFolder == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
    if (!workspaceEntity.getId().equals(rootFolder.getWorkspaceEntityId())) {
        return Response.status(Status.NOT_FOUND).build();
    }
    return Response.ok(createRestModel(workspaceMaterial)).build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 63 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.

the class WorkspaceRESTService method getEvaluatedAssignmentInfo.

@GET
@Path("/workspaces/{WORKSPACEENTITYID}/evaluatedAssignmentInfo")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response getEvaluatedAssignmentInfo(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId) {
    Map<String, Long> result = new HashMap<String, Long>();
    // Workspace and user
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    UserEntity userEntity = sessionController.getLoggedUserEntity();
    // Total number of evaluated assignments
    List<WorkspaceMaterial> evaluatedAssignments = workspaceMaterialController.listVisibleWorkspaceMaterialsByAssignmentType(workspaceEntity, WorkspaceMaterialAssignmentType.EVALUATED);
    result.put("assignmentsTotal", new Long(evaluatedAssignments.size()));
    // Done number of evaluated assignments
    List<WorkspaceMaterialReplyState> replyStates = new ArrayList<WorkspaceMaterialReplyState>();
    replyStates.add(WorkspaceMaterialReplyState.FAILED);
    replyStates.add(WorkspaceMaterialReplyState.PASSED);
    replyStates.add(WorkspaceMaterialReplyState.SUBMITTED);
    Long assignmentsDone = workspaceMaterialReplyController.getReplyCountByUserEntityAndReplyStatesAndWorkspaceMaterials(userEntity.getId(), replyStates, evaluatedAssignments);
    result.put("assignmentsDone", assignmentsDone);
    return Response.ok(result).build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WorkspaceMaterialReplyState(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReplyState) 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)

Example 64 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.

the class WorkspaceRESTService method getAudioAnswer.

@GET
@Path("/audioanswer/{CLIPID}")
@RESTPermit(handling = Handling.INLINE)
public Response getAudioAnswer(@PathParam("CLIPID") String clipId) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    WorkspaceMaterialAudioFieldAnswerClip answerClip = workspaceMaterialFieldAnswerController.findWorkspaceMaterialAudioFieldAnswerClipByClipId(clipId);
    if (answerClip != null) {
        fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply workspaceMaterialReply = answerClip.getFieldAnswer().getReply();
        if (workspaceMaterialReply == null) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find reply from answer audio %d", answerClip.getId())).build();
        }
        WorkspaceMaterial workspaceMaterial = workspaceMaterialReply.getWorkspaceMaterial();
        if (workspaceMaterial == null) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace material from reply %d", workspaceMaterialReply.getId())).build();
        }
        WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
        if (workspaceRootFolder == null) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace root folder for material %d", workspaceMaterial.getId())).build();
        }
        WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceRootFolder.getWorkspaceEntityId());
        if (workspaceEntity == null) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace entity for root folder %d", workspaceRootFolder.getId())).build();
        }
        if (!workspaceMaterialReply.getUserEntityId().equals(sessionController.getLoggedUserEntity().getId())) {
            if (!sessionController.hasWorkspacePermission(MuikkuPermissions.ACCESS_STUDENT_ANSWERS, workspaceEntity)) {
                return Response.status(Status.FORBIDDEN).build();
            }
        }
        return Response.ok(answerClip.getContent()).type(answerClip.getContentType()).header("Content-Disposition", "attachment; filename=\"" + answerClip.getFileName().replaceAll("\"", "\\\"") + "\"").build();
    }
    return Response.status(Status.NOT_FOUND).build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterialAudioFieldAnswerClip(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 65 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.

the class WorkspaceRESTService method createWorkspaceMaterial.

@POST
@Path("/workspaces/{ID}/materials/")
@RESTPermitUnimplemented
public Response createWorkspaceMaterial(@PathParam("ID") Long workspaceEntityId, @QueryParam("sourceNodeId") Long sourceNodeId, @QueryParam("targetNodeId") Long targetNodeId, @QueryParam("sourceWorkspaceEntityId") Long sourceWorkspaceEntityId, @QueryParam("targetWorkspaceEntityId") Long targetWorkspaceEntityId, @QueryParam("copyOnlyChildren") Boolean copyOnlyChildren, @QueryParam("cloneMaterials") @DefaultValue("false") Boolean cloneMaterials, @QueryParam("updateLinkedMaterials") @DefaultValue("false") Boolean updateLinkedMaterials, fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterial entity) {
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if ((sourceNodeId != null) || (sourceWorkspaceEntityId != null)) {
        if (sourceNodeId == null) {
            WorkspaceEntity sourceWorkspaceEntity = workspaceController.findWorkspaceEntityById(sourceWorkspaceEntityId);
            if (sourceWorkspaceEntity == null) {
                return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid sourceWorkspaceEntity %d", sourceWorkspaceEntityId)).build();
            }
            WorkspaceRootFolder sourceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceEntity(sourceWorkspaceEntity);
            if (sourceRootFolder == null) {
                return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid sourceWorkspaceEntity %d", sourceWorkspaceEntityId)).build();
            }
            sourceNodeId = sourceRootFolder.getId();
        }
        if (targetNodeId == null) {
            if (targetWorkspaceEntityId != null) {
                WorkspaceEntity targetWorkspaceEntity = workspaceController.findWorkspaceEntityById(targetWorkspaceEntityId);
                if (targetWorkspaceEntity == null) {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid targetWorkspaceEntity %d", sourceWorkspaceEntityId)).build();
                }
                WorkspaceRootFolder targetRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceEntity(targetWorkspaceEntity);
                if (targetRootFolder == null) {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid targetWorkspaceEntity %d", sourceWorkspaceEntityId)).build();
                }
                targetNodeId = targetRootFolder.getId();
            }
        }
        if (targetNodeId == null) {
            return Response.status(Status.BAD_REQUEST).entity("targetNodeId is required when sourceNodeId is specified").build();
        }
        if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.COPY_WORKSPACE)) {
            return Response.status(Status.FORBIDDEN).build();
        }
        // Source
        WorkspaceNode sourceNode = workspaceMaterialController.findWorkspaceNodeById(sourceNodeId);
        if (sourceNode == null) {
            return Response.status(Status.BAD_REQUEST).entity("null source").build();
        }
        // Target
        WorkspaceNode targetNode = workspaceMaterialController.findWorkspaceNodeById(targetNodeId);
        if (targetNode == null) {
            return Response.status(Status.BAD_REQUEST).entity("null target").build();
        }
        WorkspaceRootFolder targetRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(targetNode);
        if (!targetRootFolder.getWorkspaceEntityId().equals(workspaceEntity.getId())) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("targetNode does not belong to workspace entity %d", workspaceEntity.getId())).build();
        }
        // Circular reference check
        WorkspaceNode node = targetNode;
        while (node != null) {
            if (node.getId().equals(sourceNode.getId())) {
                return Response.status(Status.BAD_REQUEST).entity("Circular copy reference").build();
            }
            node = node.getParent();
        }
        // Copy
        WorkspaceNode createdNode = null;
        WorkspaceMaterial createdMaterial = null;
        if (copyOnlyChildren) {
            List<WorkspaceNode> sourceChildren = workspaceMaterialController.listWorkspaceNodesByParent(sourceNode);
            for (WorkspaceNode sourceChild : sourceChildren) {
                workspaceMaterialController.cloneWorkspaceNode(sourceChild, targetNode, cloneMaterials);
            }
        } else {
            createdNode = workspaceMaterialController.cloneWorkspaceNode(sourceNode, targetNode, cloneMaterials);
            if (createdNode.getType() == WorkspaceNodeType.MATERIAL) {
                createdMaterial = workspaceMaterialController.findWorkspaceMaterialById(createdNode.getId());
                if (entity != null && entity.getNextSiblingId() != null) {
                    WorkspaceNode nextSibling = workspaceMaterialController.findWorkspaceNodeById(entity.getNextSiblingId());
                    if (nextSibling == null) {
                        return Response.status(Status.BAD_REQUEST).entity("Specified next sibling does not exist").build();
                    }
                    workspaceMaterialController.moveAbove(createdNode, nextSibling);
                }
            }
        }
        return createdMaterial == null ? Response.noContent().build() : Response.ok(createRestModel(createdMaterial)).build();
    } else {
        if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIALS, workspaceEntity)) {
            return Response.status(Status.FORBIDDEN).build();
        }
        if (entity.getMaterialId() == null) {
            return Response.status(Status.BAD_REQUEST).entity("material_id is required when creating new WorkspaceMaterial").build();
        }
        WorkspaceNode parent = null;
        if (entity.getParentId() != null) {
            parent = workspaceMaterialController.findWorkspaceNodeById(entity.getParentId());
            if (parent == null) {
                return Response.status(Status.NOT_FOUND).entity("parent not found").build();
            }
        } else {
            parent = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceEntity(workspaceEntity);
        }
        Material material = materialController.findMaterialById(entity.getMaterialId());
        if (material == null) {
            return Response.status(Status.NOT_FOUND).entity("material not found").build();
        }
        WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(parent, material, entity.getAssignmentType(), entity.getCorrectAnswers());
        if (entity.getNextSiblingId() != null) {
            WorkspaceNode nextSibling = workspaceMaterialController.findWorkspaceNodeById(entity.getNextSiblingId());
            if (nextSibling == null) {
                return Response.status(Status.BAD_REQUEST).entity("Specified next sibling does not exist").build();
            }
            if (!nextSibling.getParent().getId().equals(parent.getId())) {
                return Response.status(Status.BAD_REQUEST).entity("Specified next sibling does not share parent with created workspace material").build();
            }
            workspaceMaterialController.moveAbove(workspaceMaterial, nextSibling);
        }
        // #1261: HtmlMaterial attachments should be added to all workspace materials sharing the same HtmlMaterial
        if (updateLinkedMaterials && parent instanceof WorkspaceMaterial) {
            Long parentMaterialId = ((WorkspaceMaterial) parent).getMaterialId();
            if (parentMaterialId != null) {
                Material parentMaterial = materialController.findMaterialById(parentMaterialId);
                if (parentMaterial instanceof HtmlMaterial) {
                    List<WorkspaceMaterial> sharedWorkspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByMaterial(parentMaterial);
                    for (WorkspaceMaterial sharedWorkspaceMaterial : sharedWorkspaceMaterials) {
                        if (sharedWorkspaceMaterial.getId().equals(workspaceMaterial.getId())) {
                            // skip the one we created above
                            continue;
                        }
                        WorkspaceMaterial sharedAttachment = workspaceMaterialController.findWorkspaceMaterialByParentAndUrlName(sharedWorkspaceMaterial, workspaceMaterial.getUrlName());
                        if (sharedAttachment == null) {
                            workspaceMaterialController.createWorkspaceMaterial(sharedWorkspaceMaterial, material, workspaceMaterial.getUrlName(), workspaceMaterial.getAssignmentType(), workspaceMaterial.getCorrectAnswers());
                        }
                    }
                }
            }
        }
        return Response.ok(createRestModel(workspaceMaterial)).build();
    }
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) Material(fi.otavanopisto.muikku.plugins.material.model.Material) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)

Aggregations

WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)66 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)24 Path (javax.ws.rs.Path)24 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)18 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)17 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)16 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)15 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)14 WorkspaceRootFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)14 GET (javax.ws.rs.GET)14 Material (fi.otavanopisto.muikku.plugins.material.model.Material)13 ArrayList (java.util.ArrayList)11 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)7 WorkspaceMaterialEvaluation (fi.otavanopisto.muikku.plugins.evaluation.model.WorkspaceMaterialEvaluation)6 BinaryMaterial (fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial)6 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)6 WorkspaceMaterialReply (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply)6 POST (javax.ws.rs.POST)6 SupplementationRequest (fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest)5 RestSupplementationRequest (fi.otavanopisto.muikku.plugins.evaluation.rest.model.RestSupplementationRequest)5