Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static ch.epfl.qedit.view.home.HomeActivity.USER;
import static ch.epfl.qedit.view.home.HomeQuizListFragment.QUIZ_ID;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.content.Intent;
import android.os.Bundle;
Expand All @@ -29,8 +29,10 @@
import ch.epfl.qedit.backend.permission.PermManagerFactory;
import ch.epfl.qedit.model.Question;
import ch.epfl.qedit.model.Quiz;
import ch.epfl.qedit.model.User;
import ch.epfl.qedit.model.answer.AnswerFormat;
import ch.epfl.qedit.model.answer.MatrixFormat;
import ch.epfl.qedit.view.quiz.QuizActivity;
import ch.epfl.qedit.view.treasurehunt.QuestionLocatorActivity;
import ch.epfl.qedit.view.treasurehunt.TreasureHuntActivity;
import java.util.Collections;
Expand All @@ -50,7 +52,7 @@ public class TreasureHuntActivityTest {
public void init(int numQuestions) {
Intent intent = new Intent();
Bundle bundle = new Bundle();

User user = new User("Charles", "Magne");
// We create a mock permission service, otherwise the UI test might fail because
// android shows a permission popup
PermManagerFactory.setInstance(new MockPermManager());
Expand All @@ -66,7 +68,7 @@ public void init(int numQuestions) {
AnswerFormat format = MatrixFormat.singleField(MatrixFormat.Field.textField(""));
Question question = new Question("title", "text", format, 42, 43, 100);
Quiz quiz = new Quiz("title", Collections.nCopies(numQuestions, question), true);

bundle.putSerializable(USER, user);
bundle.putSerializable(QUIZ_ID, quiz);
intent.putExtras(bundle);

Expand Down Expand Up @@ -149,10 +151,10 @@ public void testLastQuestionEndsTreasureHunt() {
onView(withText(R.string.treasure_hunt_start)).perform(click());
findQuestion();

// Clicking on "done" for the last question should finish the activity
// Clicking on "done" for the last question should start the correction
assertFalse(testRule.getActivity().isFinishing());
onView(withId(R.id.question_done)).perform(click());
onView(withId(android.R.id.button1)).perform(click());
assertTrue(testRule.getActivity().isFinishing());
intended(allOf(hasComponent(QuizActivity.class.getName())));
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/ch/epfl/qedit/model/answer/MatrixModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ private boolean checkFieldEquals(
}
}

public int getNumRows() {
return numRows;
}

public int getNumCols() {
return numCols;
}

@Override
public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static ch.epfl.qedit.view.edit.EditQuizSettingsDialog.NO_FILTER;
import static ch.epfl.qedit.view.edit.EditQuizSettingsDialog.QUIZ_BUILDER;
import static ch.epfl.qedit.view.home.HomeActivity.USER;
import static ch.epfl.qedit.view.quiz.QuizActivity.CORRECTION;

import android.app.Dialog;
import android.content.Intent;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class HomeQuizListFragment extends Fragment
private int modifyIndex = -1;

// The user and its quizzes (id and title of the quizzes)

private User user;
private List<Map.Entry<String, String>> quizzes;

Expand Down Expand Up @@ -280,6 +282,8 @@ private void launchQuizActivity(Quiz quiz) {
// We put the quiz into the bundle
Bundle bundle = new Bundle();
bundle.putSerializable(QUIZ_ID, quiz);
bundle.putBoolean(CORRECTION, false);
bundle.putSerializable(USER, user);
intent.putExtras(bundle);

// And start the activity
Expand Down
77 changes: 77 additions & 0 deletions app/src/main/java/ch/epfl/qedit/view/quiz/CorrectionUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ch.epfl.qedit.view.quiz;

import static ch.epfl.qedit.view.home.HomeActivity.USER;
import static ch.epfl.qedit.view.home.HomeQuizListFragment.QUIZ_ID;
import static ch.epfl.qedit.view.quiz.QuizActivity.CORRECTION;
import static ch.epfl.qedit.view.quiz.QuizActivity.GOOD_ANSWERS;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import ch.epfl.qedit.model.Question;
import ch.epfl.qedit.model.Quiz;
import ch.epfl.qedit.model.User;
import ch.epfl.qedit.model.answer.AnswerModel;
import ch.epfl.qedit.model.answer.MatrixFormat;
import ch.epfl.qedit.model.answer.MatrixModel;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public final class CorrectionUtil {
private CorrectionUtil() {}

private static ArrayList<Integer> correctedQuestions;

public static List<Question> correctedQuestions(
ImmutableList<Question> questions, HashMap<Integer, AnswerModel> answers) {
correctedQuestions = new ArrayList<>();
List<Question> corrected = new ArrayList<>();
for (int i = 0; i < questions.size(); i++) {
MatrixModel answerModel = (MatrixModel) answers.get(i);
int goodAnswer = questions.get(i).getFormat().correct(answerModel) ? 1 : 0;
correctedQuestions.add(i, goodAnswer);
corrected.add(i, makePrefilledMatrixQuestion(answerModel, questions.get(i)));
}
return corrected;
}

static Question makePrefilledMatrixQuestion(MatrixModel answerModel, Question quizQuestion) {
MatrixModel model;
if (answerModel == null) {
model = (MatrixModel) quizQuestion.getFormat().getEmptyAnswerModel();
} else {
model = answerModel;
}
MatrixFormat.Builder builder =
new MatrixFormat.Builder(model.getNumRows(), model.getNumCols());
for (int x = 0; x < model.getNumRows(); x++) {
for (int y = 0; y < model.getNumCols(); y++) {
builder.withField(x, y, MatrixFormat.Field.preFilledField(model.getAnswer(x, y)));
}
}
return new Question(quizQuestion.getTitle(), quizQuestion.getText(), builder.build());
}

public static ArrayList<Integer> getGoodAnswers() {
return correctedQuestions;
}

public static void startCorrection(
ImmutableList<Question> questions,
HashMap<Integer, AnswerModel> answers,
Context context,
User user) {
Quiz questionsLocked = new Quiz("Correction", correctedQuestions(questions, answers));
Intent intent = new Intent(context, QuizActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putSerializable(QUIZ_ID, questionsLocked);
bundle.putIntegerArrayList(GOOD_ANSWERS, CorrectionUtil.getGoodAnswers());
bundle.putSerializable(USER, user);
bundle.putBoolean(CORRECTION, true);
intent.putExtras(bundle);
context.startActivity(intent);
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/ch/epfl/qedit/view/quiz/QuizActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Objects;

public class QuizActivity extends AppCompatActivity implements ConfirmDialog.ConfirmationListener {

public static final String GOOD_ANSWERS = "ch.epfl.qedit.quiz.GOOD_ANSWERS";
public static final String CORRECTION = "ch.epfl.qedit.quiz.CORRECTION";
private QuizViewModel model;
private Boolean overviewActive;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.epfl.qedit.view.treasurehunt;

import static ch.epfl.qedit.view.home.HomeActivity.USER;
import static ch.epfl.qedit.view.home.HomeQuizListFragment.QUIZ_ID;

import android.content.Intent;
Expand All @@ -14,6 +15,8 @@
import ch.epfl.qedit.R;
import ch.epfl.qedit.model.Question;
import ch.epfl.qedit.model.Quiz;
import ch.epfl.qedit.model.User;
import ch.epfl.qedit.view.quiz.CorrectionUtil;
import ch.epfl.qedit.view.quiz.QuestionFragment;
import ch.epfl.qedit.view.util.ConfirmDialog;
import ch.epfl.qedit.viewmodel.QuizViewModel;
Expand Down Expand Up @@ -41,6 +44,7 @@ public class TreasureHuntActivity extends AppCompatActivity
// to help the user, and the second is used to show the question.
private View helperView;
private FragmentContainerView questionView;
private User user;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -54,7 +58,7 @@ protected void onCreate(Bundle savedInstanceState) {
// We retrieve the quiz from the intent that started the activity
Intent intent = getIntent();
quiz = (Quiz) Objects.requireNonNull(intent.getExtras()).getSerializable(QUIZ_ID);

user = (User) Objects.requireNonNull(intent.getExtras().getSerializable(USER));
// Uncomment those lines (and comments the two above lines) to test the UI on your emulator
// Location loc1 = new Location("");
// loc.setLongitude(1);
Expand Down Expand Up @@ -136,7 +140,9 @@ public void onConfirm(ConfirmDialog dialog) {
if (currIndex == quiz.getQuestions().size() - 1) {
// If the last question was answered, we finish the activity.
// In the future, we have to go to the result page.
finish();
CorrectionUtil.startCorrection(
quiz.getQuestions(), model.getAnswers().getValue(), this, user);

} else {
// If there are remaining questions, we locate the next one
locateQuestion(currIndex + 1);
Expand Down