instructor_dashboard.py 12.1 KB
Newer Older
1 2 3 4
"""
Instructor Dashboard Views
"""

Miles Steele committed
5
from django.utils.translation import ugettext as _
6 7
from django_future.csrf import ensure_csrf_cookie
from django.views.decorators.cache import cache_control
David Baumgold committed
8
from edxmako.shortcuts import render_to_response
9
from django.contrib.auth.models import User
10 11
from django.core.urlresolvers import reverse
from django.utils.html import escape
12
from django.http import Http404
13
from django.conf import settings
14

15
from lms.lib.xblock.runtime import quote_slashes
16
from xmodule_modifiers import wrap_xblock
17
from xmodule.html_module import HtmlDescriptor
18
from xmodule.modulestore import XML_MODULESTORE_TYPE
19
from xmodule.modulestore.django import modulestore
20 21
from xblock.field_data import DictFieldData
from xblock.fields import ScopeIds
22
from courseware.access import has_access
23
from courseware.courses import get_course_by_id, get_cms_course_link, get_course_with_access
24
from django_comment_client.utils import has_forum_access
Miles Steele committed
25
from django_comment_common.models import FORUM_ROLE_ADMINISTRATOR
26
from student.models import CourseEnrollment
27
from bulk_email.models import CourseAuthorization
28
from class_dashboard.dashboard_data import get_section_display_name, get_array_section_has_problem
29

30
from .tools import get_units_with_due_date, title_or_url, bulk_email_is_enabled_for_course
31
from opaque_keys.edx.locations import SlashSeparatedCourseKey
32 33


34 35 36
@ensure_csrf_cookie
@cache_control(no_cache=True, no_store=True, must_revalidate=True)
def instructor_dashboard_2(request, course_id):
37
    """ Display the instructor dashboard for a course. """
38 39 40
    course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_by_id(course_key, depth=None)
    is_studio_course = (modulestore().get_modulestore_type(course_key) != XML_MODULESTORE_TYPE)
41

Miles Steele committed
42
    access = {
43
        'admin': request.user.is_staff,
44 45
        'instructor': has_access(request.user, 'instructor', course),
        'staff': has_access(request.user, 'staff', course),
46
        'forum_admin': has_forum_access(
47
            request.user, course_key, FORUM_ROLE_ADMINISTRATOR
48
        ),
Miles Steele committed
49 50
    }

51
    if not access['staff']:
52
        raise Http404()
53

54
    sections = [
55 56 57 58 59
        _section_course_info(course_key, access),
        _section_membership(course_key, access),
        _section_student_admin(course_key, access),
        _section_data_download(course_key, access),
        _section_analytics(course_key, access),
60
    ]
61

62 63 64
    if (settings.FEATURES.get('INDIVIDUAL_DUE_DATES') and access['instructor']):
        sections.insert(3, _section_extensions(course))

65
    # Gate access to course email by feature flag & by course-specific authorization
66
    if bulk_email_is_enabled_for_course(course_key):
67
        sections.append(_section_send_email(course_key, access, course))
68

69 70
    # Gate access to Metrics tab by featue flag and staff authorization
    if settings.FEATURES['CLASS_DASHBOARD'] and access['staff']:
71
        sections.append(_section_metrics(course_key, access))
72

73 74
    studio_url = None
    if is_studio_course:
75
        studio_url = get_cms_course_link(course)
76

77 78
    enrollment_count = sections[0]['enrollment_count']
    disable_buttons = False
79
    max_enrollment_for_buttons = settings.FEATURES.get("MAX_ENROLLMENT_INSTR_BUTTONS")
80 81 82
    if max_enrollment_for_buttons is not None:
        disable_buttons = enrollment_count > max_enrollment_for_buttons

83 84
    context = {
        'course': course,
85
        'old_dashboard_url': reverse('instructor_dashboard_legacy', kwargs={'course_id': course_key.to_deprecated_string()}),
86
        'studio_url': studio_url,
87
        'sections': sections,
88
        'disable_buttons': disable_buttons,
89 90
    }

91
    return render_to_response('instructor/instructor_dashboard_2/instructor_dashboard_2.html', context)
92 93


94 95 96 97 98 99 100 101
"""
Section functions starting with _section return a dictionary of section data.

The dictionary must include at least {
    'section_key': 'circus_expo'
    'section_display_name': 'Circus Expo'
}

Miles Steele committed
102
section_key will be used as a css attribute, javascript tie-in, and template import filename.
103
section_display_name will be used to generate link titles in the nav bar.
Miles Steele committed
104
"""  # pylint: disable=W0105
105 106


107
def _section_course_info(course_key, access):
108
    """ Provide data for the corresponding dashboard section """
109
    course = get_course_by_id(course_key, depth=None)
110

111 112 113
    section_data = {
        'section_key': 'course_info',
        'section_display_name': _('Course Info'),
114
        'access': access,
115
        'course_id': course_key,
116
        'course_display_name': course.display_name,
117
        'enrollment_count': CourseEnrollment.num_enrolled_in(course_key),
118 119
        'has_started': course.has_started(),
        'has_ended': course.has_ended(),
120
        'list_instructor_tasks_url': reverse('list_instructor_tasks', kwargs={'course_id': course_key.to_deprecated_string()}),
121
    }
Miles Steele committed
122

Miles Steele committed
123
    try:
Miles Steele committed
124 125 126
        advance = lambda memo, (letter, score): "{}: {}, ".format(letter, score) + memo
        section_data['grade_cutoffs'] = reduce(advance, course.grade_cutoffs.items(), "")[:-2]
    except Exception:
Miles Steele committed
127
        section_data['grade_cutoffs'] = "Not Available"
128
    # section_data['offline_grades'] = offline_grades_available(course_key)
129 130

    try:
131
        section_data['course_errors'] = [(escape(a), '') for (a, _unused) in modulestore().get_course_errors(course.id)]
132 133 134 135 136 137
    except Exception:
        section_data['course_errors'] = [('Error fetching errors', '')]

    return section_data


138
def _section_membership(course_key, access):
139
    """ Provide data for the corresponding dashboard section """
140
    section_data = {
141
        'section_key': 'membership',
Miles Steele committed
142
        'section_display_name': _('Membership'),
Miles Steele committed
143
        'access': access,
144 145 146 147 148 149 150
        'enroll_button_url': reverse('students_update_enrollment', kwargs={'course_id': course_key.to_deprecated_string()}),
        'unenroll_button_url': reverse('students_update_enrollment', kwargs={'course_id': course_key.to_deprecated_string()}),
        'modify_beta_testers_button_url': reverse('bulk_beta_modify_access', kwargs={'course_id': course_key.to_deprecated_string()}),
        'list_course_role_members_url': reverse('list_course_role_members', kwargs={'course_id': course_key.to_deprecated_string()}),
        'modify_access_url': reverse('modify_access', kwargs={'course_id': course_key.to_deprecated_string()}),
        'list_forum_members_url': reverse('list_forum_members', kwargs={'course_id': course_key.to_deprecated_string()}),
        'update_forum_role_membership_url': reverse('update_forum_role_membership', kwargs={'course_id': course_key.to_deprecated_string()}),
151
    }
152 153 154
    return section_data


155
def _section_student_admin(course_key, access):
156
    """ Provide data for the corresponding dashboard section """
157
    is_small_course = False
158
    enrollment_count = CourseEnrollment.num_enrolled_in(course_key)
159 160 161 162
    max_enrollment_for_buttons = settings.FEATURES.get("MAX_ENROLLMENT_INSTR_BUTTONS")
    if max_enrollment_for_buttons is not None:
        is_small_course = enrollment_count <= max_enrollment_for_buttons

163 164
    section_data = {
        'section_key': 'student_admin',
Miles Steele committed
165
        'section_display_name': _('Student Admin'),
166
        'access': access,
167
        'is_small_course': is_small_course,
168 169 170 171 172 173
        'get_student_progress_url_url': reverse('get_student_progress_url', kwargs={'course_id': course_key.to_deprecated_string()}),
        'enrollment_url': reverse('students_update_enrollment', kwargs={'course_id': course_key.to_deprecated_string()}),
        'reset_student_attempts_url': reverse('reset_student_attempts', kwargs={'course_id': course_key.to_deprecated_string()}),
        'rescore_problem_url': reverse('rescore_problem', kwargs={'course_id': course_key.to_deprecated_string()}),
        'list_instructor_tasks_url': reverse('list_instructor_tasks', kwargs={'course_id': course_key.to_deprecated_string()}),
        'spoc_gradebook_url': reverse('spoc_gradebook', kwargs={'course_id': course_key.to_deprecated_string()}),
174
    }
175 176 177
    return section_data


178 179 180 181 182
def _section_extensions(course):
    """ Provide data for the corresponding dashboard section """
    section_data = {
        'section_key': 'extensions',
        'section_display_name': _('Extensions'),
183
        'units_with_due_dates': [(title_or_url(unit), unit.location.to_deprecated_string())
184
                                 for unit in get_units_with_due_date(course)],
185 186 187 188
        'change_due_date_url': reverse('change_due_date', kwargs={'course_id': course.id.to_deprecated_string()}),
        'reset_due_date_url': reverse('reset_due_date', kwargs={'course_id': course.id.to_deprecated_string()}),
        'show_unit_extensions_url': reverse('show_unit_extensions', kwargs={'course_id': course.id.to_deprecated_string()}),
        'show_student_extensions_url': reverse('show_student_extensions', kwargs={'course_id': course.id.to_deprecated_string()}),
189 190 191 192
    }
    return section_data


193
def _section_data_download(course_key, access):
194 195
    """ Provide data for the corresponding dashboard section """
    section_data = {
196
        'section_key': 'data_download',
Miles Steele committed
197
        'section_display_name': _('Data Download'),
198
        'access': access,
199 200 201 202 203 204
        'get_grading_config_url': reverse('get_grading_config', kwargs={'course_id': course_key.to_deprecated_string()}),
        'get_students_features_url': reverse('get_students_features', kwargs={'course_id': course_key.to_deprecated_string()}),
        'get_anon_ids_url': reverse('get_anon_ids', kwargs={'course_id': course_key.to_deprecated_string()}),
        'list_instructor_tasks_url': reverse('list_instructor_tasks', kwargs={'course_id': course_key.to_deprecated_string()}),
        'list_report_downloads_url': reverse('list_report_downloads', kwargs={'course_id': course_key.to_deprecated_string()}),
        'calculate_grades_csv_url': reverse('calculate_grades_csv', kwargs={'course_id': course_key.to_deprecated_string()}),
205 206 207
    }
    return section_data

208

209
def _section_send_email(course_key, access, course):
210
    """ Provide data for the corresponding bulk email section """
211 212 213
    html_module = HtmlDescriptor(
        course.system,
        DictFieldData({'data': ''}),
214
        ScopeIds(None, None, None, course_key.make_usage_key('html', 'fake'))
215
    )
216
    fragment = course.system.render(html_module, 'studio_view')
217 218 219 220 221
    fragment = wrap_xblock(
        'LmsRuntime', html_module, 'studio_view', fragment, None,
        extra_data={"course-id": course_key.to_deprecated_string()},
        usage_id_serializer=lambda usage_id: quote_slashes(usage_id.to_deprecated_string())
    )
222
    email_editor = fragment.content
223 224 225
    section_data = {
        'section_key': 'send_email',
        'section_display_name': _('Email'),
226
        'access': access,
227
        'send_email': reverse('send_email', kwargs={'course_id': course_key.to_deprecated_string()}),
228
        'editor': email_editor,
229 230 231 232 233 234
        'list_instructor_tasks_url': reverse(
            'list_instructor_tasks', kwargs={'course_id': course_key.to_deprecated_string()}
        ),
        'email_background_tasks_url': reverse(
            'list_background_email_tasks', kwargs={'course_id': course_key.to_deprecated_string()}
        ),
235 236 237
    }
    return section_data

238

239
def _section_analytics(course_key, access):
240 241
    """ Provide data for the corresponding dashboard section """
    section_data = {
242
        'section_key': 'analytics',
Miles Steele committed
243
        'section_display_name': _('Analytics'),
244
        'access': access,
245 246
        'get_distribution_url': reverse('get_distribution', kwargs={'course_id': course_key.to_deprecated_string()}),
        'proxy_legacy_analytics_url': reverse('proxy_legacy_analytics', kwargs={'course_id': course_key.to_deprecated_string()}),
247 248
    }
    return section_data
249 250


251
def _section_metrics(course_key, access):
252 253 254 255 256
    """Provide data for the corresponding dashboard section """
    section_data = {
        'section_key': 'metrics',
        'section_display_name': ('Metrics'),
        'access': access,
Calen Pennington committed
257
        'course_id': course_key.to_deprecated_string(),
258 259
        'sub_section_display_name': get_section_display_name(course_key),
        'section_has_problem': get_array_section_has_problem(course_key),
260 261
        'get_students_opened_subsection_url': reverse('get_students_opened_subsection'),
        'get_students_problem_grades_url': reverse('get_students_problem_grades'),
262
        'post_metrics_data_csv_url': reverse('post_metrics_data_csv'),
263 264
    }
    return section_data