function validation($data, $files)
 {
     global $CFG, $COURSE;
     $errors = parent::validation($data, $files);
     if ($data['allocateby'] != 'no') {
         if (!($users = groups_get_potential_members($data['courseid'], $data['roleid']))) {
             $errors['roleid'] = get_string('nousersinrole', 'group');
         }
         /// Check the number entered is sane
         if ($data['groupby'] == 'groups') {
             $usercnt = count($users);
             if ($data['number'] > $usercnt || $data['number'] < 1) {
                 $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
             }
         }
     }
     //try to detect group name duplicates
     $name = groups_parse_name(stripslashes(trim($data['namingscheme'])), 0);
     if (groups_get_group_by_name($COURSE->id, $name)) {
         $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
     }
     // check grouping name duplicates
     if (isset($data['grouping']) && $data['grouping'] == '-1') {
         $name = trim(stripslashes($data['groupingname']));
         if (empty($name)) {
             $errors['groupingname'] = get_string('required');
         } else {
             if (groups_get_grouping_by_name($COURSE->id, $name)) {
                 $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
             }
         }
     }
     /// Check the naming scheme
     $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
     if ($matchcnt != 1) {
         $errors['namingscheme'] = get_string('badnamingscheme', 'group');
     }
     return $errors;
 }
Beispiel #2
0
    /**
     * Performs validation of the form information
     *
     * @param array $data
     * @param array $files
     * @return array $errors An array of $errors
     */
    function validation($data, $files) {
        global $CFG, $COURSE;
        $errors = parent::validation($data, $files);

        if ($data['allocateby'] != 'no') {
            $source = array();
            if ($data['cohortid']) {
                $source['cohortid'] = $data['cohortid'];
            }
            if ($data['groupingid']) {
                $source['groupingid'] = $data['groupingid'];
            }
            if ($data['groupid']) {
                $source['groupid'] = $data['groupid'];
            }
            if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $source)) {
                $errors['roleid'] = get_string('nousersinrole', 'group');
            }

           /// Check the number entered is sane
            if ($data['groupby'] == 'groups') {
                $usercnt = count($users);

                if ($data['number'] > $usercnt || $data['number'] < 1) {
                    $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
                }
            }
        }

        //try to detect group name duplicates
        $name = groups_parse_name(trim($data['namingscheme']), 0);
        if (groups_get_group_by_name($COURSE->id, $name)) {
            $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
        }

        // check grouping name duplicates
        if ( isset($data['grouping']) && $data['grouping'] == '-1') {
            $name = trim($data['groupingname']);
            if (empty($name)) {
                $errors['groupingname'] = get_string('required');
            } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
                $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
            }
        }

       /// Check the naming scheme
        if ($data['groupby'] == 'groups' and $data['number'] == 1) {
            // we can use the name as is because there will be only one group max
        } else {
            $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
            if ($matchcnt != 1) {
                $errors['namingscheme'] = get_string('badnamingscheme', 'group');
            }
        }

        return $errors;
    }
     $numgrps = ceil($usercnt / $data->number);
     $userpergrp = $data->number;
     if (!empty($data->nosmallgroups) and $usercnt % $data->number != 0) {
         // If there would be one group with a small number of member reduce the number of groups
         $missing = $userpergrp * $numgrps - $usercnt;
         if ($missing > $userpergrp * (1 - AUTOGROUP_MIN_RATIO)) {
             // spread the users from the last small group
             $numgrps--;
             $userpergrp = floor($usercnt / $numgrps);
         }
     }
 }
 // allocate the users - all groups equal count first
 for ($i = 0; $i < $numgrps; $i++) {
     $groups[$i] = array();
     $groups[$i]['name'] = groups_parse_name(trim($data->namingscheme), $i);
     $groups[$i]['members'] = array();
     if ($data->allocateby == 'no') {
         continue;
         // do not allocate users
     }
     for ($j = 0; $j < $userpergrp; $j++) {
         if (empty($users)) {
             break 2;
         }
         $user = array_shift($users);
         $groups[$i]['members'][$user->id] = $user;
     }
 }
 // now distribute the rest
 if ($data->allocateby != 'no') {
     shuffle($users);
 }
 $groups = array();
 $i = 0;
 $cnt = 0;
 if ($data->groupby == 'groups') {
     $numgrps = $data->number;
     $userpergrp = ceil($usercnt / $numgrps);
 } else {
     $numgrps = ceil($data->number / $usercnt);
     $userpergrp = $data->number;
 }
 foreach ($users as $id => $user) {
     if (!isset($groups[$i])) {
         // Create a new group
         $groups[$i]['name'] = groups_parse_name($data->namingschemegrp['namingscheme'], $i);
     }
     @($groups[$i]['members'][] =& $users[$id]);
     $cnt++;
     if ($cnt == $userpergrp) {
         $cnt = 0;
         $i++;
     }
 }
 if (isset($data->preview)) {
     /// Print the groups preview
     $preview = '<ul>';
     foreach ($groups as $group) {
         $preview .= "<li>{$group['name']}\n<ul>";
         foreach ($group['members'] as $member) {
             $preview .= '<li>' . fullname($member) . '</li>';