Exemplo n.º 1
0
func Solve(wr srv.WrapperRequest, tc map[string]interface{}) (string, error) {
	if wr.NU.GetRole() < users.ROLE_TEACHER {
		return viewTmpl, errors.New(users.ERR_NOTOPERATIONALLOWED)
	}

	var a *answers.Answer

	decoder := json.NewDecoder(wr.R.Body)
	err := decoder.Decode(&a)
	if err != nil {
		return infoTmpl, err
	}

	a.BuildBody()
	a.AuthorId = wr.NU.ID()

	quest, err := GetQuestById(wr, a.QuestId)
	if err != nil {
		return infoTmpl, err
	}

	// Only que teacher author of the quest can add the solution
	if quest.AuthorId != a.AuthorId {
		return infoTmpl, errors.New(answers.ERR_AUTHORID)
	}

	err = answers.PutAnswer(wr, a)
	if err != nil {
		return infoTmpl, err
	}

	tc["Content"] = a
	return infoTmpl, nil
}
Exemplo n.º 2
0
func DoExercise(wr srv.WrapperRequest, tc map[string]interface{}) (string, error) {
	if wr.NU.GetRole() < users.ROLE_STUDENT {
		return viewTmpl, errors.New(users.ERR_NOTOPERATIONALLOWED)
	}

	var a *answers.Answer

	decoder := json.NewDecoder(wr.R.Body)
	err := decoder.Decode(&a)
	if err != nil {
		return infoTmpl, err
	}

	// Check if the a.AuthorId is an allowed user for
	ex, err := getExerciseById(wr, a.ExerciseId)
	if err != nil {
		return infoTmpl, err
	}
	if ok := IsTestAllowedUser(wr, ex.TestId, a.AuthorId); !ok {
		return infoTmpl, errors.New(users.ERR_NOTOPERATIONALLOWED)
	}

	a.BuildBody()
	a.AuthorId = wr.NU.ID()

	_, err = questions.GetQuestById(wr, a.QuestId)
	if err != nil {
		return infoTmpl, err
	}

	err = answers.PutAnswer(wr, a)
	if err != nil {
		return infoTmpl, err
	}

	tc["Content"] = a
	return infoTmpl, nil
}