public static function get_least_proficient_competencies_for_course($courseid, $skip = 0, $limit = 0) {
        global $DB;

        $fields = competency::get_sql_fields('c', 'c_');
        $params = array('courseid' => $courseid);
        $sql = 'SELECT ' . $fields . '
                  FROM (SELECT cc.competencyid, SUM(COALESCE(ucc.proficiency, 0)) AS timesproficient
                          FROM {' . course_competency::TABLE . '} cc
                     LEFT JOIN {' . self::TABLE . '} ucc
                                ON ucc.competencyid = cc.competencyid
                               AND ucc.courseid = cc.courseid
                         WHERE cc.courseid = :courseid
                      GROUP BY cc.competencyid
                     ) p
                  JOIN {' . competency::TABLE . '} c
                    ON c.id = p.competencyid
              ORDER BY p.timesproficient ASC, c.id DESC';

        $results = $DB->get_records_sql($sql, $params, $skip, $limit);

        $comps = array();
        foreach ($results as $r) {
            $c = competency::extract_record($r, 'c_');
            $comps[] = new competency(0, $c);
        }
        return $comps;
    }