Ejemplo n.º 1
0
func (c SecuredApplication) SubmitProgram(mpNumString string) (res revel.Result) {
	var program string
	var datasetId int

	mpNum, err := strconv.Atoi(mpNumString)
	if err != nil {
		c.Flash.Error("Invalid machine problem")
		return c.Render(routes.PublicApplication.Index())
	}

	user := c.connected()

	c.Params.Bind(&program, "program")
	c.Params.Bind(&datasetId, "datasetId")

	mp, err := models.FindOrCreateMachineProblemByUser(user, mpNum)
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error finding MP record",
			"data":   "System was not able to save program.",
		})
	}

	if lastAttempt, err := models.FindLastAttemptByMachineProblem(mp); err == nil && time.Since(lastAttempt.Created).Seconds() < 5 {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Attempt limiter",
			"data":   "Too many attempts. Please wait 10 seconds between attempts.",
		})
	}

	if _, err := models.CreateProgram(mp, program); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error Saving Program",
			"data":   "System was not able to save program.",
		})
	}

	runId := generateRunId(user)

	conf, _ := ReadMachineProblemConfig(mpNum)

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

	server.SubmitProgram(mp, program, datasetId, conf.Language, runId, false)
	return c.RenderJson(map[string]interface{}{
		"status":  "success",
		"runId":   runId,
		"attempt": "Attempt submitted",
	})
}
Ejemplo n.º 2
0
func (c SecuredApplication) ComputeGrade(attemptIdString string) revel.Result {

	attemptId, err := strconv.Atoi(attemptIdString)
	if err != nil {
		c.Flash.Error("Invalid attempt Id")
		return c.Render(routes.PublicApplication.Index())
	}

	attempt, err := models.FindAttempt(int64(attemptId))
	if err != nil || attempt.Id == 0 {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Failed to grade machine problem",
			"data":   "Cannot find attempt " + attemptIdString + ".",
		})
	}

	mp, err := models.FindMachineProblem(attempt.MachineProblemInstanceId)
	if err != nil || mp.Id == 0 {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Failed to grade",
			"data":   "Cannot find machine problem instance.",
		})
	}

	user := c.connected()

	if mp.UserInstanceId != user.Id {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Failed to grade",
			"data":   "Invalid user.",
		})
	}

	runId := generateRunId(user)

	conf, _ := ReadMachineProblemConfig(mp.Number)

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

	prog, _ := models.FindProgram(attempt.ProgramInstanceId)
	server.SubmitProgram(mp, prog.Text, -1, conf.Language, runId, true)

	return c.RenderJson(map[string]interface{}{
		"status":  "success",
		"mpId":    strconv.Itoa(int(mp.Id)),
		"runId":   runId,
		"attempt": "Grade submitted",
	})
}