/**
 * Get all FN Site Groups. If a course is specified, and the course is using the FN Site
 * Groups enrolment plug-in, then return the groups currently in use in the course.
 *
 * @param int    $courseid   The id of the requested course
 * @param int    $userid     The id of the requested user or all.
 * @param int    $groupingid The id of the requested grouping or all.
 * @param string $fields     The fields to return or all.
 * @return array
 */
function fn_sg_get_all_groups($courseid = SITEID, $userid = 0, $groupingid = 0, $fields = 'g.*')
{
    global $CFG;
    if (!($course = get_record('course', 'id', $courseid, null, null, null, null, 'id,enrol'))) {
        return false;
    }
    if ($courseid != SITEID && fn_sg_course_uses_sgenrol($course)) {
        /// Get site groups used in course.
        $agroups = false;
    } else {
        $agroups = groups_get_all_groups(SITEID, $userid, $groupingid, $fields);
    }
    if (!is_array($agroups)) {
        $agroups = array();
    }
    if (!($groupings = groups_get_all_groupings(SITEID))) {
        $groupings = array();
    }
    $groups = array();
    foreach ($groupings as $groupingid => $grouping) {
        $groups['g_' . $groupingid] = $grouping;
        if ($ggroups = groups_get_all_groups(SITEID, $userid, $groupingid, $fields)) {
            foreach ($ggroups as $ggroupid => $ggroup) {
                $ggroup->name = ' - ' . $ggroup->name;
                $groups[$ggroupid] = $ggroup;
                unset($agroups[$ggroupid]);
            }
        }
    }
    $nogrouping = new Object();
    $nogrouping->id = 0;
    $nogrouping->name = 'NOT IN GROUPING';
    foreach ($agroups as $agroup) {
        $agroup->name = ' - ' . $agroup->name;
    }
    if (!empty($agroups)) {
        $groups = $groups + array('g_0' => $nogrouping) + $agroups;
    }
    return $groups;
}
/**
 * This function is called when a 'group_created' event is triggered. It will then create a course group
 * for the corresponding site group in each course. This option must be enabled for this to happen.
 * If the site group enrolment scheme is in place for a course, it should not create the group.
 *
 *  @param object $eventdata The course object created.
 *  @return boolean Always return true so that the event gets cleared.
 *
 */
function fn_group_created_handler($eventdata)
{
    global $CFG;
    require_once "{$CFG->dirroot}/enrol/enrol.class.php";
    /// If we aren't using site groups, do nothing.
    if (empty($CFG->block_fn_site_groups_enabled)) {
        return true;
    }
    /// Only care about site groups.
    if ($eventdata->courseid != SITEID) {
        return true;
    }
    /// If no courses, nothing to do.
    if (!($courses = get_courses(null, "c.id", $fields = "c.id,c.enrol"))) {
        return true;
    }
    foreach ($courses as $course) {
        if ($course->id != SITEID) {
            /// We don't add course groups to courses using the site group enrolment plug-in.
            if (fn_sg_course_uses_sgenrol($course)) {
                continue;
            }
            $grpdata = new Object();
            $grpdata->name = $eventdata->name;
            $grpdata->description = $eventdata->description;
            $grpdata->enrolmentkey = $eventdata->enrolmentkey;
            $grpdata->hidepicture = $eventdata->hidepicture;
            $grpdata->courseid = $course->id;
            $grpdata->picture = $eventdata->picture;
            /// This will generate another group create event. Will that be a problem?
            fn_sg_create_group($eventdata->id, $grpdata);
        }
    }
    return true;
}