Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-16756: Groups bugfixes and multiple select support for deletion

  • Loading branch information...
commit 20c30ac5e47b2165d7625b29519e301d7276a66f 1 parent a240818
sam_marshall authored
View
42 group/clientlib.js
@@ -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.
@@ -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);
+ }
};
View
74 group/delete.php
@@ -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 = get_record('course', '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 = get_record('groups', '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);
+ }
+ begin_sql();
+ foreach($groupidarray as $groupid) {
+ if (!groups_delete_group($groupid)) {
+ print_error('erroreditgroup', 'group', $returnurl);
+ }
+ }
+ 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();
+}
+?>
View
6 group/group.php
@@ -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 = get_record('groups', 'id', $id)) {
error('Group ID was incorrect');
View
88 group/index.php
@@ -23,6 +23,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;
@@ -42,13 +56,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;
@@ -66,7 +91,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':
@@ -78,7 +107,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.
@@ -88,7 +117,7 @@
break;
case 'showaddmembersform':
- redirect('members.php?group='.$groupid);
+ redirect('members.php?group='.$groupids[0]);
break;
case 'updatemembers': //Currently reloading.
@@ -116,19 +145,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);
@@ -145,30 +173,32 @@
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 = (int)count_records('groups_members', '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 {
@@ -192,7 +222,10 @@
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";
@@ -200,9 +233,8 @@
$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="'.htmlspecialchars($roledata->name).'">';
foreach($roledata->users as $member) {
View
3  lang/en_utf8/group.php
@@ -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';
@@ -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';
Please sign in to comment.
Something went wrong with that request. Please try again.