separate course-creator stepic connector from student one
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCPushCourse.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.ide.IdeView;
4 import com.intellij.openapi.actionSystem.AnActionEvent;
5 import com.intellij.openapi.actionSystem.CommonDataKeys;
6 import com.intellij.openapi.actionSystem.LangDataKeys;
7 import com.intellij.openapi.actionSystem.Presentation;
8 import com.intellij.openapi.progress.ProgressIndicator;
9 import com.intellij.openapi.progress.ProgressManager;
10 import com.intellij.openapi.progress.Task;
11 import com.intellij.openapi.project.DumbAwareAction;
12 import com.intellij.openapi.project.Project;
13 import com.jetbrains.edu.coursecreator.CCUtils;
14 import com.jetbrains.edu.learning.StudyTaskManager;
15 import com.jetbrains.edu.learning.courseFormat.Course;
16 import com.jetbrains.edu.learning.courseFormat.Lesson;
17 import com.jetbrains.edu.learning.statistics.EduUsagesCollector;
18 import com.jetbrains.edu.learning.stepic.CCStepicConnector;
19 import com.jetbrains.edu.learning.stepic.CourseInfo;
20 import org.jetbrains.annotations.NotNull;
21
22 import java.util.List;
23
24 public class CCPushCourse extends DumbAwareAction {
25   public CCPushCourse() {
26     super("Upload Course to Stepik", "Upload Course to Stepik", null);
27   }
28
29   @Override
30   public void update(@NotNull AnActionEvent e) {
31     Presentation presentation = e.getPresentation();
32     Project project = e.getProject();
33     presentation.setEnabledAndVisible(project != null && CCUtils.isCourseCreator(project));
34     if (project != null) {
35       final Course course = StudyTaskManager.getInstance(project).getCourse();
36       if (course != null) {
37         final int id = course.getId();
38         if (id > 0) {
39           presentation.setText("Update Course on Stepik");
40         }
41       }
42     }
43   }
44
45   @Override
46   public void actionPerformed(@NotNull AnActionEvent e) {
47     final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
48     final Project project = e.getData(CommonDataKeys.PROJECT);
49     if (view == null || project == null) {
50       return;
51     }
52     final Course course = StudyTaskManager.getInstance(project).getCourse();
53     if (course == null) {
54       return;
55     }
56     if (course.getId() > 0) {
57       ProgressManager.getInstance().run(new Task.Modal(project, "Updating Course", true) {
58         @Override
59         public void run(@NotNull ProgressIndicator indicator) {
60           for (Lesson lesson : course.getLessons()) {
61             if (lesson.getId() > 0) {
62               CCStepicConnector.updateLesson(project, lesson, indicator);
63             }
64             else {
65               final CourseInfo info = CourseInfo.fromCourse(course);
66               final int lessonId = CCStepicConnector.postLesson(project, lesson, indicator);
67               final List<Integer> sections = info.getSections();
68               final Integer sectionId = sections.get(sections.size() - 1);
69               CCStepicConnector.postUnit(project, lessonId, lesson.getIndex(), sectionId);
70             }
71           }
72         }
73       });
74     }
75     else {
76       CCStepicConnector.postCourseWithProgress(project, course);
77     }
78     EduUsagesCollector.courseUploaded();
79   }
80 }