fixed Select all and delete doesn't work: "it's not allowed to delete answer placehol...
[idea/community.git] / python / educational / interactive-learning / src / com / jetbrains / edu / learning / StudyTaskManager.java
1 package com.jetbrains.edu.learning;
2
3 import com.intellij.openapi.components.*;
4 import com.intellij.openapi.project.DumbAware;
5 import com.intellij.openapi.project.Project;
6 import com.intellij.ui.JBColor;
7 import com.intellij.util.containers.hash.HashMap;
8 import com.intellij.util.xmlb.XmlSerializerUtil;
9 import com.jetbrains.edu.courseFormat.*;
10 import com.jetbrains.edu.learning.courseFormat.StudyStatus;
11 import com.jetbrains.edu.learning.courseFormat.UserTest;
12 import org.jetbrains.annotations.NotNull;
13 import org.jetbrains.annotations.Nullable;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19
20 /**
21  * Implementation of class which contains all the information
22  * about study in context of current project
23  */
24
25 @State(
26   name = "StudySettings",
27   storages = {
28     @Storage(file = StoragePathMacros.PROJECT_FILE),
29     @Storage(
30       file = StoragePathMacros.PROJECT_CONFIG_DIR + "/study_project.xml",
31       scheme = StorageScheme.DIRECTORY_BASED
32     )}
33 )
34 public class StudyTaskManager implements PersistentStateComponent<StudyTaskManager>, DumbAware {
35   private Course myCourse;
36   public Map<AnswerPlaceholder, StudyStatus> myStudyStatusMap = new HashMap<AnswerPlaceholder, StudyStatus>();
37   public Map<TaskFile, StudyStatus> myTaskStatusMap = new HashMap<TaskFile, StudyStatus>();
38   public Map<Task, List<UserTest>> myUserTests = new HashMap<Task, List<UserTest>>();
39
40   private StudyTaskManager() {
41   }
42
43   public void setCourse(@NotNull final Course course) {
44     myCourse = course;
45   }
46
47   @Nullable
48   public Course getCourse() {
49     return myCourse;
50   }
51
52   public void setStatus(AnswerPlaceholder placeholder, StudyStatus status) {
53     if (myStudyStatusMap == null) {
54       myStudyStatusMap = new HashMap<AnswerPlaceholder, StudyStatus>();
55     }
56     myStudyStatusMap.put(placeholder, status);
57   }
58
59   public void addUserTest(@NotNull final Task task, UserTest userTest) {
60     List<UserTest> userTests = myUserTests.get(task);
61     if (userTests == null) {
62       userTests = new ArrayList<UserTest>();
63       myUserTests.put(task, userTests);
64     }
65     userTests.add(userTest);
66   }
67
68   public void setUserTests(@NotNull final Task task, @NotNull final List<UserTest> userTests) {
69     myUserTests.put(task, userTests);
70   }
71
72   @NotNull
73   public List<UserTest> getUserTests(@NotNull final Task task) {
74     final List<UserTest> userTests = myUserTests.get(task);
75     return userTests != null ? userTests : Collections.<UserTest>emptyList();
76   }
77
78   public void removeUserTest(@NotNull final Task task, @NotNull final UserTest userTest) {
79     final List<UserTest> userTests = myUserTests.get(task);
80     if (userTests != null) {
81       userTests.remove(userTest);
82     }
83   }
84
85
86   public void setStatus(Task task, StudyStatus status) {
87     for (TaskFile taskFile : task.getTaskFiles().values()) {
88       setStatus(taskFile, status);
89     }
90   }
91
92   public void setStatus(TaskFile file, StudyStatus status) {
93     if (file.getAnswerPlaceholders().isEmpty()) {
94       if (myTaskStatusMap == null) {
95         myTaskStatusMap = new HashMap<TaskFile, StudyStatus>();
96       }
97       myTaskStatusMap.put(file, status);
98     }
99     for (AnswerPlaceholder answerPlaceholder : file.getAnswerPlaceholders()) {
100       setStatus(answerPlaceholder, status);
101     }
102   }
103
104   public StudyStatus getStatus(AnswerPlaceholder placeholder) {
105     StudyStatus status = myStudyStatusMap.get(placeholder);
106     if (status == null) {
107       status = StudyStatus.Unchecked;
108       myStudyStatusMap.put(placeholder, status);
109     }
110     return status;
111   }
112
113
114   public StudyStatus getStatus(@NotNull final Lesson lesson) {
115     for (Task task : lesson.getTaskList()) {
116       StudyStatus taskStatus = getStatus(task);
117       if (taskStatus == StudyStatus.Unchecked || taskStatus == StudyStatus.Failed) {
118         return StudyStatus.Unchecked;
119       }
120     }
121     return StudyStatus.Solved;
122   }
123
124   public StudyStatus getStatus(@NotNull final Task task) {
125     for (TaskFile taskFile : task.getTaskFiles().values()) {
126       StudyStatus taskFileStatus = getStatus(taskFile);
127       if (taskFileStatus == StudyStatus.Unchecked) {
128         return StudyStatus.Unchecked;
129       }
130       if (taskFileStatus == StudyStatus.Failed) {
131         return StudyStatus.Failed;
132       }
133     }
134     return StudyStatus.Solved;
135   }
136
137   private StudyStatus getStatus(@NotNull final TaskFile file) {
138     if (file.getAnswerPlaceholders().isEmpty()) {
139       if (myTaskStatusMap == null) return StudyStatus.Solved;
140       return myTaskStatusMap.get(file);
141
142     }
143     for (AnswerPlaceholder answerPlaceholder : file.getAnswerPlaceholders()) {
144       StudyStatus placeholderStatus = getStatus(answerPlaceholder);
145       if (placeholderStatus == StudyStatus.Failed) {
146         return StudyStatus.Failed;
147       }
148       if (placeholderStatus == StudyStatus.Unchecked) {
149         return StudyStatus.Unchecked;
150       }
151     }
152     return StudyStatus.Solved;
153   }
154
155
156   public JBColor getColor(@NotNull final AnswerPlaceholder placeholder) {
157     final StudyStatus status = getStatus(placeholder);
158     if (status == StudyStatus.Solved) {
159       return JBColor.GREEN;
160     }
161     if (status == StudyStatus.Failed) {
162       return JBColor.RED;
163     }
164     return JBColor.BLUE;
165   }
166
167   public boolean hasFailedAnswerPlaceholders(@NotNull final TaskFile taskFile) {
168     return taskFile.getAnswerPlaceholders().size() > 0 && getStatus(taskFile) == StudyStatus.Failed;
169   }
170   @Nullable
171   @Override
172   public StudyTaskManager getState() {
173     return this;
174   }
175
176   @Override
177   public void loadState(StudyTaskManager state) {
178     XmlSerializerUtil.copyBean(state, this);
179     if (myCourse != null) {
180       myCourse.initCourse(true);
181     }
182   }
183
184   public static StudyTaskManager getInstance(@NotNull final Project project) {
185     return ServiceManager.getService(project, StudyTaskManager.class);
186   }
187 }