Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/group/autogroup.php
Go to the documentation of this file.
00001 <?php
00011 require_once('../config.php');
00012 require_once('lib.php');
00013 require_once('autogroup_form.php');
00014 
00015 if (!defined('AUTOGROUP_MIN_RATIO')) {
00016     define('AUTOGROUP_MIN_RATIO', 0.7); // means minimum member count is 70% in the smallest group
00017 }
00018 
00019 $courseid = required_param('courseid', PARAM_INT);
00020 $PAGE->set_url('/group/autogroup.php', array('courseid' => $courseid));
00021 
00022 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
00023     print_error('invalidcourseid');
00024 }
00025 
00026 // Make sure that the user has permissions to manage groups.
00027 require_login($course);
00028 
00029 $context       = get_context_instance(CONTEXT_COURSE, $courseid);
00030 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
00031 require_capability('moodle/course:managegroups', $context);
00032 
00033 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id;
00034 
00035 $strgroups           = get_string('groups');
00036 $strparticipants     = get_string('participants');
00037 $strautocreategroups = get_string('autocreategroups', 'group');
00038 
00039 // Print the page and form
00040 $preview = '';
00041 $error = '';
00042 
00044 $rolenames = array();
00045 if ($roles = get_profile_roles($context)) {
00046     foreach ($roles as $role) {
00047         $rolenames[$role->id] = strip_tags(role_get_name($role, $context));   // Used in menus etc later on
00048     }
00049 }
00050 
00052 $editform = new autogroup_form(null, array('roles' => $rolenames));
00053 $editform->set_data(array('courseid' => $courseid, 'seed' => time()));
00054 
00056 if ($editform->is_cancelled()) {
00057     redirect($returnurl);
00058 
00059 } elseif ($data = $editform->get_data()) {
00060 
00062     switch ($data->allocateby) {
00063         case 'no':
00064         case 'random':
00065         case 'lastname':
00066             $orderby = 'lastname, firstname'; break;
00067         case 'firstname':
00068             $orderby = 'firstname, lastname'; break;
00069         case 'idnumber':
00070             $orderby = 'idnumber'; break;
00071         default:
00072             print_error('unknoworder');
00073     }
00074     $users = groups_get_potential_members($data->courseid, $data->roleid, $data->cohortid, $orderby);
00075     $usercnt = count($users);
00076 
00077     if ($data->allocateby == 'random') {
00078         srand($data->seed);
00079         shuffle($users);
00080     }
00081 
00082     $groups = array();
00083 
00084     // Plan the allocation
00085     if ($data->groupby == 'groups') {
00086         $numgrps    = $data->number;
00087         $userpergrp = floor($usercnt/$numgrps);
00088 
00089     } else { // members
00090         $numgrps    = ceil($usercnt/$data->number);
00091         $userpergrp = $data->number;
00092 
00093         if (!empty($data->nosmallgroups) and $usercnt % $data->number != 0) {
00094             // If there would be one group with a small number of member reduce the number of groups
00095             $missing = $userpergrp * $numgrps - $usercnt;
00096             if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) {
00097                 // spread the users from the last small group
00098                 $numgrps--;
00099                 $userpergrp = floor($usercnt/$numgrps);
00100             }
00101         }
00102     }
00103 
00104     // allocate the users - all groups equal count first
00105     for ($i=0; $i<$numgrps; $i++) {
00106         $groups[$i] = array();
00107         $groups[$i]['name']    = groups_parse_name(trim($data->namingscheme), $i);
00108         $groups[$i]['members'] = array();
00109         if ($data->allocateby == 'no') {
00110             continue; // do not allocate users
00111         }
00112         for ($j=0; $j<$userpergrp; $j++) {
00113             if (empty($users)) {
00114                 break 2;
00115             }
00116             $user = array_shift($users);
00117             $groups[$i]['members'][$user->id] = $user;
00118         }
00119     }
00120     // now distribute the rest
00121     if ($data->allocateby != 'no') {
00122         for ($i=0; $i<$numgrps; $i++) {
00123             if (empty($users)) {
00124                 break 1;
00125             }
00126             $user = array_shift($users);
00127             $groups[$i]['members'][$user->id] = $user;
00128         }
00129     }
00130 
00131     if (isset($data->preview)) {
00132         $table = new html_table();
00133         if ($data->allocateby == 'no') {
00134             $table->head  = array(get_string('groupscount', 'group', $numgrps));
00135             $table->size  = array('100%');
00136             $table->align = array('left');
00137             $table->width = '40%';
00138         } else {
00139             $table->head  = array(get_string('groupscount', 'group', $numgrps), get_string('groupmembers', 'group'), get_string('usercounttotal', 'group', $usercnt));
00140             $table->size  = array('20%', '70%', '10%');
00141             $table->align = array('left', 'left', 'center');
00142             $table->width = '90%';
00143         }
00144         $table->data  = array();
00145 
00146         foreach ($groups as $group) {
00147             $line = array();
00148             if (groups_get_group_by_name($courseid, $group['name'])) {
00149                 $line[] = '<span class="notifyproblem">'.get_string('groupnameexists', 'group', $group['name']).'</span>';
00150                 $error = get_string('groupnameexists', 'group', $group['name']);
00151             } else {
00152                 $line[] = $group['name'];
00153             }
00154             if ($data->allocateby != 'no') {
00155                 $unames = array();
00156                 foreach ($group['members'] as $user) {
00157                     $unames[] = fullname($user, true);
00158                 }
00159                 $line[] = implode(', ', $unames);
00160                 $line[] = count($group['members']);
00161             }
00162             $table->data[] = $line;
00163         }
00164 
00165         $preview .= html_writer::table($table);
00166 
00167     } else {
00168         $grouping = null;
00169         $createdgrouping = null;
00170         $createdgroups = array();
00171         $failed = false;
00172 
00173         // prepare grouping
00174         if (!empty($data->grouping)) {
00175             if ($data->grouping < 0) {
00176                 $grouping = new stdClass();
00177                 $grouping->courseid = $COURSE->id;
00178                 $grouping->name     = trim($data->groupingname);
00179                 $grouping->id = groups_create_grouping($grouping);
00180                 $createdgrouping = $grouping->id;
00181             } else {
00182                 $grouping = groups_get_grouping($data->grouping);
00183             }
00184         }
00185 
00186         // Save the groups data
00187         foreach ($groups as $key=>$group) {
00188             if (groups_get_group_by_name($courseid, $group['name'])) {
00189                 $error = get_string('groupnameexists', 'group', $group['name']);
00190                 $failed = true;
00191                 break;
00192             }
00193             $newgroup = new stdClass();
00194             $newgroup->courseid = $data->courseid;
00195             $newgroup->name     = $group['name'];
00196             $groupid = groups_create_group($newgroup);
00197             $createdgroups[] = $groupid;
00198             foreach($group['members'] as $user) {
00199                 groups_add_member($groupid, $user->id);
00200             }
00201             if ($grouping) {
00202                 groups_assign_grouping($grouping->id, $groupid);
00203             }
00204         }
00205 
00206         if ($failed) {
00207             foreach ($createdgroups as $groupid) {
00208                 groups_delete_group($groupid);
00209             }
00210             if ($createdgrouping) {
00211                 groups_delete_grouping($createdgrouping);
00212             }
00213         } else {
00214             redirect($returnurl);
00215         }
00216     }
00217 }
00218 
00219 $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
00220 $PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
00221 $PAGE->navbar->add($strautocreategroups);
00222 
00224 $PAGE->set_title($strgroups);
00225 $PAGE->set_heading($course->fullname. ': '.$strgroups);
00226 echo $OUTPUT->header();
00227 echo $OUTPUT->heading($strautocreategroups);
00228 
00229 if ($error != '') {
00230     echo $OUTPUT->notification($error);
00231 }
00232 
00234 $editform->display();
00235 
00236 if($preview !== '') {
00237     echo $OUTPUT->heading(get_string('groupspreview', 'group'));
00238 
00239     echo $preview;
00240 }
00241 
00242 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations