MDL-45958 courselib: Refactor module duplication code to remove duplication