summaryrefslogtreecommitdiff
path: root/python/edu/learn-python/src/com/jetbrains/python/edu/course/Task.java
blob: 2323412f4374c1a61f3ce6ba2a9b3129bd6eefd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.jetbrains.python.edu.course;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.xmlb.annotations.Transient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.jetbrains.python.edu.StudyUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Implementation of task which contains task files, tests, input file for tests
 */
public class Task implements Stateful{
  public static final String TASK_DIR = "task";
  private static final String ourTestFile = "tests.py";
  public String name;
  private static final String ourTextFile = "task.html";
  public Map<String, TaskFile> taskFiles = new HashMap<String, TaskFile>();
  private Lesson myLesson;
  public int myIndex;
  public List<UserTest> userTests = new ArrayList<UserTest>();
  public static final String USER_TESTS = "userTests";

  public Map<String, TaskFile> getTaskFiles() {
    return taskFiles;
  }

  @Transient
  public StudyStatus getStatus() {
    for (TaskFile taskFile : taskFiles.values()) {
      StudyStatus taskFileStatus = taskFile.getStatus();
      if (taskFileStatus == StudyStatus.Unchecked) {
        return StudyStatus.Unchecked;
      }
      if (taskFileStatus == StudyStatus.Failed) {
        return StudyStatus.Failed;
      }
    }
    return StudyStatus.Solved;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setStatus(@NotNull final StudyStatus status, @NotNull final StudyStatus oldStatus) {
    LessonInfo lessonInfo = myLesson.getLessonInfo();
    if (status != oldStatus) {
      lessonInfo.update(oldStatus, -1);
      lessonInfo.update(status, +1);
    }
    for (TaskFile taskFile : taskFiles.values()) {
      taskFile.setStatus(status, oldStatus);
    }
  }

  public List<UserTest> getUserTests() {
    return userTests;
  }

  public String getTestFile() {
    return ourTestFile;
  }

  public String getText() {
    return ourTextFile;
  }

  /**
   * Creates task directory in its lesson folder in project user created
   *
   * @param lessonDir    project directory of lesson which task belongs to
   * @param resourceRoot directory where original task file stored
   * @throws java.io.IOException
   */
  public void create(@NotNull final VirtualFile lessonDir, @NotNull final File resourceRoot) throws IOException {
    VirtualFile taskDir = lessonDir.createChildDirectory(this, TASK_DIR + Integer.toString(myIndex + 1));
    File newResourceRoot = new File(resourceRoot, taskDir.getName());
    int i = 0;
    for (Map.Entry<String, TaskFile> taskFile : taskFiles.entrySet()) {
      TaskFile taskFileContent = taskFile.getValue();
      taskFileContent.setIndex(i);
      i++;
      taskFileContent.create(taskDir, newResourceRoot, taskFile.getKey());
    }
    File[] filesInTask = newResourceRoot.listFiles();
    if (filesInTask != null) {
      for (File file : filesInTask) {
        String fileName = file.getName();
        if (!isTaskFile(fileName)) {
          File resourceFile = new File(newResourceRoot, fileName);
          File fileInProject = new File(taskDir.getCanonicalPath(), fileName);
          FileUtil.copy(resourceFile, fileInProject);
        }
      }
    }
  }

  private boolean isTaskFile(@NotNull final String fileName) {
    return taskFiles.get(fileName) != null;
  }

  @Nullable
  public TaskFile getFile(@NotNull final String fileName) {
    return taskFiles.get(fileName);
  }

  /**
   * Initializes state of task file
   *
   * @param lesson lesson which task belongs to
   */
  public void init(final Lesson lesson, boolean isRestarted) {
    myLesson = lesson;
    for (TaskFile taskFile : taskFiles.values()) {
      taskFile.init(this, isRestarted);
    }
  }

  public Task next() {
    Lesson currentLesson = this.myLesson;
    List<Task> taskList = myLesson.getTaskList();
    if (myIndex + 1 < taskList.size()) {
      return taskList.get(myIndex + 1);
    }
    Lesson nextLesson = currentLesson.next();
    if (nextLesson == null) {
      return null;
    }
    return StudyUtils.getFirst(nextLesson.getTaskList());
  }

  public Task prev() {
    Lesson currentLesson = this.myLesson;
    if (myIndex - 1 >= 0) {
      return myLesson.getTaskList().get(myIndex - 1);
    }
    Lesson prevLesson = currentLesson.prev();
    if (prevLesson == null) {
      return null;
    }
    //getting last task in previous lesson
    return prevLesson.getTaskList().get(prevLesson.getTaskList().size() - 1);
  }

  public void setIndex(int index) {
    myIndex = index;
  }

  public int getIndex() {
    return myIndex;
  }

  public Lesson getLesson() {
    return myLesson;
  }


  @Nullable
  public VirtualFile getTaskDir(Project project) {
    String lessonDirName = Lesson.LESSON_DIR + String.valueOf(myLesson.getIndex() + 1);
    String taskDirName = TASK_DIR + String.valueOf(myIndex + 1);
    VirtualFile courseDir = project.getBaseDir();
    if (courseDir != null) {
      VirtualFile lessonDir = courseDir.findChild(lessonDirName);
      if (lessonDir != null) {
        return lessonDir.findChild(taskDirName);
      }
    }
    return null;
  }

  /**
   * Gets text of resource file such as test input file or task text in needed format
   *
   * @param fileName name of resource file which should exist in task directory
   * @param wrapHTML if it's necessary to wrap text with html tags
   * @return text of resource file wrapped with html tags if necessary
   */
  @Nullable
  public String getResourceText(@NotNull final Project project, @NotNull final String fileName, boolean wrapHTML) {
    VirtualFile taskDir = getTaskDir(project);
    if (taskDir != null) {
      return StudyUtils.getFileText(taskDir.getCanonicalPath(), fileName, wrapHTML);
    }
    return null;
  }

}