From: skodak Date: Mon, 19 Nov 2007 20:31:57 +0000 (+0000) Subject: MDL-12249 groups UI cleanup and improvements - see tracker for details; merged from... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f16fa0a34b69d2903a8655f292aebea1564780ce;p=moodle.git MDL-12249 groups UI cleanup and improvements - see tracker for details; merged from MOODLE_19_STABLE --- diff --git a/group/assign.php b/group/assign.php index 0857090566..0c7211ad83 100644 --- a/group/assign.php +++ b/group/assign.php @@ -1,4 +1,4 @@ -add) and !empty($frm->addselect)) { foreach ($frm->addselect as $groupid) { - $groupid = (int)$groupid; - if (record_exists('groupings_groups', 'groupingid', $grouping->id, 'groupid', $groupid)) { - continue; - } - $assign = new object(); - $assign->groupingid = $grouping->id; - $assign->groupid = $groupid; - $assign->timeadded = time(); - insert_record('groupings_groups', $assign); + groups_assign_grouping($grouping->id, (int)$groupid); } } else if (isset($frm->remove) and !empty($frm->removeselect)) { - foreach ($frm->removeselect as $groupid) { - $groupid = (int)$groupid; - delete_records('groupings_groups', 'groupingid', $grouping->id, 'groupid', $groupid); + groups_unassign_grouping($grouping->id, (int)$groupid); } } } @@ -82,20 +72,6 @@ if ($currentmembers) { $managers = get_role_users($roleid, $context, true, 'u.id', 'u.id ASC', $canseehidden); } } - - if ($potentialmembers != false) { - // Put the groupings into a hash and sorts them - foreach ($potentialmembers as $user) { - if(!empty($managers[$user->id])) { - $nonmembers[$user->id] = '#'.$user->firstname.' '.$user->lastname; - } - else { - $nonmembers[$user->id] = $user->firstname.' '.$user->lastname; - } - $potentialmemberscount++; - } - natcasesort($nonmembers); - } } else { $currentmembersoptions .= ''; } @@ -133,7 +109,6 @@ print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '
- @@ -147,7 +122,6 @@ print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '
-

diff --git a/group/autogroup.php b/group/autogroup.php index c6a9e49cff..73b9631637 100644 --- a/group/autogroup.php +++ b/group/autogroup.php @@ -9,57 +9,50 @@ */ require_once('../config.php'); +require_once('lib.php'); require_once('autogroup_form.php'); define('AUTOGROUP_MIN_RATIO', 0.7); $courseid = required_param('courseid', PARAM_INT); -if (!$course = get_record('course', 'id',$courseid)) { +if (!$course = get_record('course', 'id', $courseid)) { error('invalidcourse'); } // Make sure that the user has permissions to manage groups. require_login($course); -$context = get_context_instance(CONTEXT_COURSE, $courseid); -$sitecontext = get_context_instance(CONTEXT_SYSTEM); +$context = get_context_instance(CONTEXT_COURSE, $courseid); +$systemcontext = get_context_instance(CONTEXT_SYSTEM); require_capability('moodle/course:managegroups', $context); $returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id; -$strgroups = get_string('groups'); -$strparticipants = get_string('participants'); -$stroverview = get_string('overview', 'group'); -$strgrouping = get_string('grouping', 'group'); -$strgroup = get_string('group', 'group'); -$strnotingrouping = get_string('notingrouping', 'group'); -$strfiltergroups = get_string('filtergroups', 'group'); - +$strgroups = get_string('groups'); +$strparticipants = get_string('participants'); +$strautocreategroups = get_string('autocreategroups', 'group'); // Print the page and form $navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'), - array('name'=>$strgroups, 'link'=>'', 'type'=>'misc')); + array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.php?id=$courseid", 'type' => 'misc'), + array('name' => $strautocreategroups, 'link' => null, 'type' => 'misc')); $navigation = build_navigation($navlinks); +$preview = ''; +$error = ''; /// Get applicable roles $rolenames = array(); -$avoidroles = array(); - if ($roles = get_roles_used_in_context($context, true)) { $canviewroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context); - $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext); + $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $systemcontext); foreach ($roles as $role) { if (!isset($canviewroles[$role->id])) { // Avoid this role (eg course creator) - $avoidroles[] = $role->id; - unset($roles[$role->id]); continue; } if (isset($doanythingroles[$role->id])) { // Avoid this role (ie admin) - $avoidroles[] = $role->id; - unset($roles[$role->id]); continue; } $rolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on @@ -67,113 +60,195 @@ if ($roles = get_roles_used_in_context($context, true)) { } /// Create the form -$editform = new autogroup_form('autogroup.php', array('roles' => $rolenames)); -$editform->set_data(array('courseid' => $courseid, - 'seed' => time())); - - +$editform = new autogroup_form(null, array('roles' => $rolenames)); +$editform->set_data(array('courseid' => $courseid, 'seed' => time())); /// Handle form submission if ($editform->is_cancelled()) { redirect($returnurl); -} elseif ($data = $editform->get_data()) { - - /// Allocate members from the selected role to groups - if ($data->allocateby == 'random') { - $orderby = 'firstname'; - } else { - $orderby = $data->allocateby; + +} elseif ($data = $editform->get_data(false)) { + + /// Allocate members from the selected role to groups + switch ($data->allocateby) { + case 'no': + case 'random': + case 'lastname': + $orderby = 'lastname, firstname'; break; + case 'firstname': + $orderby = 'firstname, lastname'; break; + case 'idnumber': + $orderby = 'idnumber'; break; + default: + error('Unknown ordering'); } - $users = groups_get_potental_members($data->courseid, $data->roleid, $orderby); + $users = groups_get_potential_members($data->courseid, $data->roleid, $orderby); $usercnt = count($users); - + if ($data->allocateby == 'random') { - srand ($data->seed); + srand($data->seed); shuffle($users); } - + $groups = array(); - $i = 0; - $cnt = 0; - + // Plan the allocation if ($data->groupby == 'groups') { - $numgrps = $data->number; - $userpergrp = ceil($usercnt/$numgrps); - } else { - $numgrps = ceil($usercnt/$data->number); - $userpergrp = $data->number > $usercnt ? $usercnt : $data->number; + $numgrps = $data->number; + $userpergrp = floor($usercnt/$numgrps); + + } else { // members + $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 * AUTOGROUP_MIN_RATIO) { + $numgrps--; + $userpergrp = ceil($usercnt/$numgrps); + } + } + } - // If there would be one group with a small number of member reduce the number of groups - $remainder = $userpergrp - ($userpergrp * $numgrps - $usercnt); - if ($remainder && $remainder < $userpergrp * AUTOGROUP_MIN_RATIO) { - $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; } } - - // Do the allocation - $remainggroups = $numgrps; - $remaingusers = $usercnt; - - 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 >= round($usercnt / $remainggroups)) { - $usercnt -= $cnt; - $cnt = 0; - $i++; - $remainggroups--; + // now distribute the rest + if ($data->allocateby != 'no') { + for ($i=0; $i<$numgrps; $i++) { + if (empty($users)) { + break 1; + } + $user = array_shift($users); + $groups[$i]['members'][$user->id] = $user; } } - - + if (isset($data->preview)) { - /// Print the groups preview - $preview = ''; - if (isset($remainder) && $remainder != $userpergrp && $data->groupby == 'members') { - $preview .= '

'.get_string('evenallocation', 'group').'

'; + $table = new object(); + if ($data->allocateby == 'no') { + $table->head = array(get_string('groupscount', 'group', $numgrps)); + $table->size = array('100%'); + $table->align = array('left'); + $table->width = '40%'; + } else { + $table->head = array(get_string('groupscount', 'group', $numgrps), get_string('groupmembers', 'group'), get_string('usercounttotal', 'group', $usercnt)); + $table->size = array('20%', '70%', '10%'); + $table->align = array('left', 'left', 'center'); + $table->width = '90%'; } - $preview .= '
    '; + $table->data = array(); + foreach ($groups as $group) { - $preview .= "
  • $group[name] (".count($group['members'])." ".get_string('membersingroup', 'group').")\n
      "; - foreach ($group['members'] as $member) { - $preview .= '
    • '.fullname($member).'
    • '; + $line = array(); + if (groups_get_group_by_name($courseid, $group['name'])) { + $line[] = ''.get_string('groupnameexists', 'group', $group['name']).''; + $error = get_string('groupnameexists', 'group', $group['name']); + } else { + $line[] = $group['name']; } - $preview .= "

    \n
  • \n"; + if ($data->allocateby != 'no') { + $unames = array(); + foreach ($group['members'] as $user) { + $unames[] = fullname($user, true); + } + $line[] = implode(', ', $unames); + $line[] = count($group['members']); + } + $table->data[] = $line; } - $preview .= '
'; + + $preview .= print_table($table, true); + } else { - /// Save the groups data - foreach ($groups as $group) { - $newgroup->timecreated = time(); - $newgroup->timemodified = $newgroup->timecreated; + $grouping = null; + $createdgrouping = null; + $createdgroups = array(); + $failed = false; + + // prepare grouping + if (!empty($data->grouping)) { + $groupingname = trim($data->groupingname); + if ($data->grouping < 0) { + $grouping = new object(); + $grouping->courseid = $COURSE->id; + $grouping->name = $groupingname; + if (!$grouping->id = groups_create_grouping(addslashes_recursive($grouping))) { + $error = 'Can not create grouping'; //should not happen + $failed = true; + } + $createdgrouping = $grouping->id; + } else { + $grouping = groups_get_grouping($data->grouping); + } + } + + // Save the groups data + foreach ($groups as $key=>$group) { + if (groups_get_group_by_name($courseid, $group['name'])) { + $error = get_string('groupnameexists', 'group', $group['name']); + $failed = true; + break; + } + $newgroup = new object(); $newgroup->courseid = $data->courseid; - $newgroup->name = $group['name']; - $groupid = insert_record('groups', $newgroup); + $newgroup->name = $group['name']; + if (!$groupid = groups_create_group(addslashes_recursive($newgroup))) { + $error = 'Can not create group!'; // should not happen + $failed = true; + break; + } + $createdgroups[] = $groupid; foreach($group['members'] as $user) { - $member->groupid = $groupid; - $member->userid = $user->id; - $member->timeadded = time(); - insert_record('groups_members', $member); + groups_add_member($groupid, $user->id); + } + if ($grouping) { + groups_assign_grouping($grouping->id, $groupid); } } - redirect($returnurl); - } + if ($failed) { + foreach ($createdgroups as $groupid) { + groups_delete_group($groupid); + } + if ($createdgrouping) { + groups_delete_grouping($createdgrouping); + } + } else { + redirect($returnurl); + } + } } - + /// Print header print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course)); +print_heading($strautocreategroups); + +if ($error != '') { + notify($error); +} /// Display the form $editform->display(); -if(isset($preview)) { - print_heading_block(get_string('groupspreview', 'group')); - print_box($preview); + +if($preview !== '') { + print_heading(get_string('groupspreview', 'group')); + + echo $preview; } print_footer($course); diff --git a/group/autogroup_form.php b/group/autogroup_form.php index 0f846d9ac6..e9be2fc6db 100644 --- a/group/autogroup_form.php +++ b/group/autogroup_form.php @@ -13,46 +13,74 @@ class autogroup_form extends moodleform { $mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group')); - $options = array(get_string('all')); + $options = array(0=>get_string('all')); $options += $this->_customdata['roles']; $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options); - $mform->addRule('roleid', get_string('required'), 'required', null, 'client'); + if (!empty($COURSE->defaultrole) and array_key_exists($COURSE->defaultrole, $options)) { + $mform->setDefault('roleid', $COURSE->defaultrole); + } else if (!empty($CFG->defaultcourseroleid) and array_key_exists($CFG->defaultcourseroleid, $options)) { + $mform->setDefault('roleid', $CFG->defaultcourseroleid); + } - - $options = array('groups' => get_string('groups', 'group'), - 'members' => get_string('members', 'group')); + $options = array('groups' => get_string('numgroups', 'group'), + 'members' => get_string('nummembers', 'group')); $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options); - $mform->addRule('groupby', get_string('required'), 'required', null, 'client'); - + $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"'); $mform->setType('number', PARAM_INT); $mform->addRule('number', null, 'numeric', null, 'client'); $mform->addRule('number', get_string('required'), 'required', null, 'client'); - - $options = array('random' => get_string('random', 'group'), - 'firstname' => get_string('firstname', 'group'), - 'lastname' => get_string('lastname', 'group')); - + + $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group')); + $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members'); + $mform->setAdvanced('nosmallgroups'); + + $options = array('no' => get_string('noallocation', 'group'), + 'random' => get_string('random', 'group'), + 'firstname' => get_string('byfirstname', 'group'), + 'lastname' => get_string('bylastname', 'group'), + 'idnumber' => get_string('byidnumber', 'group')); $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options); - $mform->addRule('allocateby', get_string('required'), 'required', null, 'client'); - - $grp[] = $mform->createElement('text', 'namingscheme'); - $grp[] = $mform->createElement('static', 'namingschemehelp', null, get_string('namingschemehelp', 'group')); - $mform->addGroup($grp, 'namingschemegrp', get_string('namingscheme', 'group'), '
'); - - $mform->setType('namingschemegrp[namingscheme]', PARAM_RAW); - $mform->setDefault('namingschemegrp[namingscheme]', get_string('group', 'group').' @'); - $mform->addRule('namingschemegrp', get_string('required'), 'required', null, 'client'); - $mform->setAdvanced('namingschemegrp'); - + $mform->setDefault('allocateby', 'random'); + $mform->setAdvanced('allocateby'); + + $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group')); + $mform->setHelpButton('namingscheme', array(false, get_string('namingschemehelp', 'group'), + false, true, false, get_string('namingschemehelp', 'group'))); + $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client'); + $mform->setType('namingscheme', PARAM_MULTILANG); + // there must not be duplicate group names in course + $template = get_string('grouptemplate', 'group'); + $gname = groups_parse_name($template, 0); + if (!groups_get_group_by_name($COURSE->id, $gname)) { + $mform->setDefault('namingscheme', $template); + } + + if (!empty($CFG->enablegroupings)) { + $options = array('0' => get_string('no'), + '-1'=> get_string('newgrouping', 'group')); + if ($groupings = groups_get_all_groupings($COURSE->id)) { + foreach ($groupings as $grouping) { + $options[$grouping->id] = strip_tags(format_string($grouping->name)); + } + } + $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options); + if ($groupings) { + $mform->setDefault('grouping', '-1'); + } + + $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options); + $mform->setType('groupingname', PARAM_MULTILANG); + $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1'); + } $mform->addElement('hidden','courseid'); $mform->setType('courseid', PARAM_INT); - + $mform->addElement('hidden','seed'); $mform->setType('seed', PARAM_INT); - - $buttonarray=array(); + + $buttonarray = array(); $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx'); $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit')); $buttonarray[] = &$mform->createElement('cancel'); @@ -61,39 +89,50 @@ class autogroup_form extends moodleform { } - function validation($data) { + function validation($data, $files) { global $CFG, $COURSE; $errors = array(); - if (!$users = groups_get_potental_members($data['courseid'], $data['roleid'])) { - $errors['roleid'] = get_string('nousersinrole', 'group'); + 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); } - $usercnt = count($users); - - /// Check the number entered is sane - if ($data['groupby'] == 'groups') { - if ($data['number'] > $usercnt || $data['number'] < 1) { - $errors['number'] = get_string('toomanygroups', 'group', $usercnt); + // check grouping name duplicates + if ($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['namingschemegrp']['namingscheme'], $matches); - + + /// Check the naming scheme + $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches); + if ($matchcnt != 1) { - $errors['namingschemegrp'] = get_string('badnamingscheme', 'group'); + $errors['namingscheme'] = get_string('badnamingscheme', 'group'); } - - - if (count($errors) > 0) { - return $errors; - } else { - return true; - } - - } + return $errors; + } } ?> diff --git a/group/group.php b/group/group.php index b39757879e..d638126e7c 100644 --- a/group/group.php +++ b/group/group.php @@ -86,7 +86,7 @@ if ($editform->is_cancelled()) { } } else { if (!$id = groups_create_group($data, $editform->_upload_manager)) { - error('Error updating group'); + error('Error creating group'); } $returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&group='.$id; } diff --git a/group/group_form.php b/group/group_form.php index 727540d6ef..738e2c713e 100644 --- a/group/group_form.php +++ b/group/group_form.php @@ -47,7 +47,7 @@ class group_form extends moodleform { $errors = array(); - $name = stripslashes($data['name']); + $name = trim(stripslashes($data['name'])); if ($data['id'] and $group = get_record('groups', 'id', $data['id'])) { if ($group->name != $name) { if (groups_get_group_by_name($COURSE->id, $name)) { diff --git a/group/grouping.php b/group/grouping.php index dc5ad1b482..760d581ea3 100644 --- a/group/grouping.php +++ b/group/grouping.php @@ -82,16 +82,13 @@ if ($editform->is_cancelled()) { $success = true; if ($data->id) { - $data->timemodified = time(); - if (!update_record('groupings', $data)) { - error('Error updating group'); + if (!groups_update_grouping($data)) { + error('Error updating grouping'); } } else { - $data->timecreated = time(); - $data->timemodified = $data->timecreated; - if (!$data->id = insert_record('groupings', $data)) { - error('Error updating grouping'); + if (!groups_create_grouping($data)) { + error('Error creating grouping'); } } diff --git a/group/grouping_form.php b/group/grouping_form.php index 2f2dc039e1..d61ab7c1f3 100644 --- a/group/grouping_form.php +++ b/group/grouping_form.php @@ -32,7 +32,7 @@ class grouping_form extends moodleform { $errors = array(); - $name = stripslashes($data['name']); + $name = trim(stripslashes($data['name'])); if ($data['id'] and $grouping = get_record('groupings', 'id', $data['id'])) { if ($grouping->name != $name) { if (groups_get_grouping_by_name($COURSE->id, $name)) { diff --git a/group/groupings.php b/group/groupings.php index 0abbc8ef52..fdab604719 100644 --- a/group/groupings.php +++ b/group/groupings.php @@ -67,6 +67,7 @@ if ($groupings = get_records('groupings', 'courseid', $course->id)) { $data[] = $line; } } +$tabke = new object(); $table->head = array($strgrouping, $strgroups, $struses, $stredit); $table->size = array('30%', '50%', '10%', '10%'); $table->align = array('left', 'left', 'center', 'center'); diff --git a/group/index.php b/group/index.php index caddc53f92..ea316c881f 100644 --- a/group/index.php +++ b/group/index.php @@ -70,7 +70,7 @@ switch ($action) { case 'showautocreategroupsform': redirect('autogroup.php?courseid='.$courseid); - break; + break; case 'showgroupsettingsform': redirect('group.php?courseid='.$courseid.'&id='.$groupid); @@ -106,12 +106,9 @@ $navigation = build_navigation($navlinks); /// Print header print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course)); - -if (!empty($CFG->enablegroupings)) { - // Add tabs - $currenttab = 'groups'; - require('tabs.php'); -} +// Add tabs +$currenttab = 'groups'; +require('tabs.php'); $disabled = 'disabled="disabled"'; @@ -145,7 +142,7 @@ echo '

'."\n"; - + echo '

'."\n"; diff --git a/group/lib.php b/group/lib.php index 9a7bc68c86..7d06010b4a 100644 --- a/group/lib.php +++ b/group/lib.php @@ -17,8 +17,8 @@ * Adds a specified user to a group * @param int $userid The user id * @param int $groupid The group id - * @return boolean True if user added successfully or the user is already a - * member of the group, false otherwise. + * @return boolean True if user added successfully or the user is already a + * member of the group, false otherwise. */ function groups_add_member($groupid, $userid) { if (!groups_group_exists($groupid)) { @@ -45,7 +45,7 @@ function groups_add_member($groupid, $userid) { $eventdata = new object(); $eventdata->groupid = $groupid; $eventdata->userid = $userid; - events_trigger('group_user_added', $eventdata); + events_trigger('group_user_added', $eventdata); return true; } @@ -67,7 +67,7 @@ function groups_remove_member($groupid, $userid) { if (!delete_records('groups_members', 'groupid', $groupid, 'userid', $userid)) { return false; - } + } //update group info set_field('groups', 'timemodified', time(), 'id', $groupid); @@ -76,7 +76,7 @@ function groups_remove_member($groupid, $userid) { /** * Add a new group - * @param object $data group properties (with magic quotes); + * @param object $data group properties (with magic quotes) * @param object $um upload manager with group picture * @return id of group or false if error */ @@ -86,6 +86,7 @@ function groups_create_group($data, $um=false) { $data->timecreated = time(); $data->timemodified = $data->timecreated; + $data->name = trim($data->name); $id = insert_record('groups', $data); if ($id and $um) { @@ -99,8 +100,22 @@ function groups_create_group($data, $um=false) { } /** - * Add a new group - * @param object $data group properties (with magic quotes); + * Add a new grouping + * @param object $data grouping properties (with magic quotes) + * @return id of grouping or false if error + */ +function groups_create_grouping($data) { + global $CFG; + + $data->timecreated = time(); + $data->timemodified = $data->timecreated; + $data->name = trim($data->name); + return insert_record('groupings', $data); +} + +/** + * Update group + * @param object $data group properties (with magic quotes) * @param object $um upload manager with group picture * @return boolean success */ @@ -109,6 +124,7 @@ function groups_update_group($data, $um=false) { require_once("$CFG->libdir/gdlib.php"); $data->timemodified = time(); + $data->name = trim($data->name); $result = update_record('groups', $data); if ($result and $um) { @@ -121,6 +137,18 @@ function groups_update_group($data, $um=false) { return $result; } +/** + * Update grouping + * @param object $data grouping properties (with magic quotes) + * @return boolean success + */ +function groups_update_grouping($data) { + global $CFG; + $data->timemodified = time(); + $data->name = trim($data->name); + return update_record('groupings', $data); +} + /** * Delete a group best effort, first removing members and links with courses and groupings. * Removes group avatar too. @@ -145,6 +173,11 @@ function groups_delete_group($groupid) { return delete_records('groups', 'id', $groupid); } +/** + * Delete grouping + * @param int $groupingid + * @return bool success + */ function groups_delete_grouping($groupingid) { if (empty($groupingid)) { return false; @@ -161,6 +194,12 @@ function groups_delete_grouping($groupingid) { return delete_records('groupings', 'id', $groupingid); } +/** + * Remove all users from group + * @param int $courseid + * @param bool $showfeedback + * @return bool success + */ function groups_delete_group_members($courseid, $showfeedback=false) { global $CFG; @@ -175,6 +214,12 @@ function groups_delete_group_members($courseid, $showfeedback=false) { return true; } +/** + * Delete all groups from course + * @param int $courseid + * @param bool $showfeedback + * @return bool success + */ function groups_delete_groups($courseid, $showfeedback=false) { global $CFG; require_once($CFG->libdir.'/gdlib.php'); @@ -201,6 +246,12 @@ function groups_delete_groups($courseid, $showfeedback=false) { return true; } +/** + * Delete all groupings from course + * @param int $courseid + * @param bool $showfeedback + * @return bool success + */ function groups_delete_groupings($courseid, $showfeedback=false) { global $CFG; @@ -287,7 +338,7 @@ function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') { $from = " FROM {$CFG->prefix}user u INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid"; - + $where = " WHERE ra.contextid ".get_related_contexts_string($context)." AND u.deleted = 0 AND ra.roleid in $roleids @@ -296,7 +347,7 @@ function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') { WHERE groupid = $groupid) $wheresearch"; $groupby = " GROUP BY u.id, u.firstname, u.lastname "; - + return get_records_sql($select.$from.$where.$groupby); } @@ -308,9 +359,9 @@ function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') { * @param string $orderby The colum to sort users by * @return array An array of the users */ -function groups_get_potental_members($courseid, $roleid = null, $orderby = 'lastname') { +function groups_get_potential_members($courseid, $roleid = null, $orderby = 'lastname,firstname') { global $CFG; - + $context = get_context_instance(CONTEXT_COURSE, $courseid); $sitecontext = get_context_instance(CONTEXT_SYSTEM); $rolenames = array(); @@ -335,7 +386,7 @@ function groups_get_potental_members($courseid, $roleid = null, $orderby = 'last $rolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on } } - + $select = 'SELECT u.id, u.username, u.firstname, u.lastname, u.idnumber '; $from = "FROM {$CFG->prefix}user u INNER JOIN {$CFG->prefix}role_assignments r on u.id=r.userid "; @@ -354,22 +405,21 @@ function groups_get_potental_members($courseid, $roleid = null, $orderby = 'last } else { $listofcontexts = '('.$sitecontext->id.')'; // must be site } - + if ($roleid) { $selectrole = " AND r.roleid = $roleid "; } else { $selectrole = " "; } - + $where = "WHERE (r.contextid = $context->id OR r.contextid in $listofcontexts) - AND u.deleted = 0 $selectrole - AND u.username != 'guest' - $adminroles - "; + AND u.deleted = 0 $selectrole + AND u.username != 'guest' + $adminroles "; $order = "ORDER BY $orderby "; - + return(get_records_sql($select.$from.$where.$order)); - + } /** @@ -379,34 +429,43 @@ function groups_get_potental_members($courseid, $roleid = null, $orderby = 'last * @return string the parsed format string */ function groups_parse_name($format, $groupnumber) { - if (strstr($format, '@') !== false) { // Convert $groupnumber to a character series - $tmp = $groupnumber; - $places = 1; - $serial = ''; - - if ($groupnumber > 25) { - while (floor($groupnumber / pow(25, $places)) > 0) { - $places++; - } - - } else { - $places = 1; + $letter = 'A'; + for($i=0; $i<$groupnumber; $i++) { + $letter++; } - - for($i=$places;$i>0;$i--) { - if ($i>1) { - $serial .= chr(65+floor($tmp/ pow(26, $i))); - $tmp -= floor($tmp/ pow(26, $i))*pow(26, $i); - } else { - $serial .= chr(65+$tmp%26); - } - } - $str = preg_replace('/[@]/', $serial, $format); + $str = str_replace('@', $letter, $format); } else { - $str = preg_replace('/[#]/', $groupnumber+1, $format); + $str = str_replace('#', $groupnumber+1, $format); } return($str); } +/** + * Assigns group into grouping + * @param int groupingid + * @param int groupid + * @return bool success + */ +function groups_assign_grouping($groupingid, $groupid) { + if (record_exists('groupings_groups', 'groupingid', $groupingid, 'groupid', $groupid)) { + return true; + } + $assign = new object(); + $assign->groupingid = $groupingid; + $assign->groupid = $groupid; + $assign->timeadded = time(); + return (bool)insert_record('groupings_groups', $assign); +} + +/** + * Unassigns group grom grouping + * @param int groupingid + * @param int groupid + * @return bool success + */ +function groups_unassign_grouping($groupingid, $groupid) { + return delete_records('groupings_groups', 'groupingid', $groupingid, 'groupid', $groupid); +} + ?> \ No newline at end of file diff --git a/group/members.php b/group/members.php index 143681c7ee..d1cac6d362 100644 --- a/group/members.php +++ b/group/members.php @@ -111,7 +111,7 @@ if ($potentialmemberscount <= MAX_USERS_PER_PAGE) { while ($usergroup = rs_fetch_next_record($rs)) { $usergroups[$usergroup->userid][$usergroup->id] = $usergroup; } - + // Put the groupings into a hash and sorts them foreach ($potentialmembers as $userid => $user) { $nonmembers[$userid] = fullname($user)." (".@count($usergroups[$userid]).")"; @@ -147,7 +147,7 @@ print_header("$course->shortname: $strgroups", $course->fullname, $navigation, ' ?>