A Moodle block plugin that lets you use a template course or template modules as a library, and copy modules (activities/resources) from that template into your current course.
- Features
- Requirements & Compatibility
- Installation
- Configuration
- Usage
- Developer Notes
- Behat & Testing
- Limitations & Known Issues
- License & Credits
- Designate a template category and template course(s) to act as libraries of modules.
- Display modules of a template course grouped by section.
- Copy selected modules into a target course with UI (choose target section).
- Uses Moodle’s backup/restore API for copying activities.
- JavaScript / AMD front-end to dynamically load module lists.
- Mustache templates for UI rendering.
- Moodle 4.x (tested with Moodle 4.5+)
- PHP version compatible with your Moodle instance
- The user performing copy operations must have backup/restore capabilities (see Developer Notes)
- Clone or download this block into
blocks/modulelibraryin your Moodle code tree. - Run the Moodle upgrade (Site administration → Notifications).
- Ensure file permissions are correct (especially for backups to
moodledata). - Configure the block (see next section).
After installation:
- Go to Site administration → Plugins → Blocks → Module Library
- Set the Template category to a category with your template courses.
- Optionally configure other settings (if needed).
Once configured, you can add the block to courses and copy modules from templates.
- Add the Module Library block to a course where you want to import modules.
- In the block, select a template course from the dropdown.
- Sections and modules load via AJAX.
- For each module, press Copy.
- A form appears to select the target section and confirm copy.
- The module is backed up and restored into your course, and placed in the section you chose.
- Copying modules uses Moodle’s backup & restore APIs, requiring capabilities like:
moodle/backup:backupactivitymoodle/restore:restoreactivitymoodle/course:manageactivities
- If a user is only a teacher but doesn’t have these capabilities in relevant contexts, copy will fail with
error/backup_user_missing_capability. - In testing (e.g. Behat), you may need to temporarily assign capabilities or impersonate an admin user to allow the operation to succeed.
- The block sets
keeptempdirectoriesonbackup = truetemporarily so backup folders persist until cleanup. - After restore, it cleans up the backup path if
keeptempdirectoriesonbackupwas originally false. - Always restore
CFG->keeptempdirectoriesonbackupto its original value.
- Uses Mustache templates (
block_content.mustache,modules.mustache,copy_form.mustache). - Ensure the first
<option>in anyrequired<select>hasvalue=""(placeholder) to satisfy HTML validation. - Avoid deprecated Bootstrap 4 classes (e.g.
form-group,font-weight-bold) — use Bootstrap 5 equivalents (mb-3,fw-bold).
- The block’s JS module (AMD) loads module lists and copy forms via AJAX calls to external functions.
- Must handle loading, errors, and dynamic rendering properly.
To write Behat tests for this block:
- Use a
behat_hooks.phpinblocks/modulelibrary/tests/behat/to set up the template category, template courses, and configuration automatically. - If the copy fails with
backup_user_missing_capability, ensure the Behat test user is site admin or has required capabilities. - Use Behat steps like:
And I select "Template Course Name" from the "Select template course" singleselect When I click on "button[data-name='Test Quiz']" "css_element" And I select "Section 1" from the "target-section" singleselect And I press "Confirm copy"