protected function find_coursescompetencies() {
        global $DB;
        $this->progress->start_progress(get_string('findingcoursecompetencies', 'tool_lpmigrate'), 3);
        $this->progress->increment_progress();

        $joins = array();
        $conditions = array();
        $params = array();

        // Limit to mapped objects.
        list($insql, $inparams) = $DB->get_in_or_equal($this->fromids, SQL_PARAMS_NAMED);
        $conditions[] = "c.id $insql";
        $params += $inparams;

        // Restriction on course IDs.
        if (!empty($this->allowedcourses)) {
            list($insql, $inparams) = $DB->get_in_or_equal($this->allowedcourses, SQL_PARAMS_NAMED);
            $conditions[] = "cc.courseid $insql";
            $params += $inparams;
        }
        if (!empty($this->disallowedcourses)) {
            list($insql, $inparams) = $DB->get_in_or_equal($this->disallowedcourses, SQL_PARAMS_NAMED, 'param', false);
            $conditions[] = "cc.courseid $insql";
            $params += $inparams;
        }

        // Restriction on start date.
        if (!empty($this->coursestartdatefrom)) {
            $joins[] = "JOIN {course} co
                          ON co.id = cc.courseid";
            $conditions[] = "co.startdate >= :startdate";
            $params += array('startdate' => $this->coursestartdatefrom);
        }

        // Find the courses.
        $ccs = array();
        $ccsoutcomes = array();
        $joins = implode(' ', $joins);
        $conditions = implode(' AND ', $conditions);
        $sql = "SELECT cc.id, cc.courseid, cc.competencyid, cc.ruleoutcome
                  FROM {" . course_competency::TABLE . "} cc
                  JOIN {" . competency::TABLE . "} c
                    ON c.id = cc.competencyid
                       $joins
                 WHERE $conditions
              ORDER BY cc.sortorder, cc.id";

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

        foreach ($records as $record) {
            if (!isset($ccs[$record->courseid])) {
                $ccs[$record->courseid] = array();
                $ccsoutcomes[$record->courseid] = array();
            }
            $ccs[$record->courseid][] = $record->competencyid;
            $ccsoutcomes[$record->courseid][$record->competencyid] = $record->ruleoutcome;
        }
        $records->close();

        $this->coursescompetencies = $ccs;
        $this->coursescompetenciesoutcomes = $ccsoutcomes;
        $this->coursesfound = $ccs;

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