Exemplo n.º 1
0
func (c SecuredApplication) SaveQuestion(mpNumString string, questionNumString string) revel.Result {
	var answer string

	mpNum, err := strconv.Atoi(mpNumString)
	questionNum, err := strconv.Atoi(questionNumString)

	c.Params.Bind(&answer, "answer")

	if answer == "" {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Blank answer was not saved.",
		})
	}

	user := c.connected()

	mp, err := models.FindOrCreateMachineProblemByUser(user, mpNum)
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Cannot find machine problem.",
		})
	}

	conf, _ := ReadMachineProblemConfig(mp.Number)
	if MachineProblemCodingDeadlineExpiredQ(conf) {
		return c.RenderJson(map[string]interface{}{
			"status": "error-deadline",
			"data":   "Coding deadline has passed.",
		})
	}

	questions, err := models.FindOrCreateQuestionsByMachineProblem(mp)
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Error: create questions.",
		})
	}

	err = models.SaveQuestion(mp, questions, int64(questionNum), answer)

	stats.Incr("App", "QuestionSave")

	return c.RenderJson(map[string]interface{}{
		"status": "success",
		"data":   answer,
	})
}
Exemplo n.º 2
0
func machineProblemQuestions(user models.User, mp models.MachineProblem) ([]models.QuestionItem, error) {
	questions, err := models.FindOrCreateQuestionsByMachineProblem(mp)
	if err != nil {
		return nil, err
	}

	questionItems, err := models.FindQuestionItems(mp, questions)
	if err != nil {
		stats.TRACE.Println("Cannot get question items.")
		return nil, err
	}

	return questionItems, nil
}
Exemplo n.º 3
0
func (c SecuredApplication) QuestionHistory(mpNumString string, questionNumString string) revel.Result {
	mpNum, err := strconv.Atoi(mpNumString)
	if err != nil {
		c.Flash.Error("Invalid mp number")
		return c.Render(routes.PublicApplication.Index())
	}
	questionNum, err := strconv.Atoi(questionNumString)
	if err != nil {
		c.Flash.Error("Invalid question number")
		return c.Render(routes.PublicApplication.Index())
	}

	user := c.connected()

	mp, err := models.FindOrCreateMachineProblemByUser(user, mpNum)
	if err != nil {
		c.Flash.Error("Invalid mp Id")
		return c.Render(routes.PublicApplication.Index())
	}

	qs, err := models.FindOrCreateQuestionsByMachineProblem(mp)
	if err != nil {
		c.Flash.Error("Invalid questions")
		return c.Render(routes.PublicApplication.Index())
	}

	q, err := models.FindQuestionHistory(qs, int64(questionNum))
	if err != nil {
		c.Flash.Error("Invalid question items")
		return c.Render(routes.PublicApplication.Index())
	}

	c.RenderArgs["mp"] = mp
	c.RenderArgs["number"] = questionNum
	c.RenderArgs["questions"] = q

	return c.Render()
}