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());
}));
}
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;
}
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;
}
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;
}
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();
}
}
Aggregations