forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-11419 - groups: interface enhancements + new features:
* Display the grouping a course module belongs to on the course page - for course managers only. * When adding users to groups, display the groups a user already belongs to. * Added an overview report that shows groupings, groups and members for a course. * Added a dialogue to automatically create groups and assign members based on either the number of desired groups or the number of desired users per group.
- Loading branch information
mattc-catalyst
committed
Sep 24, 2007
1 parent
964170c
commit acf000b
Showing
12 changed files
with
768 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php // $Id$ | ||
/** | ||
* Create and allocate users go groups | ||
* | ||
* @author Matt Clarkson mattc@catalyst.net.nz | ||
* @version 0.0.1 | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License | ||
* @package groups | ||
*/ | ||
|
||
require_once('../config.php'); | ||
require_once('autogroup_form.php'); | ||
|
||
$courseid = required_param('courseid', PARAM_INT); | ||
|
||
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); | ||
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'); | ||
|
||
|
||
// 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')); | ||
$navigation = build_navigation($navlinks); | ||
|
||
|
||
/// 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); | ||
|
||
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 | ||
} | ||
} | ||
|
||
/// Create the form | ||
$editform = new autogroup_form('autogroup.php', 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; | ||
} | ||
$users = groups_get_potental_members($data->courseid, $data->roleid, $orderby); | ||
$usercnt = count($users); | ||
|
||
if ($data->allocateby == 'random') { | ||
srand ($data->seed); | ||
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>'; | ||
} | ||
$preview .= "</ul>\n</li>\n"; | ||
} | ||
$preview .= '</ul>'; | ||
} else { | ||
/// Save the groups data | ||
foreach ($groups as $group) { | ||
$newgroup->timecreated = time(); | ||
$newgroup->timemodified = $newgroup->timecreated; | ||
$newgroup->courseid = $data->courseid; | ||
$newgroup->name = $group['name']; | ||
$groupid = insert_record('groups', $newgroup); | ||
foreach($group['members'] as $user) { | ||
$member->groupid = $groupid; | ||
$member->userid = $user->id; | ||
$member->timeadded = time(); | ||
insert_record('groups_members', $member); | ||
} | ||
} | ||
redirect($returnurl); | ||
} | ||
|
||
} | ||
|
||
/// Print header | ||
print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course)); | ||
|
||
/// Display the form | ||
$editform->display(); | ||
if(isset($preview)) { | ||
print_heading_block(get_string('groupspreview', 'group')); | ||
print_box($preview); | ||
} | ||
|
||
print_footer($course); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php // $Id$ | ||
|
||
require_once($CFG->dirroot.'/lib/formslib.php'); | ||
|
||
/// get url variables | ||
class autogroup_form extends moodleform { | ||
|
||
// Define the form | ||
function definition() { | ||
global $CFG, $COURSE; | ||
|
||
$mform =& $this->_form; | ||
|
||
$mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group')); | ||
|
||
$options = array(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'); | ||
|
||
|
||
$options = array('groups' => get_string('groups', 'group'), | ||
'members' => get_string('members', '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('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'), '<br />'); | ||
|
||
$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->addElement('hidden','courseid'); | ||
$mform->setType('courseid', PARAM_INT); | ||
|
||
$mform->addElement('hidden','seed'); | ||
$mform->setType('seed', PARAM_INT); | ||
|
||
$buttonarray=array(); | ||
$buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx'); | ||
$buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit')); | ||
$buttonarray[] = &$mform->createElement('cancel'); | ||
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); | ||
$mform->closeHeaderBefore('buttonar'); | ||
} | ||
|
||
|
||
function validation($data) { | ||
global $CFG, $COURSE; | ||
$errors = array(); | ||
|
||
if (!$users = groups_get_potental_members($data['courseid'], $data['roleid'])) { | ||
$errors['roleid'] = get_string('nousersinrole', 'group'); | ||
} | ||
$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 the naming scheme | ||
$matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingschemegrp']['namingscheme'], $matches); | ||
|
||
if ($matchcnt != 1) { | ||
$errors['namingschemegrp'] = get_string('badnamingscheme', 'group'); | ||
} | ||
|
||
|
||
if (count($errors) > 0) { | ||
return $errors; | ||
} else { | ||
return true; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.