def remove_student(request, course_slug, group_slug): course = get_object_or_404(CourseOffering, slug = course_slug) group = get_object_or_404(Group, courseoffering = course, slug = group_slug) members = GroupMember.objects.filter(group = group).select_related('group', 'student', 'student__person', 'activity') # check permissions if is_course_staff_by_slug(request, course_slug): is_staff = True elif is_course_student_by_slug(request, course_slug): is_staff = False memberships = [m for m in members if m.student.person.userid == request.user.username] if not memberships: # student must be in this group return HttpResponseForbidden() else: return HttpResponseForbidden() if request.method == "POST": for m in members: f = StudentForm(request.POST, prefix=unicode(m.student.person.userid_or_emplid()) + '_' + m.activity.slug) if (is_staff or m.student_editable(request.user.username)=="") \ and f.is_valid() and f.cleaned_data['selected'] == True: m.delete() #LOG EVENT# l = LogEntry(userid=request.user.username, description="deleted %s in group %s for %s." % (m.student.person.userid, group.name, m.activity), related_object=m.group) l.save() if is_staff: return HttpResponseRedirect(reverse('groups.views.view_group', kwargs={'course_slug': course_slug, 'group_slug': group.slug})) else: return HttpResponseRedirect(reverse('groups.views.groupmanage', kwargs={'course_slug': course_slug})) else: data = [] for m in members: editable = m.student_editable(request.user.username) if is_staff or editable == "": f = StudentForm(prefix=unicode(m.student.person.userid_or_emplid()) + '_' + m.activity.slug) data.append({'form': f, 'member': m}) else: data.append({'form': None, 'member': m, 'reason': editable}) return render_to_response('groups/remove_student.html', \ {'course':course, 'group' : group, 'data':data, 'is_staff':is_staff}, \ context_instance = RequestContext(request))
def create(request, course_slug): person = get_object_or_404(Person, userid=request.user.username) course = get_object_or_404(CourseOffering, slug=course_slug) group_manager = Member.objects.exclude(role="DROP").get(person=person, offering=course) groupForSemesterForm = GroupForSemesterForm() activities = Activity.objects.exclude(status='INVI').filter( offering=course, group=True, deleted=False) activityList = [] for activity in activities: activityForm = ActivityForm(prefix=activity.slug) activityList.append({'activityForm': activityForm, 'name' : activity.name,\ 'percent' : activity.percent, 'due_date' : activity.due_date}) if is_course_student_by_slug(request, course_slug): return render_to_response('groups/create_student.html', \ {'manager':group_manager, 'course':course, 'groupForSemester':groupForSemesterForm, 'activityList':activityList},\ context_instance = RequestContext(request)) elif is_course_staff_by_slug(request, course_slug): #For instructor page, there is a student table for him/her to choose the students who belong to the new group students = Member.objects.select_related('person').filter( offering=course, role='STUD') studentList = [] for student in students: studentForm = StudentForm(prefix=student.person.userid) studentList.append({'studentForm': studentForm, 'first_name' : student.person.first_name,\ 'last_name' : student.person.last_name, 'userid' : student.person.userid,\ 'emplid' : student.person.emplid}) return render_to_response('groups/create_instructor.html', \ {'manager':group_manager, 'course':course,'groupForSemester':groupForSemesterForm, 'activityList':activityList, \ 'studentList':studentList}, context_instance = RequestContext(request)) else: return HttpResponseForbidden()
def create(request, course_slug): person = get_object_or_404(Person, userid=request.user.username) course = get_object_or_404(CourseOffering, slug=course_slug) span = course.group_span_activities() # allow 'activity=foo' in query string to suggest default selected for the form if 'activity' in request.GET: selected_activity = get_object_or_404(Activity, offering=course, slug=request.GET['activity'], status__in=['RLS', 'URLS']) else: selected_activity = None group_manager = Member.objects.exclude(role="DROP").get(person=person, offering=course) groupForSemesterForm = GroupForSemesterForm() activities = Activity.objects.exclude(status='INVI').filter( offering=course, group=True, deleted=False) activityList = [] for activity in activities: default = (span and not selected_activity) or ( selected_activity and activity == selected_activity) activityForm = ActivityForm(prefix=activity.slug, initial={'selected': default}) activityList.append({ 'activityForm': activityForm, 'name': activity.name, 'percent': activity.percent, 'due_date': activity.due_date }) if is_course_student_by_slug(request, course_slug): return render( request, 'groups/create_student.html', { 'manager': group_manager, 'course': course, 'groupForSemester': groupForSemesterForm, 'activityList': activityList }) elif is_course_staff_by_slug(request, course_slug): #For instructor page, there is a student table for him/her to choose the students who belong to the new group students = Member.objects.select_related('person').filter( offering=course, role='STUD') studentList = [] for student in students: studentForm = StudentForm(prefix=student.person.userid) studentList.append({'studentForm': studentForm, 'first_name' : student.person.first_name,\ 'last_name' : student.person.last_name, 'userid' : student.person.userid,\ 'emplid' : student.person.emplid}) return render(request, 'groups/create_instructor.html', {'manager':group_manager, 'course':course,'groupForSemester':groupForSemesterForm, 'activityList':activityList, \ 'studentList':studentList}) else: return HttpResponseForbidden()
def assign_student(request, course_slug, group_slug): course = get_object_or_404(CourseOffering, slug=course_slug) group = get_object_or_404(Group, slug=group_slug, courseoffering=course) activities = Activity.objects.filter(offering=course, group=True, deleted=False) members = Member.objects.filter(offering=course, role='STUD').select_related('person') if request.method == "POST": add_act = [] for a in activities: form = ActivityForm(request.POST, prefix=a.slug) if form.is_valid() and form.cleaned_data['selected'] == True: add_act.append(a) for m in members: form = StudentForm(request.POST, prefix=m.person.userid) if form.is_valid() and form.cleaned_data['selected'] == True: for a in add_act: old_gm = GroupMember.objects.filter(activity_id=a.id, student=m) if len(old_gm) > 0: messages.error( request, "%s is already in a group for %s." % (m.person.name(), a.name)) else: gm = GroupMember(group=group, student=m, confirmed=True, activity_id=a.id) gm.save() messages.success( request, "%s added to group %s for %s." % (m.person.name(), group.name, a.name)) #LOG EVENT# l = LogEntry( userid=request.user.username, description="added %s to group %s for %s." % (m.person.userid, group.name, a), related_object=gm) l.save() return HttpResponseRedirect( reverse('offering:groups:view_group', kwargs={ 'course_slug': course.slug, 'group_slug': group.slug })) else: activity_data = [] for a in activities: form = ActivityForm(prefix=a.slug) activity_data.append({'form': form, 'act': a}) student_data = [] for m in members: form = StudentForm(prefix=m.person.userid) student_data.append({'form': form, 'member': m}) return render(request, 'groups/assign_student.html', \ {'course':course, 'group':group, 'activity_data': activity_data, 'student_data': student_data})
def remove_student(request, course_slug, group_slug): course = get_object_or_404(CourseOffering, slug=course_slug) group = get_object_or_404(Group, courseoffering=course, slug=group_slug) members = GroupMember.objects.filter(group=group).select_related( 'group', 'student', 'student__person', 'activity') # check permissions if is_course_staff_by_slug(request, course_slug): is_staff = True elif is_course_student_by_slug(request, course_slug): is_staff = False memberships = [ m for m in members if m.student.person.userid == request.user.username ] if not memberships: # student must be in this group return HttpResponseForbidden() else: return HttpResponseForbidden() if request.method == "POST": for m in members: f = StudentForm( request.POST, prefix=unicode(m.student.person.userid_or_emplid()) + '_' + m.activity.slug) if (is_staff or m.student_editable(request.user.username)=="") \ and f.is_valid() and f.cleaned_data['selected'] == True: m.delete() #LOG EVENT# l = LogEntry(userid=request.user.username, description="deleted %s in group %s for %s." % (m.student.person.userid, group.name, m.activity), related_object=m.group) l.save() if is_staff: return HttpResponseRedirect( reverse('offering:groups:view_group', kwargs={ 'course_slug': course_slug, 'group_slug': group.slug })) else: return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) else: data = [] for m in members: editable = m.student_editable(request.user.username) if is_staff or editable == "": f = StudentForm( prefix=unicode(m.student.person.userid_or_emplid()) + '_' + m.activity.slug) data.append({'form': f, 'member': m}) else: data.append({'form': None, 'member': m, 'reason': editable}) return render(request, 'groups/remove_student.html', \ {'course':course, 'group' : group, 'data':data, 'is_staff':is_staff})
def submit(request, course_slug): person = get_object_or_404(Person, userid=request.user.username) course = get_object_or_404(CourseOffering, slug=course_slug) member = Member.objects.exclude(role='DROP').get(person=person, offering=course) is_staff = is_course_staff_by_slug(request, course_slug) span = course.group_span_activities() error_info = None name = request.POST.get('GroupName') if name: name = name[:30] #Check if group has a unique name if Group.objects.filter(name=name, courseoffering=course): error_info = "A group named \"%s\" already exists" % (name) messages.add_message(request, messages.ERROR, error_info) return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) #Check if the group name is empty, these two checks may need to be moved to forms later. if name == "": error_info = "Group name cannot be empty: please enter a group name." messages.add_message(request, messages.ERROR, error_info) return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) else: # find selected activities selected_act = [] activities = Activity.objects.filter(offering=course, group=True, deleted=False) if not is_staff: activities = activities.exclude(status='INVI') for activity in activities: activityForm = ActivityForm(request.POST, prefix=activity.slug) if activityForm.is_valid( ) and activityForm.cleaned_data['selected'] == True: selected_act.append(activity) # no selected activities: fail. if not selected_act: messages.add_message(request, messages.ERROR, "Group not created: no activities selected.") return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) # check groups_span_activities restriction if it's set if not span and not is_staff and len(selected_act) > 1: # students cannot violate groups_span_activities restriction, but instructors can messages.add_message( request, messages.ERROR, "Group not created: groups cannot last for more than one activity in this course." ) return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) groupForSemester = False #validate database integrity before saving anything. #If one student is in a group for an activity, he/she cannot be in another group for the same activity. if is_course_student_by_slug(request, course_slug): isStudentCreatedGroup = True studentList = [] studentList.append(member) elif is_course_staff_by_slug(request, course_slug): isStudentCreatedGroup = False studentList = [] students = Member.objects.select_related('person').filter( offering=course, role='STUD') for student in students: studentForm = StudentForm(request.POST, prefix=student.person.userid) if studentForm.is_valid( ) and studentForm.cleaned_data['selected'] == True: studentList.append(student) #Check if students has already in a group if _validateIntegrity(request, isStudentCreatedGroup, groupForSemester, course, studentList, selected_act) == False: return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) #No selected members,group creating will fail. if not studentList: messages.add_message(request, messages.ERROR, "Group not created: no members selected.") return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) group = Group(name=name, manager=member, courseoffering=course, groupForSemester=groupForSemester) group.save() #LOG EVENT# l = LogEntry(userid=request.user.username, description="created a new group %s for %s." % (group.name, course), related_object=group) l.save() if is_course_student_by_slug(request, course_slug): for activity in selected_act: groupMember = GroupMember(group=group, student=member, confirmed=True, activity_id=activity.id) groupMember.save() #LOG EVENT# l = LogEntry( userid=request.user.username, description= "automatically became a group member of %s for activity %s." % (group.name, groupMember.activity), related_object=groupMember) l.save() messages.add_message(request, messages.SUCCESS, 'Group Created') return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) elif is_course_staff_by_slug(request, course_slug): students = Member.objects.select_related('person').filter( offering=course, role='STUD') for student in students: studentForm = StudentForm(request.POST, prefix=student.person.userid) if studentForm.is_valid( ) and studentForm.cleaned_data['selected'] == True: for activity in selected_act: groupMember = GroupMember(group=group, student=student, confirmed=True, activity_id=activity.id) groupMember.save() #LOG EVENT# l = LogEntry( userid=request.user.username, description= "added %s as a group member to %s for activity %s." % (student.person.userid, group.name, groupMember.activity), related_object=groupMember) l.save() n = NewsItem(user=student.person, author=member.person, course=group.courseoffering, source_app="group", title="Added to Group", content="You have been added the group %s." % (group.name), url=reverse( 'offering:groups:groupmanage', kwargs={'course_slug': course.slug})) n.save() messages.add_message(request, messages.SUCCESS, 'Group Created') return HttpResponseRedirect( reverse('offering:groups:view_group', kwargs={ 'course_slug': course_slug, 'group_slug': group.slug })) else: return HttpResponseForbidden()
def submit(request, course_slug): person = get_object_or_404(Person,userid=request.user.username) course = get_object_or_404(CourseOffering, slug = course_slug) member = Member.objects.exclude(role='DROP').get(person=person, offering=course) error_info=None name = request.POST.get('GroupName') if name: name = name[:30] #Check if group has a unique name if Group.objects.filter(name=name,courseoffering=course): error_info="A group named \"%s\" already exists" % (name) messages.add_message(request, messages.ERROR, error_info) return HttpResponseRedirect(reverse('groups.views.groupmanage', kwargs={'course_slug': course_slug})) #Check if the group name is empty, these two checks may need to be moved to forms later. if name == "": error_info = "Group name cannot be empty: please enter a group name." messages.add_message(request, messages.ERROR, error_info) return HttpResponseRedirect(reverse('groups.views.groupmanage', kwargs={'course_slug': course_slug})) else: # find selected activities selected_act = [] activities = Activity.objects.filter(offering=course, group=True, deleted=False) if not is_course_staff_by_slug(request, course_slug): activities = activities.exclude(status='INVI') for activity in activities: activityForm = ActivityForm(request.POST, prefix=activity.slug) if activityForm.is_valid() and activityForm.cleaned_data['selected'] == True: selected_act.append(activity) # no selected activities: fail. if not selected_act: messages.add_message(request, messages.ERROR, "Group not created: no activities selected.") return HttpResponseRedirect(reverse('groups.views.groupmanage', kwargs={'course_slug': course_slug})) #groupForSemesterForm = GroupForSemesterForm(request.POST) #if groupForSemesterForm.is_valid(): # groupForSemester = groupForSemesterForm.cleaned_data['selected'] groupForSemester = False #validate database integrity before saving anything. #If one student is in a group for an activity, he/she cannot be in another group for the same activity. if is_course_student_by_slug(request, course_slug): isStudentCreatedGroup = True studentList = [] studentList.append(member) elif is_course_staff_by_slug(request, course_slug): isStudentCreatedGroup = False studentList = [] students = Member.objects.select_related('person').filter(offering = course, role = 'STUD') for student in students: studentForm = StudentForm(request.POST, prefix = student.person.userid) if studentForm.is_valid() and studentForm.cleaned_data['selected'] == True: studentList.append(student) #Check if students has already in a group if _validateIntegrity(request,isStudentCreatedGroup, groupForSemester, course, studentList, selected_act) == False: return HttpResponseRedirect(reverse('groups.views.groupmanage', kwargs={'course_slug': course_slug})) #No selected members,group creating will fail. if not studentList: messages.add_message(request, messages.ERROR, "Group not created: no members selected.") return HttpResponseRedirect(reverse('groups.views.groupmanage', kwargs={'course_slug': course_slug})) group = Group(name=name, manager=member, courseoffering=course, groupForSemester = groupForSemester) group.save() #LOG EVENT# l = LogEntry(userid=request.user.username, description="created a new group %s for %s." % (group.name, course), related_object=group ) l.save() if is_course_student_by_slug(request, course_slug): for activity in selected_act: groupMember = GroupMember(group=group, student=member, confirmed=True, activity=activity) groupMember.save() #LOG EVENT# l = LogEntry(userid=request.user.username, description="automatically became a group member of %s for activity %s." % (group.name, groupMember.activity), related_object=groupMember ) l.save() messages.add_message(request, messages.SUCCESS, 'Group Created') return HttpResponseRedirect(reverse('groups.views.groupmanage', kwargs={'course_slug': course_slug})) elif is_course_staff_by_slug(request, course_slug): students = Member.objects.select_related('person').filter(offering = course, role = 'STUD') for student in students: studentForm = StudentForm(request.POST, prefix = student.person.userid) if studentForm.is_valid() and studentForm.cleaned_data['selected'] == True: for activity in selected_act: groupMember = GroupMember(group=group, student=student, confirmed=True, activity=activity) groupMember.save() #LOG EVENT# l = LogEntry(userid=request.user.username, description="added %s as a group member to %s for activity %s." % (student.person.userid,group.name, groupMember.activity), related_object=groupMember ) l.save() n = NewsItem(user=student.person, author=member.person, course=group.courseoffering, source_app="group", title="Added to Group", content="You have been added the group %s." % (group.name), url=reverse('groups.views.groupmanage', kwargs={'course_slug':course.slug}) ) n.save() messages.add_message(request, messages.SUCCESS, 'Group Created') return HttpResponseRedirect(reverse('groups.views.view_group', kwargs={'course_slug': course_slug, 'group_slug': group.slug})) else: return HttpResponseForbidden()