Skip to content

Commit

Permalink
feat(client,server): support problem skeletons (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar authored Sep 30, 2023
1 parent 96f09a9 commit 06e9821
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package judgels.jerahmeel.api.chapter.problem.programming;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.Set;
import judgels.jerahmeel.api.problem.ProblemProgress;
import judgels.sandalphon.api.problem.programming.ProblemSkeleton;
import judgels.sandalphon.api.problem.programming.ProblemWorksheet;
import org.immutables.value.Value;

@Value.Immutable
@JsonDeserialize(as = ImmutableChapterProblemWorksheet.class)
public interface ChapterProblemWorksheet extends judgels.jerahmeel.api.chapter.problem.ChapterProblemWorksheet {
ProblemWorksheet getWorksheet();
Set<ProblemSkeleton> getSkeletons();
ProblemProgress getProgress();

class Builder extends ImmutableChapterProblemWorksheet.Builder{}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package judgels.sandalphon.api.problem.programming;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.Set;
import org.immutables.value.Value;

@Value.Immutable
@JsonDeserialize(as = ImmutableProblemSkeleton.class)
public interface ProblemSkeleton {
Set<String> getLanguages();
byte[] getContent();

class Builder extends ImmutableProblemSkeleton.Builder {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public ChapterProblemWorksheet getProblemWorksheet(
.from(sandalphonClient.getProgrammingProblemWorksheet(req, uriInfo, problemJid, language))
.reasonNotAllowedToSubmit(reasonNotAllowedToSubmit)
.build())
.skeletons(sandalphonClient.getProgrammingProblemSkeletons(problemJid))
.progress(statsStore.getProblemProgressesMap(actorJid, Set.of(problemJid)).get(problemJid))
.build();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import judgels.sandalphon.api.problem.bundle.Item;
import judgels.sandalphon.api.problem.bundle.ItemConfig;
import judgels.sandalphon.api.problem.programming.ProblemLimits;
import judgels.sandalphon.api.problem.programming.ProblemSkeleton;
import judgels.sandalphon.api.problem.programming.ProblemSubmissionConfig;
import judgels.sandalphon.lesson.LessonStore;
import judgels.sandalphon.lesson.statement.LessonStatementStore;
Expand Down Expand Up @@ -164,6 +165,10 @@ public judgels.sandalphon.api.problem.programming.ProblemWorksheet getProgrammin
.build();
}

public Set<ProblemSkeleton> getProgrammingProblemSkeletons(String problemJid) {
return problemStatementStore.getSkeletons(null, problemJid);
}

public judgels.sandalphon.api.problem.bundle.ProblemWorksheet getBundleProblemWorksheet(
HttpServletRequest req,
UriInfo uriInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import judgels.fs.FileInfo;
import judgels.fs.FileSystem;
import judgels.gabriel.languages.GradingLanguageRegistry;
import judgels.sandalphon.api.problem.ProblemStatement;
import judgels.sandalphon.api.problem.ProblemType;
import judgels.sandalphon.api.problem.programming.ProblemSkeleton;
import judgels.sandalphon.problem.base.BaseProblemStore;
import judgels.sandalphon.problem.base.ProblemFs;
import judgels.sandalphon.problem.bundle.statement.BundleProblemStatementUtils;
import judgels.sandalphon.problem.programming.statement.ProgrammingProblemStatementUtils;
import judgels.sandalphon.resource.StatementLanguageStatus;
import org.apache.commons.io.FilenameUtils;

public class ProblemStatementStore extends BaseProblemStore {
@Inject
Expand Down Expand Up @@ -150,6 +154,29 @@ public String getStatementMediaFileURL(String userJid, String problemJid, String
return problemFs.getPublicFileUrl(mediaFilePath);
}

public Set<ProblemSkeleton> getSkeletons(String userJid, String problemJid) {
Set<ProblemSkeleton> skeletons = new HashSet<>();
for (FileInfo file : getStatementMediaFiles(null, problemJid)) {
if (file.getName().toLowerCase().startsWith("skeleton.")) {
Path mediaFilePath = getStatementMediaDirPath(userJid, problemJid).resolve(file.getName());
String extension = FilenameUtils.getExtension(file.getName());

Set<String> languages = new HashSet<>();
for (String language : GradingLanguageRegistry.getInstance().getLanguages().keySet()) {
if (GradingLanguageRegistry.getInstance().get(language).getAllowedExtensions().contains(extension)) {
languages.add(language);
}
}

skeletons.add(new ProblemSkeleton.Builder()
.languages(languages)
.content(problemFs.readByteArrayFromFile(mediaFilePath))
.build());
}
}
return skeletons;
}

private Path getStatementsDirPath(String userJid, String problemJid) {
return getRootDirPath(userJid, problemJid).resolve("statements");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getAllowedGradingLanguages, gradingLanguageNamesMap } from '../../../..
import './ProblemSubmissionEditor.scss';

export function ProblemSubmissionEditor({
skeletons,
config: { sourceKeys, gradingEngine, gradingLanguageRestriction },
onSubmit,
reasonNotAllowedToSubmit,
Expand Down Expand Up @@ -62,6 +63,12 @@ export function ProblemSubmissionEditor({
gradingLanguage: defaultGradingLanguage,
};

(skeletons || []).forEach(skeleton => {
if (skeleton.languages.indexOf(defaultGradingLanguage) >= 0) {
initialValues.editor = atob(skeleton.content);
}
});

return (
<Form onSubmit={onSubmitEditor} initialValues={initialValues}>
{({ values, handleSubmit, submitting }) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('ChapterProblemProgrammingPage', () => {
},
defaultLanguage: 'id',
languages: ['en', 'id'],
skeletons: [],
worksheet: {
statement: {
title: 'Problem',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import * as chapterProblemSubmissionActions from '../submissions/modules/chapter
import * as webPrefsActions from '../../../../../../../../../../modules/webPrefs/webPrefsActions';

function ChapterProblemWorkspacePage({
worksheet: { problem, worksheet },
worksheet,
course,
chapter,
gradingLanguage,
onCreateSubmission,
onUpdateGradingLanguage,
}) {
const { submissionConfig, reasonNotAllowedToSubmit } = worksheet;
const { submissionConfig, reasonNotAllowedToSubmit } = worksheet.worksheet;
const { problem, skeletons } = worksheet;

const createSubmission = async data => {
onUpdateGradingLanguage(data.gradingLanguage);
Expand All @@ -27,7 +28,7 @@ function ChapterProblemWorkspacePage({
sendGAEvent({
category: 'Courses',
action: 'Submit problem',
label: chapter.name + ': ' + problem.alis,
label: chapter.name + ': ' + problem.alias,
});
if (getGradingLanguageFamily(data.gradingLanguage)) {
sendGAEvent({
Expand All @@ -42,6 +43,7 @@ function ChapterProblemWorkspacePage({

return (
<ProblemSubmissionEditor
skeletons={skeletons}
config={submissionConfig}
onSubmit={createSubmission}
reasonNotAllowedToSubmit={reasonNotAllowedToSubmit}
Expand Down

0 comments on commit 06e9821

Please sign in to comment.