def course_detail(request, semester_id, course_id): semester = get_object_or_404(Semester, id=semester_id) course = get_object_or_404(semester.course_set, id=course_id, semester=semester) if not course.can_user_see_results(request.user): raise PermissionDenied sections = calculate_results(course) public_view = request.GET.get('public_view') == 'true' # if parameter is not given, show own view. represented_users = list(request.user.represented_users.all()) represented_users.append(request.user) # filter text answers for section in sections: results = [] for result in section.results: if isinstance(result, TextResult): answers = [answer for answer in result.answers if user_can_see_text_answer(request.user, represented_users, answer, public_view)] if answers: results.append(TextResult(question=result.question, answers=answers)) else: results.append(result) section.results[:] = results # remove empty sections sections = [section for section in sections if section.results] # group by contributor course_sections = [] contributor_sections = OrderedDict() for section in sections: if not section.results: continue if section.contributor is None: course_sections.append(section) else: contributor_sections.setdefault(section.contributor, {'total_votes': 0, 'sections': []})['sections'].append(section) # Sum up all Sections for this contributor. # If section is not a RatingResult: # Add 1 as we assume it is a TextResult or something similar that should be displayed. contributor_sections[section.contributor]['total_votes'] +=\ sum([s.total_count if isinstance(s, RatingResult) else 1 for s in section.results]) # Show a warning if course is still in evaluation (for reviewer preview). evaluation_warning = course.state != 'published' # Results for a course might not be visible because there are not enough answers # but it can still be "published" e.g. to show the comment results to contributors. # Users who can open the results page see a warning message in this case. sufficient_votes_warning = not course.can_publish_grades show_grades = request.user.is_reviewer or course.can_publish_grades course.avg_grade, course.avg_deviation = calculate_average_grades_and_deviation(course) template_data = dict( course=course, course_sections=course_sections, contributor_sections=contributor_sections, evaluation_warning=evaluation_warning, sufficient_votes_warning=sufficient_votes_warning, show_grades=show_grades, reviewer=request.user.is_reviewer, contributor=course.is_user_contributor_or_delegate(request.user), can_download_grades=request.user.can_download_grades, public_view=public_view) return render(request, "results_course_detail.html", template_data)
def course_detail(request, semester_id, course_id): semester = get_object_or_404(Semester, id=semester_id) course = get_object_or_404(semester.course_set, id=course_id, semester=semester) if not course.can_user_see_results(request.user): raise PermissionDenied sections = calculate_results(course) if request.user.is_reviewer: public_view = request.GET.get( 'public_view' ) != 'false' # if parameter is not given, show public view. else: public_view = request.GET.get( 'public_view' ) == 'true' # if parameter is not given, show own view. # If grades are not published, there is no public view if not course.can_publish_grades: public_view = False represented_users = list(request.user.represented_users.all()) represented_users.append(request.user) show_grades = request.user.is_reviewer or course.can_publish_grades # filter text answers for section in sections: results = [] for result in section.results: if isinstance(result, TextResult): answers = [ answer for answer in result.answers if user_can_see_text_answer( request.user, represented_users, answer, public_view) ] if answers: results.append( TextResult(question=result.question, answers=answers)) else: results.append(result) section.results[:] = results # filter empty headings for section in sections: filtered_results = [] for index in range(len(section.results)): result = section.results[index] # filter out if there are no more questions or the next question is also a heading question if isinstance(result, HeadingResult): if index == len(section.results) - 1 or isinstance( section.results[index + 1], HeadingResult): continue filtered_results.append(result) section.results[:] = filtered_results # remove empty sections sections = [section for section in sections if section.results] # group by contributor course_sections_top = [] course_sections_bottom = [] contributor_sections = OrderedDict() for section in sections: if section.contributor is None: if section.questionnaire.is_below_contributors: course_sections_bottom.append(section) else: course_sections_top.append(section) else: contributor_sections.setdefault(section.contributor, { 'total_votes': 0, 'sections': [] })['sections'].append(section) for result in section.results: if isinstance(result, TextResult): contributor_sections[ section.contributor]['total_votes'] += 1 elif isinstance(result, RatingResult) or isinstance( result, YesNoResult): # Only count rating results if we show the grades. if show_grades: contributor_sections[section.contributor][ 'total_votes'] += result.total_count # Show a warning if course is still in evaluation (for reviewer preview). evaluation_warning = course.state != 'published' # Results for a course might not be visible because there are not enough answers # but it can still be "published" e.g. to show the comment results to contributors. # Users who can open the results page see a warning message in this case. sufficient_votes_warning = not course.can_publish_grades course.avg_grade, course.avg_deviation = calculate_average_grades_and_deviation( course) template_data = dict(course=course, course_sections_top=course_sections_top, course_sections_bottom=course_sections_bottom, contributor_sections=contributor_sections, evaluation_warning=evaluation_warning, sufficient_votes_warning=sufficient_votes_warning, show_grades=show_grades, reviewer=request.user.is_reviewer, contributor=course.is_user_contributor_or_delegate( request.user), can_download_grades=request.user.can_download_grades, public_view=public_view) return render(request, "results_course_detail.html", template_data)
def course_detail(request, semester_id, course_id): semester = get_object_or_404(Semester, id=semester_id) course = get_object_or_404(semester.course_set, id=course_id, semester=semester) if not course.can_user_see_results_page(request.user): raise PermissionDenied sections = calculate_results(course) if request.user.is_reviewer: public_view = request.GET.get( 'public_view' ) != 'false' # if parameter is not given, show public view. else: public_view = request.GET.get( 'public_view' ) == 'true' # if parameter is not given, show own view. # If grades are not published, there is no public view if not course.has_enough_voters_to_publish_grades: public_view = False represented_users = list(request.user.represented_users.all()) represented_users.append(request.user) show_grades = course.can_user_see_grades(request.user) # remove text answers and grades if the user may not see them for section in sections: results = [] for result in section.results: if isinstance(result, TextResult): answers = [ answer for answer in result.answers if user_can_see_text_answer( request.user, represented_users, answer, public_view) ] if answers: results.append( TextResult(question=result.question, answers=answers)) elif isinstance(result, RatingResult) and not show_grades: results.append( RatingResult(question=result.question, total_count=result.total_count, average=None, counts=None, warning=result.warning)) elif isinstance(result, YesNoResult) and not show_grades: results.append( YesNoResult(question=result.question, total_count=result.total_count, average=None, counts=None, warning=result.warning, approval_count=None)) else: results.append(result) section.results[:] = results # filter empty headings for section in sections: filtered_results = [] for index in range(len(section.results)): result = section.results[index] # filter out if there are no more questions or the next question is also a heading question if isinstance(result, HeadingResult): if index == len(section.results) - 1 or isinstance( section.results[index + 1], HeadingResult): continue filtered_results.append(result) section.results[:] = filtered_results # remove empty sections sections = [section for section in sections if section.results] # group by contributor course_sections_top = [] course_sections_bottom = [] contributor_sections = OrderedDict() for section in sections: if section.contributor is None: if section.questionnaire.is_below_contributors: course_sections_bottom.append(section) else: course_sections_top.append(section) else: contributor_sections.setdefault(section.contributor, { 'total_votes': 0, 'sections': [] })['sections'].append(section) for result in section.results: if isinstance(result, TextResult): contributor_sections[ section.contributor]['total_votes'] += 1 elif isinstance(result, RatingResult) or isinstance( result, YesNoResult): # Only count rating results if we show the grades. if show_grades: contributor_sections[section.contributor][ 'total_votes'] += result.total_count course.distribution = calculate_average_distribution( course) if show_grades else None course.avg_grade = distribution_to_grade(course.distribution) template_data = dict(course=course, course_sections_top=course_sections_top, course_sections_bottom=course_sections_bottom, contributor_sections=contributor_sections, reviewer=request.user.is_reviewer, contributor=course.is_user_contributor_or_delegate( request.user), can_download_grades=request.user.can_download_grades, public_view=public_view) return render(request, "results_course_detail.html", template_data)