protected function find_modulecompetencies() {
        global $DB;
        if (empty($this->coursescompetencies)) {
            return;
        }

        $this->progress->start_progress(get_string('findingmodulecompetencies', 'tool_lpmigrate'), 3);
        $this->progress->increment_progress();

        // Limit to mapped objects.
        list($inidsql, $inidparams) = $DB->get_in_or_equal($this->fromids, SQL_PARAMS_NAMED);

        // Limit to known courses.
        list($incoursesql, $incourseparams) = $DB->get_in_or_equal(array_keys($this->coursescompetencies), SQL_PARAMS_NAMED);
        $sql = "SELECT mc.id, cm.course AS courseid, mc.cmid, mc.competencyid, mc.ruleoutcome
                  FROM {" . course_module_competency::TABLE . "} mc
                  JOIN {course_modules} cm
                    ON cm.id = mc.cmid
                   AND cm.course $incoursesql
                  JOIN {" . competency::TABLE . "} c
                    ON c.id = mc.competencyid
                 WHERE c.id $inidsql
              ORDER BY mc.sortorder, mc.id";
        $params = $inidparams + $incourseparams;

        $records = $DB->get_recordset_sql($sql, $params);
        $this->progress->increment_progress();
        $cmsfound = array();

        $cmcs = array();
        $cmcsoutcomes = array();
        foreach ($records as $record) {
            if (!isset($cmcs[$record->courseid])) {
                $cmcs[$record->courseid] = array();
                $cmcsoutcomes[$record->courseid] = array();
            }
            if (!isset($cmcs[$record->courseid][$record->cmid])) {
                $cmcs[$record->courseid][$record->cmid] = array();
                $cmcsoutcomes[$record->courseid][$record->cmid] = array();
            }
            $cmcs[$record->courseid][$record->cmid][] = $record->competencyid;
            $cmcsoutcomes[$record->courseid][$record->cmid][$record->competencyid] = $record->ruleoutcome;
            $cmsfound[$record->cmid] = true;
        }
        $records->close();

        $this->modulecompetencies = $cmcs;
        $this->modulecompetenciesoutcomes = $cmcsoutcomes;
        $this->cmsfound = $cmsfound;

        $this->progress->increment_progress();
        $this->progress->end_progress();
    }