use of com.jetbrains.edu.learning.courseFormat.Course in project intellij-community by JetBrains.
the class StudySmartChecker method smartCheck.
public static void smartCheck(@NotNull final AnswerPlaceholder placeholder, @NotNull final Project project, @NotNull final VirtualFile answerFile, @NotNull final TaskFile answerTaskFile, @NotNull final TaskFile usersTaskFile, @NotNull final StudyTestRunner testRunner, @NotNull final VirtualFile virtualFile, @NotNull final Document usersDocument) {
VirtualFile fileWindows = null;
File resourceFile = null;
VirtualFile windowCopy = null;
try {
final int index = placeholder.getIndex();
String windowCopyName = answerFile.getNameWithoutExtension() + index + EduNames.WINDOW_POSTFIX + answerFile.getExtension();
windowCopy = answerFile.copy(project, answerFile.getParent(), windowCopyName);
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document windowDocument = documentManager.getDocument(windowCopy);
if (windowDocument != null) {
resourceFile = StudyUtils.copyResourceFile(virtualFile.getName(), windowCopy.getName(), project, usersTaskFile.getTask());
TaskFile windowTaskFile = answerTaskFile.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(virtualFile));
if (windowTaskFile == null) {
return;
}
EduDocumentListener listener = new EduDocumentListener(windowTaskFile);
windowDocument.addDocumentListener(listener);
int start = placeholder.getOffset();
int end = start + placeholder.getRealLength();
final AnswerPlaceholder userAnswerPlaceholder = usersTaskFile.getAnswerPlaceholders().get(placeholder.getIndex());
int userStart = userAnswerPlaceholder.getOffset();
int userEnd = userStart + userAnswerPlaceholder.getRealLength();
String text = usersDocument.getText(new TextRange(userStart, userEnd));
windowDocument.replaceString(start, end, text);
ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(windowDocument));
fileWindows = EduUtils.flushWindows(windowTaskFile, windowCopy);
Process smartTestProcess = testRunner.createCheckProcess(project, windowCopy.getPath());
final CapturingProcessHandler handler = new CapturingProcessHandler(smartTestProcess, null, windowCopy.getPath());
final ProcessOutput output = handler.runProcess();
final Course course = StudyTaskManager.getInstance(project).getCourse();
if (course != null) {
boolean res = StudyTestsOutputParser.getTestsOutput(output, course.isAdaptive()).isSuccess();
StudyTaskManager.getInstance(project).setStatus(userAnswerPlaceholder, res ? StudyStatus.Solved : StudyStatus.Failed);
}
}
} catch (ExecutionException | IOException e) {
LOG.error(e);
} finally {
StudyUtils.deleteFile(windowCopy);
StudyUtils.deleteFile(fileWindows);
if (resourceFile != null && resourceFile.exists() && !resourceFile.delete()) {
LOG.error("failed to delete", resourceFile.getPath());
}
}
}
use of com.jetbrains.edu.learning.courseFormat.Course in project intellij-community by JetBrains.
the class StudyMoveDelegate method canMove.
@Override
public boolean canMove(PsiElement[] elements, @Nullable PsiElement targetContainer) {
if (elements.length == 1) {
Project project = elements[0].getProject();
Course course = StudyTaskManager.getInstance(project).getCourse();
if (course == null || !EduNames.STUDY.equals(course.getCourseMode())) {
return false;
}
return !StudyUtils.isRenameableOrMoveable(project, course, elements[0]);
}
return false;
}
use of com.jetbrains.edu.learning.courseFormat.Course in project intellij-community by JetBrains.
the class CCGetCourseFromStepic method createCourse.
private static void createCourse(Project project, String courseId) {
final VirtualFile baseDir = project.getBaseDir();
final CourseInfo info = CCStepicConnector.getCourseInfo(courseId);
if (info == null)
return;
final Course course = EduStepicConnector.getCourse(project, info);
if (course != null) {
flushCourse(course);
final File courseDirectory = StudyUtils.getCourseDirectory(course);
ApplicationManager.getApplication().invokeAndWait(() -> ApplicationManager.getApplication().runWriteAction(() -> {
final VirtualFile[] children = baseDir.getChildren();
for (VirtualFile child : children) {
StudyUtils.deleteFile(child);
}
StudyGenerator.createCourse(course, baseDir, courseDirectory, project);
}));
StudyTaskManager.getInstance(project).setCourse(course);
File courseDir = new File(OUR_COURSES_DIR, course.getName() + "-" + project.getName());
course.setCourseDirectory(courseDir.getPath());
course.setCourseMode(CCUtils.COURSE_MODE);
project.getBaseDir().refresh(false, true);
int index = 1;
int taskIndex = 1;
for (Lesson lesson : course.getLessons()) {
final VirtualFile lessonDir = project.getBaseDir().findChild(EduNames.LESSON + String.valueOf(index));
lesson.setIndex(index);
if (lessonDir == null)
continue;
for (Task task : lesson.getTaskList()) {
final VirtualFile taskDir = lessonDir.findChild(EduNames.TASK + String.valueOf(taskIndex));
task.setIndex(taskIndex);
task.setLesson(lesson);
if (taskDir == null)
continue;
for (final Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
ApplicationManager.getApplication().invokeAndWait(() -> ApplicationManager.getApplication().runWriteAction(() -> createAnswerFile(project, taskDir, entry)));
}
taskIndex += 1;
}
index += 1;
taskIndex = 1;
}
course.initCourse(true);
ApplicationManager.getApplication().invokeAndWait(() -> StudyUtils.registerStudyToolWindow(course, project));
}
VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
ProjectView.getInstance(project).refresh();
}
use of com.jetbrains.edu.learning.courseFormat.Course in project intellij-community by JetBrains.
the class CCLessonMoveHandlerDelegate method doMove.
@Override
public void doMove(final Project project, PsiElement[] elements, @Nullable PsiElement targetContainer, @Nullable MoveCallback callback) {
if (targetContainer == null || !(targetContainer instanceof PsiDirectory)) {
return;
}
final Course course = StudyTaskManager.getInstance(project).getCourse();
if (course == null) {
return;
}
final PsiDirectory sourceDirectory = (PsiDirectory) elements[0];
final Lesson sourceLesson = course.getLesson(sourceDirectory.getName());
final Lesson targetLesson = course.getLesson(((PsiDirectory) targetContainer).getName());
if (targetLesson == null) {
Messages.showInfoMessage("Lessons can be moved only to other lessons", "Incorrect Target For Move");
return;
}
final CCMoveStudyItemDialog dialog = new CCMoveStudyItemDialog(project, EduNames.LESSON, targetLesson.getName());
dialog.show();
if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE) {
return;
}
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
try {
sourceDirectory.getVirtualFile().rename(this, "tmp");
} catch (IOException e) {
LOG.error(e);
}
}
});
final VirtualFile[] lessonDirs = project.getBaseDir().getChildren();
final Function<VirtualFile, StudyItem> getStudyItem = file -> course.getLesson(file.getName());
int sourceLessonIndex = sourceLesson.getIndex();
sourceLesson.setIndex(-1);
CCUtils.updateHigherElements(lessonDirs, getStudyItem, sourceLessonIndex, EduNames.LESSON, -1);
final int newItemIndex = targetLesson.getIndex() + dialog.getIndexDelta();
CCUtils.updateHigherElements(lessonDirs, getStudyItem, newItemIndex - 1, EduNames.LESSON, 1);
sourceLesson.setIndex(newItemIndex);
Collections.sort(course.getLessons(), EduUtils.INDEX_COMPARATOR);
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
try {
sourceDirectory.getVirtualFile().rename(this, EduNames.LESSON + newItemIndex);
} catch (IOException e) {
LOG.error(e);
}
}
});
}
use of com.jetbrains.edu.learning.courseFormat.Course in project intellij-community by JetBrains.
the class CCTaskMoveHandlerDelegate method isTaskDir.
private static boolean isTaskDir(PsiDirectory sourceDirectory) {
if (sourceDirectory == null) {
return false;
}
Project project = sourceDirectory.getProject();
Course course = StudyTaskManager.getInstance(project).getCourse();
if (course == null || !CCUtils.isCourseCreator(project)) {
return false;
}
return EduUtils.getTask(sourceDirectory, course) != null;
}
Aggregations