public static ArrayList<Question> getAllQuestionByType(int type) { ArrayList<Question> questions = new ArrayList<>(); SQLiteDatabase sqLiteDatabase = openConnection(); String query = "SELECT Questions. *, Images.ImageData FROM Questions " + "LEFT JOIN Images ON Questions.ImageId = Images.Id WHERE Questions.Type = ?"; Cursor cursor = sqLiteDatabase.rawQuery(query, new String[] {"" + type}); cursor.moveToFirst(); int tmp = 1; while (!cursor.isAfterLast()) { Question question = new Question(); question.index = tmp; question.id = cursor.getInt(0); question.question = fixQuestion(byteArrayToString(cursor.getBlob(2))); question.answer1 = fixAnswer(cursor.getString(3)); question.answer2 = fixAnswer(cursor.getString(4)); question.answer3 = fixAnswer(cursor.getString(5)); question.answer4 = fixAnswer(cursor.getString(6)); question.correctAnswer = cursor.getInt(7); question.fixedAnswer = cursor.getInt(9); question.isSentFixedAnswer = cursor.getInt(10); question.imageData = cursor.getBlob(11); question.type = cursor.getInt(1); questions.add(question); cursor.moveToNext(); tmp++; } cursor.close(); closeConnection(); return questions; }
public static List<Question> generateQuestionSet(int difficulty, int questionSetSize) { List<Question> questionList = new ArrayList<>(questionSetSize); for (int i = 0; i < questionSetSize; ++i) { try { Question q = generateQuestion(difficulty, 4 * questionSetSize, true); q.question = q.getQuestion().replaceAll("\\s\\s", " "); q.question = q.getQuestion().replaceAll("\\(\\s", "("); q.question = q.getQuestion().replaceAll("\\s\\)", ")"); q.question = q.getQuestion().replaceAll("\\s\\?", "?"); q.question = q.getQuestion().replaceAll("\\s\\.", "."); for (int j = 0; j < q.getChoices().size(); ++j) { q.getChoices().set(j, q.getChoices().get(j).trim()); } questionList.add(q); } catch (SQLException e) { e.printStackTrace(); } } return questionList; }
public static ArrayList<Question> getQuestionsByTypeAndExamId( int type, int examId, boolean isRandom, int numberOfQuestions) { ArrayList<Question> questions = new ArrayList<>(); SQLiteDatabase sqLiteDatabase = openConnection(); String query; Cursor cursor; if (!isRandom) { query = "select tmp.* from Exams, (SELECT Questions. *, Images.ImageData FROM Questions LEFT JOIN " + "Images ON Questions.ImageId = Images.Id) as tmp where " + "Exams.ExamId=? and Exams.Type=? and Exams.QuestionId=tmp.Id"; cursor = sqLiteDatabase.rawQuery(query, new String[] {"" + examId, "" + type}); } else { query = "SELECT Questions. *, Images.ImageData FROM Questions LEFT JOIN " + "Images ON Questions.ImageId = Images.Id WHERE Questions.Type = ? " + "order by Random() limit " + numberOfQuestions; cursor = sqLiteDatabase.rawQuery(query, new String[] {"" + type}); } cursor.moveToFirst(); int tmp = 1; while (!cursor.isAfterLast()) { Question question = new Question(); question.index = tmp; question.id = cursor.getInt(0); question.question = fixQuestion(byteArrayToString(cursor.getBlob(2))); question.answer1 = fixAnswer(cursor.getString(3)); question.answer2 = fixAnswer(cursor.getString(4)); question.answer3 = fixAnswer(cursor.getString(5)); question.answer4 = fixAnswer(cursor.getString(6)); question.correctAnswer = cursor.getInt(7); question.fixedAnswer = cursor.getInt(9); question.isSentFixedAnswer = cursor.getInt(10); question.imageData = cursor.getBlob(11); question.type = cursor.getInt(1); questions.add(question); cursor.moveToNext(); tmp++; } cursor.close(); closeConnection(); return questions; }
private Question GetSingleQuestion(JSONObject json) throws JSONException { Question item = new Question(); item.id = json.getInt("id"); item.question = json.getString("question"); item.control = json.getString("control"); item.sequenceNumber = json.getInt("sequenceNumber"); if (item.control.equals("DROP")) { List<PossibleAnswer> answers = GetPossibleAnswers(json.getJSONArray("possibleanswers")); item.possibleAnswers = answers; } if (item.control.equals("FILD")) { // handle EditFieldInfo class - TBD if time permits } return item; }