Exemple #1
0
// Fill the exercises array in the test
func loadExercises(wr srv.WrapperRequest, t *Test, err error) error {
	if err != nil {
		return err
	}
	testEx := NewExerciseBuffer()

	qry := data.NewConn(wr, "tests-exercises")
	qry.AddFilter("TestId =", t.Id)
	err = qry.GetMany(&testEx)
	if err != nil {
		return err
	}

	t.Exercises = testEx

	// check if the test will be loaded for a student, in this case
	// do not read the solutions of the questions
	withSolution := true
	if wr.NU.GetRole() < users.ROLE_TEACHER {
		withSolution = false
	}
	// now, load the questions struct for each exercise
	for i := range t.Exercises {
		var q *questions.Question
		if withSolution {
			q, _ = questions.GetQuestById(wr, t.Exercises[i].QuestId)
		} else {
			q, _ = questions.GetQuestByIdWithoutSol(wr, t.Exercises[i].QuestId)
		}
		t.Exercises[i].Quest = q
	}

	return nil
}
Exemple #2
0
// Check if the exercises are valid to be in the test. If not, return
// an error to avoid commit them in the database
func checkExercises(wr srv.WrapperRequest, t *Test, err error) error {
	if err != nil {
		return err
	}
	for i := range t.Exercises {
		q, _ := questions.GetQuestById(wr, t.Exercises[i].QuestId)
		if q.Solution.Body.IsUnsolved() {
			return fmt.Errorf("%s", ERR_TESTEXERCISESNOTVALID)
		}
	}
	return nil
}
Exemple #3
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
}