public JSONObject checkTest(JSONRPC2Request req, HttpServletRequest request) throws JSONRPC2Error, Exception { JSONObject jsonResult = new JSONObject(); JSONArray jsonWrongQuestions = new JSONArray(); QuestionsList wrongQuestions = new QuestionsList(); // retrievers Map<String, Object> params = req.getNamedParams(); NamedParamsRetriever np = new NamedParamsRetriever(params); Map<String, Object> tParams = np.getMap("test"); NamedParamsRetriever testNp = new NamedParamsRetriever(tParams); // get guessed questions values QuestionsList guessQuestions = new QuestionsList(); List<Object> questions = testNp.getList("questions"); for (Object q : questions) { // set new retriever for each question NamedParamsRetriever questNp = new NamedParamsRetriever((Map<String, Object>) q); // retrieve values String answer = questNp.getString("answer"); String body = questNp.getString("body"); int id = questNp.getInt("id"); // create question with the values and add it to the guessed questions Question quest = new Question(); quest.setId(id); quest.setBody(body); quest.setAnswer(answer); guessQuestions.add(quest); } // get test int tid = testNp.getInt("id"); MySQLDAO dao = new MySQLDAO(); Test t = dao.loadTest(tid); // get true questions MySQLDAO questionsDao = new MySQLDAO(); QuestionsList trueQuestions = questionsDao.loadQuestionsByTest(tid); // cycle through questions and find these with same id for (Question tq : trueQuestions) { for (Question gq : guessQuestions) { if (tq.getId() == gq.getId()) { // if answers are different, than there is mistake System.out.println("id match!"); if (!tq.getAnswer().equals(gq.getAnswer())) { System.out.println("Wrong question!"); wrongQuestions.add(gq); } } } } jsonWrongQuestions = wrongQuestions.toJSONArray(); jsonResult.put("test", t); jsonResult.put("wrongQuestions", jsonWrongQuestions); return jsonResult; }
public JSONObject addTestQuestion(JSONRPC2Request req, HttpServletRequest request) throws Exception, SQLException { JSONObject jsonResult = new JSONObject(); // set retrievers Map<String, Object> params = req.getNamedParams(); NamedParamsRetriever np = new NamedParamsRetriever(params); Map<String, Object> tParams = np.getMap("test"); NamedParamsRetriever testNp = new NamedParamsRetriever(tParams); Map<String, Object> qParams = np.getMap("question"); NamedParamsRetriever questNp = new NamedParamsRetriever(qParams); int tid = testNp.getInt(("id")); int qid = questNp.getInt(("id")); // load test and question MySQLDAO dao = new MySQLDAO(); Test t = dao.loadTest(tid); dao = new MySQLDAO(); Question q = dao.loadQuestion(qid); // load current user User u = getCurrentUser(request); if (u.getId() == t.getOwnerId()) { // check for duplicates boolean duplicate = false; for (Question quest : t.getQuestions()) { if (quest.getId() == qid) { duplicate = true; } } if (!duplicate) { dao = new MySQLDAO(); t = dao.saveTestQuestion(t, q); } } else { throw new Exception("no privileges : you do not own this test!"); } // set result JSONObject jsonTest = t.toJSONObject(); JSONObject jsonQuestion = q.toJSONObject(); jsonResult.put("test", jsonTest); jsonResult.put("question", jsonQuestion); return jsonResult; }