Example #1
0
func (c SecuredApplication) RecentPrograms(mpNumString string, countString string) revel.Result {
	var mp models.MachineProblem

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

	count, _ := strconv.Atoi(countString)

	user := c.connected()

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

	if progs, err := recentPrograms(user, mp, count); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Failed to find recent programs",
		})
	} else {
		return c.RenderJson(map[string]interface{}{
			"status": "success",
			"data":   progs,
		})
	}

}
Example #2
0
func (c SecuredApplication) SaveProgram(mpNumString string) revel.Result {
	var program string
	var mp models.MachineProblem

	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")

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

	if mp, err = models.FindOrCreateMachineProblemByUser(user, mpNum); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Could not create mp.",
		})
	}

	if prog, err := models.CreateProgram(mp, program); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Could not save program.",
		})
	} else {
		return c.RenderJson(map[string]interface{}{
			"status": "success",
			"id":     prog.Id,
		})
	}
}
Example #3
0
func (c SecuredApplication) GradeHistory(mpNumString string) revel.Result {

	mpNum, err := strconv.Atoi(mpNumString)
	if err != nil {
		c.Flash.Error("Invalid mp 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())
	}

	grades, err := models.FindGradesByMachineProblem(mp)
	if err != nil {
		c.Flash.Error("Invalid grades items")
		return c.Render(routes.PublicApplication.Index())
	}
	c.RenderArgs["grades"] = grades

	return c.Render()
}
Example #4
0
func (c SecuredApplication) RevertMachineProblem(mpNumString string) revel.Result {

	var mp models.MachineProblem

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

	user := c.connected()
	stats.Incr("App", "ProgramRevert")

	if mp, err = models.FindOrCreateMachineProblemByUser(user, mpNum); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Could not create mp.",
		})
	}

	if _, err := revertMachineProblemProgram(user, mp); err == nil {
		return c.RenderJson(map[string]interface{}{
			"status": "success",
		})
	} else {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
		})
	}
}
Example #5
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",
	})
}
Example #6
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,
	})
}
Example #7
0
func (c SecuredApplication) MachineProblem(mpNumString string) revel.Result {

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

	user := c.connected()

	conf, _ := ReadMachineProblemConfig(mpNum)

	if MachineProblemCodingDeadlineExpiredQ(conf) {
		c.RenderArgs["coding_expired"] = true
	}
	if MachineProblemPeerReviewDeadlineExpiredQ(conf) {
		c.RenderArgs["peer_review_expired"] = true
	}

	var mp models.MachineProblem
	if mp, err = models.FindOrCreateMachineProblemByUser(user, mpNum); err != nil {
		c.Flash.Error("Cannot create machine problem")
		return c.Render(routes.PublicApplication.Index())
	}

	c.RenderArgs["title"] = conf.Name
	c.RenderArgs["mp_num"] = mpNumString
	c.RenderArgs["mp_description"] = conf.Description

	c.RenderArgs["mp_config"] = conf

	if prog, err := machineProblemProgram(user, mp); err == nil {
		c.RenderArgs["mp_program"] = prog.Text
	}

	if questions, err := machineProblemQuestions(user, mp); err == nil {
		c.RenderArgs["mp_questions"] = questions
	}

	if history, err := recentPrograms(user, mp, 100); err == nil {
		c.RenderArgs["mp_program_history"] = history
	}

	c.RenderArgs["mp_attempts"] = getAttemptsSummary(user, mp)

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

	return c.Render()
}
Example #8
0
func (c SecuredApplication) RecentProgram(mpNumString string) revel.Result {
	var mp models.MachineProblem

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

	user := c.connected()

	if mp, err = models.FindOrCreateMachineProblemByUser(user, mpNum); err != nil {
		return c.RenderText("Cannot find MP")
	}

	if prog, err := machineProblemProgram(user, mp); err != nil {
		return c.RenderText("Cannot find program")
	} else {
		return c.RenderText(prog.Text)
	}
}
Example #9
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()
}
Example #10
0
func (c SecuredApplication) Attempts(mpNumString string) revel.Result {
	var mp models.MachineProblem

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

	user := c.connected()

	if mp, err = models.FindOrCreateMachineProblemByUser(user, mpNum); err != nil {
		c.Flash.Error("Cannot query machine problem number")
		return c.Render(routes.PublicApplication.Index())
	}

	c.RenderArgs["mp_attempts"] = getAttemptsSummary(user, mp)

	conf, _ := ReadMachineProblemConfig(mpNum)
	c.RenderArgs["title"] = conf.Name + " Attempts"

	return c.Render()
}
Example #11
0
func (c SecuredApplication) ShowGrade(mpNumString string) revel.Result {

	mpNum, err := strconv.Atoi(mpNumString)
	if err != nil {
		c.Flash.Error("Invalid mp Id")
		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())
	}

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

	return c.Grade(strconv.Itoa(int(grade.Id)))
}
Example #12
0
func (c SecuredApplication) UpdatePeerReview(mpNumString string) revel.Result {
	mpNum, err := strconv.Atoi(mpNumString)
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Cannot Find Machine Problem",
			"data":   "Cannot find machine problem --- invalid MP Id.",
			"error":  err,
		})
	}

	user := c.connected()

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

	if _, err := models.FindLastAttemptByMachineProblem(mp); err != nil {
		if _, err = models.CreateDummyAttemptByMachineProblem(mp); err != nil {
			return c.RenderJson(map[string]interface{}{
				"status": "error",
				"title":  "Error: Cannot Submit Peer Review.",
				"data":   "We require that you have attempted the MP to submit a peer review.",
				"error":  err,
			})
		}
	}

	var reviewsString string
	c.Params.Bind(&reviewsString, "reviews")

	//revel.TRACE.Println(reviewsString)

	var reviews []PeerReviewRecord
	if err := json.Unmarshal([]byte(reviewsString), &reviews); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: Cannot Parse Review Request.",
			"data":   "The system was not able to parse your review request.",
			"error":  err,
		})
	}

	prs := make([]models.PeerReview, len(reviews))
	for ii, review := range reviews {
		pr, err := models.FindPeerReview(review.Id)
		if err != nil || pr.Reviewer != user.Id {
			return c.RenderJson(map[string]interface{}{
				"status": "error",
				"title":  "Error: Cannot Submit Review.",
				"data":   "Cannot submit review request -- invalid reviewer.",
			})
		}
		if strings.TrimSpace(review.QuestionComment) == "" ||
			strings.TrimSpace(review.CodeComment) == "" {
			return c.RenderJson(map[string]interface{}{
				"status": "error",
				"title":  "Error: Cannot Submit Empty Review.",
				"data":   "Cannot submit review. Please make sure to offer useful comments to your peers.",
			})
		}
		prs[ii] = pr
	}

	for ii, pr := range prs {
		review := reviews[ii]
		_, err := models.UpdatePeerReview(pr, review.CodeScore, review.CodeComment,
			review.QuestionScore, review.QuestionComment)
		if err != nil {
			return c.RenderJson(map[string]interface{}{
				"status": "error",
				"title":  "Error: Cannot Save Review",
				"data":   "The system was not able to submit your review to the database. Report this issue if problem presists.",
			})
		}
	}

	var grade models.Grade
	if grade, err = models.UpdateGradePeerReview(user, mp); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: Peer Review Grade was not Updated.",
			"data": "The system was not able to update your peer review grade. " +
				"Report this issue if problem presists. " +
				fmt.Sprint(err),
		})
	}

	if err := doPostCourseraGrade(user, mp, grade, "peer", false); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: Did not save grade to Coursera.",
			"data":   "Make sure you are connected to Coursera",
		})
	}

	link := "/grade/" + strconv.Itoa(int(grade.Id))
	return c.RenderJson(map[string]interface{}{
		"status": "success",
		"title":  "Peer Review has been Submitted",
		"data": "Review has been logged and submitted. Make sure to visit the <a href=\"" +
			link +
			"\"> machine problem grade page</a> and re-submit your grade to Coursera.",
		"link": link,
	})
}
Example #13
0
func (c SecuredApplication) PeerReview(mpNumString string) revel.Result {

	mpNum, err := strconv.Atoi(mpNumString)
	if err != nil {
		c.Flash.Error("Invalid mp Id")
		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())
	}

	conf, _ := ReadMachineProblemConfig(mpNum)

	c.RenderArgs["mp_config"] = conf
	c.RenderArgs["title"] = conf.Name + " Peer Review"

	if MachineProblemPeerReviewDeadlineExpiredQ(conf) {
		c.RenderArgs["past_deadline"] = true
	} else if MachineProblemCodingDeadlineExpiredQ(conf) {

		reviews, err := models.GetPeerReviewsByReviewerAndMachineProblem(user, mp)

		if err != nil || len(reviews) < 3 {
			ii := 0
			for len(reviews) < 3 && ii < 100 {
				if rev, err := models.CreatePeerReviewWithReviewerAndMachineProblem(user, mp); err == nil {
					reviews = append(reviews, rev)
				}
				ii++
			}
		}

		type reviewT struct {
			Index         int
			Review        models.PeerReview
			Questions     models.Questions
			QuestionItems []models.QuestionItem
			Program       models.Program
		}

		var res []reviewT

		for _, r := range reviews {
			if grade, err := models.FindGrade(r.GradeInstanceId); err == nil {
				if attempt, err := models.FindAttempt(grade.AttemptInstanceId); err == nil {
					program, _ := models.FindProgram(attempt.ProgramInstanceId)
					tmp, _ := models.FindMachineProblem(grade.MachineProblemId)
					q, _ := models.FindQuestionsByMachineProblem(tmp)
					qs, _ := models.FindQuestionItems(mp, q)
					k := reviewT{
						Index:         len(res) + 1,
						Review:        r,
						Questions:     q,
						QuestionItems: qs,
						Program:       program,
					}
					res = append(res, k)
				}
			}
		}

		c.RenderArgs["reviews"] = res
	} else {
		c.RenderArgs["still_coding"] = true
	}

	return c.Render()
}