Search in sources :

Example 16 with StepikApiClient

use of org.stepik.api.client.StepikApiClient 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 17 with StepikApiClient

use of org.stepik.api.client.StepikApiClient in project intellij-plugins by StepicOrg.

the class Node method getStatus.

@NotNull
@Override
public StudyStatus getStatus() {
    if (isUnknownStatus()) {
        status = UNCHECKED;
        executor.execute(() -> {
            StepikApiClient stepikApiClient = authAndGetStepikApiClient();
            if (!isAuthenticated()) {
                return;
            }
            Map<String, StudyNode> progressMap = new HashMap<>();
            Node.this.getChildren().stream().filter(StudyNode::isUnknownStatus).forEach(child -> {
                DC data = child.getData();
                if (data != null) {
                    progressMap.put(data.getProgress(), child);
                }
            });
            D data = Node.this.getData();
            if (data != null) {
                String progressId = data.getProgress();
                if (progressId != null) {
                    progressMap.put(progressId, Node.this);
                }
                Set<String> progressIds = progressMap.keySet();
                if (!progressIds.isEmpty()) {
                    int size = progressIds.size();
                    String[] list = progressIds.toArray(new String[size]);
                    int start = 0;
                    int end;
                    while (start < size) {
                        end = start + 20;
                        if (end > size) {
                            end = size;
                        }
                        String[] part = Arrays.copyOfRange(list, start, end);
                        start = end;
                        Progresses progresses;
                        try {
                            progresses = stepikApiClient.progresses().get().id(part).execute();
                        } catch (StepikClientException e) {
                            logger.warn(e);
                            return;
                        }
                        progresses.getItems().forEach(progress -> {
                            String id = progress.getId();
                            StudyNode node = progressMap.get(id);
                            if (progress.isPassed()) {
                                node.setRawStatus(SOLVED);
                            }
                        });
                    }
                }
            }
            ApplicationManager.getApplication().invokeLater(() -> {
                if (!project.isDisposed()) {
                    ProjectView.getInstance(project).refresh();
                }
            });
        });
    }
    return status;
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) SOLVED(org.stepik.core.courseFormat.StudyStatus.SOLVED) UNCHECKED(org.stepik.core.courseFormat.StudyStatus.UNCHECKED) HashMap(java.util.HashMap) Progresses(org.stepik.api.objects.progresses.Progresses) StepikClientException(org.stepik.api.exceptions.StepikClientException) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with StepikApiClient

use of org.stepik.api.client.StepikApiClient in project intellij-plugins by StepicOrg.

the class StudyUtils method getRecommendation.

@Nullable
public static StudyNode<?, ?> getRecommendation(@NotNull StudyNode root) {
    StudyObject data = root.getData();
    if (data == null || !data.isAdaptive()) {
        return null;
    }
    StudyNode studyNode = null;
    StepikApiClient stepikClient = authAndGetStepikApiClient();
    if (!isAuthenticated()) {
        return null;
    }
    try {
        Recommendations recommendations = stepikClient.recommendations().get().course(root.getId()).execute();
        if (!recommendations.isEmpty()) {
            Recommendation recommendation = recommendations.getFirst();
            long lesson = recommendation.getLesson();
            Steps steps = stepikClient.steps().get().lesson(lesson).execute();
            if (!steps.isEmpty()) {
                long stepId = steps.getFirst().getId();
                studyNode = root.getChildByClassAndId(Step.class, stepId);
            }
        }
    } catch (StepikClientException e) {
        logger.warn(e);
    }
    return studyNode;
}
Also used : Recommendations(org.stepik.api.objects.recommendations.Recommendations) Steps(org.stepik.api.objects.steps.Steps) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) StudyObject(org.stepik.api.objects.StudyObject) Step(org.stepik.api.objects.steps.Step) StepikClientException(org.stepik.api.exceptions.StepikClientException) Recommendation(org.stepik.api.objects.recommendations.Recommendation) StudyNode(org.stepik.core.courseFormat.StudyNode) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with StepikApiClient

use of org.stepik.api.client.StepikApiClient 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 20 with StepikApiClient

use of org.stepik.api.client.StepikApiClient in project intellij-plugins by StepicOrg.

the class QuizHelper method initStepOptions.

void initStepOptions() {
    if (initialized) {
        return;
    }
    initialized = true;
    status = UNCHECKED;
    action = GET_FIRST_ATTEMPT;
    StepikApiClient stepikApiClient = authAndGetStepikApiClient();
    User user = getCurrentUser();
    if (user.isGuest()) {
        action = NEED_LOGIN;
        fail();
        initialized = false;
        return;
    }
    long userId = user.getId();
    try {
        if (!loadAttempt(stepikApiClient, userId)) {
            fail();
            initialized = false;
            return;
        }
        loadSubmission(stepikApiClient, userId);
        done();
        initialized = true;
    } catch (StepikClientException e) {
        logger.warn("Failed init test-step options", e);
        fail();
    }
}
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) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Aggregations

StepikApiClient (org.stepik.api.client.StepikApiClient)22 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)18 StepikClientException (org.stepik.api.exceptions.StepikClientException)12 NotNull (org.jetbrains.annotations.NotNull)9 StudyNode (org.stepik.core.courseFormat.StudyNode)5 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)4 StepNode (org.stepik.core.courseFormat.StepNode)4 Task (com.intellij.openapi.progress.Task)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Nullable (org.jetbrains.annotations.Nullable)3 StudyObject (org.stepik.api.objects.StudyObject)3 TokenInfo (org.stepik.api.objects.auth.TokenInfo)3 Submission (org.stepik.api.objects.submissions.Submission)3 User (org.stepik.api.objects.users.User)3 StepikAuthManager.getCurrentUser (org.stepik.core.stepik.StepikAuthManager.getCurrentUser)3 Application (com.intellij.openapi.application.Application)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Project (com.intellij.openapi.project.Project)2