Search in sources :

Example 1 with Submissions

use of org.stepik.api.objects.submissions.Submissions in project intellij-plugins by StepicOrg.

the class StepikSendAction method getSubmissionId.

@Nullable
private static Long getSubmissionId(@NotNull Project project, @NotNull StepikApiClient stepikApiClient, @NotNull StepNode stepNode, long intAttemptId) {
    SupportedLanguages currentLang = stepNode.getCurrentLang();
    String code = getCode(project, stepNode, currentLang);
    if (code == null) {
        logger.info(String.format("Sending step failed: id=%s. Step content is null", stepNode.getId()));
        return null;
    }
    Submissions submissions;
    try {
        submissions = stepikApiClient.submissions().post().attempt(intAttemptId).language(currentLang.getName()).code(code).execute();
        if (submissions.isEmpty()) {
            notifyFailed(project, stepNode, "Submissions is empty", null);
            return null;
        }
    } catch (StepikClientException e) {
        notifyFailed(project, stepNode, "Failed post submission", e);
        return null;
    }
    return submissions.getFirst().getId();
}
Also used : SupportedLanguages(org.stepik.core.SupportedLanguages) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Submissions

use of org.stepik.api.objects.submissions.Submissions in project intellij-plugins by StepicOrg.

the class QuizHelper method getSubmissionsCount.

public int getSubmissionsCount() {
    if (submissionsCount == -1) {
        StepikApiClient stepikApiClient = authAndGetStepikApiClient();
        User user = getCurrentUser();
        if (user.isGuest()) {
            action = NEED_LOGIN;
            return 0;
        }
        long userId = user.getId();
        submissionsCount = 0;
        int page = 1;
        Submissions submissions;
        do {
            try {
                submissions = stepikApiClient.submissions().get().step(getStepNode().getId()).user(userId).page(page).execute();
            } catch (StepikClientException e) {
                logger.warn("Failed get submissions count", e);
                return 0;
            }
            submissionsCount += submissions.getCount();
            page++;
        } while (submissions.getMeta().getHasNext());
    }
    return submissionsCount;
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) User(org.stepik.api.objects.users.User) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Example 3 with Submissions

use of org.stepik.api.objects.submissions.Submissions in project intellij-plugins by StepicOrg.

the class QuizHelper method loadSubmission.

private void loadSubmission(StepikApiClient stepikApiClient, long userId) {
    long attemptId = attempt.getId();
    StepNode stepNode = getStepNode();
    StepikSubmissionsGetQuery query = stepikApiClient.submissions().get().order(Order.DESC).user(userId).step(stepNode.getId());
    if (!useLastSubmission) {
        query.attempt(attemptId);
    }
    Submissions submissions = query.execute();
    modified = false;
    if (!submissions.isEmpty()) {
        submission = submissions.getFirst();
        boolean lastSubmission = submission.getId() == stepNode.getLastSubmissionId();
        boolean outdated = stepNode.getLastReplyTime().after(submission.getTime());
        if (lastSubmission && outdated) {
            reply = stepNode.getLastReply();
            modified = !submission.getReply().equals(reply);
        } else {
            reply = submission.getReply();
            stepNode.setLastReply(submission.getReply());
            stepNode.setLastSubmissionId(submission.getId());
        }
        if (attemptId == submission.getAttempt()) {
            status = submission.getStatus();
        }
        if (ACTIVE.equals(attempt.getStatus()) && status.equals("correct")) {
            action = GET_ATTEMPT;
        }
        stepNode.setStatus(StudyStatus.of(status));
    } else {
        reply = stepNode.getLastReply();
        modified = true;
    }
}
Also used : StepNode(org.stepik.core.courseFormat.StepNode) Submissions(org.stepik.api.objects.submissions.Submissions) StepikSubmissionsGetQuery(org.stepik.api.queries.submissions.StepikSubmissionsGetQuery)

Example 4 with Submissions

use of org.stepik.api.objects.submissions.Submissions in project intellij-plugins by StepicOrg.

the class DownloadSubmission method getSubmissions.

@Nullable
private List<Submission> getSubmissions(@NotNull StepikApiClient stepikApiClient, @NotNull StepNode stepNode) {
    try {
        long stepId = stepNode.getId();
        long userId = getCurrentUser().getId();
        Submissions submissions = stepikApiClient.submissions().get().step(stepId).user(userId).order(Order.DESC).execute();
        return submissions.getSubmissions();
    } catch (StepikClientException e) {
        logger.warn("Failed get submissions", e);
        return null;
    }
}
Also used : Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with Submissions

use of org.stepik.api.objects.submissions.Submissions in project intellij-plugins by StepicOrg.

the class ProgrammingLanguageUtils method getOrCreateMainFile.

@Nullable
private static PsiFile getOrCreateMainFile(@NotNull Project project, @NotNull VirtualFile parent, @NotNull SupportedLanguages language, @NotNull StepNode stepNode) {
    String fileName = language.getMainFileName();
    final VirtualFile[] file = { parent.findChild(fileName) };
    Application application = ApplicationManager.getApplication();
    if (file[0] == null) {
        application.invokeAndWait(() -> application.runWriteAction(() -> {
            try {
                file[0] = parent.createChildData(null, fileName);
                String template = null;
                StepikApiClient stepikApiClient = authAndGetStepikApiClient();
                User user = getCurrentUser();
                if (!user.isGuest()) {
                    try {
                        Submissions submissions = stepikApiClient.submissions().get().user(user.getId()).order(Order.DESC).step(stepNode.getId()).execute();
                        if (!submissions.isEmpty()) {
                            Optional<Submission> lastSubmission = submissions.getItems().stream().filter(submission -> SupportedLanguages.langOfName(submission.getReply().getLanguage()) == language).limit(1).findFirst();
                            if (lastSubmission.isPresent()) {
                                template = lastSubmission.get().getReply().getCode();
                            }
                        }
                    } catch (StepikClientException e) {
                        logger.warn(e);
                    }
                }
                if (template == null) {
                    template = stepNode.getTemplate(language);
                }
                file[0].setBinaryContent(template.getBytes());
            } catch (IOException e) {
                file[0] = null;
            }
        }));
    }
    return application.runReadAction((Computable<PsiFile>) () -> PsiManager.getInstance(project).findFile(file[0]));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) Application(com.intellij.openapi.application.Application) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) ProjectFilesUtils.getOrCreateSrcPsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreateSrcPsiDirectory) Computable(com.intellij.openapi.util.Computable) StepikClientException(org.stepik.api.exceptions.StepikClientException) User(org.stepik.api.objects.users.User) PsiManager(com.intellij.psi.PsiManager) ArrayList(java.util.ArrayList) ProjectView(com.intellij.ide.projectView.ProjectView) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) ProjectFilesUtils.getOrCreatePsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreatePsiDirectory) Metrics(org.stepik.core.metrics.Metrics) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) SUCCESSFUL(org.stepik.core.metrics.MetricsStatus.SUCCESSFUL) Submission(org.stepik.api.objects.submissions.Submission) SupportedLanguages(org.stepik.core.SupportedLanguages) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) StepNode(org.stepik.core.courseFormat.StepNode) IOException(java.io.IOException) StudyUtils(org.stepik.core.StudyUtils) MoveFilesOrDirectoriesUtil(com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesUtil) Nullable(org.jetbrains.annotations.Nullable) EduNames(org.stepik.core.core.EduNames) Submissions(org.stepik.api.objects.submissions.Submissions) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Optional(java.util.Optional) PsiDirectory(com.intellij.psi.PsiDirectory) StepikApiClient(org.stepik.api.client.StepikApiClient) NotNull(org.jetbrains.annotations.NotNull) Order(org.stepik.api.queries.Order) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) User(org.stepik.api.objects.users.User) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Optional(java.util.Optional) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) Application(com.intellij.openapi.application.Application) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Submissions (org.stepik.api.objects.submissions.Submissions)6 StepikClientException (org.stepik.api.exceptions.StepikClientException)5 Nullable (org.jetbrains.annotations.Nullable)3 StepikApiClient (org.stepik.api.client.StepikApiClient)2 Submission (org.stepik.api.objects.submissions.Submission)2 User (org.stepik.api.objects.users.User)2 SupportedLanguages (org.stepik.core.SupportedLanguages)2 StepNode (org.stepik.core.courseFormat.StepNode)2 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)2 StepikAuthManager.getCurrentUser (org.stepik.core.stepik.StepikAuthManager.getCurrentUser)2 ProjectView (com.intellij.ide.projectView.ProjectView)1 Application (com.intellij.openapi.application.Application)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Document (com.intellij.openapi.editor.Document)1 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)1 Project (com.intellij.openapi.project.Project)1 Computable (com.intellij.openapi.util.Computable)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1