Skip to content

feat: GET a course run's competency criteria for the learner Progress tab - #4

Closed
jesperhodge wants to merge 1 commit into
733-basefrom
733-course-criteria
Closed

jesperhodge wants to merge 1 commit into
733-basefrom
733-course-criteria

Conversation

@jesperhodge

@jesperhodge jesperhodge commented Nov 18, 2026 •

Copy link
Copy Markdown

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 by get_course_competency_criteria(course_key) in openedx_learning.api. It returns four flat lists that reference each other by id: taxonomies, competencies, groups and criteria. 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 mounts openedx_learning.urls, never serves it. The new permission openedx_learning.view_course_competency_criteria allows 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 upstream main merged 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.py and test_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_learning and openedx_tagging pass on a throwaway MySQL 8 container (1484 passed). pylint, mypy, pycodestyle, pydocstyle, isort, lint-imports and makemigrations --check are clean. Curl results are from this branch installed editable in a Tutor LMS with the platform PR checked out.

  • Signed out is 401 with no data, even with a malformed key: view tests, plus curl against the LMS.
  • Any signed-in user gets the full result under openedx-core's default permission: view test.
  • The permission is checked by name against the course key with branch and version removed: view test with a recording deny rule, which also gets 403 with no data.
  • A malformed key (library, 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-key gives 400).
  • Course-v1, old slash, and CCX keys are accepted; a well-formed key with no course run, or a course run with no criteria, returns four empty lists: view and API tests.
  • A +branch@draft or +version@ key returns the same result as the plain key: view tests, plus curl with identical counts.
  • The result reassembles from taxonomy down to criterion with the fields the Progress tab displays: API test asserting every field, plus curl showing the real payload.
  • Only course run X's branch of a shared root is returned, and a competency used only in course run Y is absent: API test.
  • Two competency taxonomies each appear with their own competencies, and tags from a non-competency taxonomy on the same subsections never appear: API test, plus curl.
  • Lineage runs from the taxonomy top down to the tag ("Problem Solving", "Problem Definition", "Root Cause Analysis"; "Communication" alone): API test, plus curl.
  • The system default profile (0.75) and a criterion override (0.82) come back as rule type Grade, op gte, with that value and scale percent: API tests, plus curl showing the 0.82 override.
  • Repeated requests return everything in the same order (groups by (ordering, id), the rest by id): API test.
  • A criterion archived through [BE] Delete Competency Criterion openedx/openedx-core#674 is left out while its sibling stays: API test using delete_or_archive_competency_criterion with learner status, plus curl.
  • A bottom-tier or course-level group archived through [BE] Build endpoint for removing a Competency Criteria Group openedx/openedx-core#675 takes its whole subtree out, with no dangling parent references; a competency left empty disappears: parametrized API test using delete_or_archive_competency_criteria_group.
  • An archived tag, an archived taxonomy, or a disabled taxonomy hides that competency while the other taxonomy's competency stays: parametrized API test.
  • A course whose criteria are all archived, or whose only taxonomy is disabled, returns four empty lists: parametrized API test.
  • Unit-test-only states: an archived parent or grandparent tag, an archived ObjectTag, an archived group flag without the cascade (including an archived root), a bottom-tier group whose only criterion is archived, criteria on a course-level group or deeper group, an archived rule profile, and an unknown rule type returned verbatim: one API test each.
  • The query count is fixed: 1 for a course without criteria, and 3 for one criterion on a top-level tag, one on a nested tag, and a course with 10 competencies across several taxonomies: API tests. The view runs the same number of queries with and without pagination and for any page size: view test.
  • Pagination is off without page or page_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.
  • The learner module serves the route and openedx_learning.urls does not; only GET is allowed: view tests, plus curl (Studio returns 404 for the learner route).

Decisions made during implementation

  • The course key goes in the URL path. The pattern is broad (.+) instead of the platform's COURSE_KEY_PATTERN, so every malformed value reaches the view and gets the 400 the ticket requires, while slash keys still match.
  • Every group carries competency_id, and only the root has parent_id null. Pagination keeps whole competencies together by filtering on it, and a client gets a group's competency without walking up the tree.
  • Array order: groups by (ordering, id), and taxonomies, competencies and criteria by id.
  • The learner URL namespace is cbe_learner. Sharing the authoring namespace cbe would make a project that mounts both modules raise urls.W005.
  • Archived ancestor tags are excluded through a fixed chain of joins built from TAXONOMY_MAX_DEPTH, which keeps the query count at 3 whatever the tag depth.
  • Bottom-tier groups are fetched by the course's competency tags and walked down in Python, which supports any depth with a fixed query count. The cost is that rows for other course runs' groups under the same competencies are read and discarded.
  • The existing course key parsing moved into parse_plain_course_key() so the new view and CompetencyCriteriaQueryParamsSerializer share it, with no behavior change for the existing endpoints.

Follow-ups (not in this PR)

  • Deleting a subsection in Studio fires XBLOCK_DELETED, whose platform handler hard-deletes the block's ObjectTags, so its criterion is deleted with them, or the delete is blocked by PROTECT when 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

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>
Comment on lines +251 to +264
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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise PermissionDenied()
raise PermissionDenied()

Comment on lines +289 to +303
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(),
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract to a function _get_paginated_response

})


class CourseCompetencyCriteriaView(generics.GenericAPIView):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +326 to +348
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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +375 to +377
# 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment and extract code into a highly readable function that expresses what it does through good naming.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +352 to +355
``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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``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``

Comment on lines +403 to +422
# 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))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's a CourseCriteriaCriterion? That seems like criterion is doubled.

archived: bool
cascaded_group_count: int
cascaded_criteria_count: int

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jesperhodge jesperhodge left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jesperhodge

Copy link
Copy Markdown
Author

Note

🤖 Claude Opus 5.5 responding on behalf of jesperhodge

Moved to openedx#945, rebased onto upstream main. It does not need openedx#940. The description, the review comments, and the AI review files are copied there, and the fixes will land there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant