Search in sources :

Example 11 with StepikApiClient

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

the class Utils method getLessonStudyObject.

private static StudyObject getLessonStudyObject(long lessonId, long unitId) {
    StepikApiClient stepikApiClient = authAndGetStepikApiClient();
    CompoundUnitLesson unitLesson = getCompoundUnitLessonStudyObject(stepikApiClient, unitId, lessonId);
    Unit unit = unitLesson.getUnit();
    if (unit.getId() != 0) {
        Section section = getSectionStudyObject(stepikApiClient, unit.getSection());
        if (section != null) {
            return getCourseStudyObject(section.getCourse());
        }
    }
    return unitLesson;
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) CompoundUnitLesson(org.stepik.api.objects.lessons.CompoundUnitLesson) Unit(org.stepik.api.objects.units.Unit) Section(org.stepik.api.objects.sections.Section)

Example 12 with StepikApiClient

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

the class StepikSendAction method check.

private void check(@NotNull Project project) {
    logger.info("Start checking step");
    StudyNode<?, ?> selected = StepikProjectManager.getSelected(project);
    if (!(selected instanceof StepNode)) {
        logger.info("Stop checking step: step is null or is not StepNode ");
        return;
    }
    StepNode stepNode = (StepNode) selected;
    String title = "Checking Step: " + stepNode.getName();
    ProgressManager.getInstance().run(new Task.Backgroundable(project, title) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            StepikApiClient stepikApiClient = authAndGetStepikApiClient(true);
            if (!isAuthenticated()) {
                return;
            }
            Long submissionId = sendStep(stepikApiClient, project, stepNode);
            if (submissionId == null) {
                return;
            }
            Metrics.sendAction(project, stepNode, SUCCESSFUL);
            SendAction.checkStepStatus(project, stepikApiClient, stepNode, submissionId, indicator);
            logger.info(String.format("Finish checking step: id=%s", stepNode.getId()));
        }
    });
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) Task(com.intellij.openapi.progress.Task) StepNode(org.stepik.core.courseFormat.StepNode) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 13 with StepikApiClient

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

the class Utils method getCourseStudyObject.

@NotNull
private static StudyObject getCourseStudyObject(long id) {
    StepikApiClient stepikApiClient = authAndGetStepikApiClient();
    Course course = getCourse(stepikApiClient, id);
    return course != null ? course : EMPTY_STUDY_OBJECT;
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) Course(org.stepik.api.objects.courses.Course) NotNull(org.jetbrains.annotations.NotNull)

Example 14 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 15 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)

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