Search in sources :

Example 1 with Submission

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

the class DownloadSubmission method downloadSubmission.

private void downloadSubmission(@Nullable Project project) {
    if (project == null) {
        return;
    }
    StudyNode<?, ?> studyNode = StepikProjectManager.getSelected(project);
    if (!(studyNode instanceof StepNode)) {
        return;
    }
    StepNode stepNode = (StepNode) studyNode;
    String title = "Download submission";
    StepikApiClient stepikApiClient = authAndGetStepikApiClient(true);
    if (!isAuthenticated()) {
        return;
    }
    List<Submission> submissions = ProgressManager.getInstance().run(new Task.WithResult<List<Submission>, RuntimeException>(project, title, true) {

        @Override
        protected List<Submission> compute(@NotNull ProgressIndicator progressIndicator) throws RuntimeException {
            progressIndicator.setIndeterminate(true);
            StudyNode parent = stepNode.getParent();
            String lessonName = parent != null ? parent.getName() : "";
            progressIndicator.setText(lessonName);
            progressIndicator.setText2(stepNode.getName());
            List<Submission> submissions = getSubmissions(stepikApiClient, stepNode);
            if (Utils.isCanceled()) {
                Metrics.downloadAction(project, stepNode, USER_CANCELED);
                return null;
            }
            if (submissions == null) {
                Metrics.downloadAction(project, stepNode, DATA_NOT_LOADED);
                return Collections.emptyList();
            }
            SupportedLanguages currentLang = stepNode.getCurrentLang();
            return filterSubmissions(submissions, currentLang);
        }
    });
    if (submissions == null) {
        return;
    }
    ApplicationManager.getApplication().invokeAndWait(() -> showPopup(project, stepNode, submissions));
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) Task(com.intellij.openapi.progress.Task) Submission(org.stepik.api.objects.submissions.Submission) StudyNode(org.stepik.core.courseFormat.StudyNode) SupportedLanguages(org.stepik.core.SupportedLanguages) StepNode(org.stepik.core.courseFormat.StepNode) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List)

Example 2 with Submission

use of org.stepik.api.objects.submissions.Submission 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)

Example 3 with Submission

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

the class FormListener method sendStep.

private static void sendStep(@NotNull Project project, @NotNull StepNode stepNode, @NotNull Elements elements, @NotNull StepType type, long attemptId, @Nullable String data) {
    StepikApiClient stepikApiClient = authAndGetStepikApiClient(true);
    StepikSubmissionsPostQuery query = stepikApiClient.submissions().post().attempt(attemptId);
    Reply reply = getReply(stepNode, type, elements, data);
    if (reply == null) {
        return;
    }
    query.reply(reply).executeAsync().whenComplete(((submissions, e) -> {
        if (submissions == null) {
            logger.warn("Failed send step from browser", e);
            StepikProjectManager.updateSelection(project);
            return;
        }
        if (submissions.isEmpty()) {
            logger.warn("Failed send step from browser", e);
            return;
        }
        Submission submission = submissions.getFirst();
        SendAction.checkStepStatus(project, stepikApiClient, stepNode, submission.getId(), new EmptyProgressIndicator());
    }));
}
Also used : HTMLFormElement(org.w3c.dom.html.HTMLFormElement) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) VirtualFile(com.intellij.openapi.vfs.VirtualFile) StudyNode(org.stepik.core.courseFormat.StudyNode) StepikSubmissionsPostQuery(org.stepik.api.queries.submissions.StepikSubmissionsPostQuery) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) Submission(org.stepik.api.objects.submissions.Submission) StepikAuthState(org.stepik.core.stepik.StepikAuthState) Files(java.nio.file.Files) StepNode(org.stepik.core.courseFormat.StepNode) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) StudyUtils(org.stepik.core.StudyUtils) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) FileChooser(javafx.stage.FileChooser) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) SendAction(org.stepik.plugin.actions.SendAction) EventListener(org.w3c.dom.events.EventListener) StepType(org.stepik.core.courseFormat.StepType) Event(org.w3c.dom.events.Event) StepikProjectManager(org.stepik.core.StepikProjectManager) StepikApiClient(org.stepik.api.client.StepikApiClient) Reply(org.stepik.api.objects.submissions.Reply) StepikAuthManager(org.stepik.core.stepik.StepikAuthManager) NotNull(org.jetbrains.annotations.NotNull) StepDescriptionUtils.getReply(org.stepik.core.ui.StepDescriptionUtils.getReply) ProjectFilesUtils.getOrCreateSrcDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreateSrcDirectory) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) Submission(org.stepik.api.objects.submissions.Submission) Reply(org.stepik.api.objects.submissions.Reply) StepDescriptionUtils.getReply(org.stepik.core.ui.StepDescriptionUtils.getReply) StepikSubmissionsPostQuery(org.stepik.api.queries.submissions.StepikSubmissionsPostQuery)

Example 4 with Submission

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

the class SendAction method checkStepStatus.

public static void checkStepStatus(@NotNull Project project, @NotNull StepikApiClient stepikApiClient, @NotNull StepNode stepNode, final long submissionId, @NotNull ProgressIndicator indicator) {
    String stepIdString = "id=" + stepNode.getId();
    logger.info("Started check a status for step: " + stepIdString);
    String stepStatus = EVALUATION;
    int timer = 0;
    String hint;
    indicator.setIndeterminate(false);
    Submission currentSubmission = null;
    while (timer < FIVE_MINUTES) {
        try {
            Submissions submission = stepikApiClient.submissions().get().id(submissionId).execute();
            if (!submission.isEmpty()) {
                currentSubmission = submission.getFirst();
                ActionUtils.setupCheckProgress(indicator, currentSubmission, timer);
                stepStatus = currentSubmission.getStatus();
                if (!EVALUATION.equals(stepStatus)) {
                    break;
                }
            }
            Thread.sleep(PERIOD);
            if (Utils.isCanceled()) {
                Metrics.getStepStatusAction(project, stepNode, USER_CANCELED);
                return;
            }
            timer += PERIOD;
        } catch (StepikClientException | InterruptedException e) {
            ActionUtils.notifyError(project, "Error", "Get Status error");
            logger.info("Stop check a status for step: " + stepIdString, e);
            return;
        }
    }
    if (currentSubmission == null) {
        logger.info(String.format("Stop check a status for step: %s without result", stepIdString));
        return;
    }
    MetricsStatus actionStatus = EVALUATION.equals(stepStatus) ? TIME_OVER : SUCCESSFUL;
    Metrics.getStepStatusAction(project, stepNode, actionStatus);
    indicator.setIndeterminate(true);
    indicator.setText("");
    hint = currentSubmission.getHint();
    if (stepNode.getType() == StepType.CODE) {
        notify(project, stepNode, stepStatus, hint);
    }
    ApplicationManager.getApplication().invokeLater(() -> {
        if (!project.isDisposed()) {
            ProjectView.getInstance(project).refresh();
        }
        StepikProjectManager.updateSelection(project);
    });
    logger.info(String.format("Finish check a status for step: %s with status: %s", stepIdString, stepStatus));
}
Also used : Submission(org.stepik.api.objects.submissions.Submission) MetricsStatus(org.stepik.core.metrics.MetricsStatus) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Aggregations

Submission (org.stepik.api.objects.submissions.Submission)4 StepikApiClient (org.stepik.api.client.StepikApiClient)3 StepNode (org.stepik.core.courseFormat.StepNode)3 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)3 Logger (com.intellij.openapi.diagnostic.Logger)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 IOException (java.io.IOException)2 List (java.util.List)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 StepikClientException (org.stepik.api.exceptions.StepikClientException)2 Submissions (org.stepik.api.objects.submissions.Submissions)2 StudyUtils (org.stepik.core.StudyUtils)2 SupportedLanguages (org.stepik.core.SupportedLanguages)2 StudyNode (org.stepik.core.courseFormat.StudyNode)2 ProjectView (com.intellij.ide.projectView.ProjectView)1 Application (com.intellij.openapi.application.Application)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Document (com.intellij.openapi.editor.Document)1