use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial 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();
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.
the class TaskMathMl method cleanElement.
@Override
protected void cleanElement(Element element) {
boolean needsModify = false;
if ("img".equals(element.getTagName()) && element.hasAttribute("ix_mathmldata")) {
String src = element.getAttribute("src");
if (StringUtils.startsWith(src, "/~Repository/ResourceAPI/RenderMathML")) {
src = "http://muikku.otavanopisto.fi" + src;
needsModify = true;
} else if (StringUtils.startsWith(src, "muikku.otavanopisto.fi/~Repository/ResourceAPI/RenderMathML")) {
src = "http://" + src;
needsModify = true;
} else if (StringUtils.startsWith(src, "http://muikku.otavanopisto.fi/~Repository/ResourceAPI/RenderMathML")) {
needsModify = true;
}
if (needsModify) {
try {
logger.info("Converting MathML from " + src);
URL url = new URL(src);
InputStream is = url.openStream();
byte[] data = {};
try {
data = IOUtils.toByteArray(is);
} finally {
is.close();
}
String name = "mathml" + StringUtils.leftPad(++imageCounter + "", 3, '0') + ".png";
String license = null;
BinaryMaterial material = binaryMaterialController.createBinaryMaterial(name, "image/png", data, license);
WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(getWorkspaceMaterial(), material);
String workspaceUrl = StringUtils.prependIfMissing(workspaceMaterialController.getCompletePath(workspaceMaterial), "/");
logger.info("MathML converted to " + workspaceUrl);
element.setAttribute("src", workspaceUrl);
element.removeAttribute("ix_mathml");
element.removeAttribute("ix_mathmldata");
markModified();
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to process MathML from URL " + src, e);
}
}
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.
the class AcceptanceTestsRESTService method deleteWorkspaceMaterial.
@DELETE
@Path("/workspaces/{WORKSPACEENTITYID}/htmlmaterials/{WORKSPACEMATERIALID}")
@RESTPermit(handling = Handling.UNSECURED)
public Response deleteWorkspaceMaterial(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId) {
WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
if (workspaceMaterial == null) {
return Response.status(Status.NOT_FOUND).entity("Not Found").build();
}
HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(workspaceMaterial.getMaterialId());
if (htmlMaterial == null) {
return Response.status(Status.BAD_REQUEST).entity("Not a html material").build();
}
try {
workspaceMaterialController.deleteWorkspaceMaterial(workspaceMaterial, true);
} catch (WorkspaceMaterialContainsAnswersExeption e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
List<WorkspaceMaterialEvaluation> evaluations = evaluationController.listWorkspaceMaterialEvaluationsByWorkspaceMaterialId(workspaceMaterialId);
for (WorkspaceMaterialEvaluation evaluation : evaluations) {
evaluationController.deleteWorkspaceMaterialEvaluation(evaluation);
}
htmlMaterialController.deleteHtmlMaterial(htmlMaterial);
return Response.noContent().build();
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.
the class WorkspaceRESTService method getAllFileAnswers.
@GET
@Produces("application/zip")
@Path("/allfileanswers/{FILEID}")
@RESTPermit(handling = Handling.INLINE)
public Response getAllFileAnswers(@PathParam("FILEID") String fileId, @QueryParam("archiveName") String archiveName) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).build();
}
// Find the initial file
WorkspaceMaterialFileFieldAnswerFile answerFile = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerFileByFileId(fileId);
if (answerFile == null) {
return Response.status(Status.NOT_FOUND).build();
}
// Access check
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();
}
}
// Fetch all files belonging to the same answer as the initial file
List<WorkspaceMaterialFileFieldAnswerFile> answerFiles = workspaceMaterialFieldAnswerController.listWorkspaceMaterialFileFieldAnswerFilesByFieldAnswer(answerFile.getFieldAnswer());
if (CollectionUtils.isEmpty(answerFiles)) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("answerFiles not found")).build();
}
try {
Set<String> fileNames = new HashSet<String>();
StreamingOutput output = new StreamingOutput() {
@Override
public void write(OutputStream out) throws IOException {
ZipOutputStream zout = new ZipOutputStream(out);
for (WorkspaceMaterialFileFieldAnswerFile file : answerFiles) {
// Prevent duplicate file names
String fileName = file.getFileName();
if (fileNames.contains(fileName)) {
int counter = 1;
String name = file.getFileName();
String prefix = "";
if (StringUtils.contains(name, ".")) {
prefix = StringUtils.substringAfterLast(name, ".");
name = StringUtils.substringBeforeLast(name, ".");
}
if (!StringUtils.isEmpty(prefix)) {
prefix = String.format(".%s", prefix);
}
while (fileNames.contains(fileName)) {
fileName = String.format("%s (%s)%s", name, counter++, prefix);
}
}
fileNames.add(fileName);
// Zip file
ZipEntry ze = new ZipEntry(fileName);
zout.putNextEntry(ze);
InputStream inputStream = new ByteArrayInputStream(file.getContent());
IOUtils.copy(inputStream, zout);
zout.closeEntry();
}
zout.flush();
zout.close();
}
};
archiveName = StringUtils.defaultIfEmpty(archiveName, "files.zip");
return Response.ok(output).header("Content-Disposition", "attachment; filename=\"" + archiveName.replaceAll("\"", "\\\"") + "\"").build();
} catch (Exception e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.
the class WorkspaceRESTService method listWorkspaceMaterialReplies.
@GET
@Path("/workspaces/{WORKSPACEENTITYID}/materials/{WORKSPACEMATERIALID}/replies")
@RESTPermitUnimplemented
public Response listWorkspaceMaterialReplies(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId) {
UserEntity loggedUser = sessionController.getLoggedUserEntity();
if (loggedUser == null) {
return Response.status(Status.UNAUTHORIZED).entity("Unauthorized").build();
}
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).entity("Could not find workspace entity").build();
}
WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
if (workspaceMaterial == null) {
return Response.status(Status.NOT_FOUND).entity("Could not find workspace material").build();
}
WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
if (workspaceRootFolder == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Could not find workspace root folder").build();
}
if (!workspaceRootFolder.getWorkspaceEntityId().equals(workspaceEntity.getId())) {
return Response.status(Status.BAD_REQUEST).entity("Invalid workspace material id or workspace entity id").build();
}
fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply materialReply = workspaceMaterialReplyController.findWorkspaceMaterialReplyByWorkspaceMaterialAndUserEntity(workspaceMaterial, loggedUser);
if (materialReply != null) {
return Response.ok(createRestModel(new fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply[] { materialReply })).build();
} else {
return Response.ok(createRestModel(new fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply[] {})).build();
}
}
Aggregations