3838
3939def _process_learning_context (
4040 learning_context_key : LearningContextKey ,
41- course_processor : Callable [[CourseKey , dict [str , Any ]], list [int ]],
41+ course_processor : Callable [[CourseKey , dict [str , Any ], int | None ], list [int ]],
4242 options : dict [str , Any ],
43+ user_id : int | None = None ,
4344) -> list [int ]:
4445 """
4546 Process a learning context (course or learning path) using the given course processor function.
@@ -53,19 +54,20 @@ def _process_learning_context(
5354 course_processor: A function that processes a single course and returns eligible user IDs
5455 options: Options to pass to the processor. For learning paths, may contain a "steps" key
5556 with step-specific options in the format: {"steps": {"<course_key>": {...}}}
57+ user_id: Optional. If provided, will check eligibility for the specific user.
5658
5759 Returns:
5860 A list of eligible user IDs
5961 """
6062 if learning_context_key .is_course :
61- return course_processor (learning_context_key , options )
63+ return course_processor (learning_context_key , options , user_id )
6264
6365 learning_path = LearningPath .objects .get (key = learning_context_key )
6466
6567 results = None
6668 for course in learning_path .steps .all ():
6769 course_options = options .get ("steps" , {}).get (str (course .course_key ), options )
68- course_results = set (course_processor (course .course_key , course_options ))
70+ course_results = set (course_processor (course .course_key , course_options , user_id ))
6971
7072 if results is None :
7173 results = course_results
@@ -162,30 +164,31 @@ def _are_grades_passing_criteria(
162164 return total_score >= required_grades .get ('total' , 0 )
163165
164166
165- def _retrieve_course_subsection_grades (course_id : CourseKey , options : dict [str , Any ]) -> list [int ]:
167+ def _retrieve_course_subsection_grades (course_id : CourseKey , options : dict [str , Any ], user_id : int | None = None ) -> list [int ]:
166168 """Implementation for retrieving course grades."""
167169 required_grades : dict [str , int ] = options ['required_grades' ]
168170 required_grades = {key .lower (): value * 100 for key , value in required_grades .items ()}
169171
170- users = get_course_enrollments (course_id )
172+ users = get_course_enrollments (course_id , user_id )
171173 grades = _get_grades_by_format (course_id , users )
172174 log .debug (grades )
173175 weights = _get_category_weights (course_id )
174176
175177 eligible_users = []
176- for user_id , user_grades in grades .items ():
178+ for uid , user_grades in grades .items ():
177179 if _are_grades_passing_criteria (user_grades , required_grades , weights ):
178- eligible_users .append (user_id )
180+ eligible_users .append (uid )
179181
180182 return eligible_users
181183
182184
183- def retrieve_subsection_grades (learning_context_key : LearningContextKey , options : dict [str , Any ]) -> list [int ]:
185+ def retrieve_subsection_grades (learning_context_key : LearningContextKey , options : dict [str , Any ], user_id : int | None = None ) -> list [int ]:
184186 """
185187 Retrieve the users that have passing grades in all required categories.
186188
187189 :param learning_context_key: The learning context key (course or learning path).
188190 :param options: The custom options for the credential.
191+ :param user_id: Optional. If provided, will check eligibility for the specific user.
189192 :returns: The IDs of the users that have passing grades in all required categories.
190193
191194 Options:
@@ -232,7 +235,7 @@ def retrieve_subsection_grades(learning_context_key: LearningContextKey, options
232235 }
233236 }
234237 """
235- return _process_learning_context (learning_context_key , _retrieve_course_subsection_grades , options )
238+ return _process_learning_context (learning_context_key , _retrieve_course_subsection_grades , options , user_id )
236239
237240
238241def _prepare_request_to_completion_aggregator (course_id : CourseKey , query_params : dict , url : str ) -> APIView :
@@ -262,7 +265,7 @@ def _prepare_request_to_completion_aggregator(course_id: CourseKey, query_params
262265 return view
263266
264267
265- def _retrieve_course_completions (course_id : CourseKey , options : dict [str , Any ]) -> list [int ]:
268+ def _retrieve_course_completions (course_id : CourseKey , options : dict [str , Any ], user_id : int | None = None ) -> list [int ]:
266269 """Implementation for retrieving course completions."""
267270 # If it turns out to be too slow, we can:
268271 # 1. Modify the Completion Aggregator to emit a signal/event when a user achieves a certain completion threshold.
@@ -290,15 +293,20 @@ def _retrieve_course_completions(course_id: CourseKey, options: dict[str, Any])
290293 query_params ['page' ] += 1
291294 view = _prepare_request_to_completion_aggregator (course_id , query_params .copy (), url )
292295
296+ if user_id :
297+ username = get_user_model ().objects .get (id = user_id ).username
298+ return [user_id ] if username in completions else []
299+
293300 return list (get_user_model ().objects .filter (username__in = completions ).values_list ('id' , flat = True ))
294301
295302
296- def retrieve_completions (learning_context_key : LearningContextKey , options : dict [str , Any ]) -> list [int ]:
303+ def retrieve_completions (learning_context_key : LearningContextKey , options : dict [str , Any ], user_id : int | None = None ) -> list [int ]:
297304 """
298305 Retrieve the course completions for all users through the Completion Aggregator API.
299306
300307 :param learning_context_key: The learning context key (course or learning path).
301- :param options: The custom options for the credential.
308+ :param options: The custom options for the credentia
309+ :param user_id: Optional. If provided, will check eligibility for the specific user.l.
302310 :returns: The IDs of the users that have achieved the required completion percentage.
303311
304312 Options:
@@ -322,7 +330,7 @@ def retrieve_completions(learning_context_key: LearningContextKey, options: dict
322330 return _process_learning_context (learning_context_key , _retrieve_course_completions , options )
323331
324332
325- def retrieve_completions_and_grades (learning_context_key : LearningContextKey , options : dict [str , Any ]) -> list [int ]:
333+ def retrieve_completions_and_grades (learning_context_key : LearningContextKey , options : dict [str , Any ], user_id : int | None = None ) -> list [int ]:
326334 """
327335 Retrieve the users that meet both completion and grade criteria.
328336
@@ -331,6 +339,7 @@ def retrieve_completions_and_grades(learning_context_key: LearningContextKey, op
331339
332340 :param learning_context_key: The learning context key (course or learning path).
333341 :param options: The custom options for the credential.
342+ :param user_id: Optional. If provided, will check eligibility for the specific user.
334343 :returns: The IDs of the users that meet both sets of criteria.
335344
336345 Options:
0 commit comments