Example #1
0
func (c SecuredApplication) ProgramDiff(programIdString1 string, programIdString2 string) revel.Result {

	programId1, err := strconv.Atoi(programIdString1)
	if err != nil {
		c.Flash.Error("Invalid program id")
		return c.Render(routes.PublicApplication.Index())
	}
	programId2, err := strconv.Atoi(programIdString2)
	if err != nil {
		c.Flash.Error("Invalid program id")
		return c.Render(routes.PublicApplication.Index())
	}

	user := c.connected()

	prog1, err1 := models.FindProgram(int64(programId1))
	prog2, err2 := models.FindProgram(int64(programId2))

	if err1 != nil || err2 != nil {
		return c.RenderText("error")
	}
	mp1, err1 := models.FindMachineProblem(prog1.MachineProblemInstanceId)
	mp2, err2 := models.FindMachineProblem(prog2.MachineProblemInstanceId)
	if err1 != nil || err2 != nil || mp1.Id != mp2.Id || mp1.UserInstanceId != user.Id || mp2.UserInstanceId != user.Id {
		return c.RenderText("error")
	}

	c.RenderArgs["program1"] = prog1
	c.RenderArgs["program2"] = prog2
	return c.Render()
}
Example #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",
	})
}
Example #3
0
func (c PublicApplication) SharedAttempt(runId string) revel.Result {
	attempt, err := models.FindAttemptByRunId(runId)
	if err != nil {
		c.Flash.Error("Shared attempt not found.")
		return c.Redirect(routes.PublicApplication.Index())
	}

	mp, err := models.FindMachineProblem(attempt.MachineProblemInstanceId)
	if err != nil {
		c.Flash.Error("Cannot query attempt id")
		return c.Render(routes.PublicApplication.Index())
	}

	attempt_c_data := new(server.InternalCData)

	if err := json.Unmarshal([]byte(attempt.InternalCData), attempt_c_data); err == nil {
		c.RenderArgs["attempt_c_data"] = attempt_c_data
	}

	prog, err := models.FindProgram(attempt.ProgramInstanceId)
	if err != nil {
		c.Flash.Error("Cannot find program.")
		return c.Render(routes.PublicApplication.Index())
	}

	conf, _ := ReadMachineProblemConfig(mp.Number)

	c.RenderArgs["mp"] = mp
	c.RenderArgs["title"] = conf.Name + " Shared Attempt"

	c.RenderArgs["mp_config"] = conf
	c.RenderArgs["attempt"] = attempt
	c.RenderArgs["program"] = prog

	if attemptFailed(attempt) {
		c.RenderArgs["status"] = attemptFailedMessage(attempt)
	} else {
		c.RenderArgs["status"] = "Correct solution for this dataset."
	}

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

	programId, err := strconv.Atoi(programIdString)
	if err != nil {
		c.Flash.Error("Invalid program id")
		return c.Render(routes.PublicApplication.Index())
	}

	user := c.connected()

	prog, err := models.FindProgram(int64(programId))
	if err != nil {
		return c.RenderText("error")
	}
	mp, err := models.FindMachineProblem(prog.MachineProblemInstanceId)
	if err != nil || mp.UserInstanceId != user.Id {
		return c.RenderText("error")
	} else {
		c.RenderArgs["program"] = prog
		return c.Render()
	}
}
Example #5
0
func (c SecuredApplication) GradeRunInformation(attemptIdString string, runIdString string) revel.Result {

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

	user := c.connected()

	attempt, err := models.FindAttempt(int64(attemptId))
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error-not-found",
			"title":  "Did not finish running attempt.",
			"data": "Cannot find attempt with attempt id = " +
				attemptIdString + " ... " +
				fmt.Sprint(err),
		})
	}

	mp, err := models.FindMachineProblem(attempt.MachineProblemInstanceId)
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error-not-found",
			"title":  "Did not finish running attempt.",
			"data": "Cannot find mp with mp id = " +
				fmt.Sprint(attempt.MachineProblemInstanceId, " .... ", err),
		})
	}

	grade, err := models.FindGradeByRunId(runIdString)
	if err != nil {
		//revel.TRACE.Println("did not find grade...")
		return c.RenderJson(map[string]interface{}{
			"status": "error-not-found",
			"title":  "Did not finish running attempt.",
			"data": "Cannot find mp with grade with runid  = " +
				runIdString + " ... " + fmt.Sprint(err),
		})
	}

	if !models.AllGraded(grade) {
		//revel.TRACE.Println("not all graded...")
		return c.RenderJson(map[string]interface{}{
			"status": "error-not-found",
			"title":  "Did not finish grading.",
			"data":   "Cannot display incomplete grade for mp = " + strconv.Itoa(int(mp.Number)),
		})
	} else if err := doPostCourseraGrade(user, mp, grade, "code", false); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "success",
			"title":  "Info: Did not save grade to Coursera.",
			"data":   "Make sure you are connected to Coursera",
			"id":     grade.Id,
		})
	}

	grade, err = models.FindGradeByRunId(runIdString)

	return c.RenderJson(map[string]interface{}{
		"status": "success",
		"data":   grade,
		"id":     grade.Id,
	})
}
Example #6
0
func (c SecuredApplication) Grade(gradeIdString string) revel.Result {

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

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

	attempt, err := models.FindAttempt(grade.AttemptInstanceId)
	if err != nil || attempt.Id == 0 {
		c.Flash.Error("Invalid attempt")
		return c.Render(routes.PublicApplication.Index())
	}

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

	c.RenderArgs["grade"] = grade
	if grade.Reasons != "" {
		c.RenderArgs["reasons"] = strings.Split(grade.Reasons, ",")
	}

	c.RenderArgs["mp"] = mp

	conf, _ := ReadMachineProblemConfig(mp.Number)
	c.RenderArgs["title"] = conf.Name + " Grade"

	c.RenderArgs["grade"] = grade

	if qs, err := models.FindQuestionsByMachineProblem(mp); err == nil {
		if q, err := models.FindQuestionItems(mp, qs); err == nil {
			c.RenderArgs["questions"] = q
		}
	}

	c.RenderArgs["attempt"] = attempt

	if prog, err := models.FindProgram(attempt.ProgramInstanceId); err == nil {
		c.RenderArgs["program"] = prog

		conf, _ := ReadMachineProblemConfig(mp.Number)
		if MachineProblemCodingDeadlineExpiredQ(conf) {
			bg, err := getBigCodeSuggestion(mp.Number, prog)
			if err != nil {
				revel.INFO.Println("Failed to get big code suggestion  :::  ", err)
			} else if len(bg) != 3 {
				revel.INFO.Println("The number of suggestions recieved was  :::  ", len(bg), " was expecting 3")
			} else {
				//revel.INFO.Println("Got bigcode suggestions")
				//revel.INFO.Println("min  = ", bg[0])
				c.RenderArgs["Program"] = prog
				c.RenderArgs["mp_config"] = conf
				c.RenderArgs["bigcode"] = bg
				c.RenderArgs["bigcode_min"] = bg[0]
				c.RenderArgs["bigcode_max"] = bg[1]
				c.RenderArgs["bigcode_random"] = bg[2]

				randomSelection := rand.Intn(3)
				switch randomSelection {
				case 0:
					c.RenderArgs["bigcode_min_active"] = "active"
					c.RenderArgs["bigcode_max_active"] = ""
					c.RenderArgs["bigcode_random_active"] = ""
				case 1:
					c.RenderArgs["bigcode_min_active"] = ""
					c.RenderArgs["bigcode_max_active"] = "active"
					c.RenderArgs["bigcode_random_active"] = ""
				case 2:
					c.RenderArgs["bigcode_min_active"] = ""
					c.RenderArgs["bigcode_max_active"] = ""
					c.RenderArgs["bigcode_random_active"] = "active"
				}
			}
		}
	}

	if grade.PeerReviewScore > 0 {
		if pr, err := models.FindPeerReviewsWithGrade(grade); err == nil {
			c.RenderArgs["peer_reviews"] = pr
		}
	}

	return c.Render()
}
Example #7
0
func (c SecuredApplication) Attempt(attemptIdString string) revel.Result {

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

	user := c.connected()

	attempt, err := models.FindAttempt(int64(attemptId))
	if err != nil {
		c.Flash.Error("Cannot query attempt id")
		return c.Render(routes.PublicApplication.Index())
	}

	mp, err := models.FindMachineProblem(attempt.MachineProblemInstanceId)
	if err != nil || mp.UserInstanceId != user.Id {
		c.Flash.Error("Cannot query attempt id")
		return c.Render(routes.PublicApplication.Index())
	}

	attempt_c_data := new(server.InternalCData)

	if err := json.Unmarshal([]byte(attempt.InternalCData), attempt_c_data); err == nil {
		c.RenderArgs["attempt_c_data"] = attempt_c_data
	}

	conf, _ := ReadMachineProblemConfig(mp.Number)
	if !MachineProblemCodingDeadlineExpiredQ(conf) {
		c.RenderArgs["show_grade_button"] = true
	}

	prog, err := models.FindProgram(attempt.ProgramInstanceId)
	if err != nil {
		c.Flash.Error("Cannot find program.")
		return c.Render(routes.PublicApplication.Index())
	}

	c.RenderArgs["mp"] = mp
	c.RenderArgs["title"] = conf.Name + " Attempt"

	c.RenderArgs["mp_config"] = conf
	c.RenderArgs["attempt"] = attempt
	c.RenderArgs["program"] = prog

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

	q, err := models.FindQuestionItems(mp, qs)
	if err != nil {
		c.Flash.Error("Invalid question items")
		return c.Render(routes.PublicApplication.Index())
	} else {
		c.RenderArgs["questions"] = q
	}

	if attemptFailed(attempt) {
		c.RenderArgs["status"] = attemptFailedMessage(attempt)
	} else {
		c.RenderArgs["status"] = "Correct solution for this dataset."
	}

	return c.Render()
}
Example #8
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()
}
Example #9
0
func (c CourseraApplication) PostGrade(gradeIdString string, toPostString string, forceString string) revel.Result {

	gradeId, err := strconv.Atoi(gradeIdString)
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: Cannot Submit Grade to Coursera.",
			"data":   "Invalid grade Id.",
		})
	}

	user := c.connected()

	grade, err := models.FindGrade(int64(gradeId))
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: Cannot Submit Grade to Coursera.",
			"data":   "Cannot find grade record.",
		})
	}

	mp, err := models.FindMachineProblem(grade.MachineProblemId)
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: Cannot Submit Grade to Coursera.",
			"data":   "Cannot find machine problem record.",
		})
	}

	mpUser, err := models.FindUser(mp.UserInstanceId)
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: Cannot Submit Grade to Coursera.",
			"data":   "User not found.",
		})
	}

	if mpUser.Id != user.Id {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: Cannot Submit Grade to Coursera.",
			"data":   "User did not match.",
		})
	}

	forceQ := forceString == "true"

	if forceQ == false && grade.PeerReviewScore > 0 && grade.PeerReviewScore < grade.CourseraPeerReviewGrade {
		s := fmt.Sprint("Trying to post a code grade of ", grade.PeerReviewScore,
			" to Coursera, while Coursera has a higher score of ", grade.CourseraPeerReviewGrade)
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: PeerReview Grade lower than what has been previously posted to Coursera",
			"data":   s,
		})
	}
	if forceQ == false && grade.CodeScore > 0 && grade.CodeScore < grade.CourseraCodingGrade {
		s := fmt.Sprint("Trying to post a code grade of ", grade.CodeScore,
			" to Coursera, while Coursera has a higher score of ", grade.CourseraCodingGrade)
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error: Code Grade lower than what has been previously posted to Coursera",
			"data":   s,
		})
	}

	if err = doPostCourseraGrade(user, mp, grade, toPostString, forceQ); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Was not able to post coursera grade. Make sure you are connected to coursera first.",
		})
	}

	c.Flash.Success("Grade has been posted to Coursera.")

	return c.RenderJson(map[string]interface{}{
		"status": "success",
		"title":  "Grade has been posted to Coursera.",
		"data":   "Grade has been posted to Coursera.",
	})
}
Example #10
0
// TODO: This should not live here
func CreateAttemptWithStates(states []WorkerState) (attempts []models.Attempt, grade models.Grade, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprint(r))
		}
	}()

	if len(states) == 0 {
		err = errors.New("Invalid attempt")
		return
	}

	firstState := states[0]

	mp, err := models.FindMachineProblem(firstState.MachineProblemId)
	if err != nil {
		return
	}

	prog, err := models.CreateProgram(mp, firstState.Program)
	if err != nil {
		return
	}

	toGrade := false

	for _, ws := range states {

		jsInternalCData, _ := json.Marshal(ws.InternalCData)

		attempt := models.Attempt{
			MachineProblemInstanceId: mp.Id,
			ProgramInstanceId:        prog.Id,
			RunId:                    ws.RunId,
			DatasetId:                ws.DatasetId,
			CompilationFailed:        ws.CompilationFailed,
			CompileStderr:            strings.TrimSpace(ws.CompileStderr),
			CompileStdout:            strings.TrimSpace(ws.CompileStdout),
			RunFailed:                ws.RunFailed,
			RunStdout:                strings.TrimSpace(ws.RunStdout),
			RunStderr:                strings.TrimSpace(ws.RunStderr),
			TimeoutError:             ws.TimeoutError,
			TimeoutValue:             ws.TimeoutValue,
			Sandboxed:                ws.Sandboxed,
			SandboxKeyword:           ws.SandboxKeyword,
			CompileElapsedTime:       ws.CompileEndTime.Sub(ws.CompileStartTime).Nanoseconds(),
			RunElapsedTime:           ws.RunEndTime.Sub(ws.RunStartTime).Nanoseconds(),
			CompileStartTime:         ws.CompileStartTime,
			CompileEndTime:           ws.CompileEndTime,
			RunStartTime:             ws.RunStartTime,
			RunEndTime:               ws.RunEndTime,
			RequestStartTime:         ws.RequestStartTime,
			RequestEndTime:           ws.RequestEndTime,
			SolutionCorrect:          ws.SolutionCorrect,
			SolutionMessage:          ws.SolutionMessage,
			UserOutput:               ws.UserOutput,
			Language:                 ws.Language,
			OnAllDatasets:            ws.OnAllDatasets,
			InternalCData:            string(jsInternalCData),
			Created:                  time.Now(),
			Updated:                  time.Now(),
		}

		if ws.ForGrading {
			attempt.GradedQ = true
			toGrade = true
		}

		err = models.DB.Save(&attempt).Error
		if err != nil {
			revel.TRACE.Println("Failed saving attempt..  ", err)
			stats.Incr("Master", "FailedAttemptStore")
		} else {
			attempts = append(attempts, attempt)
		}
	}

	if toGrade {
		stats.Incr("Master", "UpdatingGrade")
		//revel.TRACE.Println("Updating grade...")
		grade, err = models.UpdateGradeWithAttempts(attempts)
		if err != nil {
			revel.TRACE.Println("Error updating grade  ", err)
		}
	}

	return
}