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

5
import logging
6
import datetime
stephensanchez committed
7 8
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
9
import uuid
10
import pytz
11 12 13

from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_POST
14
from django.utils.translation import ugettext as _, ugettext_noop
15
from django.views.decorators.csrf import ensure_csrf_cookie
16
from django.views.decorators.cache import cache_control
David Baumgold committed
17
from edxmako.shortcuts import render_to_response
18 19
from django.core.urlresolvers import reverse
from django.utils.html import escape
stephensanchez committed
20
from django.http import Http404, HttpResponseServerError
21
from django.conf import settings
22
from util.json_request import JsonResponse
23
from mock import patch
24

25
from openedx.core.lib.xblock_utils import wrap_xblock
26
from openedx.core.lib.url_utils import quote_slashes
27
from xmodule.html_module import HtmlDescriptor
28
from xmodule.modulestore.django import modulestore
29
from xmodule.tabs import CourseTab
30 31
from xblock.field_data import DictFieldData
from xblock.fields import ScopeIds
32
from courseware.access import has_access
33
from courseware.courses import get_course_by_id, get_studio_url
34
from django_comment_client.utils import has_forum_access
Miles Steele committed
35
from django_comment_common.models import FORUM_ROLE_ADMINISTRATOR
36
from openedx.core.djangoapps.course_groups.cohorts import get_course_cohorts, is_course_cohorted, DEFAULT_COHORT_NAME
37
from student.models import CourseEnrollment
38
from shoppingcart.models import Coupon, PaidCourseRegistration, CourseRegCodeItem
39
from course_modes.models import CourseMode, CourseModesArchive
stephensanchez committed
40
from student.roles import CourseFinanceAdminRole, CourseSalesAdminRole
41 42 43 44 45 46
from certificates.models import (
    CertificateGenerationConfiguration,
    CertificateWhitelist,
    GeneratedCertificate,
    CertificateStatuses,
    CertificateGenerationHistory,
47
    CertificateInvalidation,
48
)
49
from certificates import api as certs_api
50
from bulk_email.models import BulkEmailFlag
51
from util.date_utils import get_default_time_display
52

53
from class_dashboard.dashboard_data import get_section_display_name, get_array_section_has_problem
54
from .tools import get_units_with_due_date, title_or_url
55
from opaque_keys.edx.locations import SlashSeparatedCourseKey
asadiqbal committed
56
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
57

58
from openedx.core.djangolib.markup import HTML, Text
cahrens committed
59

60 61
log = logging.getLogger(__name__)

62

63
class InstructorDashboardTab(CourseTab):
64 65 66 67
    """
    Defines the Instructor Dashboard view type that is shown as a course tab.
    """

68
    type = "instructor"
69
    title = ugettext_noop('Instructor')
70
    view_name = "instructor_dashboard"
71
    is_dynamic = True    # The "Instructor" tab is instead dynamically added when it is enabled
72 73

    @classmethod
74
    def is_enabled(cls, course, user=None):
75 76 77
        """
        Returns true if the specified user has staff access.
        """
78
        return bool(user and has_access(user, 'staff', course, course.id))
79 80


81 82 83 84 85 86 87 88 89 90 91 92 93 94
def show_analytics_dashboard_message(course_key):
    """
    Defines whether or not the analytics dashboard URL should be displayed.

    Arguments:
        course_key (CourseLocator): The course locator to display the analytics dashboard message on.
    """
    if hasattr(course_key, 'ccx'):
        ccx_analytics_enabled = settings.FEATURES.get('ENABLE_CCX_ANALYTICS_DASHBOARD_URL', False)
        return settings.ANALYTICS_DASHBOARD_URL and ccx_analytics_enabled

    return settings.ANALYTICS_DASHBOARD_URL


95 96 97
@ensure_csrf_cookie
@cache_control(no_cache=True, no_store=True, must_revalidate=True)
def instructor_dashboard_2(request, course_id):
98
    """ Display the instructor dashboard for a course. """
stephensanchez committed
99 100 101 102 103 104 105
    try:
        course_key = CourseKey.from_string(course_id)
    except InvalidKeyError:
        log.error(u"Unable to find course with course key %s while loading the Instructor Dashboard.", course_id)
        return HttpResponseServerError()

    course = get_course_by_id(course_key, depth=0)
106

Miles Steele committed
107
    access = {
108
        'admin': request.user.is_staff,
109
        'instructor': bool(has_access(request.user, 'instructor', course)),
110
        'finance_admin': CourseFinanceAdminRole(course_key).has_user(request.user),
stephensanchez committed
111
        'sales_admin': CourseSalesAdminRole(course_key).has_user(request.user),
112
        'staff': bool(has_access(request.user, 'staff', course)),
113
        'forum_admin': has_forum_access(request.user, course_key, FORUM_ROLE_ADMINISTRATOR),
Miles Steele committed
114 115
    }

116 117
    if not access['staff']:
        raise Http404()
118

119
    is_white_label = CourseMode.is_white_label(course_key)
120

asadiqbal committed
121 122
    reports_enabled = configuration_helpers.get_value('SHOW_ECOMMERCE_REPORTS', False)

123
    sections = [
124
        _section_course_info(course, access),
125
        _section_membership(course, access, is_white_label),
126
        _section_cohort_management(course, access),
127
        _section_student_admin(course, access),
128
        _section_data_download(course, access),
129
    ]
130

131
    analytics_dashboard_message = None
132
    if show_analytics_dashboard_message(course_key):
133 134
        # Construct a URL to the external analytics dashboard
        analytics_dashboard_url = '{0}/courses/{1}'.format(settings.ANALYTICS_DASHBOARD_URL, unicode(course_key))
cahrens committed
135
        link_start = HTML("<a href=\"{}\" target=\"_blank\">").format(analytics_dashboard_url)
136 137 138 139
        analytics_dashboard_message = _(
            "To gain insights into student enrollment and participation {link_start}"
            "visit {analytics_dashboard_name}, our new course analytics product{link_end}."
        )
cahrens committed
140 141
        analytics_dashboard_message = Text(analytics_dashboard_message).format(
            link_start=link_start, link_end=HTML("</a>"), analytics_dashboard_name=settings.ANALYTICS_DASHBOARD_NAME)
142 143 144 145 146

        # Temporarily show the "Analytics" section until we have a better way of linking to Insights
        sections.append(_section_analytics(course, access))

    # Check if there is corresponding entry in the CourseMode Table related to the Instructor Dashboard course
147
    course_mode_has_price = False
stephensanchez committed
148 149
    paid_modes = CourseMode.paid_modes_for_course(course_key)
    if len(paid_modes) == 1:
150
        course_mode_has_price = True
stephensanchez committed
151 152 153 154 155 156 157
    elif len(paid_modes) > 1:
        log.error(
            u"Course %s has %s course modes with payment options. Course must only have "
            u"one paid course mode to enable eCommerce options.",
            unicode(course_key), len(paid_modes)
        )

158
    if settings.FEATURES.get('INDIVIDUAL_DUE_DATES') and access['instructor']:
159 160
        sections.insert(3, _section_extensions(course))

161
    # Gate access to course email by feature flag & by course-specific authorization
162
    if BulkEmailFlag.feature_enabled(course_key):
163
        sections.append(_section_send_email(course, access))
164

165 166
    # Gate access to Metrics tab by featue flag and staff authorization
    if settings.FEATURES['CLASS_DASHBOARD'] and access['staff']:
167
        sections.append(_section_metrics(course, access))
168

David Baumgold committed
169
    # Gate access to Ecommerce tab
stephensanchez committed
170
    if course_mode_has_price and (access['finance_admin'] or access['sales_admin']):
asadiqbal committed
171
        sections.append(_section_e_commerce(course, access, paid_modes[0], is_white_label, reports_enabled))
172

173 174 175 176 177 178 179 180 181
    # Gate access to Special Exam tab depending if either timed exams or proctored exams
    # are enabled in the course

    # NOTE: For now, if we only have procotred exams enabled, then only platform Staff
    # (user.is_staff) will be able to view the special exams tab. This may
    # change in the future
    can_see_special_exams = (
        ((course.enable_proctored_exams and request.user.is_staff) or course.enable_timed_exams) and
        settings.FEATURES.get('ENABLE_SPECIAL_EXAMS', False)
182
    )
183 184
    if can_see_special_exams:
        sections.append(_section_special_exams(course, access))
185

186 187 188
    # Certificates panel
    # This is used to generate example certificates
    # and enable self-generated certificates for a course.
189 190
    # Note: This is hidden for all CCXs
    certs_enabled = CertificateGenerationConfiguration.current().enabled and not hasattr(course_key, 'ccx')
191 192 193
    if certs_enabled and access['admin']:
        sections.append(_section_certificates(course))

194
    disable_buttons = not _is_small_course(course_key)
195

196
    certificate_white_list = CertificateWhitelist.get_certificate_white_list(course_key)
197 198 199 200
    generate_certificate_exceptions_url = reverse(  # pylint: disable=invalid-name
        'generate_certificate_exceptions',
        kwargs={'course_id': unicode(course_key), 'generate_for': ''}
    )
asadiqbal committed
201 202 203 204
    generate_bulk_certificate_exceptions_url = reverse(  # pylint: disable=invalid-name
        'generate_bulk_certificate_exceptions',
        kwargs={'course_id': unicode(course_key)}
    )
205 206 207
    certificate_exception_view_url = reverse(
        'certificate_exception_view',
        kwargs={'course_id': unicode(course_key)}
208 209
    )

210 211 212 213 214 215 216
    certificate_invalidation_view_url = reverse(  # pylint: disable=invalid-name
        'certificate_invalidation_view',
        kwargs={'course_id': unicode(course_key)}
    )

    certificate_invalidations = CertificateInvalidation.get_certificate_invalidations(course_key)

217 218
    context = {
        'course': course,
219
        'studio_url': get_studio_url(course, 'course'),
220
        'sections': sections,
221
        'disable_buttons': disable_buttons,
222 223
        'analytics_dashboard_message': analytics_dashboard_message,
        'certificate_white_list': certificate_white_list,
224
        'certificate_invalidations': certificate_invalidations,
225
        'generate_certificate_exceptions_url': generate_certificate_exceptions_url,
asadiqbal committed
226
        'generate_bulk_certificate_exceptions_url': generate_bulk_certificate_exceptions_url,
227 228
        'certificate_exception_view_url': certificate_exception_view_url,
        'certificate_invalidation_view_url': certificate_invalidation_view_url,
229
    }
230

231
    return render_to_response('instructor/instructor_dashboard_2/instructor_dashboard_2.html', context)
232 233


234
## Section functions starting with _section return a dictionary of section data.
235

236 237 238 239
## The dictionary must include at least {
##     'section_key': 'circus_expo'
##     'section_display_name': 'Circus Expo'
## }
240

241 242
## section_key will be used as a css attribute, javascript tie-in, and template import filename.
## section_display_name will be used to generate link titles in the nav bar.
243 244


245
def _section_e_commerce(course, access, paid_mode, coupons_enabled, reports_enabled):
246
    """ Provide data for the corresponding dashboard section """
247
    course_key = course.id
248
    coupons = Coupon.objects.filter(course_id=course_key).order_by('-is_active')
stephensanchez committed
249 250
    course_price = paid_mode.min_price

251
    total_amount = None
252
    if access['finance_admin']:
253 254 255
        single_purchase_total = PaidCourseRegistration.get_total_amount_of_purchased_item(course_key)
        bulk_purchase_total = CourseRegCodeItem.get_total_amount_of_purchased_item(course_key)
        total_amount = single_purchase_total + bulk_purchase_total
256 257 258 259 260

    section_data = {
        'section_key': 'e-commerce',
        'section_display_name': _('E-Commerce'),
        'access': access,
261
        'course_id': unicode(course_key),
262
        'currency_symbol': settings.PAID_COURSE_REGISTRATION_CURRENCY[1],
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        'ajax_remove_coupon_url': reverse('remove_coupon', kwargs={'course_id': unicode(course_key)}),
        'ajax_get_coupon_info': reverse('get_coupon_info', kwargs={'course_id': unicode(course_key)}),
        'get_user_invoice_preference_url': reverse('get_user_invoice_preference', kwargs={'course_id': unicode(course_key)}),
        'sale_validation_url': reverse('sale_validation', kwargs={'course_id': unicode(course_key)}),
        'ajax_update_coupon': reverse('update_coupon', kwargs={'course_id': unicode(course_key)}),
        'ajax_add_coupon': reverse('add_coupon', kwargs={'course_id': unicode(course_key)}),
        'get_sale_records_url': reverse('get_sale_records', kwargs={'course_id': unicode(course_key)}),
        'get_sale_order_records_url': reverse('get_sale_order_records', kwargs={'course_id': unicode(course_key)}),
        'instructor_url': reverse('instructor_dashboard', kwargs={'course_id': unicode(course_key)}),
        'get_registration_code_csv_url': reverse('get_registration_codes', kwargs={'course_id': unicode(course_key)}),
        'generate_registration_code_csv_url': reverse('generate_registration_codes', kwargs={'course_id': unicode(course_key)}),
        'active_registration_code_csv_url': reverse('active_registration_codes', kwargs={'course_id': unicode(course_key)}),
        'spent_registration_code_csv_url': reverse('spent_registration_codes', kwargs={'course_id': unicode(course_key)}),
        'set_course_mode_url': reverse('set_course_mode_price', kwargs={'course_id': unicode(course_key)}),
        'download_coupon_codes_url': reverse('get_coupon_codes', kwargs={'course_id': unicode(course_key)}),
278
        'enrollment_report_url': reverse('get_enrollment_report', kwargs={'course_id': unicode(course_key)}),
Afzal Wali committed
279
        'exec_summary_report_url': reverse('get_exec_summary_report', kwargs={'course_id': unicode(course_key)}),
280 281 282
        'list_financial_report_downloads_url': reverse('list_financial_report_downloads',
                                                       kwargs={'course_id': unicode(course_key)}),
        'list_instructor_tasks_url': reverse('list_instructor_tasks', kwargs={'course_id': unicode(course_key)}),
283
        'look_up_registration_code': reverse('look_up_registration_code', kwargs={'course_id': unicode(course_key)}),
284
        'coupons': coupons,
stephensanchez committed
285 286
        'sales_admin': access['sales_admin'],
        'coupons_enabled': coupons_enabled,
287
        'reports_enabled': reports_enabled,
288 289
        'course_price': course_price,
        'total_amount': total_amount
290 291 292 293
    }
    return section_data


294
def _section_special_exams(course, access):
295 296 297 298
    """ Provide data for the corresponding dashboard section """
    course_key = course.id

    section_data = {
299 300
        'section_key': 'special_exams',
        'section_display_name': _('Special Exams'),
301 302 303 304 305 306
        'access': access,
        'course_id': unicode(course_key)
    }
    return section_data


307 308 309 310 311 312 313 314 315 316 317 318 319 320
def _section_certificates(course):
    """Section information for the certificates panel.

    The certificates panel allows global staff to generate
    example certificates and enable self-generated certificates
    for a course.

    Arguments:
        course (Course)

    Returns:
        dict

    """
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    example_cert_status = None
    html_cert_enabled = certs_api.has_html_certificates_enabled(course.id, course)
    if html_cert_enabled:
        can_enable_for_course = True
    else:
        example_cert_status = certs_api.example_certificates_status(course.id)

        # Allow the user to enable self-generated certificates for students
        # *only* once a set of example certificates has been successfully generated.
        # If certificates have been misconfigured for the course (for example, if
        # the PDF template hasn't been uploaded yet), then we don't want
        # to turn on self-generated certificates for students!
        can_enable_for_course = (
            example_cert_status is not None and
            all(
                cert_status['status'] == 'success'
                for cert_status in example_cert_status
            )
339
        )
340
    instructor_generation_enabled = settings.FEATURES.get('CERTIFICATES_INSTRUCTOR_GENERATION', False)
341 342 343 344
    certificate_statuses_with_count = {
        certificate['status']: certificate['count']
        for certificate in GeneratedCertificate.get_unique_statuses(course_key=course.id)
    }
345

346 347 348 349 350 351
    return {
        'section_key': 'certificates',
        'section_display_name': _('Certificates'),
        'example_certificate_status': example_cert_status,
        'can_enable_for_course': can_enable_for_course,
        'enabled_for_course': certs_api.cert_generation_enabled(course.id),
352
        'is_self_paced': course.self_paced,
353
        'instructor_generation_enabled': instructor_generation_enabled,
354
        'html_cert_enabled': html_cert_enabled,
asadiqbal committed
355
        'active_certificate': certs_api.get_active_web_certificate(course),
356 357
        'certificate_statuses_with_count': certificate_statuses_with_count,
        'status': CertificateStatuses,
358 359
        'certificate_generation_history':
            CertificateGenerationHistory.objects.filter(course_id=course.id).order_by("-created"),
360 361 362 363 364 365 366 367
        'urls': {
            'generate_example_certificates': reverse(
                'generate_example_certificates',
                kwargs={'course_id': course.id}
            ),
            'enable_certificate_generation': reverse(
                'enable_certificate_generation',
                kwargs={'course_id': course.id}
368 369 370 371 372
            ),
            'start_certificate_generation': reverse(
                'start_certificate_generation',
                kwargs={'course_id': course.id}
            ),
373 374 375 376
            'start_certificate_regeneration': reverse(
                'start_certificate_regeneration',
                kwargs={'course_id': course.id}
            ),
377 378 379 380
            'list_instructor_tasks_url': reverse(
                'list_instructor_tasks',
                kwargs={'course_id': course.id}
            ),
381 382 383 384
        }
    }


385 386 387 388 389 390 391 392 393 394 395
@ensure_csrf_cookie
@cache_control(no_cache=True, no_store=True, must_revalidate=True)
@require_POST
@login_required
def set_course_mode_price(request, course_id):
    """
    set the new course price and add new entry in the CourseModesArchive Table
    """
    try:
        course_price = int(request.POST['course_price'])
    except ValueError:
396 397 398 399
        return JsonResponse(
            {'message': _("Please Enter the numeric value for the course price")},
            status=400)  # status code 400: Bad Request

400 401 402 403 404
    currency = request.POST['currency']
    course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)

    course_honor_mode = CourseMode.objects.filter(mode_slug='honor', course_id=course_key)
    if not course_honor_mode:
405 406 407 408
        return JsonResponse(
            {'message': _("CourseMode with the mode slug({mode_slug}) DoesNotExist").format(mode_slug='honor')},
            status=400)  # status code 400: Bad Request

409 410
    CourseModesArchive.objects.create(
        course_id=course_id, mode_slug='honor', mode_display_name='Honor Code Certificate',
411
        min_price=course_honor_mode[0].min_price, currency=course_honor_mode[0].currency,
412 413 414 415 416 417
        expiration_datetime=datetime.datetime.now(pytz.utc), expiration_date=datetime.date.today()
    )
    course_honor_mode.update(
        min_price=course_price,
        currency=currency
    )
418
    return JsonResponse({'message': _("CourseMode price updated successfully")})
419 420


421
def _section_course_info(course, access):
422
    """ Provide data for the corresponding dashboard section """
423
    course_key = course.id
424

425 426 427
    section_data = {
        'section_key': 'course_info',
        'section_display_name': _('Course Info'),
428
        'access': access,
429
        'course_id': course_key,
430 431 432
        'course_display_name': course.display_name,
        'has_started': course.has_started(),
        'has_ended': course.has_ended(),
433
        'start_date': get_default_time_display(course.start),
434
        'end_date': get_default_time_display(course.end) or _('No end date set'),
435
        'num_sections': len(course.children),
436
        'list_instructor_tasks_url': reverse('list_instructor_tasks', kwargs={'course_id': unicode(course_key)}),
437
    }
Miles Steele committed
438

439
    if settings.FEATURES.get('DISPLAY_ANALYTICS_ENROLLMENTS'):
440
        section_data['enrollment_count'] = CourseEnrollment.objects.enrollment_counts(course_key)
441

442
    if show_analytics_dashboard_message(course_key):
443
        #  dashboard_link is already made safe in _get_dashboard_link
444
        dashboard_link = _get_dashboard_link(course_key)
445 446
        #  so we can use Text() here so it's not double-escaped and rendering HTML on the front-end
        message = Text(_("Enrollment data is now available in {dashboard_link}.")).format(dashboard_link=dashboard_link)
447 448
        section_data['enrollment_message'] = message

449 450 451
    if settings.FEATURES.get('ENABLE_SYSADMIN_DASHBOARD'):
        section_data['detailed_gitlogs_url'] = reverse('gitlogs_detail', kwargs={'course_id': unicode(course_key)})

Miles Steele committed
452
    try:
cahrens committed
453
        sorted_cutoffs = sorted(course.grade_cutoffs.items(), key=lambda i: i[1], reverse=True)
Miles Steele committed
454
        advance = lambda memo, (letter, score): "{}: {}, ".format(letter, score) + memo
cahrens committed
455
        section_data['grade_cutoffs'] = reduce(advance, sorted_cutoffs, "")[:-2]
456
    except Exception:  # pylint: disable=broad-except
Miles Steele committed
457
        section_data['grade_cutoffs'] = "Not Available"
458 459

    try:
460
        section_data['course_errors'] = [(escape(a), '') for (a, _unused) in modulestore().get_course_errors(course.id)]
461
    except Exception:  # pylint: disable=broad-except
462 463 464 465 466
        section_data['course_errors'] = [('Error fetching errors', '')]

    return section_data


467
def _section_membership(course, access, is_white_label):
468
    """ Provide data for the corresponding dashboard section """
469
    course_key = course.id
470
    ccx_enabled = settings.FEATURES.get('CUSTOM_COURSES_EDX', False) and course.enable_ccx
471
    section_data = {
472
        'section_key': 'membership',
Miles Steele committed
473
        'section_display_name': _('Membership'),
Miles Steele committed
474
        'access': access,
475
        'ccx_is_enabled': ccx_enabled,
476
        'is_white_label': is_white_label,
477 478 479 480 481 482 483 484
        'enroll_button_url': reverse('students_update_enrollment', kwargs={'course_id': unicode(course_key)}),
        'unenroll_button_url': reverse('students_update_enrollment', kwargs={'course_id': unicode(course_key)}),
        'upload_student_csv_button_url': reverse('register_and_enroll_students', kwargs={'course_id': unicode(course_key)}),
        'modify_beta_testers_button_url': reverse('bulk_beta_modify_access', kwargs={'course_id': unicode(course_key)}),
        'list_course_role_members_url': reverse('list_course_role_members', kwargs={'course_id': unicode(course_key)}),
        'modify_access_url': reverse('modify_access', kwargs={'course_id': unicode(course_key)}),
        'list_forum_members_url': reverse('list_forum_members', kwargs={'course_id': unicode(course_key)}),
        'update_forum_role_membership_url': reverse('update_forum_role_membership', kwargs={'course_id': unicode(course_key)}),
485 486 487 488 489 490 491
    }
    return section_data


def _section_cohort_management(course, access):
    """ Provide data for the corresponding cohort management section """
    course_key = course.id
492
    ccx_enabled = hasattr(course_key, 'ccx')
493 494
    section_data = {
        'section_key': 'cohort_management',
495
        'section_display_name': _('Cohorts'),
496
        'access': access,
497
        'ccx_is_enabled': ccx_enabled,
498 499 500 501 502
        'course_cohort_settings_url': reverse(
            'course_cohort_settings',
            kwargs={'course_key_string': unicode(course_key)}
        ),
        'cohorts_url': reverse('cohorts', kwargs={'course_key_string': unicode(course_key)}),
503
        'upload_cohorts_csv_url': reverse('add_users_to_cohorts', kwargs={'course_id': unicode(course_key)}),
504
        'discussion_topics_url': reverse('cohort_discussion_topics', kwargs={'course_key_string': unicode(course_key)}),
505 506 507
        'verified_track_cohorting_url': reverse(
            'verified_track_cohorting', kwargs={'course_key_string': unicode(course_key)}
        ),
508
    }
509 510 511
    return section_data


512
def _is_small_course(course_key):
513
    """ Compares against MAX_ENROLLMENT_INSTR_BUTTONS to determine if course enrollment is considered small. """
514
    is_small_course = False
515
    enrollment_count = CourseEnrollment.objects.num_enrolled_in(course_key)
516 517 518
    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
519 520 521
    return is_small_course


522
def _section_student_admin(course, access):
523 524
    """ Provide data for the corresponding dashboard section """
    course_key = course.id
525
    is_small_course = _is_small_course(course_key)
526

527 528
    section_data = {
        'section_key': 'student_admin',
Miles Steele committed
529
        'section_display_name': _('Student Admin'),
530
        'access': access,
531
        'is_small_course': is_small_course,
532 533 534
        'get_student_progress_url_url': reverse('get_student_progress_url', kwargs={'course_id': unicode(course_key)}),
        'enrollment_url': reverse('students_update_enrollment', kwargs={'course_id': unicode(course_key)}),
        'reset_student_attempts_url': reverse('reset_student_attempts', kwargs={'course_id': unicode(course_key)}),
535 536 537 538
        'reset_student_attempts_for_entrance_exam_url': reverse(
            'reset_student_attempts_for_entrance_exam',
            kwargs={'course_id': unicode(course_key)},
        ),
539
        'rescore_problem_url': reverse('rescore_problem', kwargs={'course_id': unicode(course_key)}),
540
        'rescore_entrance_exam_url': reverse('rescore_entrance_exam', kwargs={'course_id': unicode(course_key)}),
541 542 543 544
        'student_can_skip_entrance_exam_url': reverse(
            'mark_student_can_skip_entrance_exam',
            kwargs={'course_id': unicode(course_key)},
        ),
545
        'list_instructor_tasks_url': reverse('list_instructor_tasks', kwargs={'course_id': unicode(course_key)}),
546 547
        'list_entrace_exam_instructor_tasks_url': reverse('list_entrance_exam_instructor_tasks',
                                                          kwargs={'course_id': unicode(course_key)}),
548
        'spoc_gradebook_url': reverse('spoc_gradebook', kwargs={'course_id': unicode(course_key)}),
549
    }
550 551 552
    return section_data


553 554 555 556 557
def _section_extensions(course):
    """ Provide data for the corresponding dashboard section """
    section_data = {
        'section_key': 'extensions',
        'section_display_name': _('Extensions'),
558
        'units_with_due_dates': [(title_or_url(unit), unicode(unit.location))
559
                                 for unit in get_units_with_due_date(course)],
560 561 562 563
        'change_due_date_url': reverse('change_due_date', kwargs={'course_id': unicode(course.id)}),
        'reset_due_date_url': reverse('reset_due_date', kwargs={'course_id': unicode(course.id)}),
        'show_unit_extensions_url': reverse('show_unit_extensions', kwargs={'course_id': unicode(course.id)}),
        'show_student_extensions_url': reverse('show_student_extensions', kwargs={'course_id': unicode(course.id)}),
564 565 566 567
    }
    return section_data


568
def _section_data_download(course, access):
569
    """ Provide data for the corresponding dashboard section """
570
    course_key = course.id
571 572

    show_proctored_report_button = (
573
        settings.FEATURES.get('ENABLE_SPECIAL_EXAMS', False) and
574 575 576
        course.enable_proctored_exams
    )

577
    section_data = {
578
        'section_key': 'data_download',
Miles Steele committed
579
        'section_display_name': _('Data Download'),
580
        'access': access,
581
        'show_generate_proctored_exam_report_button': show_proctored_report_button,
582
        'get_problem_responses_url': reverse('get_problem_responses', kwargs={'course_id': unicode(course_key)}),
583 584
        'get_grading_config_url': reverse('get_grading_config', kwargs={'course_id': unicode(course_key)}),
        'get_students_features_url': reverse('get_students_features', kwargs={'course_id': unicode(course_key)}),
asadiqbal committed
585 586 587
        'get_issued_certificates_url': reverse(
            'get_issued_certificates', kwargs={'course_id': unicode(course_key)}
        ),
588 589 590
        'get_students_who_may_enroll_url': reverse(
            'get_students_who_may_enroll', kwargs={'course_id': unicode(course_key)}
        ),
591
        'get_anon_ids_url': reverse('get_anon_ids', kwargs={'course_id': unicode(course_key)}),
592
        'list_proctored_results_url': reverse('get_proctored_exam_results', kwargs={'course_id': unicode(course_key)}),
593 594 595
        'list_instructor_tasks_url': reverse('list_instructor_tasks', kwargs={'course_id': unicode(course_key)}),
        'list_report_downloads_url': reverse('list_report_downloads', kwargs={'course_id': unicode(course_key)}),
        'calculate_grades_csv_url': reverse('calculate_grades_csv', kwargs={'course_id': unicode(course_key)}),
596
        'problem_grade_report_url': reverse('problem_grade_report', kwargs={'course_id': unicode(course_key)}),
597 598
        'course_has_survey': True if course.course_survey_name else False,
        'course_survey_results_url': reverse('get_course_survey_results', kwargs={'course_id': unicode(course_key)}),
599
        'export_ora2_data_url': reverse('export_ora2_data', kwargs={'course_id': unicode(course_key)}),
600 601 602
    }
    return section_data

603

604
def null_applicable_aside_types(block):  # pylint: disable=unused-argument
605
    """
606
    get_aside method for monkey-patching into applicable_aside_types
607 608 609 610 611 612
    while rendering an HtmlDescriptor for email text editing. This returns
    an empty list.
    """
    return []


613
def _section_send_email(course, access):
614
    """ Provide data for the corresponding bulk email section """
615 616
    course_key = course.id

617 618
    # Monkey-patch applicable_aside_types to return no asides for the duration of this render
    with patch.object(course.runtime, 'applicable_aside_types', null_applicable_aside_types):
619 620 621 622 623 624 625
        # This HtmlDescriptor is only being used to generate a nice text editor.
        html_module = HtmlDescriptor(
            course.system,
            DictFieldData({'data': ''}),
            ScopeIds(None, None, None, course_key.make_usage_key('html', 'fake'))
        )
        fragment = course.system.render(html_module, 'studio_view')
626 627
    fragment = wrap_xblock(
        'LmsRuntime', html_module, 'studio_view', fragment, None,
628 629
        extra_data={"course-id": unicode(course_key)},
        usage_id_serializer=lambda usage_id: quote_slashes(unicode(usage_id)),
630 631 632
        # Generate a new request_token here at random, because this module isn't connected to any other
        # xblock rendering.
        request_token=uuid.uuid1().get_hex()
633
    )
634 635 636
    cohorts = []
    if is_course_cohorted(course_key):
        cohorts = get_course_cohorts(course)
637
    email_editor = fragment.content
638 639 640
    section_data = {
        'section_key': 'send_email',
        'section_display_name': _('Email'),
641
        'access': access,
642
        'send_email': reverse('send_email', kwargs={'course_id': unicode(course_key)}),
643
        'editor': email_editor,
644 645
        'cohorts': cohorts,
        'default_cohort_name': DEFAULT_COHORT_NAME,
646
        'list_instructor_tasks_url': reverse(
647
            'list_instructor_tasks', kwargs={'course_id': unicode(course_key)}
648 649
        ),
        'email_background_tasks_url': reverse(
650
            'list_background_email_tasks', kwargs={'course_id': unicode(course_key)}
651
        ),
652
        'email_content_history_url': reverse(
653
            'list_email_content', kwargs={'course_id': unicode(course_key)}
654
        ),
655 656 657
    }
    return section_data

658

659
def _get_dashboard_link(course_key):
660
    """ Construct a URL to the external analytics dashboard """
661
    analytics_dashboard_url = '{0}/courses/{1}'.format(settings.ANALYTICS_DASHBOARD_URL, unicode(course_key))
cahrens committed
662 663 664
    link = HTML(u"<a href=\"{0}\" target=\"_blank\">{1}</a>").format(
        analytics_dashboard_url, settings.ANALYTICS_DASHBOARD_NAME
    )
665 666 667
    return link


668
def _section_analytics(course, access):
669 670
    """ Provide data for the corresponding dashboard section """
    section_data = {
671
        'section_key': 'instructor_analytics',
Miles Steele committed
672
        'section_display_name': _('Analytics'),
673
        'access': access,
674
        'course_id': unicode(course.id),
675 676
    }
    return section_data
677 678


679
def _section_metrics(course, access):
680
    """Provide data for the corresponding dashboard section """
681
    course_key = course.id
682 683
    section_data = {
        'section_key': 'metrics',
684
        'section_display_name': _('Metrics'),
685
        'access': access,
686
        'course_id': unicode(course_key),
687 688
        'sub_section_display_name': get_section_display_name(course_key),
        'section_has_problem': get_array_section_has_problem(course_key),
689 690
        'get_students_opened_subsection_url': reverse('get_students_opened_subsection'),
        'get_students_problem_grades_url': reverse('get_students_problem_grades'),
691
        'post_metrics_data_csv_url': reverse('post_metrics_data_csv'),
692 693
    }
    return section_data