Line 115 - 121 and line 125 - 131 can share code with different inputs.

Moreover, mods/_core/courses/admin/default_mods.php and mods/_core/modules/default_mods.php seem using the same logic and similar code, which can be extracted and shared as well.