General plugins (Local): Use template on course creation

Maintained by Picture of Charles FultonCharles Fulton, Picture of Andrew ZitoAndrew Zito
This local plugin allows site administrators to create "template" courses which will be restored into new courses on course creation.
180 sites
24 fans
Current versions available: 4

This local plugin allows site administrators to create "template" courses which will be restored into new courses on course creation. The intended use case is defining common blocks and activities for a given academic term.


The administrator will need to create a "template" course which contains the desired blocks and resources. This course will need a specially-named short name. By default the plugin will search for a course with the short name Template-[TERMCODE], where [TERMCODE] is the matching value for YYYYYY. For example, if a course had the termcode 201610, the module would search for a course with the short name Template-201610.

The administrator will need to define a regular expression for extracting the term code from the course idnumber. This will be used to identify which course template (if any) should be used on creation. For example, if your courses have idnumbers in the format XXXXXX.YYYYYY, where YYYYYY is the termcode, then the regular expression /[0-9]+\.([0-9]+)/ will return YYYYYY.

The plugin listens on the \core\event\course_created event and fires immediately on course creation. Once you've given a course the necessary short name you don't need to do anything further. The plugin will create a backup of the template course and import it into the new course.

You should consider overriding Moodle's default block settings in config.php: $CFG->defaultblocks_override = '';. Otherwise you will get two sets of blocks on course creation. Manually configure the blocks in the template course instead. 


Screenshot #0


Picture of Charles Fulton
Charles Fulton (Lead maintainer)
Please login to view contributors details and/or to contact them

Comments RSS

Show comments
  • Picture of MapIestrip .
    Thu, Dec 28, 2017, 11:10 PM
    I am trying to get this plugin to work, but it has thus far not done anything for me. The plugin has installed correctly, and I can find its details in Local Plugins/Use template on course creation. I have set the term code to /^[^\d]*(\d+)/ and the shortname format to Template-[TERMCODE]

    I have created a template in the Miscelaneous category. It is titled "Certificate Template", has the shortname "Template-1", and includes a forum activity and a standard block.

    I create a new course in a separate category with a title of, let's say "foo" and a shortname of "bar". Its idea is "1-002-02". The regex code should return a "1" in this situation. However, when I view my created course, it does not look like it fits the template I created. Does anyone know why this has happened?

    Best regards,
    Mable Simons
  • Picture of Charles Fulton
    Thu, Dec 28, 2017, 11:16 PM
    That regex seems wrong to me. If you're trying to get 1 from an idnumber of 1-002-02 you'd want to return the first digit, not a later digit. Something like this ought to work: /^(\d)-\d+-\d+/

  • Picture of MapIestrip .
    Fri, Dec 29, 2017, 4:12 PM
    Thanks Charles,
    I tried using your supplied regex, but the plugin still doesn't work. I have also tried other regex codes I found through Google (Stackexchange). I don't believe the regex is the issue here. Any other ideas what may be wrong?

    Mable Simons
  • Picture of Brian Jackson
    Wed, Jan 31, 2018, 7:13 AM
    I'm trying to get this plugin working, but having no luck so far...when I manually create a course that should match the pattern, my template content does not show up in the course.

    Course short names are as follows:
    SUBJ--NUM_YYYY-TT_SEC (ENG--900_2017-SP_01 as an example)

    Current term extraction pattern:
    /([0-9]+\-[A-Z]+)/ (should return 2017-SP for the example course short name above)

    Template name format:

    Template Course Name:

    So if I create a test course with the short name ENG--900_2017-SP_01, it does not seem to be triggering the automatic restore from the template. Any thought as to what might be wrong? Maybe the regex?
  • Picture of Charles Fulton
    Wed, Jan 31, 2018, 9:56 AM
    Hi Brian, the plugin looks at the idnumber and not the shortname of a newly-created course. Please see for specific examples.

  • Picture of Brian Jackson
    Thu, Feb 1, 2018, 7:13 AM
    Ah, my bad - it appears to be working now, thanks for the assistance!
  • Picture of stefan weber
    Mon, Aug 20, 2018, 7:11 PM
    hi charles - is it possible to forego the regex and just use the same template for all courses? our courses are automatically created without any specific ID
  • Picture of Charles Fulton
    Mon, Aug 20, 2018, 10:10 PM
    Hi Stefan, not with the current version of the plugin. Adding an explicit configuration option to allow targeting all new courses (without regard for the idnumber) is a possible enhancement for a future release.
  • Picture of stefan weber
    Wed, Aug 22, 2018, 3:13 PM
    do you know if its possible to create a regex that always returns the same value? like "throw away all numbers and replace with CONST"
  • Picture of Pete Howard
    Tue, Nov 13, 2018, 12:16 AM
    You could try:
    This will (I think) match every ID and return an empty string. You should then give your template course a shortname of "template-".
  • Picture of Scott Krajewski
    Thu, May 2, 2019, 4:06 AM
    I'm trying this regex and the plugin says the Term code value (the regex) is not valid. Any ideas?
  • Picture of Charles Fulton
    Fri, May 3, 2019, 3:26 AM
    Scott: did you wrap your regular expression in slashes (e.g. /my-regex-here/)? The plugin expects them but doesn't add them if they're not present.
  • Picture of Scott Krajewski
    Fri, May 3, 2019, 5:04 AM
    Thanks Charles. This is what I'm using that is coming up as invalid though it works in the regex testing site I linked (without the slashes).
  • Picture of Cory Robbins
    Fri, May 15, 2020, 9:10 PM
    Hi, Charles. Thanks for developing this. I can't seem to get it to work though. I've tried multiple times using custom regex, and I can't get the courses to actually restore the templates. I finally decided to just straight up copy the example courses listed in the wiki, but even then, when using the supplied regex, I don't get the copy of the course. It is just a default course. I've looked at the logs, and it does have a record for the template being backed up when I create the new course, but there isn't a restore record anywhere. Am I missing something?
  • Picture of Cory Robbins
    Mon, May 18, 2020, 10:43 PM
    Just a heads up... It seems that it DOES work if I do a traditional backup and restore of my template course to a new course that begins with the shortname naming convention. I guess there may be an issue with when shortnames are changed? Anyway, it seems to be working.
1 2
Please login to post comments