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