Moodle - PHP cli script to mass import template course data to all other courses

With the great UI of the Moodle (I'm using version 2.7) you can import data (blocks, activities, filters, questionbank,...) from a template course into a course. But if you want to apply all the template course data to many courses, you have to do manually for every single course. The question is, what if I can automate that process?

To solve this problem, I decided to write a php command line script to do the cloning without the web interface. So, the first place I had to look at is the /<dir root>/backup/import.php file. Then, I made some hacks and created my own script at /<dir root>/mycustomscriptfolder/massimport.php:



Usage:

+ To clone the template course data to a single course:

$ php massimport.php <template course id> <course id>

Example: to apply template course with id = 3 data to course id = 145:

$ php massimport.php 3 145

+ To clone the template course data to all other courses in your moodle:

$ php massimport.php <template course id>

Example: to apply template course with id = 3 to all other courses:

$ php massimport.php 3


References:
[0] http://docs.moodle.org/dev/Backup_2.0_general_architecture
[1] https://moodle.org/mod/forum/discuss.php?d=194308#p1137714