Add support for clear_student_state to allow staff to reset Attempts

I have an XBlock which works fine and offers all the basic functionality. Now I need to add support for learners so that they can reset user’s attempts count. I’ve added the method clear_student_state, but it doesn’t work, perhaps I’m missing something.
cc: @regis

@braden Can you please check the above problem?

@Zubair, it would be much easier for us to help if you can show us the code.

That said, as far as I can tell clear_student_state is only ever called via reset_student_attempts, and that is protected by a GIVE_STUDENT_EXTENSION permission check. That in turn calls enrollmment.reset_student_attempts to do the actual resetting, which then finally calls clear_student_state - in some cases.

I suspect that to let a student reset their own attempts, in addition to implementing clear_student_state you’d have to find a way to call enrollment.reset_student_attempts. You could just call the reset_student_attempts API from javascript, but that would fail unless you gave all your students the GIVE_STUDENT_EXTENSION permission, somehow.

@Zubair I’m assuming you mean for instructors to reset student attempts, right? For students you can easily just implement a button in your own XBlock using a regular XBlock handler.

Please review the code here and here.

From what I can tell, if your XBlock state has a field called attempts, it will be reset to zero without any other work on your part. Otherwise the only other built-in option is to pass the delete_module flag which will delete the whole student state for that user and (only in this case) will also call your custom clear_student_state method for any additional deletion of state that’s stored outside of the usual XBlock fields.

Thank you for your replies @arbrandes @braden.
@arbrandes I want to enable the option for instructors so that they can reset attempts for a particular learner.

@braden yes, your assumption is correct. As shown in the above screenshot, all available options work for default XBlocks but, for my custom XBlock Reset Learner's Attempts to Zero doesn’t work, so I was wondering is there any method/flag that I need to override inside my XBlock for this to work?

@Zubair You should put some breakpoints or logs into the code here and see what’s happening.

The “Reset Learner’s Attempts to Zero” should be working if and only if your XBlock has an XBlock Field called attempts which is an integer field. Then it should be reset to zero using that link. The “Delete Learner State” should be what calls clear_student_state and it should also delete the whole student state (xblock fields) anyways.

I’m using Juniper release, it fails at this line, It can’t find the item (custom XBlock) in StudentModule, and since code using get it throws an exception.

If there’s no studentmodule, then there’s no state to reset anyways. Or if there is a state, it’s not being stored in the usual XBlock fields. How are you storing the student state?

User state is store in a variable with the scope of user_state.

user_responses = Dict(help="Student submitted state", default={}, scope=Scope.user_state)

In that case, the data should be stored in CoursewareStudentModule.state; I suggest you first find the studentmodule in the django admin to confirm that the data is there, and then do some debugging to figure out why this line can’t find the item in StudentModule.