Ejemplo n.º 1
0
// AddGroupMemberHandler is a http handler adding an additional member to a active group.
func AddGroupMemberHandler(w http.ResponseWriter, r *http.Request) {
	view := AddGroupMemberView{}
	view.Error = true
	enc := json.NewEncoder(w)

	// Checks if the user is signed in and a teacher.
	member, err := checkTeacherApproval(w, r, true)
	if err != nil {
		err = enc.Encode(ErrSignIn)
		return
	}

	orgname := r.FormValue("course")
	if orgname == "" || !git.HasOrganization(orgname) {
		err = enc.Encode(ErrUnknownCourse)
		return
	}

	groupid, err := strconv.Atoi(r.FormValue("groupid"))
	if err != nil {
		view.ErrorMsg = err.Error()
		err = enc.Encode(view)
		return
	}

	if !git.HasGroup(groupid) {
		err = enc.Encode(ErrUnknownGroup)
		return
	}

	org, err := git.NewOrganization(orgname, false)
	if err != nil {
		view.ErrorMsg = err.Error()
		err = enc.Encode(view)
		return
	}

	defer func() {
		err := org.Save()
		if err != nil {
			org.Unlock()
			log.Println(err)
		}
	}()

	if !org.IsTeacher(member) {
		err = enc.Encode(ErrNotTeacher)
		return
	}

	group, err := git.NewGroup(orgname, groupid, false)
	if err != nil {
		view.ErrorMsg = err.Error()
		err = enc.Encode(view)
		return
	}

	defer func() {
		err := group.Save()
		if err != nil {
			group.Unlock()
			log.Println(err)
		}
	}()

	if group.TeamID == 0 {
		teams, err := org.ListTeams()
		if err != nil {
			view.ErrorMsg = err.Error()
			err = enc.Encode(view)
			return
		}

		if team, ok := teams[git.GroupRepoPrefix+strconv.Itoa(groupid)]; ok {
			group.TeamID = team.ID
		} else {
			view.ErrorMsg = "Error finding team on GitHub."
			err = enc.Encode(view)
			return
		}
	}

	r.ParseForm()
	members := r.PostForm["member"]

	for _, username := range members {
		if username == "" || !git.HasMember(username) {
			continue
		}

		group.AddMember(username)

		org.AddMemberToTeam(group.TeamID, username)
		delete(org.PendingRandomGroup, username)
	}

	group.Activate()

	view.Added = true
	view.Error = false
	enc.Encode(view)
}
Ejemplo n.º 2
0
// ShowResultHandler is a http handler for showing a page detailing
// lab resutls for a single user or group.
func ShowResultHandler(w http.ResponseWriter, r *http.Request) {
	// Checks if the user is signed in and a teacher.
	member, err := checkTeacherApproval(w, r, true)
	if err != nil {
		return
	}

	// Gets the org and check if valid
	orgname := ""
	if path := strings.Split(r.URL.Path, "/"); len(path) == 4 {
		if !git.HasOrganization(path[3]) {
			http.Redirect(w, r, pages.HOMEPAGE, 307)
			return
		}

		orgname = path[3]
	} else {
		http.Redirect(w, r, pages.HOMEPAGE, 307)
		return
	}

	username := r.FormValue("user")
	if username == "" {
		http.Redirect(w, r, pages.HOMEPAGE, 307)
		return
	}

	if !git.HasOrganization(orgname) {
		http.Redirect(w, r, pages.HOMEPAGE, 307)
		return
	}

	org, err := git.NewOrganization(orgname, true)
	if err != nil {
		http.Error(w, err.Error(), 500)
	}

	isgroup := false
	groupid := -1
	labnum := 0
	if !git.HasMember(username) {
		groupnum, err := strconv.Atoi(username[len("group"):])
		if err != nil {
			http.Redirect(w, r, pages.HOMEPAGE, 307)
			return
		}
		if git.HasGroup(groupnum) {
			isgroup = true
			group, err := git.NewGroup(org.Name, groupnum, true)
			if err != nil {
				http.Redirect(w, r, pages.HOMEPAGE, 307)
				return
			}

			groupid = group.ID

			if group.CurrentLabNum >= org.GroupAssignments {
				labnum = org.GroupAssignments
			} else {
				labnum = group.CurrentLabNum
			}
		} else {
			http.Redirect(w, r, pages.HOMEPAGE, 307)
			return
		}
	} else {
		user, err := git.NewMemberFromUsername(username, true)
		if err != nil {
			http.Error(w, err.Error(), 500)
		}

		nr := user.Courses[org.Name].CurrentLabNum
		if nr >= org.IndividualAssignments {
			labnum = org.IndividualAssignments
		} else {
			labnum = nr
		}
	}

	view := ShowResultView{
		StdTemplate: StdTemplate{
			Member: member,
		},
		Org:      org,
		Username: username,
		Labnum:   labnum,
		IsGroup:  isgroup,
		GroupID:  groupid,
	}
	execTemplate("teacherresultpage.html", w, view)
}
Ejemplo n.º 3
0
// RemovePendingGroupHandler is used to remove a group.
func RemovePendingGroupHandler(w http.ResponseWriter, r *http.Request) {
	// Checks if the user is signed in and a teacher.
	member, err := checkTeacherApproval(w, r, true)
	if err != nil {
		http.Redirect(w, r, "/", 307)
		log.Println(err)
		return
	}

	groupid, err := strconv.Atoi(r.FormValue("groupid"))
	if err != nil {
		http.Error(w, "Group ID is not a number: "+err.Error(), 404)
		return
	}
	course := r.FormValue("course")

	if !git.HasOrganization(course) {
		http.Error(w, "Unknown course.", 404)
		return
	}

	org, err := git.NewOrganization(course, false)
	if err != nil {
		http.Error(w, "Unknown course.", 404)
		return
	}

	defer func() {
		err := org.Save()
		if err != nil {
			org.Unlock()
			log.Println(err)
		}
	}()

	if !org.IsTeacher(member) {
		http.Error(w, "Is not a teacher or assistant for this course.", 404)
		return
	}

	if !git.HasGroup(groupid) {
		groupname := git.GroupRepoPrefix + strconv.Itoa(groupid)
		if _, ok := org.Groups[groupname]; ok {
			delete(org.Groups, groupname)
			return
		}

		http.Error(w, "Unknown group.", 404)
		return
	}

	if _, ok := org.PendingGroup[groupid]; ok {
		delete(org.PendingGroup, groupid)
	}

	groupname := git.GroupRepoPrefix + strconv.Itoa(groupid)
	if _, ok := org.Groups[groupname]; ok {
		delete(org.Groups, groupname)
	}

	group, err := git.NewGroup(org.Name, groupid, false)
	if err != nil {
		http.Error(w, "Could not get the group: "+err.Error(), 404)
		return
	}

	group.Delete()
}