Repository navigation
feat: GET a course run's competency criteria for the learner Progress tab - #4
jesperhodge wants to merge 1 commit into
Conversation
Adds get_course_competency_criteria() and GET api/cbe/v1/courses/<course_key>/criteria/, served from a new learner-only URL module, openedx_learning.learner_urls. The endpoint returns a course run's competency criteria hierarchy as four flat lists (taxonomies, competencies, groups, criteria), leaves out archived and disabled records, and prunes branches left with no criteria. Access is the new openedx_learning.view_course_competency_criteria permission, which allows any signed-in user by default and which openedx-platform overrides. Part of openedx#733 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| The body is four flat lists that reference each other by id: ``taxonomies``, ``competencies``, | ||
| ``groups`` and ``criteria``. See ``get_course_competency_criteria`` for what each holds and which | ||
| rows are left out. The course key may carry a branch or a version, which are ignored. A value that | ||
| is not a course run key is a 400, and a course run with no criteria is a 200 with four empty lists. | ||
|
|
||
| Who may read is decided by ``openedx_learning.view_course_competency_criteria``, checked with the | ||
| plain course key. openedx-core's default rule admits any signed-in caller, and openedx-platform | ||
| replaces it to admit only learners with an active enrollment and course staff. Checks run in this | ||
| order: sign-in (401), key (400), permission (403). | ||
|
|
||
| The response is not paginated unless ``page`` or ``page_size`` is in the query string. A page holds | ||
| up to ``page_size`` competencies, with their taxonomies, all their groups and the criteria of those | ||
| groups, plus ``count`` (all competencies), ``num_pages``, ``current_page``, ``next`` and ``previous``. | ||
| A page past the end is a 404. |
There was a problem hiding this comment.
Give an example with the body shape instead of text paragraphs.
| {"course_key": [_("'%(value)s' is not a valid course run key.") % {"value": course_key}]} | ||
| ) from exc | ||
| if not request.user.has_perm("openedx_learning.view_course_competency_criteria", plain_course_key): | ||
| raise PermissionDenied() |
There was a problem hiding this comment.
| raise PermissionDenied() | |
| raise PermissionDenied() | |
| competencies = self.paginator.paginate_queryset(competencies, request, view=self) | ||
| competency_ids = {competency.id for competency in competencies} | ||
| taxonomy_ids = {competency.taxonomy_id for competency in competencies} | ||
| taxonomies = [taxonomy for taxonomy in taxonomies if taxonomy.id in taxonomy_ids] | ||
| groups = [group for group in groups if group.competency_id in competency_ids] | ||
| group_ids = {group.id for group in groups} | ||
| criteria = [criterion for criterion in criteria if criterion.group_id in group_ids] | ||
| page = self.paginator.page | ||
| pagination = { | ||
| "count": page.paginator.count, | ||
| "num_pages": page.paginator.num_pages, | ||
| "current_page": page.number, | ||
| "next": self.paginator.get_next_link(), | ||
| "previous": self.paginator.get_previous_link(), | ||
| } |
There was a problem hiding this comment.
Extract to a function _get_paginated_response
| }) | ||
|
|
||
|
|
||
| class CourseCompetencyCriteriaView(generics.GenericAPIView): |
There was a problem hiding this comment.
We should split views into authoring and learner views. This should go into a different file, and the class should also include Learner in its name somewhere.
| This function reads data and checks no permission; the caller decides who may see the result. | ||
| ``course_key`` must be a plain course run key with its branch and version already stripped, and is | ||
| matched exactly against the course run of each course-level group. A key with no course run, or a | ||
| course run with no live criteria, returns a result whose four lists are all empty. | ||
|
|
||
| A competency's criteria tree has a root group, one course-level group per course run, and bottom-tier | ||
| groups nested below a course-level group at any depth. The result holds, for each course-level group of | ||
| the requested run, its root group, the course-level group, and every group below it. A group below a | ||
| course-level group of another course run is never returned, even under a shared root. Criteria are | ||
| returned for every returned group except the root: a criterion attached directly to a root group is | ||
| never returned. | ||
|
|
||
| Left out: | ||
|
|
||
| - An archived criterion, and a criterion whose ObjectTag is archived. | ||
| - An archived group, and everything beneath it, even when the archive cascade did not reach the | ||
| groups below. An archived root group hides the whole competency. | ||
| - A competency whose tag, or any ancestor tag, is archived. A competency whose taxonomy is archived | ||
| or disabled. Only tags of competency taxonomies are competencies. | ||
| - After those, any group with no remaining criterion in its subtree, any competency with no remaining | ||
| course-level group, and any taxonomy with no remaining competency. | ||
|
|
||
| An archived rule profile does not hide a criterion. The criterion's rule is the profile's |
There was a problem hiding this comment.
The docstring is way to large.
It should only state:
- what arguments come in (incl. shape)
- what gets returned (incl. shape)
- what the caller of the function critically needs to know
| # Bottom-tier groups carry no course, so they can only be found through their tag and then walked down | ||
| # from the requested run's course-level groups. Another run's branch under the same root is never | ||
| # reached by that walk. |
There was a problem hiding this comment.
Remove comment and extract code into a highly readable function that expresses what it does through good naming.
There was a problem hiding this comment.
What is all this for, anyway? Finding subtree_groups? And is anything excluded?
| returned for every returned group except the root: a criterion attached directly to a root group is | ||
| never returned. | ||
|
|
||
| Left out: |
There was a problem hiding this comment.
Okay I grant that the caller needs to know what is excluded.
Try to make this simple to understand. Use ASCII art to illustrate how it is for the tree.
| ``groups`` are ordered by ``(ordering, id)``, and ``taxonomies``, ``competencies`` and ``criteria`` by | ||
| ``id``, so repeated calls return equal results. The number of database queries is fixed (one, when the | ||
| course run has no course-level group, otherwise three) however many taxonomies, competencies, groups | ||
| and criteria the course has. |
There was a problem hiding this comment.
| ``groups`` are ordered by ``(ordering, id)``, and ``taxonomies``, ``competencies`` and ``criteria`` by | |
| ``id``, so repeated calls return equal results. The number of database queries is fixed (one, when the | |
| course run has no course-level group, otherwise three) however many taxonomies, competencies, groups | |
| and criteria the course has. | |
| Ordering by: | |
| ``groups``: ``(ordering, id)`` | |
| ``taxonomies``, ``competencies``, ``criteria``: ``id`` |
| # A group is kept when its subtree holds a criterion, so mark each criterion's group and its ancestors. | ||
| # The walk ends above the course-level group, whose parent, the root, is not in subtree_groups. | ||
| kept_group_ids: set[int] = set() | ||
| for criterion in criteria: | ||
| group_id: int | None = criterion.group_id | ||
| while group_id in subtree_groups and group_id not in kept_group_ids: | ||
| kept_group_ids.add(group_id) | ||
| group_id = subtree_groups[group_id].parent_id | ||
| kept_course_level_groups = [group for group in course_level_groups if group.id in kept_group_ids] | ||
|
|
||
| roots = {group.parent_id: group.parent for group in kept_course_level_groups} | ||
| tags = {group.tag_id: group.tag for group in kept_course_level_groups} | ||
| taxonomies = {} | ||
| for tag in tags.values(): | ||
| # A group's tag is always a competency tag, so its taxonomy is always set. | ||
| assert tag.taxonomy is not None | ||
| taxonomies[tag.taxonomy_id] = tag.taxonomy | ||
| groups = [group for group in roots.values() if group is not None] | ||
| groups.extend(group for group in subtree_groups.values() if group.id in kept_group_ids) | ||
| groups.sort(key=lambda group: (group.ordering, group.id)) |
There was a problem hiding this comment.
Also remove comment and extract this into another function. The comment is unclear (what does "mark" mean and why?) The second line of the comment is also not clear (too vague, and doesn't explain why the walk ends there). But the comment gets removed anyway - the thing is just this needs to be clear enough from the code itself.
And the code is very unreadable. It should clearly express its intention through applying the principles of maintainability.
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True, slots=True) | ||
| class CourseCriteriaCriterion: |
There was a problem hiding this comment.
What's a CourseCriteriaCriterion? That seems like criterion is doubled.
| archived: bool | ||
| cascaded_group_count: int | ||
| cascaded_criteria_count: int | ||
|
|
There was a problem hiding this comment.
Why are these data classes needed? The data model is already in place, what values do these add and why aren't they in the code yet then?
There was a problem hiding this comment.
I'm not saying they are a bad idea, just want to understand why they are introduced in this PR specifically, and trying to understand their intention
|
Note 🤖 Claude Opus 5.5 responding on behalf of jesperhodge Moved to openedx#945, rebased onto upstream |
The learner Progress tab needs to show which competencies a course run covers, which subsections test each one, and what threshold each criterion requires (openedx#733). Nothing could return that for a course run: the existing CBE reads are per-competency authoring endpoints.
This adds
GET api/cbe/v1/courses/<course_key>/criteria/, backed byget_course_competency_criteria(course_key)inopenedx_learning.api. It returns four flat lists that reference each other by id:taxonomies,competencies,groupsandcriteria. Archived and disabled records are left out, and branches left with no criteria are pruned. The route lives in a new learner-only URL module,openedx_learning.learner_urls, so Studio, which mountsopenedx_learning.urls, never serves it. The new permissionopenedx_learning.view_course_competency_criteriaallows any signed-in user by default, and openedx-platform overrides it to active enrollment or course staff in Unicon/openedx-platform#4. The function and endpoint are UNSTABLE until openedx#734 and openedx#839 ship.This PR targets
733-base, which is upstreammainmerged with openedx#940, so only the openedx#733 changes show. It moves upstream once PR 940 merges.How each outcome was proven
Unit and integration tests are in
tests/openedx_learning/applets/cbe/test_course_criteria_api.pyandtest_course_criteria_views.py: 57 tests, written first and failing (51 failures) before the implementation existed. The whole suite passes on SQLite (1862 passed).openedx_learningandopenedx_taggingpass on a throwaway MySQL 8 container (1484 passed). pylint, mypy, pycodestyle, pydocstyle, isort, lint-imports andmakemigrations --checkare clean. Curl results are from this branch installed editable in a Tutor LMS with the platform PR checked out.lib:, usage, version-only, garbage) is 400 with a message naming the course run key, even when the permission would deny: view tests, plus curl (not-a-keygives 400).+branch@draftor+version@key returns the same result as the plain key: view tests, plus curl with identical counts.(ordering, id), the rest byid): API test.delete_or_archive_competency_criterionwith learner status, plus curl.delete_or_archive_competency_criteria_group.pageorpage_size, pages hold whole competencies with every referenced parent, report count, num_pages, current_page, next and previous, and a page past the end is 404: view tests, plus curl.openedx_learning.urlsdoes not; only GET is allowed: view tests, plus curl (Studio returns 404 for the learner route).Decisions made during implementation
.+) instead of the platform'sCOURSE_KEY_PATTERN, so every malformed value reaches the view and gets the 400 the ticket requires, while slash keys still match.competency_id, and only the root hasparent_idnull. Pagination keeps whole competencies together by filtering on it, and a client gets a group's competency without walking up the tree.(ordering, id), and taxonomies, competencies and criteria byid.cbe_learner. Sharing the authoring namespacecbewould make a project that mounts both modules raiseurls.W005.TAXONOMY_MAX_DEPTH, which keeps the query count at 3 whatever the tag depth.parse_plain_course_key()so the new view andCompetencyCriteriaQueryParamsSerializershare it, with no behavior change for the existing endpoints.Follow-ups (not in this PR)
XBLOCK_DELETED, whose platform handler hard-deletes the block's ObjectTags, so its criterion is deleted with them, or the delete is blocked byPROTECTwhen learner status exists. The ticket's "subsection since deleted" scenario therefore holds only for a criterion that survives deletion. The endpoint itself never filters by course structure.Reviewed with sol-loop: 3 rounds with gpt-6.2-sol. Sol verdict: approved
Created with Claude Opus 5.5 in Claude Code; Claude Sonnet sub-agents wrote the tests and the implementation.
🤖 Generated with Claude Code