Beispiel #1
0
    def testCreateGradedAssignmentsNotification(self):
        notification = Notification.create_graded_assignments_notification(
            self.course, self.assignment, self.assignment.submissions.all()
        )
        self.assertTrue(self.student1.notifications.filter(pk=notification.pk).exists())

        no_grade_student = self.assignment.submissions.filter(score__isnull=True)[0].user
        self.assertFalse(no_grade_student.notifications.filter(pk=notification.pk).exists())
Beispiel #2
0
    def testCreateGradedAssignmentsNotification(self):
        notification = Notification.create_graded_assignments_notification(
            self.course, self.assignment, self.assignment.submissions.all())
        self.assertTrue(
            self.student1.notifications.filter(pk=notification.pk).exists())

        no_grade_student = self.assignment.submissions.filter(
            score__isnull=True)[0].user
        self.assertFalse(
            no_grade_student.notifications.filter(pk=notification.pk).exists())
Beispiel #3
0
def instructor_view_assignment(request, course_name, assignment_id):
    try:
        assignment = request.assignment
    except AttributeError:
        assignment = get_object_or_404(Assignment, id=assignment_id)

    #delete notifications
    #request.user.notifications.filter(assignment=assignment, resource__isnull=True, is_new=True).update(is_new=False)

    form = None
    if request.method == "POST" and request.POST.get(
            "submit_type") == "assignment":
        form = AssignmentForm(instance=assignment,
                              course=request.course,
                              data=request.POST)
        if form.is_valid() and form.has_changed():
            form.save()
            messages.success(request, "Assignment updated successfully.")
    else:
        form = AssignmentForm(instance=assignment, course=request.course)

    quiz = None
    grade_formset = None
    grade_form = None
    has_submitted_quiz = []
    submissions = assignment.get_submissions(request)
    if assignment.type == 's':  #if assignment is submitted online
        file_submissions = QueryList(
            AssignmentFileSubmission.objects.filter(
                assignment_submission__assignment=assignment))

        for file in file_submissions:
            file._set_cached_course(request.course)  #for performance

        for submission in submissions:
            submission.current_files = file_submissions.filter(
                assignment_submission_id=submission.id)

    elif assignment.type == 'c' or assignment.type == 'o':  #if assignment is taken in class or taken online
        if assignment.type == 'o':  #if assignment is taken online
            quiz_submissions = QuizSubmission.objects.filter(
                submission__assignment=assignment)

            for quiz_submission in quiz_submissions:
                if quiz_submission.submission in submissions:
                    has_submitted_quiz.append(quiz_submission.submission.id)

        def custom_field_callback(field):
            return field.formfield(required=False)

        AssignmentGradeFormset = modelformset_factory(
            AssignmentSubmission,
            fields=("score", "text"),
            formfield_callback=custom_field_callback,
            extra=0)

        grade_queryset = assignment.submissions.select_related(
            "user").order_by("user__last_name", "user__first_name")

        if request.method == "POST" and request.POST.get(
                "submit_type") == "grades":
            grade_formset = AssignmentGradeFormset(queryset=grade_queryset,
                                                   data=request.POST,
                                                   auto_id=False)
            if grade_formset.is_valid():
                unsaved_submissions = grade_formset.save(commit=False)
                for submission in unsaved_submissions:
                    submission.status = 1  #graded
                    submission.save()

                if assignment.type == 'c':
                    Notification.create_graded_assignments_notification(
                        request.course, assignment, submissions)

                messages.success(request, "Students were graded successfully.")
        else:
            grade_formset = AssignmentGradeFormset(queryset=grade_queryset,
                                                   auto_id=False)

    #resource form handling
    resource_form = None
    if request.method == "POST" and request.POST.get(
            "submit_type") == "resource":
        resource_form = ResourceForm(assignment=assignment,
                                     data=request.POST,
                                     files=request.FILES,
                                     auto_id=False)
        if resource_form.is_valid():
            resource = resource_form.save(request.user.username)

            Notification.create_added_resource_notification(
                request.course, assignment, resource)

            messages.success(request, "Resource added successfully.")

            resource_form = ResourceForm(assignment=assignment, auto_id=False)
    else:
        resource_form = ResourceForm(assignment=assignment, auto_id=False)

    resources = assignment.resources.all()

    for resource in resources:
        resource._set_cached_course(request.course)  #for performance

    return render_with_context(
        request, "assignments/instructor/view_assignment.html", {
            'assignment': assignment,
            'assignment_form': form,
            'grade_formset': grade_formset,
            'grade_form': grade_form,
            'resource_form': resource_form,
            'resources': resources,
            'submissions': submissions,
            'has_submitted_quiz': has_submitted_quiz
        })
Beispiel #4
0
def instructor_view_assignment(request, course_name, assignment_id):
	try:
		assignment = request.assignment
	except AttributeError:
		assignment = get_object_or_404(Assignment, id=assignment_id)
	
	#delete notifications
	#request.user.notifications.filter(assignment=assignment, resource__isnull=True, is_new=True).update(is_new=False)
	
	form = None
	if request.method == "POST" and request.POST.get("submit_type") == "assignment":
		form = AssignmentForm(instance=assignment, course=request.course, data=request.POST)
		if form.is_valid() and form.has_changed():
			form.save()
			messages.success(request, "Assignment updated successfully.")
	else:
		form = AssignmentForm(instance=assignment, course=request.course)
	
	quiz = None
	grade_formset = None
	grade_form = None
	has_submitted_quiz = []
	submissions = assignment.get_submissions(request)
	if assignment.type == 's': #if assignment is submitted online						
		file_submissions = QueryList(
			AssignmentFileSubmission.objects.filter(assignment_submission__assignment=assignment)
		)
		
		for file in file_submissions:
			file._set_cached_course(request.course) #for performance
		
		for submission in submissions:
			submission.current_files = file_submissions.filter(assignment_submission_id=submission.id)
		
	elif assignment.type == 'c' or assignment.type == 'o': #if assignment is taken in class or taken online		
		if assignment.type == 'o': #if assignment is taken online
			quiz_submissions = QuizSubmission.objects.filter(submission__assignment=assignment)
			
			for quiz_submission in quiz_submissions:
				if quiz_submission.submission in submissions:
					has_submitted_quiz.append(quiz_submission.submission.id)
		
		
		def custom_field_callback(field):
			return field.formfield(required=False)
		
		AssignmentGradeFormset = modelformset_factory(AssignmentSubmission, fields=("score", "text"),
													   formfield_callback=custom_field_callback, extra=0)
		
		grade_queryset = assignment.submissions.select_related("user").order_by("user__last_name", "user__first_name")
		
		if request.method == "POST" and request.POST.get("submit_type") == "grades":
			grade_formset = AssignmentGradeFormset(queryset=grade_queryset, data=request.POST, auto_id=False)
			if grade_formset.is_valid():
				unsaved_submissions = grade_formset.save(commit=False)
				for submission in unsaved_submissions:
					submission.status = 1 #graded
					submission.save()
			
				if assignment.type == 'c':
					Notification.create_graded_assignments_notification(request.course, assignment, submissions)
				
				messages.success(request, "Students were graded successfully.")
		else:
			grade_formset = AssignmentGradeFormset(queryset=grade_queryset, auto_id=False)
			
			
	#resource form handling
	resource_form = None
	if request.method == "POST" and request.POST.get("submit_type") == "resource":
		resource_form = ResourceForm(assignment=assignment, data=request.POST, files=request.FILES, auto_id=False)
		if resource_form.is_valid():
			resource = resource_form.save(request.user.username)
			
			Notification.create_added_resource_notification(request.course, assignment, resource)
			
			messages.success(request, "Resource added successfully.")
			
			resource_form = ResourceForm(assignment=assignment, auto_id=False)
	else:
		resource_form = ResourceForm(assignment=assignment, auto_id=False)

	resources = assignment.resources.all()
													
	for resource in resources:
		resource._set_cached_course(request.course) #for performance

	return render_with_context(request, "assignments/instructor/view_assignment.html", 
						{ 'assignment': assignment, 'assignment_form': form, 
						'grade_formset': grade_formset, 'grade_form': grade_form, 
						'resource_form': resource_form, 'resources': resources,
						'submissions': submissions, 'has_submitted_quiz': has_submitted_quiz })