def undo_override_subsection_grade(self, user_id, course_key_or_id, usage_key_or_id): """ Delete the override subsection grade row (the PersistentSubsectionGrade model must already exist) Fires off a recalculate_subsection_grade async task to update the PersistentSubsectionGrade table. If the override does not exist, no error is raised, it just triggers the recalculation. """ # prevent circular imports: from .signals.handlers import SUBSECTION_OVERRIDE_EVENT_TYPE course_key = _get_key(course_key_or_id, CourseKey) usage_key = _get_key(usage_key_or_id, UsageKey) override = self.get_subsection_grade_override(user_id, course_key, usage_key) # Older rejected exam attempts that transition to verified might not have an override created if override is not None: override.delete() # Cache a new event id and event type which the signal handler will use to emit a tracking log event. create_new_event_transaction_id() set_event_transaction_type(SUBSECTION_OVERRIDE_EVENT_TYPE) # Signal will trigger subsection recalculation which will call PersistentSubsectionGrade.update_or_create_grade # which will no longer use the above deleted override, and instead return the grade to the original score from # the actual problem responses before writing to the table. SUBSECTION_OVERRIDE_CHANGED.send( sender=None, user_id=user_id, course_id=unicode(course_key), usage_id=unicode(usage_key), only_if_higher=False, modified=datetime.now().replace(tzinfo=pytz.UTC), # Not used when score_deleted=True score_deleted=True, score_db_table=ScoreDatabaseTableEnum.overrides )
def compute_grades_for_course_v2(self, **kwargs): """ Compute grades for a set of students in the specified course. The set of students will be determined by the order of enrollment date, and limited to at most <batch_size> students, starting from the specified offset. TODO: Roll this back into compute_grades_for_course once all workers have the version with **kwargs. Sets the ESTIMATE_FIRST_ATTEMPTED flag, then calls the original task as a synchronous function. estimate_first_attempted: controls whether to unconditionally set the ESTIMATE_FIRST_ATTEMPTED waffle switch. If false or not provided, use the global value of the ESTIMATE_FIRST_ATTEMPTED waffle switch. """ if 'event_transaction_id' in kwargs: set_event_transaction_id(kwargs['event_transaction_id']) if 'event_transaction_type' in kwargs: set_event_transaction_type(kwargs['event_transaction_type']) if kwargs.get('estimate_first_attempted'): waffle().override_for_request(ESTIMATE_FIRST_ATTEMPTED, True) try: return compute_grades_for_course(kwargs['course_key'], kwargs['offset'], kwargs['batch_size']) except Exception as exc: # pylint: disable=broad-except raise self.retry(kwargs=kwargs, exc=exc)
def undo_override_subsection_grade(self, user_id, course_key_or_id, usage_key_or_id): """ Delete the override subsection grade row (the PersistentSubsectionGrade model must already exist) Fires off a recalculate_subsection_grade async task to update the PersistentSubsectionGrade table. If the override does not exist, no error is raised, it just triggers the recalculation. """ course_key = _get_key(course_key_or_id, CourseKey) usage_key = _get_key(usage_key_or_id, UsageKey) override = self.get_subsection_grade_override(user_id, course_key, usage_key) # Older rejected exam attempts that transition to verified might not have an override created if override is not None: override.delete() # Cache a new event id and event type which the signal handler will use to emit a tracking log event. create_new_event_transaction_id() set_event_transaction_type(SUBSECTION_OVERRIDE_EVENT_TYPE) # Signal will trigger subsection recalculation which will call PersistentSubsectionGrade.update_or_create_grade # which will no longer use the above deleted override, and instead return the grade to the original score from # the actual problem responses before writing to the table. SUBSECTION_OVERRIDE_CHANGED.send( sender=None, user_id=user_id, course_id=unicode(course_key), usage_id=unicode(usage_key), only_if_higher=False, modified=datetime.now().replace( tzinfo=pytz.UTC), # Not used when score_deleted=True score_deleted=True, score_db_table=ScoreDatabaseTableEnum.overrides)
def _emit_event(kwargs): """ Emits a problem submitted event only if there is no current event transaction type, i.e. we have not reached this point in the code via a rescore or student state deletion. If the event transaction type has already been set and the transacation is a rescore, emits a problem rescored event. """ root_type = get_event_transaction_type() if not root_type: root_id = get_event_transaction_id() if not root_id: root_id = create_new_event_transaction_id() set_event_transaction_type(PROBLEM_SUBMITTED_EVENT_TYPE) tracker.emit( unicode(PROBLEM_SUBMITTED_EVENT_TYPE), { 'user_id': unicode(kwargs['user_id']), 'course_id': unicode(kwargs['course_id']), 'problem_id': unicode(kwargs['usage_id']), 'event_transaction_id': unicode(root_id), 'event_transaction_type': unicode(PROBLEM_SUBMITTED_EVENT_TYPE), 'weighted_earned': kwargs.get('weighted_earned'), 'weighted_possible': kwargs.get('weighted_possible'), } ) if root_type in [GRADES_RESCORE_EVENT_TYPE, GRADES_OVERRIDE_EVENT_TYPE]: current_user = get_current_user() instructor_id = getattr(current_user, 'id', None) tracker.emit( unicode(GRADES_RESCORE_EVENT_TYPE), { 'course_id': unicode(kwargs['course_id']), 'user_id': unicode(kwargs['user_id']), 'problem_id': unicode(kwargs['usage_id']), 'new_weighted_earned': kwargs.get('weighted_earned'), 'new_weighted_possible': kwargs.get('weighted_possible'), 'only_if_higher': kwargs.get('only_if_higher'), 'instructor_id': unicode(instructor_id), 'event_transaction_id': unicode(get_event_transaction_id()), 'event_transaction_type': unicode(root_type), } ) if root_type in [SUBSECTION_OVERRIDE_EVENT_TYPE]: tracker.emit( unicode(SUBSECTION_OVERRIDE_EVENT_TYPE), { 'course_id': unicode(kwargs['course_id']), 'user_id': unicode(kwargs['user_id']), 'problem_id': unicode(kwargs['usage_id']), 'only_if_higher': kwargs.get('only_if_higher'), 'override_deleted': kwargs.get('score_deleted', False), 'event_transaction_id': unicode(get_event_transaction_id()), 'event_transaction_type': unicode(root_type), } )
def _create_override(self, request_user, subsection_grade_model, **override_data): """ Helper method to create a `PersistentSubsectionGradeOverride` object and send a `SUBSECTION_OVERRIDE_CHANGED` signal. """ override = PersistentSubsectionGradeOverride.update_or_create_override( requesting_user=request_user, subsection_grade_model=subsection_grade_model, feature=PersistentSubsectionGradeOverrideHistory.GRADEBOOK, **override_data) set_event_transaction_type(SUBSECTION_GRADE_CALCULATED) create_new_event_transaction_id() recalculate_subsection_grade_v3.apply(kwargs=dict( user_id=subsection_grade_model.user_id, anonymous_user_id=None, course_id=text_type(subsection_grade_model.course_id), usage_id=text_type(subsection_grade_model.usage_key), only_if_higher=False, expected_modified_time=to_timestamp(override.modified), score_deleted=False, event_transaction_id=unicode(get_event_transaction_id()), event_transaction_type=unicode(get_event_transaction_type()), score_db_table=ScoreDatabaseTableEnum.overrides, force_update_subsections=True, )) # Emit events to let our tracking system to know we updated subsection grade subsection_grade_calculated(subsection_grade_model) return override
def recalculate_subsection_grade_v2(**kwargs): """ Updates a saved subsection grade. Arguments: user_id (int): id of applicable User object course_id (string): identifying the course usage_id (string): identifying the course block only_if_higher (boolean): indicating whether grades should be updated only if the new raw_earned is higher than the previous value. expected_modified_time (serialized timestamp): indicates when the task was queued so that we can verify the underlying data update. score_deleted (boolean): indicating whether the grade change is a result of the problem's score being deleted. event_transaction_id(string): uuid identifying the current event transaction. event_transaction_type(string): human-readable type of the event at the root of the current event transaction. """ try: course_key = CourseLocator.from_string(kwargs['course_id']) if not PersistentGradesEnabledFlag.feature_enabled(course_key): return score_deleted = kwargs['score_deleted'] scored_block_usage_key = UsageKey.from_string(kwargs['usage_id']).replace(course_key=course_key) expected_modified_time = from_timestamp(kwargs['expected_modified_time']) # The request cache is not maintained on celery workers, # where this code runs. So we take the values from the # main request cache and store them in the local request # cache. This correlates model-level grading events with # higher-level ones. set_event_transaction_id(kwargs.pop('event_transaction_id', None)) set_event_transaction_type(kwargs.pop('event_transaction_type', None)) # Verify the database has been updated with the scores when the task was # created. This race condition occurs if the transaction in the task # creator's process hasn't committed before the task initiates in the worker # process. if not _has_database_updated_with_new_score( kwargs['user_id'], scored_block_usage_key, expected_modified_time, score_deleted, ): raise _retry_recalculate_subsection_grade(**kwargs) _update_subsection_grades( course_key, scored_block_usage_key, kwargs['only_if_higher'], kwargs['user_id'], ) except Exception as exc: # pylint: disable=broad-except if not isinstance(exc, KNOWN_RETRY_ERRORS): log.info("tnl-6244 grades unexpected failure: {}. kwargs={}".format( repr(exc), kwargs )) raise _retry_recalculate_subsection_grade(exc=exc, **kwargs)
def _reset_or_delete_module(studentmodule): if delete_module: studentmodule.delete() create_new_event_transaction_id() set_event_transaction_type(grades_events.STATE_DELETED_EVENT_TYPE) tracker.emit( six.text_type(grades_events.STATE_DELETED_EVENT_TYPE), { 'user_id': six.text_type(student.id), 'course_id': six.text_type(course_id), 'problem_id': six.text_type(module_state_key), 'instructor_id': six.text_type(requesting_user.id), 'event_transaction_id': six.text_type(get_event_transaction_id()), 'event_transaction_type': six.text_type(grades_events.STATE_DELETED_EVENT_TYPE), }) if not submission_cleared: _fire_score_changed_for_block( course_id, student, block, module_state_key, ) else: _reset_module_attempts(studentmodule)
def _emit_problem_submitted_event(kwargs): """ Emits a problem submitted event only if there is no current event transaction type, i.e. we have not reached this point in the code via a rescore or student state deletion. """ root_type = get_event_transaction_type() if not root_type: root_id = get_event_transaction_id() if not root_id: root_id = create_new_event_transaction_id() set_event_transaction_type(PROBLEM_SUBMITTED_EVENT_TYPE) tracker.emit( unicode(PROBLEM_SUBMITTED_EVENT_TYPE), { 'user_id': unicode(kwargs['user_id']), 'course_id': unicode(kwargs['course_id']), 'problem_id': unicode(kwargs['usage_id']), 'event_transaction_id': unicode(root_id), 'event_transaction_type': unicode(PROBLEM_SUBMITTED_EVENT_TYPE), 'weighted_earned': kwargs.get('weighted_earned'), 'weighted_possible': kwargs.get('weighted_possible'), } )
def grade_updated(**kwargs): """ Emits the appropriate grade-related event after checking for which event-transaction is active. Emits a problem.submitted event only if there is no current event transaction type, i.e. we have not reached this point in the code via an outer event type (such as problem.rescored or score_overridden). """ root_type = get_event_transaction_type() if not root_type: root_id = get_event_transaction_id() if not root_id: root_id = create_new_event_transaction_id() set_event_transaction_type(PROBLEM_SUBMITTED_EVENT_TYPE) tracker.emit( unicode(PROBLEM_SUBMITTED_EVENT_TYPE), { 'user_id': unicode(kwargs['user_id']), 'course_id': unicode(kwargs['course_id']), 'problem_id': unicode(kwargs['usage_id']), 'event_transaction_id': unicode(root_id), 'event_transaction_type': unicode(PROBLEM_SUBMITTED_EVENT_TYPE), 'weighted_earned': kwargs.get('weighted_earned'), 'weighted_possible': kwargs.get('weighted_possible'), } ) elif root_type in [GRADES_RESCORE_EVENT_TYPE, GRADES_OVERRIDE_EVENT_TYPE]: current_user = get_current_user() instructor_id = getattr(current_user, 'id', None) tracker.emit( unicode(root_type), { 'course_id': unicode(kwargs['course_id']), 'user_id': unicode(kwargs['user_id']), 'problem_id': unicode(kwargs['usage_id']), 'new_weighted_earned': kwargs.get('weighted_earned'), 'new_weighted_possible': kwargs.get('weighted_possible'), 'only_if_higher': kwargs.get('only_if_higher'), 'instructor_id': unicode(instructor_id), 'event_transaction_id': unicode(get_event_transaction_id()), 'event_transaction_type': unicode(root_type), } ) elif root_type in [SUBSECTION_OVERRIDE_EVENT_TYPE]: tracker.emit( unicode(root_type), { 'course_id': unicode(kwargs['course_id']), 'user_id': unicode(kwargs['user_id']), 'problem_id': unicode(kwargs['usage_id']), 'only_if_higher': kwargs.get('only_if_higher'), 'override_deleted': kwargs.get('score_deleted', False), 'event_transaction_id': unicode(get_event_transaction_id()), 'event_transaction_type': unicode(root_type), } )
def grade_updated(**kwargs): """ Emits the appropriate grade-related event after checking for which event-transaction is active. Emits a problem.submitted event only if there is no current event transaction type, i.e. we have not reached this point in the code via an outer event type (such as problem.rescored or score_overridden). """ root_type = get_event_transaction_type() if not root_type: root_id = get_event_transaction_id() if not root_id: root_id = create_new_event_transaction_id() set_event_transaction_type(PROBLEM_SUBMITTED_EVENT_TYPE) tracker.emit( six.text_type(PROBLEM_SUBMITTED_EVENT_TYPE), { 'user_id': six.text_type(kwargs['user_id']), 'course_id': six.text_type(kwargs['course_id']), 'problem_id': six.text_type(kwargs['usage_id']), 'event_transaction_id': six.text_type(root_id), 'event_transaction_type': six.text_type(PROBLEM_SUBMITTED_EVENT_TYPE), 'weighted_earned': kwargs.get('weighted_earned'), 'weighted_possible': kwargs.get('weighted_possible'), } ) elif root_type in [GRADES_RESCORE_EVENT_TYPE, GRADES_OVERRIDE_EVENT_TYPE]: current_user = get_current_user() instructor_id = getattr(current_user, 'id', None) tracker.emit( six.text_type(root_type), { 'course_id': six.text_type(kwargs['course_id']), 'user_id': six.text_type(kwargs['user_id']), 'problem_id': six.text_type(kwargs['usage_id']), 'new_weighted_earned': kwargs.get('weighted_earned'), 'new_weighted_possible': kwargs.get('weighted_possible'), 'only_if_higher': kwargs.get('only_if_higher'), 'instructor_id': six.text_type(instructor_id), 'event_transaction_id': six.text_type(get_event_transaction_id()), 'event_transaction_type': six.text_type(root_type), } ) elif root_type in [SUBSECTION_OVERRIDE_EVENT_TYPE]: tracker.emit( six.text_type(root_type), { 'course_id': six.text_type(kwargs['course_id']), 'user_id': six.text_type(kwargs['user_id']), 'problem_id': six.text_type(kwargs['usage_id']), 'only_if_higher': kwargs.get('only_if_higher'), 'override_deleted': kwargs.get('score_deleted', False), 'event_transaction_id': six.text_type(get_event_transaction_id()), 'event_transaction_type': six.text_type(root_type), } )
def _create_override(self, request_user, subsection_grade_model, **override_data): """ Helper method to create a `PersistentSubsectionGradeOverride` object and send a `SUBSECTION_OVERRIDE_CHANGED` signal. """ override = PersistentSubsectionGradeOverride.update_or_create_override( requesting_user=request_user, subsection_grade_model=subsection_grade_model, feature=grades_constants.GradeOverrideFeatureEnum.gradebook, **override_data ) set_event_transaction_type(grades_events.SUBSECTION_GRADE_CALCULATED) create_new_event_transaction_id() recalculate_subsection_grade_v3.apply( kwargs=dict( user_id=subsection_grade_model.user_id, anonymous_user_id=None, course_id=text_type(subsection_grade_model.course_id), usage_id=text_type(subsection_grade_model.usage_key), only_if_higher=False, expected_modified_time=to_timestamp(override.modified), score_deleted=False, event_transaction_id=six.text_type(get_event_transaction_id()), event_transaction_type=six.text_type(get_event_transaction_type()), score_db_table=grades_constants.ScoreDatabaseTableEnum.overrides, force_update_subsections=True, ) ) # Emit events to let our tracking system to know we updated subsection grade grades_events.subsection_grade_calculated(subsection_grade_model) return override
def override_subsection_grade( user_id, course_key_or_id, usage_key_or_id, overrider=None, earned_all=None, earned_graded=None, feature=constants.GradeOverrideFeatureEnum.proctoring, comment=None, ): """ Creates a PersistentSubsectionGradeOverride corresponding to the given user, course, and usage_key. Will also create a ``PersistentSubsectionGrade`` for this (user, course, usage_key) if none currently exists. Fires off a recalculate_subsection_grade async task to update the PersistentCourseGrade table. Will not override ``earned_all`` or ``earned_graded`` value if they are ``None``. Both of these parameters have ``None`` as their default value. """ course_key = _get_key(course_key_or_id, CourseKey) usage_key = _get_key(usage_key_or_id, UsageKey) try: grade = get_subsection_grade(user_id, usage_key.course_key, usage_key) except ObjectDoesNotExist: grade = _create_subsection_grade(user_id, course_key, usage_key) override = update_or_create_override( grade, requesting_user=overrider, subsection_grade_model=grade, feature=feature, system=feature, earned_all_override=earned_all, earned_graded_override=earned_graded, comment=comment, ) # Cache a new event id and event type which the signal handler will use to emit a tracking log event. create_new_event_transaction_id() set_event_transaction_type(events.SUBSECTION_OVERRIDE_EVENT_TYPE) # This will eventually trigger a re-computation of the course grade, # taking the new PersistentSubsectionGradeOverride into account. signals.SUBSECTION_OVERRIDE_CHANGED.send( sender=None, user_id=user_id, course_id=text_type(course_key), usage_id=text_type(usage_key), only_if_higher=False, modified=override.modified, score_deleted=False, score_db_table=constants.ScoreDatabaseTableEnum.overrides)
def handle(self, *args, **options): if 'modified_start' not in options: raise CommandError('modified_start must be provided.') if 'modified_end' not in options: raise CommandError('modified_end must be provided.') modified_start = utc.localize( datetime.strptime(options['modified_start'], DATE_FORMAT)) modified_end = utc.localize( datetime.strptime(options['modified_end'], DATE_FORMAT)) event_transaction_id = create_new_event_transaction_id() set_event_transaction_type(PROBLEM_SUBMITTED_EVENT_TYPE) kwargs = { 'modified__range': (modified_start, modified_end), 'module_type': 'problem' } for record in StudentModule.objects.filter(**kwargs): if not record.course_id.is_course: # This is not a course, so we don't store subsection grades for it. continue task_args = { "user_id": record.student_id, "course_id": six.text_type(record.course_id), "usage_id": six.text_type(record.module_state_key), "only_if_higher": False, "expected_modified_time": to_timestamp(record.modified), "score_deleted": False, "event_transaction_id": six.text_type(event_transaction_id), "event_transaction_type": PROBLEM_SUBMITTED_EVENT_TYPE, "score_db_table": ScoreDatabaseTableEnum.courseware_student_module, } recalculate_subsection_grade_v3.apply_async(kwargs=task_args) kwargs = {'created_at__range': (modified_start, modified_end)} for record in Submission.objects.filter(**kwargs): if not record.student_item.course_id.is_course: # This is not a course, so ignore it continue task_args = { "user_id": user_by_anonymous_id(record.student_item.student_id).id, "anonymous_user_id": record.student_item.student_id, "course_id": six.text_type(record.student_item.course_id), "usage_id": six.text_type(record.student_item.item_id), "only_if_higher": False, "expected_modified_time": to_timestamp(record.created_at), "score_deleted": False, "event_transaction_id": six.text_type(event_transaction_id), "event_transaction_type": PROBLEM_SUBMITTED_EVENT_TYPE, "score_db_table": ScoreDatabaseTableEnum.submissions, } recalculate_subsection_grade_v3.apply_async(kwargs=task_args)
def override_subsection_grade( self, user_id, course_key_or_id, usage_key_or_id, earned_all=None, earned_graded=None ): """ Creates a PersistentSubsectionGradeOverride corresponding to the given user, course, and usage_key. Will also create a ``PersistentSubsectionGrade`` for this (user, course, usage_key) if none currently exists. Fires off a recalculate_subsection_grade async task to update the PersistentCourseGrade table. Will not override ``earned_all`` or ``earned_graded`` value if they are ``None``. Both of these parameters have ``None`` as their default value. """ course_key = _get_key(course_key_or_id, CourseKey) usage_key = _get_key(usage_key_or_id, UsageKey) try: grade = PersistentSubsectionGrade.read_grade( user_id=user_id, usage_key=usage_key ) except PersistentSubsectionGrade.DoesNotExist: grade = self._create_subsection_grade(user_id, course_key, usage_key) override = PersistentSubsectionGradeOverride.update_or_create_override( requesting_user=None, subsection_grade_model=grade, feature=GradeOverrideFeatureEnum.proctoring, earned_all_override=earned_all, earned_graded_override=earned_graded, ) # Cache a new event id and event type which the signal handler will use to emit a tracking log event. create_new_event_transaction_id() set_event_transaction_type(SUBSECTION_OVERRIDE_EVENT_TYPE) # This will eventually trigger a re-computation of the course grade, # taking the new PersistentSubsectionGradeOverride into account. SUBSECTION_OVERRIDE_CHANGED.send( sender=None, user_id=user_id, course_id=text_type(course_key), usage_id=text_type(usage_key), only_if_higher=False, modified=override.modified, score_deleted=False, score_db_table=ScoreDatabaseTableEnum.overrides )
def _emit_event(kwargs): """ Emits a problem submitted event only if there is no current event transaction type, i.e. we have not reached this point in the code via a rescore or student state deletion. If the event transaction type has already been set and the transacation is a rescore, emits a problem rescored event. """ root_type = get_event_transaction_type() if not root_type: root_id = get_event_transaction_id() if not root_id: root_id = create_new_event_transaction_id() set_event_transaction_type(PROBLEM_SUBMITTED_EVENT_TYPE) tracker.emit( unicode(PROBLEM_SUBMITTED_EVENT_TYPE), { 'user_id': unicode(kwargs['user_id']), 'course_id': unicode(kwargs['course_id']), 'problem_id': unicode(kwargs['usage_id']), 'event_transaction_id': unicode(root_id), 'event_transaction_type': unicode(PROBLEM_SUBMITTED_EVENT_TYPE), 'weighted_earned': kwargs.get('weighted_earned'), 'weighted_possible': kwargs.get('weighted_possible'), } ) if root_type in [GRADES_RESCORE_EVENT_TYPE, GRADES_OVERRIDE_EVENT_TYPE]: current_user = get_current_user() instructor_id = getattr(current_user, 'id', None) tracker.emit( unicode(GRADES_RESCORE_EVENT_TYPE), { 'course_id': unicode(kwargs['course_id']), 'user_id': unicode(kwargs['user_id']), 'problem_id': unicode(kwargs['usage_id']), 'new_weighted_earned': kwargs.get('weighted_earned'), 'new_weighted_possible': kwargs.get('weighted_possible'), 'only_if_higher': kwargs.get('only_if_higher'), 'instructor_id': unicode(instructor_id), 'event_transaction_id': unicode(get_event_transaction_id()), 'event_transaction_type': unicode(root_type), } )
def _emit_event(kwargs): """ Emits a problem submitted event only if there is no current event transaction type, i.e. we have not reached this point in the code via a rescore or student state deletion. If the event transaction type has already been set and the transacation is a rescore, emits a problem rescored event. """ root_type = get_event_transaction_type() if not root_type: root_id = get_event_transaction_id() if not root_id: root_id = create_new_event_transaction_id() set_event_transaction_type(PROBLEM_SUBMITTED_EVENT_TYPE) tracker.emit( unicode(PROBLEM_SUBMITTED_EVENT_TYPE), { 'user_id': unicode(kwargs['user_id']), 'course_id': unicode(kwargs['course_id']), 'problem_id': unicode(kwargs['usage_id']), 'event_transaction_id': unicode(root_id), 'event_transaction_type': unicode(PROBLEM_SUBMITTED_EVENT_TYPE), 'weighted_earned': kwargs.get('weighted_earned'), 'weighted_possible': kwargs.get('weighted_possible'), }) if root_type == 'edx.grades.problem.rescored': current_user = get_current_user() if current_user is not None and hasattr(current_user, 'id'): instructor_id = unicode(current_user.id) else: instructor_id = None tracker.emit( unicode(GRADES_RESCORE_EVENT_TYPE), { 'course_id': unicode(kwargs['course_id']), 'user_id': unicode(kwargs['user_id']), 'problem_id': unicode(kwargs['usage_id']), 'new_weighted_earned': kwargs.get('weighted_earned'), 'new_weighted_possible': kwargs.get('weighted_possible'), 'only_if_higher': kwargs.get('only_if_higher'), 'instructor_id': instructor_id, 'event_transaction_id': unicode(get_event_transaction_id()), 'event_transaction_type': unicode(GRADES_RESCORE_EVENT_TYPE), })
def undo_override_subsection_grade(user_id, course_key_or_id, usage_key_or_id, feature=''): """ Delete the override subsection grade row (the PersistentSubsectionGrade model must already exist) Fires off a recalculate_subsection_grade async task to update the PersistentSubsectionGrade table. If the override does not exist, no error is raised, it just triggers the recalculation. feature: if specified, the deletion will only occur if the override to be deleted was created by the corresponding subsystem """ course_key = _get_key(course_key_or_id, CourseKey) usage_key = _get_key(usage_key_or_id, UsageKey) try: override = get_subsection_grade_override(user_id, course_key, usage_key) except ObjectDoesNotExist: return if override is not None and (not feature or not override.system or feature == override.system): override.delete() else: return # Cache a new event id and event type which the signal handler will use to emit a tracking log event. create_new_event_transaction_id() set_event_transaction_type(events.SUBSECTION_OVERRIDE_EVENT_TYPE) # Signal will trigger subsection recalculation which will call PersistentSubsectionGrade.update_or_create_grade # which will no longer use the above deleted override, and instead return the grade to the original score from # the actual problem responses before writing to the table. signals.SUBSECTION_OVERRIDE_CHANGED.send( sender=None, user_id=user_id, course_id=text_type(course_key), usage_id=text_type(usage_key), only_if_higher=False, modified=datetime.now().replace( tzinfo=pytz.UTC), # Not used when score_deleted=True score_deleted=True, score_db_table=constants.ScoreDatabaseTableEnum.overrides)
def override_subsection_grade(self, user_id, course_key_or_id, usage_key_or_id, earned_all=None, earned_graded=None): """ Override subsection grade (the PersistentSubsectionGrade model must already exist) Fires off a recalculate_subsection_grade async task to update the PersistentSubsectionGrade table. Will not override earned_all or earned_graded value if they are None. Both default to None. """ course_key = _get_key(course_key_or_id, CourseKey) usage_key = _get_key(usage_key_or_id, UsageKey) grade = PersistentSubsectionGrade.objects.get( user_id=user_id, course_id=course_key, usage_key=usage_key ) # Create override that will prevent any future updates to grade override, _ = PersistentSubsectionGradeOverride.objects.update_or_create( grade=grade, earned_all_override=earned_all, earned_graded_override=earned_graded ) _ = PersistentSubsectionGradeOverrideHistory.objects.create( override_id=override.id, feature=PersistentSubsectionGradeOverrideHistory.PROCTORING, action=PersistentSubsectionGradeOverrideHistory.CREATE_OR_UPDATE ) # Cache a new event id and event type which the signal handler will use to emit a tracking log event. create_new_event_transaction_id() set_event_transaction_type(SUBSECTION_OVERRIDE_EVENT_TYPE) # Signal will trigger subsection recalculation which will call PersistentSubsectionGrade.update_or_create_grade # which will use the above override to update the grade before writing to the table. SUBSECTION_OVERRIDE_CHANGED.send( sender=None, user_id=user_id, course_id=unicode(course_key), usage_id=unicode(usage_key), only_if_higher=False, modified=override.modified, score_deleted=False, score_db_table=ScoreDatabaseTableEnum.overrides )
def override_subsection_grade(self, user_id, course_key_or_id, usage_key_or_id, earned_all=None, earned_graded=None): """ Override subsection grade (the PersistentSubsectionGrade model must already exist) Fires off a recalculate_subsection_grade async task to update the PersistentSubsectionGrade table. Will not override earned_all or earned_graded value if they are None. Both default to None. """ course_key = _get_key(course_key_or_id, CourseKey) usage_key = _get_key(usage_key_or_id, UsageKey) grade = PersistentSubsectionGrade.objects.get(user_id=user_id, course_id=course_key, usage_key=usage_key) # Create override that will prevent any future updates to grade override, _ = PersistentSubsectionGradeOverride.objects.update_or_create( grade=grade, earned_all_override=earned_all, earned_graded_override=earned_graded) _ = PersistentSubsectionGradeOverrideHistory.objects.create( override_id=override.id, feature=PersistentSubsectionGradeOverrideHistory.PROCTORING, action=PersistentSubsectionGradeOverrideHistory.CREATE_OR_UPDATE) # Cache a new event id and event type which the signal handler will use to emit a tracking log event. create_new_event_transaction_id() set_event_transaction_type(SUBSECTION_OVERRIDE_EVENT_TYPE) # Signal will trigger subsection recalculation which will call PersistentSubsectionGrade.update_or_create_grade # which will use the above override to update the grade before writing to the table. SUBSECTION_OVERRIDE_CHANGED.send( sender=None, user_id=user_id, course_id=unicode(course_key), usage_id=unicode(usage_key), only_if_higher=False, modified=override.modified, score_deleted=False, score_db_table=ScoreDatabaseTableEnum.overrides)
def handle(self, *args, **options): if 'modified_start' not in options: raise CommandError('modified_start must be provided.') if 'modified_end' not in options: raise CommandError('modified_end must be provided.') modified_start = utc.localize(datetime.strptime(options['modified_start'], DATE_FORMAT)) modified_end = utc.localize(datetime.strptime(options['modified_end'], DATE_FORMAT)) event_transaction_id = create_new_event_transaction_id() set_event_transaction_type(PROBLEM_SUBMITTED_EVENT_TYPE) kwargs = {'modified__range': (modified_start, modified_end), 'module_type': 'problem'} for record in StudentModule.objects.filter(**kwargs): task_args = { "user_id": record.student_id, "course_id": unicode(record.course_id), "usage_id": unicode(record.module_state_key), "only_if_higher": False, "expected_modified_time": to_timestamp(record.modified), "score_deleted": False, "event_transaction_id": unicode(event_transaction_id), "event_transaction_type": PROBLEM_SUBMITTED_EVENT_TYPE, "score_db_table": ScoreDatabaseTableEnum.courseware_student_module, } recalculate_subsection_grade_v3.apply_async(kwargs=task_args) kwargs = {'created_at__range': (modified_start, modified_end)} for record in Submission.objects.filter(**kwargs): task_args = { "user_id": user_by_anonymous_id(record.student_item.student_id).id, "anonymous_user_id": record.student_item.student_id, "course_id": unicode(record.student_item.course_id), "usage_id": unicode(record.student_item.item_id), "only_if_higher": False, "expected_modified_time": to_timestamp(record.created_at), "score_deleted": False, "event_transaction_id": unicode(event_transaction_id), "event_transaction_type": PROBLEM_SUBMITTED_EVENT_TYPE, "score_db_table": ScoreDatabaseTableEnum.submissions, } recalculate_subsection_grade_v3.apply_async(kwargs=task_args)
def compute_grades_for_course_v2(self, **kwargs): """ Compute grades for a set of students in the specified course. The set of students will be determined by the order of enrollment date, and limited to at most <batch_size> students, starting from the specified offset. TODO: Roll this back into compute_grades_for_course once all workers have the version with **kwargs. """ if 'event_transaction_id' in kwargs: set_event_transaction_id(kwargs['event_transaction_id']) if 'event_transaction_type' in kwargs: set_event_transaction_type(kwargs['event_transaction_type']) try: return compute_grades_for_course(kwargs['course_key'], kwargs['offset'], kwargs['batch_size']) except Exception as exc: raise self.retry(kwargs=kwargs, exc=exc)
def _create_override(self, request_user, subsection_grade_model, **override_data): """ Helper method to create a `PersistentSubsectionGradeOverride` object and send a `SUBSECTION_OVERRIDE_CHANGED` signal. """ override, _ = PersistentSubsectionGradeOverride.objects.update_or_create( grade=subsection_grade_model, defaults=self._clean_override_data(override_data), ) _ = PersistentSubsectionGradeOverrideHistory.objects.create( override_id=override.id, user=request_user, feature=PersistentSubsectionGradeOverrideHistory.GRADEBOOK, action=PersistentSubsectionGradeOverrideHistory.CREATE_OR_UPDATE, ) set_event_transaction_type(SUBSECTION_GRADE_CALCULATED) create_new_event_transaction_id() recalculate_subsection_grade_v3.apply( kwargs=dict( user_id=subsection_grade_model.user_id, anonymous_user_id=None, course_id=text_type(subsection_grade_model.course_id), usage_id=text_type(subsection_grade_model.usage_key), only_if_higher=False, expected_modified_time=to_timestamp(override.modified), score_deleted=False, event_transaction_id=unicode(get_event_transaction_id()), event_transaction_type=unicode(get_event_transaction_type()), score_db_table=ScoreDatabaseTableEnum.overrides, force_update_subsections=True, ) ) # Emit events to let our tracking system to know we updated subsection grade subsection_grade_calculated(subsection_grade_model)
def rescore_problem_module_state(xmodule_instance_args, module_descriptor, student_module, task_input): ''' Takes an XModule descriptor and a corresponding StudentModule object, and performs rescoring on the student's problem submission. Throws exceptions if the rescoring is fatal and should be aborted if in a loop. In particular, raises UpdateProblemModuleStateError if module fails to instantiate, or if the module doesn't support rescoring. Returns True if problem was successfully rescored for the given student, and False if problem encountered some kind of error in rescoring. ''' # unpack the StudentModule: course_id = student_module.course_id student = student_module.student usage_key = student_module.module_state_key with modulestore().bulk_operations(course_id): course = get_course_by_id(course_id) # TODO: Here is a call site where we could pass in a loaded course. I # think we certainly need it since grading is happening here, and field # overrides would be important in handling that correctly instance = _get_module_instance_for_task( course_id, student, module_descriptor, xmodule_instance_args, grade_bucket_type='rescore', course=course ) if instance is None: # Either permissions just changed, or someone is trying to be clever # and load something they shouldn't have access to. msg = "No module {location} for student {student}--access denied?".format( location=usage_key, student=student ) TASK_LOG.warning(msg) return UPDATE_STATUS_FAILED if not hasattr(instance, 'rescore'): # This should not happen, since it should be already checked in the # caller, but check here to be sure. msg = "Specified module {0} of type {1} does not support rescoring.".format(usage_key, instance.__class__) raise UpdateProblemModuleStateError(msg) # We check here to see if the problem has any submissions. If it does not, we don't want to rescore it if not instance.has_submitted_answer(): return UPDATE_STATUS_SKIPPED # Set the tracking info before this call, because it makes downstream # calls that create events. We retrieve and store the id here because # the request cache will be erased during downstream calls. create_new_event_transaction_id() set_event_transaction_type(GRADES_RESCORE_EVENT_TYPE) # specific events from CAPA are not propagated up the stack. Do we want this? try: instance.rescore(only_if_higher=task_input['only_if_higher']) except (LoncapaProblemError, StudentInputError, ResponseError): TASK_LOG.warning( u"error processing rescore call for course %(course)s, problem %(loc)s " u"and student %(student)s", dict( course=course_id, loc=usage_key, student=student ) ) return UPDATE_STATUS_FAILED instance.save() TASK_LOG.debug( u"successfully processed rescore call for course %(course)s, problem %(loc)s " u"and student %(student)s", dict( course=course_id, loc=usage_key, student=student ) ) return UPDATE_STATUS_SUCCEEDED
def override_score_module_state(xmodule_instance_args, module_descriptor, student_module, task_input): ''' Takes an XModule descriptor and a corresponding StudentModule object, and performs an override on the student's problem score. Throws exceptions if the override is fatal and should be aborted if in a loop. In particular, raises UpdateProblemModuleStateError if module fails to instantiate, or if the module doesn't support overriding, or if the score used for override is outside the acceptable range of scores (between 0 and the max score for the problem). Returns True if problem was successfully overriden for the given student, and False if problem encountered some kind of error in overriding. ''' # unpack the StudentModule: course_id = student_module.course_id student = student_module.student usage_key = student_module.module_state_key with modulestore().bulk_operations(course_id): course = get_course_by_id(course_id) instance = _get_module_instance_for_task( course_id, student, module_descriptor, xmodule_instance_args, course=course ) if instance is None: # Either permissions just changed, or someone is trying to be clever # and load something they shouldn't have access to. msg = "No module {location} for student {student}--access denied?".format( location=usage_key, student=student ) TASK_LOG.warning(msg) return UPDATE_STATUS_FAILED if not hasattr(instance, 'set_score'): msg = "Scores cannot be overridden for this problem type." raise UpdateProblemModuleStateError(msg) weighted_override_score = float(task_input['score']) if not (0 <= weighted_override_score <= instance.max_score()): msg = "Score must be between 0 and the maximum points available for the problem." raise UpdateProblemModuleStateError(msg) # Set the tracking info before this call, because it makes downstream # calls that create events. We retrieve and store the id here because # the request cache will be erased during downstream calls. create_new_event_transaction_id() set_event_transaction_type(GRADES_OVERRIDE_EVENT_TYPE) problem_weight = instance.weight if instance.weight is not None else 1 if problem_weight == 0: msg = "Scores cannot be overridden for a problem that has a weight of zero." raise UpdateProblemModuleStateError(msg) else: instance.set_score(Score( raw_earned=weighted_override_score / problem_weight, raw_possible=instance.max_score() / problem_weight )) instance.publish_grade() instance.save() TASK_LOG.debug( u"successfully processed score override for course %(course)s, problem %(loc)s " u"and student %(student)s", dict( course=course_id, loc=usage_key, student=student ) ) return UPDATE_STATUS_SUCCEEDED
def reset_student_attempts(course_id, student, module_state_key, requesting_user, delete_module=False): """ Reset student attempts for a problem. Optionally deletes all student state for the specified problem. In the previous instructor dashboard it was possible to modify/delete modules that were not problems. That has been disabled for safety. `student` is a User `problem_to_reset` is the name of a problem e.g. 'L2Node1'. To build the module_state_key 'problem/' and course information will be appended to `problem_to_reset`. Raises: ValueError: `problem_state` is invalid JSON. StudentModule.DoesNotExist: could not load the student module. submissions.SubmissionError: unexpected error occurred while resetting the score in the submissions API. """ user_id = anonymous_id_for_user(student, course_id) requesting_user_id = anonymous_id_for_user(requesting_user, course_id) submission_cleared = False try: # A block may have children. Clear state on children first. block = modulestore().get_item(module_state_key) if block.has_children: for child in block.children: try: reset_student_attempts(course_id, student, child, requesting_user, delete_module=delete_module) except StudentModule.DoesNotExist: # If a particular child doesn't have any state, no big deal, as long as the parent does. pass if delete_module: # Some blocks (openassessment) use StudentModule data as a key for internal submission data. # Inform these blocks of the reset and allow them to handle their data. clear_student_state = getattr(block, "clear_student_state", None) if callable(clear_student_state): with disconnect_submissions_signal_receiver(score_set): clear_student_state(user_id=user_id, course_id=unicode(course_id), item_id=unicode(module_state_key), requesting_user_id=requesting_user_id) submission_cleared = True except ItemNotFoundError: block = None log.warning( u"Could not find %s in modulestore when attempting to reset attempts.", module_state_key) # Reset the student's score in the submissions API, if xblock.clear_student_state has not done so already. # We need to do this before retrieving the `StudentModule` model, because a score may exist with no student module. # TODO: Should the LMS know about sub_api and call this reset, or should it generically call it on all of its # xblock services as well? See JIRA ARCH-26. if delete_module and not submission_cleared: sub_api.reset_score( user_id, text_type(course_id), text_type(module_state_key), ) module_to_reset = StudentModule.objects.get( student_id=student.id, course_id=course_id, module_state_key=module_state_key) if delete_module: module_to_reset.delete() create_new_event_transaction_id() set_event_transaction_type(grades_events.STATE_DELETED_EVENT_TYPE) tracker.emit( unicode(grades_events.STATE_DELETED_EVENT_TYPE), { 'user_id': unicode(student.id), 'course_id': unicode(course_id), 'problem_id': unicode(module_state_key), 'instructor_id': unicode(requesting_user.id), 'event_transaction_id': unicode(get_event_transaction_id()), 'event_transaction_type': unicode(grades_events.STATE_DELETED_EVENT_TYPE), }) if not submission_cleared: _fire_score_changed_for_block( course_id, student, block, module_state_key, ) else: _reset_module_attempts(module_to_reset)
def rescore_problem_module_state(xmodule_instance_args, module_descriptor, student_module, task_input): ''' Takes an XModule descriptor and a corresponding StudentModule object, and performs rescoring on the student's problem submission. Throws exceptions if the rescoring is fatal and should be aborted if in a loop. In particular, raises UpdateProblemModuleStateError if module fails to instantiate, or if the module doesn't support rescoring. Returns True if problem was successfully rescored for the given student, and False if problem encountered some kind of error in rescoring. ''' # unpack the StudentModule: course_id = student_module.course_id student = student_module.student usage_key = student_module.module_state_key with modulestore().bulk_operations(course_id): course = get_course_by_id(course_id) # TODO: Here is a call site where we could pass in a loaded course. I # think we certainly need it since grading is happening here, and field # overrides would be important in handling that correctly instance = _get_module_instance_for_task(course_id, student, module_descriptor, xmodule_instance_args, grade_bucket_type='rescore', course=course) if instance is None: # Either permissions just changed, or someone is trying to be clever # and load something they shouldn't have access to. msg = "No module {loc} for student {student}--access denied?".format( loc=usage_key, student=student) TASK_LOG.warning(msg) return UPDATE_STATUS_FAILED # TODO: (TNL-6594) Remove this switch once rescore_problem support # once CAPA uses ScorableXBlockMixin. for method in ['rescore', 'rescore_problem']: rescore_method = getattr(instance, method, None) if rescore_method is not None: break else: # for-else: Neither method exists on the block. # This should not happen, since it should be already checked in the # caller, but check here to be sure. msg = "Specified problem does not support rescoring." raise UpdateProblemModuleStateError(msg) # TODO: Remove the first part of this if-else with TNL-6594 # We check here to see if the problem has any submissions. If it does not, we don't want to rescore it if hasattr(instance, "done"): if not instance.done: return UPDATE_STATUS_SKIPPED elif not instance.has_submitted_answer(): return UPDATE_STATUS_SKIPPED # Set the tracking info before this call, because it makes downstream # calls that create events. We retrieve and store the id here because # the request cache will be erased during downstream calls. event_transaction_id = create_new_event_transaction_id() set_event_transaction_type(GRADES_RESCORE_EVENT_TYPE) result = rescore_method(only_if_higher=task_input['only_if_higher']) instance.save() if result is None or result.get(u'success') in { u'correct', u'incorrect' }: TASK_LOG.debug( u"successfully processed rescore call for course %(course)s, problem %(loc)s " u"and student %(student)s", dict(course=course_id, loc=usage_key, student=student)) if result is not None: # Only for CAPA. This will get moved to the grade handler. new_weighted_earned, new_weighted_possible = weighted_score( result['new_raw_earned'] if result else None, result['new_raw_possible'] if result else None, module_descriptor.weight, ) # TODO: remove this context manager after completion of AN-6134 context = course_context_from_course_id(course_id) with tracker.get_tracker().context(GRADES_RESCORE_EVENT_TYPE, context): tracker.emit( unicode(GRADES_RESCORE_EVENT_TYPE), { 'course_id': unicode(course_id), 'user_id': unicode(student.id), 'problem_id': unicode(usage_key), 'new_weighted_earned': new_weighted_earned, 'new_weighted_possible': new_weighted_possible, 'only_if_higher': task_input['only_if_higher'], 'instructor_id': unicode(xmodule_instance_args['request_info'] ['user_id']), 'event_transaction_id': unicode(event_transaction_id), 'event_transaction_type': unicode(GRADES_RESCORE_EVENT_TYPE), }) return UPDATE_STATUS_SUCCEEDED else: TASK_LOG.warning( u"error processing rescore call for course %(course)s, problem %(loc)s " u"and student %(student)s: %(msg)s", dict(msg=result.get('success', result), course=course_id, loc=usage_key, student=student)) return UPDATE_STATUS_FAILED
def rescore_problem_module_state(xmodule_instance_args, module_descriptor, student_module, task_input): ''' Takes an XModule descriptor and a corresponding StudentModule object, and performs rescoring on the student's problem submission. Throws exceptions if the rescoring is fatal and should be aborted if in a loop. In particular, raises UpdateProblemModuleStateError if module fails to instantiate, or if the module doesn't support rescoring. Returns True if problem was successfully rescored for the given student, and False if problem encountered some kind of error in rescoring. ''' # unpack the StudentModule: course_id = student_module.course_id student = student_module.student usage_key = student_module.module_state_key with modulestore().bulk_operations(course_id): course = get_course_by_id(course_id) # TODO: Here is a call site where we could pass in a loaded course. I # think we certainly need it since grading is happening here, and field # overrides would be important in handling that correctly instance = _get_module_instance_for_task(course_id, student, module_descriptor, xmodule_instance_args, grade_bucket_type='rescore', course=course) if instance is None: # Either permissions just changed, or someone is trying to be clever # and load something they shouldn't have access to. msg = u"No module {location} for student {student}--access denied?".format( location=usage_key, student=student) TASK_LOG.warning(msg) return UPDATE_STATUS_FAILED if not hasattr(instance, 'rescore'): # This should not happen, since it should be already checked in the # caller, but check here to be sure. msg = u"Specified module {0} of type {1} does not support rescoring.".format( usage_key, instance.__class__) raise UpdateProblemModuleStateError(msg) # We check here to see if the problem has any submissions. If it does not, we don't want to rescore it if not instance.has_submitted_answer(): return UPDATE_STATUS_SKIPPED # Set the tracking info before this call, because it makes downstream # calls that create events. We retrieve and store the id here because # the request cache will be erased during downstream calls. create_new_event_transaction_id() set_event_transaction_type(grades_events.GRADES_RESCORE_EVENT_TYPE) # specific events from CAPA are not propagated up the stack. Do we want this? try: instance.rescore(only_if_higher=task_input['only_if_higher']) except (LoncapaProblemError, StudentInputError, ResponseError): TASK_LOG.warning( u"error processing rescore call for course %(course)s, problem %(loc)s " u"and student %(student)s", dict(course=course_id, loc=usage_key, student=student)) return UPDATE_STATUS_FAILED instance.save() TASK_LOG.debug( u"successfully processed rescore call for course %(course)s, problem %(loc)s " u"and student %(student)s", dict(course=course_id, loc=usage_key, student=student)) return UPDATE_STATUS_SUCCEEDED
def _recalculate_subsection_grade(self, **kwargs): """ Updates a saved subsection grade. Keyword Arguments: user_id (int): id of applicable User object anonymous_user_id (int, OPTIONAL): Anonymous ID of the User course_id (string): identifying the course usage_id (string): identifying the course block only_if_higher (boolean): indicating whether grades should be updated only if the new raw_earned is higher than the previous value. expected_modified_time (serialized timestamp): indicates when the task was queued so that we can verify the underlying data update. score_deleted (boolean): indicating whether the grade change is a result of the problem's score being deleted. event_transaction_id (string): uuid identifying the current event transaction. event_transaction_type (string): human-readable type of the event at the root of the current event transaction. score_db_table (ScoreDatabaseTableEnum): database table that houses the changed score. Used in conjunction with expected_modified_time. """ try: course_key = CourseLocator.from_string(kwargs['course_id']) scored_block_usage_key = UsageKey.from_string(kwargs['usage_id']).replace(course_key=course_key) set_custom_metrics_for_course_key(course_key) set_custom_metric('usage_id', unicode(scored_block_usage_key)) # The request cache is not maintained on celery workers, # where this code runs. So we take the values from the # main request cache and store them in the local request # cache. This correlates model-level grading events with # higher-level ones. set_event_transaction_id(kwargs.get('event_transaction_id')) set_event_transaction_type(kwargs.get('event_transaction_type')) # Verify the database has been updated with the scores when the task was # created. This race condition occurs if the transaction in the task # creator's process hasn't committed before the task initiates in the worker # process. has_database_updated = _has_db_updated_with_new_score(self, scored_block_usage_key, **kwargs) if not has_database_updated: raise DatabaseNotReadyError _update_subsection_grades( course_key, scored_block_usage_key, kwargs['only_if_higher'], kwargs['user_id'], kwargs['score_deleted'], ) except Exception as exc: if not isinstance(exc, KNOWN_RETRY_ERRORS): log.info("tnl-6244 grades unexpected failure: {}. task id: {}. kwargs={}".format( repr(exc), self.request.id, kwargs, )) raise self.retry(kwargs=kwargs, exc=exc)
def override_score_module_state(xmodule_instance_args, module_descriptor, student_module, task_input): ''' Takes an XModule descriptor and a corresponding StudentModule object, and performs an override on the student's problem score. Throws exceptions if the override is fatal and should be aborted if in a loop. In particular, raises UpdateProblemModuleStateError if module fails to instantiate, or if the module doesn't support overriding, or if the score used for override is outside the acceptable range of scores (between 0 and the max score for the problem). Returns True if problem was successfully overriden for the given student, and False if problem encountered some kind of error in overriding. ''' # unpack the StudentModule: course_id = student_module.course_id student = student_module.student usage_key = student_module.module_state_key with modulestore().bulk_operations(course_id): course = get_course_by_id(course_id) instance = _get_module_instance_for_task(course_id, student, module_descriptor, xmodule_instance_args, course=course) if instance is None: # Either permissions just changed, or someone is trying to be clever # and load something they shouldn't have access to. msg = u"No module {location} for student {student}--access denied?".format( location=usage_key, student=student) TASK_LOG.warning(msg) return UPDATE_STATUS_FAILED if not hasattr(instance, 'set_score'): msg = "Scores cannot be overridden for this problem type." raise UpdateProblemModuleStateError(msg) weighted_override_score = float(task_input['score']) if not (0 <= weighted_override_score <= instance.max_score()): msg = "Score must be between 0 and the maximum points available for the problem." raise UpdateProblemModuleStateError(msg) # Set the tracking info before this call, because it makes downstream # calls that create events. We retrieve and store the id here because # the request cache will be erased during downstream calls. create_new_event_transaction_id() set_event_transaction_type(grades_events.GRADES_OVERRIDE_EVENT_TYPE) problem_weight = instance.weight if instance.weight is not None else 1 if problem_weight == 0: msg = "Scores cannot be overridden for a problem that has a weight of zero." raise UpdateProblemModuleStateError(msg) else: instance.set_score( Score(raw_earned=weighted_override_score / problem_weight, raw_possible=instance.max_score() / problem_weight)) instance.publish_grade() instance.save() TASK_LOG.debug( u"successfully processed score override for course %(course)s, problem %(loc)s " u"and student %(student)s", dict(course=course_id, loc=usage_key, student=student)) return UPDATE_STATUS_SUCCEEDED
def _recalculate_subsection_grade(self, **kwargs): """ Updates a saved subsection grade. Keyword Arguments: user_id (int): id of applicable User object anonymous_user_id (int, OPTIONAL): Anonymous ID of the User course_id (string): identifying the course usage_id (string): identifying the course block only_if_higher (boolean): indicating whether grades should be updated only if the new raw_earned is higher than the previous value. expected_modified_time (serialized timestamp): indicates when the task was queued so that we can verify the underlying data update. score_deleted (boolean): indicating whether the grade change is a result of the problem's score being deleted. event_transaction_id (string): uuid identifying the current event transaction. event_transaction_type (string): human-readable type of the event at the root of the current event transaction. score_db_table (ScoreDatabaseTableEnum): database table that houses the changed score. Used in conjunction with expected_modified_time. """ try: course_key = CourseLocator.from_string(kwargs['course_id']) scored_block_usage_key = UsageKey.from_string( kwargs['usage_id']).replace(course_key=course_key) set_custom_metrics_for_course_key(course_key) set_custom_metric('usage_id', unicode(scored_block_usage_key)) # The request cache is not maintained on celery workers, # where this code runs. So we take the values from the # main request cache and store them in the local request # cache. This correlates model-level grading events with # higher-level ones. set_event_transaction_id(kwargs.get('event_transaction_id')) set_event_transaction_type(kwargs.get('event_transaction_type')) # Verify the database has been updated with the scores when the task was # created. This race condition occurs if the transaction in the task # creator's process hasn't committed before the task initiates in the worker # process. has_database_updated = _has_db_updated_with_new_score( self, scored_block_usage_key, **kwargs) if not has_database_updated: raise DatabaseNotReadyError _update_subsection_grades( course_key, scored_block_usage_key, kwargs['only_if_higher'], kwargs['user_id'], kwargs['score_deleted'], ) except Exception as exc: # pylint: disable=broad-except if not isinstance(exc, KNOWN_RETRY_ERRORS): log.info( "tnl-6244 grades unexpected failure: {}. task id: {}. kwargs={}" .format( repr(exc), self.request.id, kwargs, )) raise self.retry(kwargs=kwargs, exc=exc)
def reset_student_attempts(course_id, student, module_state_key, requesting_user, delete_module=False): """ Reset student attempts for a problem. Optionally deletes all student state for the specified problem. In the previous instructor dashboard it was possible to modify/delete modules that were not problems. That has been disabled for safety. `student` is a User `problem_to_reset` is the name of a problem e.g. 'L2Node1'. To build the module_state_key 'problem/' and course information will be appended to `problem_to_reset`. Raises: ValueError: `problem_state` is invalid JSON. StudentModule.DoesNotExist: could not load the student module. submissions.SubmissionError: unexpected error occurred while resetting the score in the submissions API. """ user_id = anonymous_id_for_user(student, course_id) requesting_user_id = anonymous_id_for_user(requesting_user, course_id) submission_cleared = False try: # A block may have children. Clear state on children first. block = modulestore().get_item(module_state_key) if block.has_children: for child in block.children: try: reset_student_attempts(course_id, student, child, requesting_user, delete_module=delete_module) except StudentModule.DoesNotExist: # If a particular child doesn't have any state, no big deal, as long as the parent does. pass if delete_module: # Some blocks (openassessment) use StudentModule data as a key for internal submission data. # Inform these blocks of the reset and allow them to handle their data. clear_student_state = getattr(block, "clear_student_state", None) if callable(clear_student_state): with disconnect_submissions_signal_receiver(score_set): clear_student_state( user_id=user_id, course_id=unicode(course_id), item_id=unicode(module_state_key), requesting_user_id=requesting_user_id ) submission_cleared = True except ItemNotFoundError: block = None log.warning("Could not find %s in modulestore when attempting to reset attempts.", module_state_key) # Reset the student's score in the submissions API, if xblock.clear_student_state has not done so already. # We need to do this before retrieving the `StudentModule` model, because a score may exist with no student module. # TODO: Should the LMS know about sub_api and call this reset, or should it generically call it on all of its # xblock services as well? See JIRA ARCH-26. if delete_module and not submission_cleared: sub_api.reset_score( user_id, course_id.to_deprecated_string(), module_state_key.to_deprecated_string(), ) module_to_reset = StudentModule.objects.get( student_id=student.id, course_id=course_id, module_state_key=module_state_key ) if delete_module: module_to_reset.delete() create_new_event_transaction_id() grade_update_root_type = 'edx.grades.problem.state_deleted' set_event_transaction_type(grade_update_root_type) tracker.emit( unicode(grade_update_root_type), { 'user_id': unicode(student.id), 'course_id': unicode(course_id), 'problem_id': unicode(module_state_key), 'instructor_id': unicode(requesting_user.id), 'event_transaction_id': unicode(get_event_transaction_id()), 'event_transaction_type': unicode(grade_update_root_type), } ) if not submission_cleared: _fire_score_changed_for_block( course_id, student, block, module_state_key, ) else: _reset_module_attempts(module_to_reset)