Skip to content

Fix auto-distribution to check student role via ad-hoc task#72

Draft
Copilot wants to merge 13 commits intoexperimentalfrom
copilot/fix-category-distribution-navigation
Draft

Fix auto-distribution to check student role via ad-hoc task#72
Copilot wants to merge 13 commits intoexperimentalfrom
copilot/fix-category-distribution-navigation

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

Auto-distribution was triggered on any enrollment regardless of role, unlike manual distribution which correctly filters by student role. Additionally, enrollment isn't complete when the observer fires, causing the role check to fail.

Changes

Ad-hoc task for deferred execution (classes/task/distribute_to_user_task.php)

  • Queued by observers, executes after enrollment completes
  • Validates task data and checks student enrollment before distributing

Enrollment check function (lib/lib.php)

function block_exaport_is_enrolled_as_student($userid, $courseid) {
    // Checks: enrolled in THIS course + has "student" role shortname
    // Caches role ID, uses Moodle's is_enrolled() API
}

Observer updates (classes/observer.php, db/events.php)

  • Queue tasks instead of direct execution
  • Handle both user_enrolment_created and user_enrolment_updated events for role changes

Distribution now only occurs for users enrolled with "student" role shortname in the specific course, matching manual distribution behavior.

Original prompt

Repository: gtn/exaport (repo ID: 4559659)
Base branch for PR: experimental

Create a pull request implementing the following changes.

1) UX: Fix navigation + page format for category_distribution.php

Problem

category_distribution.php is reachable from the block but:

  • It is missing from the navigation.
  • It uses the wrong page format.
  • It should behave like importexport.php (full Moodle navigation, consistent layout, etc.).

Requirements

  • Update category_distribution.php to use the same/appropriate page setup as importexport.php:
    • Set correct $PAGE properties (URL, context, layout/page layout, title/heading, navbar items).
    • Ensure full navigation is present.
  • Add appropriate navigation entry so users can reach the page through navigation (consistent with how importexport.php is linked).
  • Ensure access control/capabilities match existing patterns for similar pages.

2) Backup/Restore: Implement minimal backup & restore for three tables

Goal

There is currently no backup/restore implementation. Implement minimal backup and restore support for:

  • Settings stored in block_exaport_templ_dist
  • Views stored in block_exaport_view_templ
  • Categories stored in block_exaport_course_templ

All three tables include courseid. The intent is:

  • Admin/teacher can create a “template course” with configured distribution templates.
  • When the course is copied (via backup/restore), these records should be included so the copied course retains the templates.
  • Autodistribute already exists and should work when enabled in the template course.

References

Requirements

  • Add standard Moodle plugin backup/restore structure for a block plugin:
    • backup/moodle2/backup_exaport_block_task.class.php
    • backup/moodle2/backup_exaport_block_stepslib.class.php
    • backup/moodle2/restore_exaport_block_task.class.php
    • backup/moodle2/restore_exaport_block_stepslib.class.php
    • backup/moodle2/restore_exaport_block_structure_step.class.php (or appropriate step classes as needed)
    • Any required backup/moodle2/* files (e.g., restore_exaport_block_plugin.class.php if needed)
  • Ensure backup includes records from the three tables scoped to the course being backed up.
  • Ensure restore writes records into the corresponding tables for the target course.
  • Handle ID mapping appropriately on restore (new IDs); if the tables reference each other or other core tables, define mappings.
  • Start small: do not attempt to back up unrelated data.
  • Add automated tests if this repository has an existing test framework/pattern for backup/restore; otherwise include at least manual testing steps in PR description/comments.

Acceptance Criteria / Manual test plan

  • In a source course, create records in the three tables for that course.
  • Perform course backup and restore into a new course.
  • Verify the restored course has equivalent records in the three tables (with new courseid = restored course id).
  • Verify category_distribution.php now shows proper navigation and consistent layout with importexport.php.

Notes

  • Keep code consistent with existing coding style in repo.
  • Ensure compatibility with Moodle 5.0 APIs as referenced.

This pull request was created from Copilot chat.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 5 commits February 25, 2026 15:35
Co-authored-by: richardwolfmayr <24298463+richardwolfmayr@users.noreply.github.com>
Co-authored-by: richardwolfmayr <24298463+richardwolfmayr@users.noreply.github.com>
Co-authored-by: richardwolfmayr <24298463+richardwolfmayr@users.noreply.github.com>
Co-authored-by: richardwolfmayr <24298463+richardwolfmayr@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix navigation and page format for category_distribution.php Implement backup/restore and fix navigation for category distribution Feb 25, 2026
Copilot AI and others added 2 commits February 27, 2026 10:59
Co-authored-by: richardwolfmayr <24298463+richardwolfmayr@users.noreply.github.com>
Copilot AI changed the title Implement backup/restore and fix navigation for category distribution Fix restore XML paths to match backup block structure Feb 27, 2026
Copilot AI changed the title Fix restore XML paths to match backup block structure Fix restore path elements to match Moodle block backup XML structure Feb 27, 2026
Co-authored-by: richardwolfmayr <24298463+richardwolfmayr@users.noreply.github.com>
Copilot AI changed the title Fix restore path elements to match Moodle block backup XML structure Fix role filtering in auto-distribution observers Feb 27, 2026
Copilot AI and others added 2 commits February 27, 2026 13:47
Co-authored-by: richardwolfmayr <24298463+richardwolfmayr@users.noreply.github.com>
Co-authored-by: richardwolfmayr <24298463+richardwolfmayr@users.noreply.github.com>
Copilot AI changed the title Fix role filtering in auto-distribution observers Fix auto-distribution to check student role via ad-hoc task Feb 27, 2026
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.

2 participants