tasks.py 13.2 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
from lms.djangoapps.instructor_task.tasks_helper import (
31 32 33 34 35 36
    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 48
    upload_proctored_exam_results_report,
    upload_ora2_data,
49
)
50 51 52


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


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

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

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

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

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

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

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


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

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

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


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

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

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


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

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

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

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


151
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
152 153 154 155 156 157 158 159 160 161 162
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)


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

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


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


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


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


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


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


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


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


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


284
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
285 286 287 288 289 290 291 292 293
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)
294 295 296 297 298 299 300 301 302 303


@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
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)