Skip to content

Commit

Permalink
MDL-16756: Groups bugfixes and multiple select support for deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
sam_marshall committed Oct 24, 2008
1 parent fe94025 commit f6eece1
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 39 deletions.
42 changes: 32 additions & 10 deletions group/clientlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,34 @@ function UpdatableMembersCombo(wwwRoot, courseId) {
* The Add/Remove Users button also needs to be disabled/enabled
* depending on whether or not a group is selected
*/
UpdatableMembersCombo.prototype.refreshMembers = function (groupId) {
// Add the loader gif image.
createLoaderImg("membersloader", "memberslabel", this.wwwRoot);
UpdatableMembersCombo.prototype.refreshMembers = function () {

// Get group selector and check selection type
var selectEl = document.getElementById("groups");
var selectionCount=0,groupId=0;
if( selectEl ) {
for (var i = 0; i < selectEl.options.length; i++) {
if(selectEl.options[i].selected) {
selectionCount++;
if(!groupId) {
groupId=selectEl.options[i].value;
}
}
}
}
var singleSelection=selectionCount == 1;

// Add the loader gif image (we only load for single selections)
if(singleSelection) {
createLoaderImg("membersloader", "memberslabel", this.wwwRoot);
}

// Update the label.
var selectEl = document.getElementById("groups");
var spanEl = document.getElementById("thegroup");
if (selectEl && selectEl.selectedIndex >= 0) {
if (singleSelection) {
spanEl.innerHTML = selectEl.options[selectEl.selectedIndex].title;
} else {
spanEl.innerHTML = '&nbsp;';
}

// Clear the members list box.
Expand All @@ -142,11 +161,14 @@ UpdatableMembersCombo.prototype.refreshMembers = function (groupId) {
}
}

document.getElementById("showaddmembersform").disabled = false;
document.getElementById("showeditgroupsettingsform").disabled = false;
document.getElementById("deletegroup").disabled = false;
var sUrl = this.wwwRoot+"/group/index.php?id="+this.courseId+"&group="+groupId+"&act_ajax_getmembersingroup";
YAHOO.util.Connect.asyncRequest("GET", sUrl, this.connectCallback, null);
document.getElementById("showaddmembersform").disabled = !singleSelection;
document.getElementById("showeditgroupsettingsform").disabled = !singleSelection;
document.getElementById("deletegroup").disabled = selectionCount == 0;

if(singleSelection) {
var sUrl = this.wwwRoot+"/group/index.php?id="+this.courseId+"&group="+groupId+"&act_ajax_getmembersingroup";
YAHOO.util.Connect.asyncRequest("GET", sUrl, this.connectCallback, null);
}
};


Expand Down
74 changes: 74 additions & 0 deletions group/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Delete group
*
* @copyright &copy; 2008 The Open University
* @author s.marshall AT open.ac.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package groups
*/

require_once('../config.php');
require_once('lib.php');

// Get and check parameters
$courseid = required_param('courseid', PARAM_INT);
$groupids = required_param('groups', PARAM_SEQUENCE);
$confirm = optional_param('confirm', 0, PARAM_BOOL);

// Make sure course is OK and user has access to manage groups
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
error('Course ID was incorrect');
}
require_login($course);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_capability('moodle/course:managegroups', $context);

// Make sure all groups are OK and belong to course
$groupidarray = explode(',',$groupids);
$groupnames = array();
foreach($groupidarray as $groupid) {
if (!$group = $DB->get_record('groups', array('id' => $groupid))) {
error('Group ID was incorrect');
}
if ($courseid != $group->courseid) {
error('Group not on required course');
}
$groupnames[] = format_string($group->name);
}

$returnurl='index.php?id='.$course->id;

if(count($groupidarray)==0) {
print_error('errorselectsome','group',$returnurl);
}

if ($confirm && data_submitted()) {
if (!confirm_sesskey() ) {
print_error('confirmsesskeybad','error',$returnurl);
}
$DB->begin_sql();
foreach($groupidarray as $groupid) {
if (!groups_delete_group($groupid)) {
print_error('erroreditgroup', 'group', $returnurl);
}
}
$DB->commit_sql();
redirect($returnurl);
} else {
print_header(get_string('deleteselectedgroup', 'group'), get_string('deleteselectedgroup', 'group'));
$optionsyes = array('courseid'=>$courseid, 'groups'=>$groupids, 'sesskey'=>sesskey(), 'confirm'=>1);
$optionsno = array('id'=>$courseid);
if(count($groupnames)==1) {
$message=get_string('deletegroupconfirm', 'group', $groupnames[0]);
} else {
$message=get_string('deletegroupsconfirm', 'group').'<ul>';
foreach($groupnames as $groupname) {
$message.='<li>'.$groupname.'</li>';
}
$message.='</ul>';
}
notice_yesno($message, 'delete.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
print_footer();
}
?>
6 changes: 6 additions & 0 deletions group/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
$delete = optional_param('delete', 0, PARAM_BOOL);
$confirm = optional_param('confirm', 0, PARAM_BOOL);

// This script used to support group delete, but that has been moved. In case
// anyone still links to it, let's redirect to the new script.
if($delete) {
redirect('delete.php?courseid='.$courseid.'&groups='.$id);
}

if ($id) {
if (!$group = $DB->get_record('groups', array('id'=>$id))) {
print_error('invalidgroupid');
Expand Down
89 changes: 60 additions & 29 deletions group/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
$groupid = optional_param('group', false, PARAM_INT);
$userid = optional_param('user', false, PARAM_INT);
$action = groups_param_action();
// Support either single group= parameter, or array groups[]
if ($groupid) {
$groupids=array($groupid);
} else {
$groupids=array();
if (isset($_REQUEST['groups'])) {
foreach ($_REQUEST['groups'] as $groupid) {
if ($groupid = clean_param($groupid, PARAM_INT)) {
$groupids[]=$groupid;
}
}
}
}
$singlegroup=count($groupids) == 1;

$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;

Expand All @@ -39,13 +53,24 @@
redirect(); //"group.php?id=$course->id"); // Not allowed to see all groups
}

// Check for multiple/no group errors
if(!$singlegroup) {
switch($action) {
case 'ajax_getmembersingroup':
case 'showgroupsettingsform':
case 'showaddmembersform':
case 'updatemembers':
print_error('errorselectone','group',$returnurl);
}
}

switch ($action) {
case false: //OK, display form.
break;

case 'ajax_getmembersingroup':
$roles = array();
if ($groupmemberroles = groups_get_members_by_role($groupid,$courseid,'u.id,u.firstname,u.lastname')) {
if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
foreach($groupmemberroles as $roleid=>$roledata) {
$shortroledata=new StdClass;
$shortroledata->name=$roledata->name;
Expand All @@ -63,7 +88,11 @@
die; // Client side JavaScript takes it from here.

case 'deletegroup':
redirect('group.php?delete=1&amp;courseid='.$courseid.'&amp;id='.$groupid);
if(count($groupids)==0) {
print_error('errorselectsome','group',$returnurl);
}
$groupidlist=implode(',',$groupids);
redirect('delete.php?courseid='.$courseid.'&groups='.$groupidlist);
break;

case 'showcreateorphangroupform':
Expand All @@ -75,7 +104,7 @@
break;

case 'showgroupsettingsform':
redirect('group.php?courseid='.$courseid.'&amp;id='.$groupid);
redirect('group.php?courseid='.$courseid.'&amp;id='.$groupids[0]);
break;

case 'updategroups': //Currently reloading.
Expand All @@ -85,7 +114,7 @@
break;

case 'showaddmembersform':
redirect('members.php?group='.$groupid);
redirect('members.php?group='.$groupids[0]);
break;

case 'updatemembers': //Currently reloading.
Expand Down Expand Up @@ -113,19 +142,18 @@
require('tabs.php');

$disabled = 'disabled="disabled"';

$showeditgroupsettingsform_disabled = $disabled;
$deletegroup_disabled = $disabled;
$showcreategroupform_disabled = $disabled;

if (!empty($groupid)) {
if (ajaxenabled()) {
// Some buttons are enabled if single group selected
$showaddmembersform_disabled = $singlegroup ? '' : $disabled;
$showeditgroupsettingsform_disabled = $singlegroup ? '' : $disabled;
$deletegroup_disabled = count($groupids)>0 ? '' : $disabled;
} else {
// Do not disable buttons. The buttons work based on the selected group,
// which you can change without reloading the page, so it is not appropriate
// to disable them if no group is selected.
$showaddmembersform_disabled = '';
$showeditgroupsettingsform_disabled = '';
$deletegroup_disabled = '';
} else {
$deletegroup_disabled = $disabled;
$showeditgroupsettingsform_disabled = $disabled;
$showaddmembersform_disabled = $disabled;
}

print_heading(format_string($course->shortname) .' '.$strgroups, 'center', 3);
Expand All @@ -141,30 +169,31 @@
echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping">&nbsp;</span></label></p>'."\n";

if (ajaxenabled()) {
$onchange = 'membersCombo.refreshMembers(this.options[this.selectedIndex].value);';
$onchange = 'membersCombo.refreshMembers();';
} else {
$onchange = '';
}


echo '<select name="group" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
echo ' onclick="window.status=this.selectedIndex==-1 ? \'\' : this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";

$groups = groups_get_all_groups($courseid);

$sel_groupid = 0;
$selectedname = '&nbsp;';

if ($groups) {
// Print out the HTML
foreach ($groups as $group) {
$select = '';
if ($groupid == $group->id) {
$select = ' selected="selected"';
$sel_groupid = $group->id;
}
$usercount = $DB->count_records('groups_members', array('groupid'=>$group->id));
$groupname = format_string($group->name).' ('.$usercount.')';

if (in_array($group->id,$groupids)) {
$select = ' selected="selected"';
if ($singlegroup) {
// Only keep selected name if there is one group selected
$selectedname = $groupname;
}
}

echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
}
} else {
Expand All @@ -188,17 +217,19 @@

echo '</td>'."\n";
echo '<td>'."\n";
echo '<p><label for="members"><span id="memberslabel">'.get_string('membersofselectedgroup', 'group').' </span><span id="thegroup">&nbsp;</span></label></p>'."\n";

echo '<p><label for="members"><span id="memberslabel">'.
get_string('membersofselectedgroup', 'group').
' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
//NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
echo '<select name="user" id="members" size="15" class="select"'."\n";
echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";

$member_names = array();

$atleastonemember = false;
if ($sel_groupid) {

if ($groupmemberroles = groups_get_members_by_role($groupid,$courseid,'u.id,u.firstname,u.lastname')) {
if ($singlegroup) {
if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
foreach($groupmemberroles as $roleid=>$roledata) {
echo '<optgroup label="'.s($roledata->name).'">';
foreach($roledata->users as $member) {
Expand Down
3 changes: 3 additions & 0 deletions lang/en_utf8/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
$string['erroreditgrouping'] = 'Error creating/updating grouping $a';
$string['erroreditgroup'] = 'Error creating/updating group $a';
$string['erroraddremoveuser'] = 'Error adding/removing user $a to group';
$string['errorselectone'] = 'Please select a single group before choosing this option';
$string['errorselectsome'] = 'Please select one or more groups before choosing this option';

$string['groupings'] = 'Groupings';
$string['grouping'] = 'Grouping';
Expand All @@ -32,6 +34,7 @@

$string['deletegroupingconfirm'] = 'Are you sure you want to delete grouping \'$a\'? (Groups in the grouping are not deleted.)';
$string['deletegroupconfirm'] = 'Are you sure you want to delete group \'$a\'?';
$string['deletegroupsconfirm'] = 'Are you sure you want to delete the following groups?';

$string['editgroupingsettings'] = 'Edit grouping settings';
$string['deletegrouping'] = 'Delete grouping';
Expand Down

0 comments on commit f6eece1

Please sign in to comment.