tasks.py 13.8 KB
Newer Older
1
"""
2
This file contains tasks that are designed to perform background operations on the
3 4
running state of a course.

Brian Wilson committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
At present, these tasks all operate on StudentModule objects in one way or another,
so they share a visitor architecture.  Each task defines an "update function" that
takes a module_descriptor, a particular StudentModule object, and xmodule_instance_args.

A task may optionally specify a "filter function" that takes a query for StudentModule
objects, and adds additional filter clauses.

A task also passes through "xmodule_instance_args", that are used to provide
information to our code that instantiates xmodule instances.

The task definition then calls the traversal function, passing in the three arguments
above, along with the id value for an InstructorTask object.  The InstructorTask
object contains a 'task_input' row which is a JSON-encoded dict containing
a problem URL and optionally a student.  These are used to set up the initial value
of the query for traversing StudentModule objects.

21
"""
22 23 24
import logging
from functools import partial

25
from celery import task
26
from django.conf import settings
27
from django.utils.translation import ugettext_noop
28 29

from bulk_email.tasks import perform_delegate_email_batches
30
from lms.djangoapps.instructor_task.tasks_base import BaseInstructorTask
31
from lms.djangoapps.instructor_task.tasks_helper.certs import generate_students_certificates
32
from lms.djangoapps.instructor_task.tasks_helper.enrollments import (
33
    upload_enrollment_report,
34
    upload_exec_summary_report,
35 36
    upload_may_enroll_csv,
    upload_students_csv
37
)
38
from lms.djangoapps.instructor_task.tasks_helper.grades import CourseGradeReport, ProblemGradeReport, ProblemResponses
39 40
from lms.djangoapps.instructor_task.tasks_helper.misc import (
    cohort_students_and_upload,
41
    upload_course_survey_report,
42
    upload_ora2_data,
43
    upload_proctored_exam_results_report
44
)
45
from lms.djangoapps.instructor_task.tasks_helper.module_state import (
46
    delete_problem_module_state,
47
    perform_module_state_update,
48
    override_score_module_state,
49
    rescore_problem_module_state,
50
    reset_attempts_module_state
51
)
52
from lms.djangoapps.instructor_task.tasks_helper.runner import run_main_task
53 54

TASK_LOG = logging.getLogger('edx.celery.task')
55 56


57
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
58
def rescore_problem(entry_id, xmodule_instance_args):
59
    """Rescores a problem in a course, for all students or one specific student.
60 61

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
62 63 64 65
    The entry contains the `course_id` that identifies the course, as well as the
    `task_input`, which contains task-specific input.

    The task_input should be a dict with the following entries:
66 67

      'problem_url': the full URL to the problem to be rescored.  (required)
68

69 70
      'student': the identifier (username or email) of a particular user whose
          problem submission should be rescored.  If not specified, all problem
71
          submissions for the problem will be rescored.
72 73 74 75

    `xmodule_instance_args` provides information needed by _get_module_instance_for_task()
    to instantiate an xmodule instance.
    """
76 77
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('rescored')
78
    update_fcn = partial(rescore_problem_module_state, xmodule_instance_args)
79

80
    visit_fcn = partial(perform_module_state_update, update_fcn, None)
81
    return run_main_task(entry_id, visit_fcn, action_name)
82 83


84
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
85 86 87 88 89 90 91 92 93 94 95 96 97
def override_problem_score(entry_id, xmodule_instance_args):
    """
    Overrides a specific learner's score on a problem.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('overridden')
    update_fcn = partial(override_score_module_state, xmodule_instance_args)

    visit_fcn = partial(perform_module_state_update, update_fcn, None)
    return run_main_task(entry_id, visit_fcn, action_name)


@task(base=BaseInstructorTask)  # pylint: disable=not-callable
98
def reset_problem_attempts(entry_id, xmodule_instance_args):
99
    """Resets problem attempts to zero for a particular problem for all students in a course.
100 101

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
102 103 104 105
    The entry contains the `course_id` that identifies the course, as well as the
    `task_input`, which contains task-specific input.

    The task_input should be a dict with the following entries:
106 107 108 109 110 111

      'problem_url': the full URL to the problem to be rescored.  (required)

    `xmodule_instance_args` provides information needed by _get_module_instance_for_task()
    to instantiate an xmodule instance.
    """
112 113
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('reset')
114 115 116
    update_fcn = partial(reset_attempts_module_state, xmodule_instance_args)
    visit_fcn = partial(perform_module_state_update, update_fcn, None)
    return run_main_task(entry_id, visit_fcn, action_name)
117 118


119
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
120
def delete_problem_state(entry_id, xmodule_instance_args):
121
    """Deletes problem state entirely for all students on a particular problem in a course.
122 123

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
124 125 126 127
    The entry contains the `course_id` that identifies the course, as well as the
    `task_input`, which contains task-specific input.

    The task_input should be a dict with the following entries:
128 129 130 131 132 133

      'problem_url': the full URL to the problem to be rescored.  (required)

    `xmodule_instance_args` provides information needed by _get_module_instance_for_task()
    to instantiate an xmodule instance.
    """
134 135
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('deleted')
136 137 138 139 140
    update_fcn = partial(delete_problem_module_state, xmodule_instance_args)
    visit_fcn = partial(perform_module_state_update, update_fcn, None)
    return run_main_task(entry_id, visit_fcn, action_name)


141
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
142 143
def send_bulk_course_email(entry_id, _xmodule_instance_args):
    """Sends emails to recipients enrolled in a course.
144 145 146 147 148

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
    The entry contains the `course_id` that identifies the course, as well as the
    `task_input`, which contains task-specific input.

149
    The task_input should be a dict with the following entries:
150

151 152 153 154
      'email_id': the full URL to the problem to be rescored.  (required)

    `_xmodule_instance_args` provides information needed by _get_module_instance_for_task()
    to instantiate an xmodule instance.  This is unused here.
155
    """
156 157
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('emailed')
158
    visit_fcn = perform_delegate_email_batches
159
    return run_main_task(entry_id, visit_fcn, action_name)
160 161


162
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
163 164 165 166 167 168 169
def calculate_problem_responses_csv(entry_id, xmodule_instance_args):
    """
    Compute student answers to a given problem and upload the CSV to
    an S3 bucket for download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('generated')
170
    task_fn = partial(ProblemResponses.generate, xmodule_instance_args)
171 172 173
    return run_main_task(entry_id, task_fn, action_name)


174
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
175 176 177 178
def calculate_grades_csv(entry_id, xmodule_instance_args):
    """
    Grade a course and push the results to an S3 bucket for download.
    """
179
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
180
    action_name = ugettext_noop('graded')
181 182 183 184 185
    TASK_LOG.info(
        u"Task: %s, InstructorTask ID: %s, Task type: %s, Preparing for task execution",
        xmodule_instance_args.get('task_id'), entry_id, action_name
    )

186
    task_fn = partial(CourseGradeReport.generate, xmodule_instance_args)
187
    return run_main_task(entry_id, task_fn, action_name)
188 189


190
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
191 192
def calculate_problem_grade_report(entry_id, xmodule_instance_args):
    """
Daniel Friedman committed
193
    Generate a CSV for a course containing all students' problem
194 195
    grades and push the results to an S3 bucket for download.
    """
196 197
    # Translators: This is a past-tense phrase that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('problem distribution graded')
198 199 200 201 202
    TASK_LOG.info(
        u"Task: %s, InstructorTask ID: %s, Task type: %s, Preparing for task execution",
        xmodule_instance_args.get('task_id'), entry_id, action_name
    )

203
    task_fn = partial(ProblemGradeReport.generate, xmodule_instance_args)
204 205 206
    return run_main_task(entry_id, task_fn, action_name)


207
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
208 209 210 211 212 213 214
def calculate_students_features_csv(entry_id, xmodule_instance_args):
    """
    Compute student profile information for a course and upload the
    CSV to an S3 bucket for download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('generated')
215
    task_fn = partial(upload_students_csv, xmodule_instance_args)
216
    return run_main_task(entry_id, task_fn, action_name)
217 218


219
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
220 221 222 223 224 225 226 227 228 229 230
def enrollment_report_features_csv(entry_id, xmodule_instance_args):
    """
    Compute student profile information for a course and upload the
    CSV to an S3 bucket for download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('generating_enrollment_report')
    task_fn = partial(upload_enrollment_report, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


231
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
Afzal Wali committed
232 233 234 235 236 237 238 239 240 241 242
def exec_summary_report_csv(entry_id, xmodule_instance_args):
    """
    Compute executive summary report for a course and upload the
    Html generated report to an S3 bucket for download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = 'generating_exec_summary_report'
    task_fn = partial(upload_exec_summary_report, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


243
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
244 245 246 247 248 249 250 251 252 253 254
def course_survey_report_csv(entry_id, xmodule_instance_args):
    """
    Compute the survey report for a course and upload the
    generated report to an S3 bucket for download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('generated')
    task_fn = partial(upload_course_survey_report, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


255
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
256 257 258 259 260 261 262 263 264 265
def proctored_exam_results_csv(entry_id, xmodule_instance_args):
    """
    Compute proctored exam results report for a course and upload the
    CSV for download.
    """
    action_name = 'generating_proctored_exam_results_report'
    task_fn = partial(upload_proctored_exam_results_report, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


266
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
267 268 269 270 271 272 273 274 275 276 277 278
def calculate_may_enroll_csv(entry_id, xmodule_instance_args):
    """
    Compute information about invited students who have not enrolled
    in a given course yet and upload the CSV to an S3 bucket for
    download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('generated')
    task_fn = partial(upload_may_enroll_csv, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


279
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
def generate_certificates(entry_id, xmodule_instance_args):
    """
    Grade students and generate certificates.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('certificates generated')
    TASK_LOG.info(
        u"Task: %s, InstructorTask ID: %s, Task type: %s, Preparing for task execution",
        xmodule_instance_args.get('task_id'), entry_id, action_name
    )

    task_fn = partial(generate_students_certificates, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


295
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
296 297 298 299 300 301 302 303 304
def cohort_students(entry_id, xmodule_instance_args):
    """
    Cohort students in bulk, and upload the results.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    # An example of such a message is: "Progress: {action} {succeeded} of {attempted} so far"
    action_name = ugettext_noop('cohorted')
    task_fn = partial(cohort_students_and_upload, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)
305 306


307
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
308 309 310 311 312 313 314
def export_ora2_data(entry_id, xmodule_instance_args):
    """
    Generate a CSV of ora2 responses and push it to S3.
    """
    action_name = ugettext_noop('generated')
    task_fn = partial(upload_ora2_data, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)