4

tl;dr; trying to get a course module instance via a course module through mysql or the official moodle api. My solution in the end seems rather scrappy though, so i would like some advice.

Hi Moodle people

I am trying to retrieve the corresponding course module instance for each row in mdl_course_modules, a instance in this context would be a single row from mdl_quiz, mdl_wiki or some other course_modules instance.

I have tried a few things, the one i expected to work was:

function get_all_course_modules($course_id) {
   global $DB;
   $course = $DB->get_record('course', array('id' => $course_id));
   $mod_info = get_fast_modinfo($course);
}

I wasn't able to get the course modules instance from $mod_info .

So i tried to do some custom mysql instead,

SELECT course.id, course.fullname, cm.course, cm.section, m.name 
 FROM mdl_course AS course 
 JOIN mdl_course_modules AS cm ON cm.course = course.id
 JOIN mdl_modules AS m ON m.id = cm.module 
 JOIN CONCAT('mdl_', m.name) AS module_type ON ...somethingsomething
 WHERE course.id = 2;

The sql isn't quite finished, i stumbled on the fact that i dont know all of the different course modules beforehand, that means that i have to use the name of the course_module dynamically to select which instance table i want to join on, or just LEFT JOIN on all of the possible course modules instance tables, but that would take forever and would stop working if someone put in a new course module instance.

Right now im doing the following:

 $result = array();
$course_mods = get_course_mods($course_id);

if($course_mods) {
    foreach($course_mods as $course_mod) {
        $DB->get_records_sql('some sql that selects the instance');
    }
}

return $result;

This last piece of code would eventually work, i would be able to able to dynamically create the query from the $course_mod variables such as $course_mod->instance, $course_mod->mod_name etc.

But i think this seems tough. Have any of you guys suceeded in getting the course_module instance in a easier way??

Thanks!!

DenLilleMand
  • 3,732
  • 5
  • 25
  • 34
  • Maybe it would help if you explained exactly what you were trying to achieve. Most of the fields in each activity instance are only relevant to the internals of the activity itself - the generic fields that are relevant to all types are available via get_fast_modinfo($courseid)->get_cms() – davosmith Aug 11 '16 at 15:15
  • The start date of the course module is hidden inside of the activity :-) – DenLilleMand Aug 11 '16 at 15:18
  • 1
    The start date is only relevant to specific activity types, so you will need custom code to deal with each different activity. get_fast_modinfo will give you a list of module types used in the course, you can then filter it by the ones you want to get start dates for, then build up your SQL statement dynamically with all the module types you want to support (using COALESCE to gather the time data from the different tables) – davosmith Aug 11 '16 at 21:25
  • 1
    Yeah, that is exactly what i am doing right now if you look at the end of my post, running through the $course_mods in a foreach and dynamically retrieving the instances, which works fine... i just feel like its kind of dirty, but it might explain it if im not even suppose to get the instance, like ever, then i would have to keep the data in the course format options table for custom data, but that would create redundancy in the database. Anyway ... im just going for this. Thanks for caring :) atleast i know now that someone else would do the same. – DenLilleMand Aug 12 '16 at 07:47

1 Answers1

2

This is the function i ended up using, to retrieve course module instances, its not so pretty, but whatever.

function get_all_course_modules($course_id) {
global $DB;
$course_mods = get_course_mods($course_id);
$result = array();
if($course_mods) {
    foreach($course_mods as $course_mod) {
        $course_mod->course_module_instance = $DB->get_record($course_mod->modname, array('id' =>$course_mod->instance ));
        $result[$course_mod->id] = $course_mod;
    }
}
return $result;
}
DenLilleMand
  • 3,732
  • 5
  • 25
  • 34