Пример #1
0
func updateTest(wr srv.WrapperRequest, t *Test) error {

	old, err := getTestById(wr, t.Id)
	if err != nil {
		return fmt.Errorf("updatetest: %v", err)
	}

	if wr.NU.ID() != old.AuthorId {
		return fmt.Errorf("updatetest: %s", users.ERR_NOTOPERATIONALLOWED)
	}

	// invariant fields
	t.TimeStamp = old.TimeStamp
	t.AuthorId = old.AuthorId

	q := data.NewConn(wr, "tests")
	err = q.Put(t)
	err = checkExercises(wr, t, err)
	err = deleteExercises(wr, t, err)
	err = addExercises(wr, t, err)
	err = deleteUsersAllowed(wr, t, err)
	err = addUsersAllowed(wr, t, err)
	err = deleteTestTags(wr, t, err)
	err = addTestTags(wr, t, err)

	if err != nil {
		return fmt.Errorf("updatetest: %v", err)
	}

	return nil
}
Пример #2
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
}
Пример #3
0
func getCurrentUser(wr *srv.WrapperRequest) error {

	//var nu nusers.NUser
	var nu appusers.AppUser

	nus := users.NewNUserBuffer()
	u := wr.U
	if u == nil {
		return errors.New("No user session founded")
	}

	q := data.NewConn(*wr, "users")
	q.AddFilter("Mail =", u.Email)
	err := q.GetMany(&nus)

	if len(nus) <= 0 {
		// If the session users not in the datastore we use de admin
		// users of the app
		if wr.IsAdminRequest() {
			//nu = nusers.New(u.Email, "Administrador", nusers.ROLE_ADMIN)
			nu = GetDefaultUser(u.Email)
		} else {
			return errors.New("No user id found")
		}
	} else {
		nu = nus[0]
	}

	wr.NU = nu
	return err
}
Пример #4
0
func deleteUser(wr srv.WrapperRequest, nu *NUser) error {

	q := data.NewConn(wr, "users")
	err := q.Delete(nu)
	err = deleteUserTags(wr, nu, err)

	if err != nil {
		return fmt.Errorf("deleteuser: %v", err)
	}
	return nil
}
Пример #5
0
// Add the tags of the question to the database
func addQuestTags(wr srv.WrapperRequest, q *Question) error {
	for _, tag := range q.Tags {
		qry := data.NewConn(wr, "questions-tags")
		qt := &QuestionTag{QuestId: q.Id, Tag: tag}
		err := qry.Put(qt)
		if err != nil {
			return err
		}
	}
	return nil
}
Пример #6
0
func getExerciseById(wr srv.WrapperRequest, ExerciseId int64) (*Exercise, error) {
	ex := new(Exercise)
	qry := data.NewConn(wr, "tests-exercises")
	ex.Id = ExerciseId

	err := qry.Get(ex)
	if err != nil {
		return nil, fmt.Errorf("getexercisebyid: %v", err)
	}

	return ex, nil
}
Пример #7
0
func deleteTest(wr srv.WrapperRequest, t *Test) error {

	q := data.NewConn(wr, "tests")
	err := q.Delete(t)
	err = deleteExercises(wr, t, err)
	err = deleteUsersAllowed(wr, t, err)
	err = deleteTestTags(wr, t, err)

	if err != nil {
		return fmt.Errorf("deletetest: %v", err)
	}
	return nil
}
Пример #8
0
// Add the tags in the test to the database
func addTestTags(wr srv.WrapperRequest, t *Test, err error) error {
	if err != nil {
		return err
	}
	q := data.NewConn(wr, "tests-tags")
	for _, tag := range t.Tags {
		tt := &TestTag{TestId: t.Id, Tag: tag}
		err := q.Put(tt)
		if err != nil {
			return err
		}
	}
	return nil
}
Пример #9
0
// Add the exercises in the test to the database
func addExercises(wr srv.WrapperRequest, t *Test, err error) error {
	if err != nil {
		return err
	}
	q := data.NewConn(wr, "tests-exercises")
	for _, ex := range t.Exercises {
		ex.TestId = t.Id
		err := q.Put(ex)
		if err != nil {
			return err
		}
	}
	return nil
}
Пример #10
0
func addUserTags(wr srv.WrapperRequest, nu *NUser, err error) error {
	if err != nil {
		return err
	}
	q := data.NewConn(wr, "users-tags")
	for _, tag := range nu.Tags {
		ut := &UserTag{UserId: nu.Id, Tag: tag}
		err := q.Put(ut)
		if err != nil {
			return fmt.Errorf("addusertags: %v", err)
		}
	}
	return nil
}
Пример #11
0
// Add the users allowed in the test to the database
func addUsersAllowed(wr srv.WrapperRequest, t *Test, err error) error {
	if err != nil {
		return err
	}
	q := data.NewConn(wr, "tests-users")
	for _, uid := range t.UList {
		tu := &TestUser{Id: 0, TestId: t.Id, UserId: uid}
		err := q.Put(tu)
		if err != nil {
			return err
		}
	}
	return nil
}
Пример #12
0
func getUserById(wr srv.WrapperRequest, id int64) (*NUser, error) {
	nu := new(NUser)
	nu.Id = id
	q := data.NewConn(wr, "users")
	err := q.Get(nu)
	if err != nil {
		return nu, fmt.Errorf("getuserbyid: %v: %s", err, ERR_USERNOTFOUND)
	}
	nu.Tags, err = getUserTags(wr, nu)

	if err != nil {
		return nu, fmt.Errorf("getuserbyid: %v", err)
	}
	return nu, nil
}
Пример #13
0
// Return the users allowed for this tests
func getUsersAllowed(wr srv.WrapperRequest, t *Test) ([]*users.NUser, error) {
	nus := make([]*users.NUser, 0)
	qu := data.NewConn(wr, "users")
	for i := range t.UList {
		us := new(users.NUser)
		us.SetID(t.UList[i])
		err := qu.Get(us)
		if err != nil {
			continue
		}
		nus = append(nus, us)
	}

	return nus, nil
}
Пример #14
0
func GetAnswersById(wr srv.WrapperRequest, id int64) (*Answer, error) {
	a := NewAnswer(-1, -1)

	qry := data.NewConn(wr, "answers")
	a.Id = id

	err := qry.Get(a)
	if err != nil {
		return a, errors.New(ERR_ANSWERNOTFOUND)
	}

	getAnswerBody(wr, a)

	return a, nil
}
Пример #15
0
func getUserByMail(wr srv.WrapperRequest, email string) (*NUser, error) {
	nus := NewNUserBuffer()
	nu := new(NUser)

	q := data.NewConn(wr, "users")
	q.AddFilter("Mail =", email)
	q.GetMany(&nus)
	if len(nus) == 0 {
		return nu, fmt.Errorf("%s", ERR_USERNOTFOUND)
	}
	nu = nus[0]
	nu.Tags, _ = getUserTags(wr, nu)

	return nu, nil
}
Пример #16
0
func putAnswerBody(wr srv.WrapperRequest, a *Answer) error {

	bodyTable := bodiesTable[a.BodyType]
	q := data.NewConn(wr, bodyTable)
	var err error

	switch a.Body.GetType() {
	case TYPE_TESTSINGLE:
		tbody := a.Body.(*TestSingleBody)
		q.Put(tbody)
		a.BodyId = tbody.ID()
	default:
		err = errors.New(ERR_ANSWERWITHOUTBODY)
	}
	return err
}
Пример #17
0
func getAnswerBody(wr srv.WrapperRequest, a *Answer) error {

	bodyTable := bodiesTable[a.BodyType]

	q := data.NewConn(wr, bodyTable)
	var err error
	switch a.BodyType {
	case TYPE_TESTSINGLE:
		body := NewTestSingleAnswer(-1)
		body.Solution = 0 // must be set to zero to unmarshall propertly in cache
		body.Id = a.BodyId
		err = q.Get(body)
		a.Body = body
	}
	return err
}
Пример #18
0
// Delete the tags of the test
func deleteTestTags(wr srv.WrapperRequest, t *Test, err error) error {
	if err != nil {
		return err
	}
	testsTags := NewTestTagBuffer()
	q := data.NewConn(wr, "tests-tags")
	q.AddFilter("TestId =", t.Id)
	q.GetMany(&testsTags)

	for _, ttag := range testsTags {
		err := q.Delete(ttag)
		if err != nil {
			return err
		}
	}
	return nil
}
Пример #19
0
// Return the test with id
func getTestById(wr srv.WrapperRequest, id int64) (*Test, error) {
	t := NewTest()

	t.Id = id
	qry := data.NewConn(wr, "tests")
	err := qry.Get(t)

	err = loadTestTags(wr, t, err)
	err = loadExercises(wr, t, err)
	err = loadAllowed(wr, t, err)

	if err != nil {
		return nil, fmt.Errorf("gettestbyid: %v", err)
	}

	return t, nil
}
Пример #20
0
// Return all tests from authorId
func getTestsByAuthor(wr srv.WrapperRequest, authorId int64) (TestBuffer, error) {
	ts := NewTestBuffer()

	qry := data.NewConn(wr, "tests")
	qry.AddFilter("AuthorId =", authorId)
	err := qry.GetMany(&ts)

	for i := range ts {
		loadTestTags(wr, ts[i], err)
	}

	if err != nil {
		return ts, fmt.Errorf("gettestbyauthor: %v", err)
	}

	return ts, nil
}
Пример #21
0
func deleteUserTags(wr srv.WrapperRequest, nu *NUser, err error) error {
	if err != nil {
		return err
	}
	userTags := NewUserTagBuffer()

	q := data.NewConn(wr, "users-tags")
	q.AddFilter("UserId =", nu.Id)
	q.GetMany(&userTags)

	for _, utag := range userTags {
		err := q.Delete(utag)
		if err != nil {
			return fmt.Errorf("deleteusertags: %v", err)
		}
	}
	return nil
}
Пример #22
0
func putUser(wr srv.WrapperRequest, nu *NUser) error {

	nu.TimeStamp = time.Now()

	_, err := getUserByMail(wr, nu.Mail)
	if err == nil {
		return fmt.Errorf("putuser: %s", ERR_DUPLICATEDUSER)
	}

	q := data.NewConn(wr, "users")
	err = q.Put(nu)
	err = addUserTags(wr, nu, err)

	if err != nil {
		return fmt.Errorf("putuser: %v", err)
	}
	return nil
}
Пример #23
0
// Return all questions tags of all questions in the database
func getAllQuestionsTags(wr srv.WrapperRequest) ([]string, error) {
	var tagsMap = make(map[string]int, 0)
	var tags = make([]string, 0)
	questionTags := NewQuestionTagBuffer()

	qry := data.NewConn(wr, "questions-tags")
	qry.GetMany(&questionTags)

	tags = make([]string, len(questionTags))
	for _, qtag := range questionTags {
		if _, ok := tagsMap[qtag.Tag]; !ok {
			tagsMap[qtag.Tag] = 1
			tags = append(tags, qtag.Tag)
		}
	}

	return tags, nil
}
Пример #24
0
// Delete the exercises list of the test
func deleteExercises(wr srv.WrapperRequest, t *Test, err error) error {
	if err != nil {
		return err
	}

	ex := NewExerciseBuffer()
	q := data.NewConn(wr, "tests-exercises")
	q.AddFilter("TestId =", t.Id)
	q.GetMany(&ex)

	for _, e := range ex {
		err := q.Delete(e)
		if err != nil {
			return err
		}
	}

	return nil
}
Пример #25
0
// Write new test in the database
func putTest(wr srv.WrapperRequest, t *Test) error {

	t.TimeStamp = time.Now()
	t.AuthorId = wr.NU.ID()

	q := data.NewConn(wr, "tests")
	err := q.Put(t)

	err = checkExercises(wr, t, err)
	err = addExercises(wr, t, err)
	err = addUsersAllowed(wr, t, err)
	err = addTestTags(wr, t, err)

	if err != nil {
		return fmt.Errorf("puttest: %v", err)
	}

	return nil
}
Пример #26
0
// Fill the test UList array (allowed users ids lists)
func loadAllowed(wr srv.WrapperRequest, t *Test, err error) error {
	if err != nil {
		return err
	}
	tus := NewTestUserBuffer()
	t.UList = make([]int64, 0)

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

	for i := range tus {
		t.UList = append(t.UList, tus[i].UserId)
	}
	return err
}
Пример #27
0
// Delete allowed users of the test
func deleteUsersAllowed(wr srv.WrapperRequest, t *Test, err error) error {
	if err != nil {
		return err
	}

	us := NewTestUserBuffer()
	q := data.NewConn(wr, "tests-users")
	q.AddFilter("TestId =", t.Id)
	q.GetMany(&us)

	for _, u := range us {
		err := q.Delete(u)
		if err != nil {
			return err
		}
	}

	return nil
}
Пример #28
0
func getUserTags(wr srv.WrapperRequest, nu *NUser) ([]string, error) {
	tags := make([]string, 0)
	userTags := NewUserTagBuffer()

	q := data.NewConn(wr, "users-tags")
	q.AddFilter("UserId =", nu.Id)
	err := q.GetMany(&userTags)
	if err != nil {
		return tags, fmt.Errorf("%v: %s", err, ERR_USERNOTFOUND)
	}

	tags = make([]string, 0)
	for _, utag := range userTags {
		tags = append(tags, utag.Tag)
	}

	return tags, nil

}
Пример #29
0
// Return the tags of the question
func getQuestTags(wr srv.WrapperRequest, q *Question) ([]string, error) {
	var tags []string
	questionTags := NewQuestionTagBuffer()

	qry := data.NewConn(wr, "questions-tags")
	qry.AddFilter("QuestId =", q.Id)
	err := qry.GetMany(&questionTags)
	if err != nil {
		return tags, err
	}

	tags = make([]string, 0)
	for _, qtag := range questionTags {
		tags = append(tags, qtag.Tag)
	}

	return tags, nil

}
Пример #30
0
// Get all the tags from all test in the database
func getAllTestsTags(wr srv.WrapperRequest) ([]string, error) {
	var tagsMap = make(map[string]int, 0)
	var tags = make([]string, 0)
	testTags := NewTestTagBuffer()

	qry := data.NewConn(wr, "tests-tags")
	err := qry.GetMany(&testTags)
	if err != nil {
		return tags, err
	}

	tags = make([]string, len(testTags))
	for _, qtag := range testTags {
		if _, ok := tagsMap[qtag.Tag]; !ok {
			tagsMap[qtag.Tag] = 1
			tags = append(tags, qtag.Tag)
		}
	}

	return tags, nil
}