function test_create_group()
 {
     $this->assertTrue($this->groupid = groups_create_group($this->courseid));
     $this->assertTrue(groups_group_exists($this->groupid));
     $this->assertTrue(groups_group_belongs_to_course($this->groupid, $this->courseid));
     $this->assertTrue($groupids = groups_get_groups($this->courseid));
     //array...
     $this->assertTrue($groupinfo = groups_set_default_group_settings());
     $groupinfo->name = 'Group ' . $this->getLabel();
     //'Temporary Group Name'
     $this->assertTrue(groups_set_group_settings($this->groupid, $groupinfo));
     $this->assertTrue($groupinfo->name == groups_get_group_name($this->groupid));
     $this->assertTrue($this->courseid == groups_get_course($this->groupid));
 }
Example #2
0
/**
 * Stores a current group in the user's session, if not already present.
 * 
 * Current group applies to all modules in the current course that share 
 * a grouping (or use no grouping).
 * 
 * This function allows the user to change group if they want, but it
 * checks they have permissions to access the new group and calls error()
 * otherwise.
 * @param object $cm Course-module object
 * @param int $groupmode Group mode
 * @param int $changegroup If specified, user wants to change to this group
 * @return Group ID
 */
function groups_m_get_and_set_current($cm, $groupmode, $changegroup = -1)
{
    // Check group mode is turned on
    if (!$groupmode) {
        return false;
    }
    // Get current group and return it if no change requested
    $currentgroupid = groups_m_get_current($cm);
    if ($changegroup < 0) {
        return $currentgroupid;
    }
    // Check 'all groups' access
    $context = get_context_instance(CONTEXT_COURSE, $cm->course);
    $allgroups = has_capability('moodle/site:accessallgroups', $context);
    // 0 is a special case for 'all groups'.
    if ($changegroup == 0) {
        if ($groupmode != VISIBLEGROUPS && !$allgroups) {
            error('You do not have access to view all groups');
        }
    } else {
        // Normal group specified
        // Check group is in the course...
        if (!groups_group_belongs_to_course($changegroup, $cm->course)) {
            error('Requested group is not in this course.');
        }
        // ...AND in the right grouping if required...
        if ($cm->groupingid && !groups_belongs_to_grouping($changegroup, $cm->groupingid)) {
            print_object($cm);
            print_object(groups_get_group($changegroup));
            error('Requested group is not in this grouping.');
        }
        // ...AND user has access to all groups, or it's in visible groups mode, or
        // user is a member.
        if (!$allgroups && $groupmode != VISIBLEGROUPS && !groups_is_member($changegroup)) {
        }
    }
    // OK, now remember this group in session
    global $SESSION;
    $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid] = $changegroup;
    return $changegroup;
}
Example #3
0
function backup_copy_group_files($preferences)
{
    global $CFG;
    $status = true;
    //First we check if "group_files" exists and create it as necessary
    //in temp/backup/$backup_code  dir
    $status = check_and_create_group_files_dir($preferences->backup_unique_code);
    //Now iterate over directories under "groups" to check if that user must be
    //copied to backup
    $rootdir = $CFG->dataroot . '/groups';
    //Check if directory exists
    if (is_dir($rootdir)) {
        $list = list_directories($rootdir);
        if ($list) {
            //Iterate
            foreach ($list as $dir) {
                //Look for dir like group in groups table
                $data = groups_group_belongs_to_course($dir, $preferences->backup_course);
                //TODO:check. get_record ('groups', 'courseid', $preferences->backup_course,'id',$dir);
                //If exists, copy it
                if ($data) {
                    $status = backup_copy_file($rootdir . "/" . $dir, $CFG->dataroot . "/temp/backup/" . $preferences->backup_unique_code . "/group_files/" . $dir);
                }
            }
        }
    }
    return $status;
}
Example #4
0
    $groupingid = GROUP_NOT_IN_GROUPING;
    $newgrouping = GROUP_NOT_IN_GROUPING;
}
/// Course must be valid
if (!($course = get_record('course', 'id', $courseid))) {
    error('Course ID was incorrect');
}
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_capability('moodle/course:managegroups', $context);
$group = false;
if ($id) {
    if (!($group = get_record('groups', 'id', $id))) {
        error('Group ID was incorrect');
    }
    $group->description = clean_text($group->description);
    if (!groups_group_belongs_to_course($group->id, $course->id)) {
        error('Group not from this course.');
    }
    $groupings = groups_get_groupings_for_group($id);
    if (empty($groupings)) {
        $groupingid = -1;
    } else {
        if (!isset($groupings[$groupingid])) {
            $groupingid = $groupings[0];
        }
    }
}
if ($groupingid != GROUP_NOT_IN_GROUPING and !groups_db_grouping_belongs_to_course($groupingid, $course->id)) {
    error('Grouping not from this course.');
}
if ($newgrouping != GROUP_NOT_IN_GROUPING and !groups_db_grouping_belongs_to_course($newgrouping, $course->id)) {
Example #5
0
/**
 * A combination function to make it easier for modules
 * to set up groups.
 *
 * It will use a given "groupid" parameter and try to use
 * that to reset the current group for the user.
 *
 * @uses VISIBLEGROUPS
 * @param course $course A {@link $COURSE} object
 * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
 * @param int $groupid Will try to use this optional parameter to
 *            reset the current group for the user
 * @return int|false Returns the current group id or false if error.
 */
function get_and_set_current_group($course, $groupmode, $groupid = -1)
{
    //TODO: ?? groups_has_permission($userid, $groupingid, $courseid, $groupid, $permissiontype);
    // Sets to the specified group, provided the current user has view permission
    if (!$groupmode) {
        // Groups don't even apply
        return false;
    }
    $currentgroupid = get_current_group($course->id);
    if ($groupid < 0) {
        // No change was specified
        return $currentgroupid;
    }
    $context = get_context_instance(CONTEXT_COURSE, $course->id);
    if ($groupid) {
        // Try to change the current group to this groupid
        if (groups_group_belongs_to_course($groupid, $course->id)) {
            // Exists  TODO:check.
            if (has_capability('moodle/site:accessallgroups', $context)) {
                // Sets current default group
                $currentgroupid = set_current_group($course->id, $groupid);
            } elseif ($groupmode == VISIBLEGROUPS) {
                // All groups are visible
                //if (ismember($group->id)){
                $currentgroupid = set_current_group($course->id, $groupid);
                //set this since he might post
                /*)}else {
                  $currentgroupid = $group->id;*/
            } elseif ($groupmode == SEPARATEGROUPS) {
                // student in separate groups switching
                if (ismember($groupid)) {
                    //check if is a member
                    $currentgroupid = set_current_group($course->id, $groupid);
                    //might need to set_current_group?
                } else {
                    notify('You do not belong to this group! (' . $groupid . ')', 'error');
                }
            }
        }
    } else {
        // When groupid = 0 it means show ALL groups
        // this is changed, non editting teacher needs access to group 0 as well,
        // for viewing work in visible groups (need to set current group for multiple pages)
        if (has_capability('moodle/site:accessallgroups', $context)) {
            // Sets current default group
            $currentgroupid = set_current_group($course->id, 0);
        } else {
            if ($groupmode == VISIBLEGROUPS) {
                // All groups are visible
                $currentgroupid = set_current_group($course->id, 0);
            }
        }
    }
    return $currentgroupid;
}