tasks.py 12.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 django.conf import settings
26
from django.utils.translation import ugettext_noop
27

28
from celery import task
29
from bulk_email.tasks import perform_delegate_email_batches
30 31 32 33 34 35 36
from instructor_task.tasks_helper import (
    run_main_task,
    BaseInstructorTask,
    perform_module_state_update,
    rescore_problem_module_state,
    reset_attempts_module_state,
    delete_problem_module_state,
37
    upload_problem_responses_csv,
38
    upload_grades_csv,
39
    upload_problem_grade_report,
40
    upload_students_csv,
41
    cohort_students_and_upload,
42 43
    upload_enrollment_report,
    upload_may_enroll_csv,
44
    upload_exec_summary_report,
45
    upload_course_survey_report,
46
    generate_students_certificates,
47
    upload_proctored_exam_results_report
48
)
49 50 51


TASK_LOG = logging.getLogger('edx.celery.task')
52 53


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

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
59 60 61 62
    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:
63 64

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

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

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

    def filter_fcn(modules_to_update):
78
        """Filter that matches problems which are marked as being done"""
79 80
        return modules_to_update.filter(state__contains='"done": true')

81 82
    visit_fcn = partial(perform_module_state_update, update_fcn, filter_fcn)
    return run_main_task(entry_id, visit_fcn, action_name)
83 84


85
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
86
def reset_problem_attempts(entry_id, xmodule_instance_args):
87
    """Resets problem attempts to zero for a particular problem for all students in a course.
88 89

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
90 91 92 93
    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:
94 95 96 97 98 99

      '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.
    """
100 101
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('reset')
102 103 104
    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)
105 106


107
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
108
def delete_problem_state(entry_id, xmodule_instance_args):
109
    """Deletes problem state entirely for all students on a particular problem in a course.
110 111

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
112 113 114 115
    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:
116 117 118 119 120 121

      '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.
    """
122 123
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('deleted')
124 125 126 127 128
    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)


129
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
130 131
def send_bulk_course_email(entry_id, _xmodule_instance_args):
    """Sends emails to recipients enrolled in a course.
132 133 134 135 136

    `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.

137
    The task_input should be a dict with the following entries:
138

139 140 141 142
      '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.
143
    """
144 145
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('emailed')
146
    visit_fcn = perform_delegate_email_batches
147
    return run_main_task(entry_id, visit_fcn, action_name)
148 149


150
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
151 152 153 154 155 156 157 158 159 160 161
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')
    task_fn = partial(upload_problem_responses_csv, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


162
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
163 164 165 166
def calculate_grades_csv(entry_id, xmodule_instance_args):
    """
    Grade a course and push the results to an S3 bucket for download.
    """
167
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
168
    action_name = ugettext_noop('graded')
169 170 171 172 173
    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
    )

174
    task_fn = partial(upload_grades_csv, xmodule_instance_args)
175
    return run_main_task(entry_id, task_fn, action_name)
176 177


178
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
179 180
def calculate_problem_grade_report(entry_id, xmodule_instance_args):
    """
Daniel Friedman committed
181
    Generate a CSV for a course containing all students' problem
182 183
    grades and push the results to an S3 bucket for download.
    """
184 185
    # Translators: This is a past-tense phrase that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('problem distribution graded')
186 187 188 189 190 191 192 193 194
    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(upload_problem_grade_report, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


195
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
196 197 198 199 200 201 202
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')
203
    task_fn = partial(upload_students_csv, xmodule_instance_args)
204
    return run_main_task(entry_id, task_fn, action_name)
205 206


207
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
208 209 210 211 212 213 214 215 216 217 218
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)


219
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
Afzal Wali committed
220 221 222 223 224 225 226 227 228 229 230
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)


231
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
232 233 234 235 236 237 238 239 240 241 242
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)


243
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
244 245 246 247 248 249 250 251 252 253
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)


254
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
255 256 257 258 259 260 261 262 263 264 265 266
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)


267
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
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)


283
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
284 285 286 287 288 289 290 291 292
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)