Skip to content
Snippets Groups Projects
LecturerQuestionController.java 15.4 KiB
Newer Older
 * This file is part of ARSnova Backend.
 * Copyright (C) 2012-2015 The ARSnova Team
 * ARSnova Backend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ARSnova Backend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.thm.arsnova.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.exceptions.BadRequestException;
import de.thm.arsnova.exceptions.ForbiddenException;
import de.thm.arsnova.exceptions.NoContentException;
import de.thm.arsnova.exceptions.NotFoundException;
import de.thm.arsnova.services.IQuestionService;
import de.thm.arsnova.web.DeprecatedApi;
Daniel Gerhardt's avatar
Daniel Gerhardt committed
@RequestMapping("/lecturerquestion")
public class LecturerQuestionController extends AbstractController {
	public static final Logger LOGGER = LoggerFactory.getLogger(LecturerQuestionController.class);

	@Autowired
	private IQuestionService questionService;

	@RequestMapping(value = "/{questionId}", method = RequestMethod.GET)
	public Question getQuestion(@PathVariable final String questionId) {
		final Question question = questionService.getQuestion(questionId);
		if (question != null) {
			return question;
		}

Christoph Thelen's avatar
Christoph Thelen committed
	@RequestMapping(value = "/", method = RequestMethod.POST)
	public Question postQuestion(@RequestBody final Question question) {
		if (questionService.saveQuestion(question) != null) {
			return question;
		}
	@RequestMapping(value = "/{questionId}", method = RequestMethod.PUT)
	public Question updateQuestion(
			@PathVariable final String questionId,
			return questionService.update(question);
		} catch (final Exception e) {
			throw new BadRequestException();
		}
	@RequestMapping(value = "/{questionId}/startNewPiRound", method = RequestMethod.GET)
	public void startSecondPiRound(
			@PathVariable final String questionId,
			@RequestParam(value = "time", defaultValue = "0", required = false) final int time
			) {

		if(time == 0) {
			questionService.startNewPiRound(questionId, null);
		} else {
			questionService.startNewPiRoundDelayed(questionId, time);
		}
	}

Christoph Thelen's avatar
Christoph Thelen committed
	@RequestMapping(value = "/{questionId}/publish", method = RequestMethod.POST)
	public void publishQuestion(
			@PathVariable final String questionId,
			@RequestParam(required = false) final Boolean publish,
		if (publish != null) {
			question.setActive(publish);
		}
		questionService.update(question);
	@RequestMapping(value = "/publish", method = RequestMethod.POST)
	public void publishAllQuestions(
			@RequestParam final String sessionkey,
			@RequestParam(required = false) final Boolean publish,
			@RequestParam(value = "lecturequestionsonly", defaultValue = "false", required = false) final boolean lectureQuestionsOnly,
			@RequestParam(value = "preparationquestionsonly", defaultValue = "false", required = false) final boolean preparationQuestionsOnly
Daniel Gerhardt's avatar
Daniel Gerhardt committed

Daniel Gerhardt's avatar
Daniel Gerhardt committed

		if (lectureQuestionsOnly) {
			questions = questionService.getLectureQuestions(sessionkey);
			questionService.publishQuestions(sessionkey, publish, questions);
		} else if (preparationQuestionsOnly) {
			questions = questionService.getPreparationQuestions(sessionkey);
			questionService.publishQuestions(sessionkey, publish, questions);
		} else {
			questionService.publishAll(sessionkey, p);
		}
Christoph Thelen's avatar
Christoph Thelen committed
	@RequestMapping(value = "/{questionId}/publishstatistics", method = RequestMethod.POST)
	public void publishStatistics(
			@PathVariable final String questionId,
			@RequestParam(required = false) final Boolean showStatistics,
		if (showStatistics != null) {
			question.setShowStatistic(showStatistics);
		}
		questionService.update(question);
	@RequestMapping(value = "/{questionId}/publishcorrectanswer", method = RequestMethod.POST)
	public void publishCorrectAnswer(
			@PathVariable final String questionId,
			@RequestParam(required = false) final Boolean showCorrectAnswer,
		if (showCorrectAnswer != null) {
			question.setShowAnswer(showCorrectAnswer);
		}
		questionService.update(question);
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public List<Question> getSkillQuestions(
			@RequestParam final String sessionkey,
			@RequestParam(value = "lecturequestionsonly", defaultValue = "false") final boolean lectureQuestionsOnly,
			@RequestParam(value = "flashcardsonly", defaultValue = "false") final boolean flashcardsOnly,
			@RequestParam(value = "preparationquestionsonly", defaultValue = "false") final boolean preparationQuestionsOnly,
			final HttpServletResponse response
		List<Question> questions;
		if (lectureQuestionsOnly) {
			questions = questionService.getLectureQuestions(sessionkey);
		} else if (flashcardsOnly) {
			questions = questionService.getFlashcards(sessionkey);
		} else if (preparationQuestionsOnly) {
			questions = questionService.getPreparationQuestions(sessionkey);
		} else {
			questions = questionService.getSkillQuestions(sessionkey);
		}
		if (questions == null || questions.isEmpty()) {
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			response.setStatus(HttpStatus.NO_CONTENT.value());
			return null;
	@RequestMapping(value = { "/" }, method = RequestMethod.DELETE)
	public void deleteSkillQuestions(
			@RequestParam final String sessionkey,
			@RequestParam(value = "lecturequestionsonly", defaultValue = "false") final boolean lectureQuestionsOnly,
			@RequestParam(value = "flashcardsonly", defaultValue = "false") final boolean flashcardsOnly,
			@RequestParam(value = "preparationquestionsonly", defaultValue = "false") final boolean preparationQuestionsOnly,
			final HttpServletResponse response
			questionService.deleteLectureQuestions(sessionkey);
			questionService.deleteFlashcards(sessionkey);
		} else if (preparationQuestionsOnly) {
			questionService.deletePreparationQuestions(sessionkey);
			questionService.deleteAllQuestions(sessionkey);
	@RequestMapping(value = "/count", method = RequestMethod.GET)
	public int getSkillQuestionCount(
			@RequestParam final String sessionkey,
			@RequestParam(value = "lecturequestionsonly", defaultValue = "false") final boolean lectureQuestionsOnly,
			@RequestParam(value = "flashcardsonly", defaultValue = "false") final boolean flashcardsOnly,
			@RequestParam(value = "preparationquestionsonly", defaultValue = "false") final boolean preparationQuestionsOnly
			) {
		if (lectureQuestionsOnly) {
			return questionService.getLectureQuestionCount(sessionkey);
		} else if (flashcardsOnly) {
			return questionService.getFlashcardCount(sessionkey);
		} else if (preparationQuestionsOnly) {
			return questionService.getPreparationQuestionCount(sessionkey);
		} else {
			return questionService.getSkillQuestionCount(sessionkey);
		}
	@RequestMapping(value = "/{questionId}", method = RequestMethod.DELETE)
	public void deleteAnswersAndQuestion(
		questionService.deleteQuestion(questionId);
	@RequestMapping(value = "/unanswered", method = RequestMethod.GET)
	public List<String> getUnAnsweredSkillQuestionIds(
			@RequestParam final String sessionkey,
			@RequestParam(value = "lecturequestionsonly", defaultValue = "false") final boolean lectureQuestionsOnly,
			@RequestParam(value = "preparationquestionsonly", defaultValue = "false") final boolean preparationQuestionsOnly
		List<String> answers;
		if (lectureQuestionsOnly) {
			answers = questionService.getUnAnsweredLectureQuestionIds(sessionkey);
		} else if (preparationQuestionsOnly) {
			answers = questionService.getUnAnsweredPreparationQuestionIds(sessionkey);
		} else {
			answers = questionService.getUnAnsweredQuestionIds(sessionkey);
		}
		if (answers == null || answers.isEmpty()) {
		return answers;
	}

	/**
	 * returns a JSON document which represents the given answer of a question.
	 *
	 * @param sessionKey
	 *            Session Keyword to which the question belongs to
	 * @param questionId
	 *            CouchDB Question ID for which the given answer should be
	 *            retrieved
	 * @return JSON Document of {@link Answer} or {@link NotFoundException}
	 * @throws NotFoundException
	 *             if wrong session, wrong question or no answer was given by
	 *             the current user
	 * @throws ForbiddenException
	 *             if not logged in
	 */
	@RequestMapping(value = "/{questionId}/myanswer", method = RequestMethod.GET)
	public Answer getMyAnswer(
			@PathVariable final String questionId,
			final HttpServletResponse response
		final Answer answer = questionService.getMyAnswer(questionId);
			response.setStatus(HttpStatus.NO_CONTENT.value());
			return null;
		return answer;
	}

	/**
	 * returns a list of {@link Answer}s encoded as a JSON document for a given
	 * question id. In this case only {@link Answer} <tt>questionId</tt>,
	 * <tt>answerText</tt>, <tt>answerSubject</tt> and <tt>answerCount</tt>
	 * properties are set
	 *
	 * @param sessionKey
	 *            Session Keyword to which the question belongs to
	 * @param questionId
	 *            CouchDB Question ID for which the given answers should be
	 *            retrieved
	 * @return List<{@link Answer}> or {@link NotFoundException}
	 * @throws NotFoundException
	 *             if wrong session, wrong question or no answers was given
	 * @throws ForbiddenException
	 *             if not logged in
	 */
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	@RequestMapping(value = "/{questionId}/answer/", method = RequestMethod.GET)
	public List<Answer> getAnswers(
			@PathVariable final String questionId,
			@RequestParam(value = "piround", required = false) final Integer piRound,
			final HttpServletResponse response
		List<Answer> answers = null;
		if (null == piRound) {
			answers = questionService.getAnswers(questionId);
		} else {
			if (piRound < 1 || piRound > 2) {
				response.setStatus(HttpStatus.BAD_REQUEST.value());

				return null;
			}
			answers = questionService.getAnswers(questionId, piRound);
		}
		if (answers == null) {
			return new ArrayList<Answer>();
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	@RequestMapping(value = "/{questionId}/answer/", method = RequestMethod.POST)
			@PathVariable final String questionId,
			@RequestBody final de.thm.arsnova.entities.transport.Answer answer,
			final HttpServletResponse response
		return questionService.saveAnswer(questionId, answer);
	@RequestMapping(value = "/{questionId}/answer/{answerId}", method = RequestMethod.PUT)
	public Answer updateAnswer(
			@PathVariable final String questionId,
Christoph Thelen's avatar
Christoph Thelen committed
			@PathVariable final String answerId,
			@RequestBody final Answer answer,
			final HttpServletResponse response
		return questionService.updateAnswer(answer);
	}

Christoph Thelen's avatar
Christoph Thelen committed
	@RequestMapping(value = "/{questionId}/answer/{answerId}", method = RequestMethod.DELETE)
Christoph Thelen's avatar
Christoph Thelen committed
			@PathVariable final String questionId,
			@PathVariable final String answerId,
			final HttpServletResponse response
Christoph Thelen's avatar
Christoph Thelen committed
		questionService.deleteAnswer(questionId, answerId);
	}

Daniel Gerhardt's avatar
Daniel Gerhardt committed
	@RequestMapping(value = "/{questionId}/answer/", method = RequestMethod.DELETE)
	public void deleteAnswers(
			@PathVariable final String questionId,
			final HttpServletResponse response
		questionService.deleteAnswers(questionId);
	}

	@RequestMapping(value = "/answers", method = RequestMethod.DELETE)
	public void deleteAllQuestionsAnswers(
			@RequestParam final String sessionkey,
			@RequestParam(value = "lecturequestionsonly", defaultValue = "false") final boolean lectureQuestionsOnly,
			@RequestParam(value = "preparationquestionsonly", defaultValue = "false") final boolean preparationQuestionsOnly,
			final HttpServletResponse response
		if (lectureQuestionsOnly) {
			questionService.deleteAllLectureAnswers(sessionkey);
		} else if (preparationQuestionsOnly) {
			questionService.deleteAllPreparationAnswers(sessionkey);
		} else {
			questionService.deleteAllQuestionsAnswers(sessionkey);
		}
	/**
	 *
	 * @param sessionKey
	 *            Session Keyword to which the question belongs to
	 * @param questionId
	 *            CouchDB Question ID for which the given answers should be
	 *            retrieved
	 * @return count of answers for given question id
	 * @throws NotFoundException
	 *             if wrong session or wrong question
	 * @throws ForbiddenException
	 *             if not logged in
	 */
	@RequestMapping(value = "/{questionId}/answercount", method = RequestMethod.GET)
	public int getAnswerCount(@PathVariable final String questionId) {
		return questionService.getAnswerCount(questionId);
	@RequestMapping(value = "/{questionId}/answerandabstentioncount", method = RequestMethod.GET)
	public List<Integer> getAnswerAndAbstentionCount(@PathVariable final String questionId) {
		List<Integer> list = Arrays.asList(
			questionService.getAnswerCount(questionId),
			questionService.getAbstentionAnswerCount(questionId)
		);
Daniel Gerhardt's avatar
Daniel Gerhardt committed

Daniel Gerhardt's avatar
Daniel Gerhardt committed

Daniel Gerhardt's avatar
Daniel Gerhardt committed
	@RequestMapping(value = "/{questionId}/freetextanswer/", method = RequestMethod.GET)
	public List<Answer> getFreetextAnswers(@PathVariable final String questionId) {
		return questionService.getFreetextAnswers(questionId);
	@RequestMapping(value = "/myanswers", method = RequestMethod.GET)
	public List<Answer> getMyAnswers(@RequestParam final String sessionkey) {
		return questionService.getMyAnswers(sessionkey);
	@RequestMapping(value = "/answercount", method = RequestMethod.GET)
	public int getTotalAnswerCount(
			@RequestParam final String sessionkey,
			@RequestParam(value = "lecturequestionsonly", defaultValue = "false") final boolean lectureQuestionsOnly,
			@RequestParam(value = "preparationquestionsonly", defaultValue = "false") final boolean preparationQuestionsOnly
		if (lectureQuestionsOnly) {
			return questionService.countLectureQuestionAnswers(sessionkey);
		} else if (preparationQuestionsOnly) {
			return questionService.countPreparationQuestionAnswers(sessionkey);
		} else {
			return questionService.getTotalAnswerCount(sessionkey);
		}