Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 20c8569

Browse files
author
Irene
committed
Fix updating exercise status after submit
Also update core
1 parent 251d158 commit 20c8569

File tree

9 files changed

+61
-10
lines changed

9 files changed

+61
-10
lines changed

src/main/java/fi/helsinki/cs/tmc/cli/backend/CourseInfo.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package fi.helsinki.cs.tmc.cli.backend;
22

3+
import fi.helsinki.cs.tmc.cli.command.OrganizationCommand;
34
import fi.helsinki.cs.tmc.core.domain.Course;
45
import fi.helsinki.cs.tmc.core.domain.Exercise;
6+
import fi.helsinki.cs.tmc.core.domain.Organization;
57

68
import java.util.ArrayList;
79
import java.util.HashMap;
@@ -15,13 +17,15 @@ public class CourseInfo {
1517
private String username;
1618
private String serverAddress;
1719
private Course course;
20+
private Organization organization;
1821
private List<String> localCompletedExercises;
1922
private HashMap<String, String> properties;
2023

2124
public CourseInfo(Account account, Course course) {
2225
this.username = account.getUsername().orNull();
2326
this.serverAddress = account.getServerAddress();
2427
this.course = course;
28+
this.organization = account.getOrganization().orNull();
2529
this.properties = new HashMap<>();
2630
this.localCompletedExercises = new ArrayList<>();
2731
}
@@ -128,6 +132,14 @@ public void replaceOldExercises(List<Exercise> newExercises) {
128132
}
129133
}
130134

135+
public Organization getOrganization() {
136+
return organization;
137+
}
138+
139+
public void setOrganization(Organization organization) {
140+
this.organization = organization;
141+
}
142+
131143
public void removeProperty(String prop) {
132144
this.properties.remove(prop);
133145
}

src/main/java/fi/helsinki/cs/tmc/cli/backend/CourseInfoIo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package fi.helsinki.cs.tmc.cli.backend;
22

33
import com.google.common.base.Optional;
4+
import fi.helsinki.cs.tmc.cli.command.OrganizationCommand;
45
import fi.helsinki.cs.tmc.core.domain.Course;
56

67
import com.google.gson.Gson;
8+
import fi.helsinki.cs.tmc.core.domain.Organization;
79
import fi.helsinki.cs.tmc.core.holders.TmcSettingsHolder;
810
import org.slf4j.Logger;
911
import org.slf4j.LoggerFactory;

src/main/java/fi/helsinki/cs/tmc/cli/backend/TmcUtil.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult;
1414
import fi.helsinki.cs.tmc.core.exceptions.FailedHttpResponseException;
1515
import fi.helsinki.cs.tmc.core.exceptions.ObsoleteClientException;
16+
import fi.helsinki.cs.tmc.core.holders.TmcSettingsHolder;
1617
import fi.helsinki.cs.tmc.langs.abstraction.ValidationResult;
1718
import fi.helsinki.cs.tmc.langs.domain.RunResult;
1819

@@ -27,6 +28,7 @@
2728
import java.net.URI;
2829
import java.util.ArrayList;
2930
import java.util.List;
31+
import java.util.Optional;
3032
import java.util.concurrent.Callable;
3133

3234
public class TmcUtil {
@@ -74,9 +76,7 @@ public static boolean tryToLogin(CliContext ctx, Account account, String passwor
7476
}
7577

7678
public static List<Course> listCourses(CliContext ctx) {
77-
Callable<List<Course>> callable;
78-
callable = ctx.getTmcCore().listCourses(ProgressObserver.NULL_OBSERVER);
79-
79+
Callable<List<Course>> callable = ctx.getTmcCore().listCourses(ProgressObserver.NULL_OBSERVER);
8080
try {
8181
return callable.call();
8282
} catch (Exception e) {
@@ -199,6 +199,17 @@ public static ValidationResult runCheckStyle(CliContext ctx, Exercise exercise)
199199
}
200200
}
201201

202+
public static List<Exercise> getCourseExercises(CliContext ctx, Course course) {
203+
try {
204+
TmcCore tmcCore = ctx.getTmcCore();
205+
Course updatedCourse = tmcCore.getCourseDetails(ProgressObserver.NULL_OBSERVER, course).call();
206+
return updatedCourse.getExercises();
207+
} catch (Exception e) {
208+
logger.error("Failed to fetch exercises for course " + course.getName());
209+
return null;
210+
}
211+
}
212+
202213
public static boolean sendFeedback(
203214
CliContext ctx, List<FeedbackAnswer> answers, URI feedbackUri) {
204215
try {
@@ -212,6 +223,17 @@ public static boolean sendFeedback(
212223
}
213224
}
214225

226+
public static Organization getOrganizationForCourse(CliContext ctx, Course course) {
227+
TmcCore tmcCore = ctx.getTmcCore();
228+
Callable<Organization> callable = tmcCore.getCourseOrganization(ProgressObserver.NULL_OBSERVER, course);
229+
try {
230+
return callable.call();
231+
} catch (Exception e) {
232+
logger.error("Could not get organization for course " + course.getName());
233+
return null;
234+
}
235+
}
236+
215237
private static void handleTmcExceptions(CliContext ctx, Exception exception) {
216238
Io io = ctx.getIo();
217239
Throwable cause = exception.getCause();

src/main/java/fi/helsinki/cs/tmc/cli/command/DownloadExercisesCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public void run(CliContext context, CommandLine args) {
8787
CliProgressObserver progobs = new CliProgressObserver(io, color1, color2);
8888

8989
ctx.useAccount(finder.getAccount());
90+
if (!ctx.getSettings().getOrganization().isPresent()) {
91+
io.errorln("Failed to download exercises. Make sure you are properly logged in.");
92+
return;
93+
}
9094
CourseInfoIo.createNewCourse(course, finder.getAccount(), workDir.getWorkingDirectory());
9195
List<Exercise> exercises = TmcUtil.downloadExercises(ctx, filtered, progobs);
9296
if (exercises == null) {

src/main/java/fi/helsinki/cs/tmc/cli/command/SubmitCommand.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fi.helsinki.cs.tmc.cli.command;
22

3+
import com.google.common.base.Optional;
34
import fi.helsinki.cs.tmc.cli.backend.CourseInfo;
45
import fi.helsinki.cs.tmc.cli.backend.CourseInfoIo;
56
import fi.helsinki.cs.tmc.cli.backend.TmcUtil;
@@ -20,6 +21,7 @@
2021
import fi.helsinki.cs.tmc.core.domain.submission.FeedbackQuestion;
2122
import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult;
2223

24+
import fi.helsinki.cs.tmc.core.holders.TmcSettingsHolder;
2325
import org.apache.commons.cli.CommandLine;
2426
import org.apache.commons.cli.Options;
2527
import org.slf4j.Logger;
@@ -129,6 +131,11 @@ public void run(CliContext context, CommandLine args) {
129131

130132
resultPrinter.printSubmissionResult(result, isOnlyExercise);
131133

134+
exercise.setAttempted(true);
135+
if (result.getStatus() == SubmissionResult.Status.OK) {
136+
exercise.setCompleted(true);
137+
}
138+
132139
List<FeedbackQuestion> feedback = result.getFeedbackQuestions();
133140
if (feedback != null && feedback.size() > 0) {
134141
feedbackLists.add(feedback);
@@ -167,22 +174,22 @@ private void sendFeedbacks(List<List<FeedbackQuestion>> feedbackLists, List<Stri
167174
private void updateCourseJson(
168175
List<Exercise> submittedExercises, CourseInfo courseInfo, Path courseInfoFile) {
169176

170-
Course updatedCourse = TmcUtil.findCourse(ctx, courseInfo.getCourseName());
171-
if (updatedCourse == null) {
172-
io.errorln("Failed to update config file for course " + courseInfo.getCourseName());
177+
List<Exercise> exercises = TmcUtil.getCourseExercises(ctx, courseInfo.getCourse());
178+
if (exercises == null) {
179+
io.println(
180+
"Failed to update config file for course " + courseInfo.getCourseName());
173181
return;
174182
}
175-
176183
for (Exercise submitted : submittedExercises) {
177-
Exercise updatedEx = courseInfo.getExercise(submitted.getName());
178-
if (updatedEx == null) {
184+
java.util.Optional<Exercise> ex = exercises.stream().filter(e -> e.getName().equals(submitted.getName())).findFirst();
185+
if (!ex.isPresent()) {
179186
io.println(
180187
"Failed to update config file for exercise "
181188
+ submitted.getName()
182189
+ ". The exercise doesn't exist in server anymore.");
183190
continue;
184191
}
185-
192+
Exercise updatedEx = ex.get();
186193
if (updatedEx.isCompleted()) {
187194
if (courseInfo.getLocalCompletedExercises().contains(updatedEx.getName())) {
188195
courseInfo.getLocalCompletedExercises().remove(updatedEx.getName());

src/main/java/fi/helsinki/cs/tmc/cli/shared/CourseFinder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public boolean search(String courseName) {
5959
if (matches.isEmpty()) {
6060
//TODO we could search here for similar courses.
6161
io.errorln("Course doesn't exist.");
62+
io.errorln("Please make sure that you are logged in with the right organization.");
6263
return false;
6364
} else if (matches.size() == 1) {
6465
return handleSingleMatchingCourses(matches);

src/test/java/fi/helsinki/cs/tmc/cli/backend/CourseInfoIoTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import fi.helsinki.cs.tmc.core.domain.Course;
44

5+
import fi.helsinki.cs.tmc.core.domain.Organization;
56
import junit.framework.Assert;
67
import org.apache.commons.io.FileUtils;
78
import org.junit.After;

src/test/java/fi/helsinki/cs/tmc/cli/backend/CourseInfoTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fi.helsinki.cs.tmc.core.domain.Course;
88
import fi.helsinki.cs.tmc.core.domain.Exercise;
99

10+
import fi.helsinki.cs.tmc.core.domain.Organization;
1011
import org.junit.Before;
1112
import org.junit.Test;
1213

src/test/java/fi/helsinki/cs/tmc/cli/io/WorkDirTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import fi.helsinki.cs.tmc.core.domain.Course;
1515
import fi.helsinki.cs.tmc.core.domain.Exercise;
1616

17+
import fi.helsinki.cs.tmc.core.domain.Organization;
1718
import org.apache.commons.io.FileUtils;
1819
import org.junit.AfterClass;
1920
import org.junit.BeforeClass;

0 commit comments

Comments
 (0)