|
| 1 | +""" |
| 2 | +Open edX Filters needed for instructor dashboard integration. |
| 3 | +""" |
| 4 | +import pkg_resources |
| 5 | +from crum import get_current_request |
| 6 | +from django.conf import settings |
| 7 | +from django.template import Context, Template |
| 8 | +from openedx_filters import PipelineStep |
| 9 | +from web_fragments.fragment import Fragment |
| 10 | + |
| 11 | +try: |
| 12 | + from cms.djangoapps.contentstore.utils import get_lms_link_for_item |
| 13 | + from lms.djangoapps.courseware.block_render import (get_block_by_usage_id, |
| 14 | + load_single_xblock) |
| 15 | + from openedx.core.djangoapps.enrollments.data import get_user_enrollments |
| 16 | + from xmodule.modulestore.django import modulestore |
| 17 | +except ImportError: |
| 18 | + load_single_xblock = None |
| 19 | + get_block_by_usage_id = None |
| 20 | + modulestore = None |
| 21 | + get_user_enrollments = None |
| 22 | + get_lms_link_for_item = None |
| 23 | + |
| 24 | +TEMPLATE_ABSOLUTE_PATH = "/instructor_dashboard/" |
| 25 | +BLOCK_CATEGORY = "feedback" |
| 26 | +TEMPLATE_CATEGORY = "feedback_instructor" |
| 27 | + |
| 28 | + |
| 29 | +class AddFeedbackTab(PipelineStep): |
| 30 | + """Add forum_notifier tab to instructor dashboard by adding a new context with feedback data.""" |
| 31 | + |
| 32 | + def run_filter( |
| 33 | + self, context, template_name |
| 34 | + ): # pylint: disable=unused-argument, arguments-differ |
| 35 | + """Execute filter that modifies the instructor dashboard context. |
| 36 | + Args: |
| 37 | + context (dict): the context for the instructor dashboard. |
| 38 | + _ (str): instructor dashboard template name. |
| 39 | + """ |
| 40 | + if not settings.FEATURES.get("ENABLE_FEEDBACK_INSTRUCTOR_VIEW", False): |
| 41 | + return { |
| 42 | + "context": context, |
| 43 | + } |
| 44 | + |
| 45 | + course = context["course"] |
| 46 | + template = Template( |
| 47 | + self.resource_string(f"static/html/{TEMPLATE_CATEGORY}.html") |
| 48 | + ) |
| 49 | + |
| 50 | + request = get_current_request() |
| 51 | + |
| 52 | + context.update( |
| 53 | + { |
| 54 | + "blocks": load_blocks(request, course), |
| 55 | + } |
| 56 | + ) |
| 57 | + |
| 58 | + html = template.render(Context(context)) |
| 59 | + frag = Fragment(html) |
| 60 | + frag.add_css(self.resource_string(f"static/css/{TEMPLATE_CATEGORY}.css")) |
| 61 | + frag.add_javascript( |
| 62 | + self.resource_string(f"static/js/src/{TEMPLATE_CATEGORY}.js") |
| 63 | + ) |
| 64 | + |
| 65 | + section_data = { |
| 66 | + "fragment": frag, |
| 67 | + "section_key": TEMPLATE_CATEGORY, |
| 68 | + "section_display_name": "Course Feedback", |
| 69 | + "course_id": str(course.id), |
| 70 | + "template_path_prefix": TEMPLATE_ABSOLUTE_PATH, |
| 71 | + } |
| 72 | + context["sections"].append(section_data) |
| 73 | + |
| 74 | + return {"context": context} |
| 75 | + |
| 76 | + def resource_string(self, path): |
| 77 | + """Handy helper for getting resources from our kit.""" |
| 78 | + data = pkg_resources.resource_string("feedback", path) |
| 79 | + return data.decode("utf8") |
| 80 | + |
| 81 | + |
| 82 | +def load_blocks(request, course): |
| 83 | + """ |
| 84 | + Load feedback blocks for a given course for all enrolled students. |
| 85 | +
|
| 86 | + Arguments: |
| 87 | + request (HttpRequest): Django request object. |
| 88 | + course (CourseLocator): Course locator object. |
| 89 | + """ |
| 90 | + course_id = str(course.id) |
| 91 | + |
| 92 | + feedback_blocks = modulestore().get_items( |
| 93 | + course.id, qualifiers={"category": BLOCK_CATEGORY} |
| 94 | + ) |
| 95 | + |
| 96 | + blocks = [] |
| 97 | + |
| 98 | + if not feedback_blocks: |
| 99 | + return [] |
| 100 | + |
| 101 | + students = get_user_enrollments(course_id).values_list("user_id", "user__username") |
| 102 | + for feedback_block in feedback_blocks: |
| 103 | + block, _ = get_block_by_usage_id( |
| 104 | + request, |
| 105 | + str(course.id), |
| 106 | + str(feedback_block.location), |
| 107 | + disable_staff_debug_info=True, |
| 108 | + course=course, |
| 109 | + ) |
| 110 | + answers = load_xblock_answers( |
| 111 | + request, |
| 112 | + students, |
| 113 | + str(course.location.course_key), |
| 114 | + str(feedback_block.location), |
| 115 | + course, |
| 116 | + ) |
| 117 | + |
| 118 | + vote_aggregate = [] |
| 119 | + total_votes = 0 |
| 120 | + total_answers = 0 |
| 121 | + |
| 122 | + if not block.vote_aggregate: |
| 123 | + block.vote_aggregate = [0] * len(block.get_prompt()["scale_text"]) |
| 124 | + for index, vote in enumerate(block.vote_aggregate): |
| 125 | + vote_aggregate.append( |
| 126 | + { |
| 127 | + "scale_text": block.get_prompt()["scale_text"][index], |
| 128 | + "count": vote, |
| 129 | + } |
| 130 | + ) |
| 131 | + total_answers += vote |
| 132 | + # We have an inverted scale, so we need to invert the index |
| 133 | + # to get the correct average rating. |
| 134 | + # Excellent = 1, Very Good = 2, Good = 3, Fair = 4, Poor = 5 |
| 135 | + # So Excellent = 5, Very Good = 4, Good = 3, Fair = 2, Poor = 1 |
| 136 | + total_votes += vote * (5 - index) |
| 137 | + |
| 138 | + try: |
| 139 | + average_rating = round(total_votes / total_answers, 2) |
| 140 | + except ZeroDivisionError: |
| 141 | + average_rating = 0 |
| 142 | + |
| 143 | + parent, _ = get_block_by_usage_id( |
| 144 | + request, |
| 145 | + str(course.id), |
| 146 | + str(feedback_block.parent), |
| 147 | + disable_staff_debug_info=True, |
| 148 | + course=course, |
| 149 | + ) |
| 150 | + |
| 151 | + blocks.append( |
| 152 | + { |
| 153 | + "display_name": block.display_name, |
| 154 | + "prompts": block.prompts, |
| 155 | + "vote_aggregate": vote_aggregate, |
| 156 | + "answers": answers[-10:], |
| 157 | + "parent": parent.display_name, |
| 158 | + "average_rating": average_rating, |
| 159 | + "url": get_lms_link_for_item(block.location), |
| 160 | + } |
| 161 | + ) |
| 162 | + return blocks |
| 163 | + |
| 164 | + |
| 165 | +def load_xblock_answers(request, students, course_id, block_id, course): |
| 166 | + """ |
| 167 | + Load answers for a given feedback xblock instance. |
| 168 | +
|
| 169 | + Arguments: |
| 170 | + request (HttpRequest): Django request object. |
| 171 | + students (list): List of enrolled students. |
| 172 | + course_id (str): Course ID. |
| 173 | + block_id (str): Block ID. |
| 174 | + course (CourseDescriptor): Course descriptor. |
| 175 | + """ |
| 176 | + answers = [] |
| 177 | + for user_id, username in students: |
| 178 | + student_xblock_instance = load_single_xblock( |
| 179 | + request, user_id, course_id, block_id, course |
| 180 | + ) |
| 181 | + if student_xblock_instance: |
| 182 | + prompt = student_xblock_instance.get_prompt() |
| 183 | + if student_xblock_instance.user_freeform: |
| 184 | + if student_xblock_instance.user_vote != -1: |
| 185 | + vote = prompt["scale_text"][student_xblock_instance.user_vote] |
| 186 | + else: |
| 187 | + vote = "No vote" |
| 188 | + answers.append( |
| 189 | + { |
| 190 | + "username": username, |
| 191 | + "user_vote": vote, |
| 192 | + "user_freeform": student_xblock_instance.user_freeform, |
| 193 | + } |
| 194 | + ) |
| 195 | + |
| 196 | + return answers |
0 commit comments