MDL-10383 - groups/groupings refactoring and improvements - basic UI for groupings...
[moodle.git] / group / assign.php
CommitLineData
f3f7610c
ML
1<?php
2/**
62d63838 3 * Add/remove group from grouping.
f3f7610c
ML
4 * @package groups
5 */
6require_once('../config.php');
7require_once('lib.php');
f3f7610c 8
62d63838 9$groupingid = required_param('id', PARAM_INT);
23431567 10
62d63838 11if (!$grouping = get_record('groupings', 'id', $groupingid)) {
2524b0f2 12 error('Incorrect group id');
13}
f3f7610c 14
62d63838 15if (! $course = get_record('course', 'id', $grouping->courseid)) {
b39552af 16 print_error('invalidcourse');
17}
2524b0f2 18$courseid = $course->id;
f3f7610c 19
62d63838 20require_login($course);
2524b0f2 21$context = get_context_instance(CONTEXT_COURSE, $courseid);
22require_capability('moodle/course:managegroups', $context);
23
62d63838 24$returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$courseid;
f3f7610c 25
2524b0f2 26
62d63838 27if ($frm = data_submitted() and confirm_sesskey()) {
f3f7610c 28
62d63838 29 if (isset($frm->cancel)) {
30 redirect($returnurl);
2524b0f2 31
62d63838 32 } else if (isset($frm->add) and !empty($frm->addselect)) {
33 foreach ($frm->addselect as $groupid) {
34 $groupid = (int)$groupid;
35 if (record_exists('groupings_groups', 'groupingid', $grouping->id, 'groupid', $groupid)) {
36 continue;
f3f7610c 37 }
62d63838 38 $assign = new object();
39 $assign->groupingid = $grouping->id;
40 $assign->groupid = $groupid;
41 $assign->timeadded = time();
42 insert_record('groupings_groups', $assign);
f3f7610c 43 }
2524b0f2 44
62d63838 45 } else if (isset($frm->remove) and !empty($frm->removeselect)) {
27c69abe 46
62d63838 47 foreach ($frm->removeselect as $groupid) {
48 $groupid = (int)$groupid;
49 delete_records('groupings_groups', 'groupingid', $grouping->id, 'groupid', $groupid);
27c69abe 50 }
27c69abe 51 }
62d63838 52}
2524b0f2 53
2524b0f2 54
62d63838 55$currentmembers = array();
56$potentialmembers = array();
57
58if ($groups = get_records('groups', 'courseid', $courseid, 'name')) {
59 if ($assignment = get_records('groupings_groups', 'groupingid', $grouping->id)) {
60 foreach ($assignment as $ass) {
61 $currentmembers[$ass->groupid] = $groups[$ass->groupid];
62 unset($groups[$ass->groupid]);
63 }
23431567 64 }
62d63838 65 $potentialmembers = $groups;
66}
27c69abe 67
62d63838 68$currentmembersoptions = '';
69$currentmemberscount = 0;
70if ($currentmembers) {
71 foreach($currentmembers as $group) {
72 $currentmembersoptions .= '<option value="'.$group->id.'.">'.format_string($group->name).'</option>';
73 $currentmemberscount ++;
74 }
75} else {
76 $currentmembersoptions .= '<option>&nbsp;</option>';
77}
23431567 78
62d63838 79$potentialmembersoptions = '';
80$potentialmemberscount = 0;
81if ($potentialmembers) {
82 foreach($potentialmembers as $group) {
83 $potentialmembersoptions .= '<option value="'.$group->id.'.">'.format_string($group->name).'</option>';
84 $potentialmemberscount ++;
27c69abe 85 }
62d63838 86} else {
87 $potentialmembersoptions .= '<option>&nbsp;</option>';
88}
f3f7610c 89
62d63838 90// Print the page and form
91$strgroups = get_string('groups');
92$strparticipants = get_string('participants');
f3f7610c 93
62d63838 94$groupingname = format_string($grouping->name);
f3f7610c 95
62d63838 96print_header("$course->shortname: $strgroups",
97 $course->fullname,
98 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
99 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
100 "-> <a href=\"$CFG->wwwroot/group/index.php?id=$courseid\">$strgroups</a>".
101 '-> '. get_string('addgroupstogroupings', 'group'), '', '', true, '', user_login_string($course, $USER));
f3f7610c 102
f3f7610c
ML
103?>
104<div id="addmembersform">
62d63838 105 <h3 class="main"><?php print_string('addgroupstogroupings', 'group'); echo ": $groupingname"; ?></h3>
f3f7610c 106
b4e80a8b 107 <form id="assignform" method="post" action="">
108 <div>
f3f7610c 109 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
f3f7610c
ML
110 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
111
b4e80a8b 112 <table summary="" cellpadding="5" cellspacing="0">
f3f7610c
ML
113 <tr>
114 <td valign="top">
62d63838 115 <label for="removeselect"><?php print_string('existingmembers', 'group', $currentmemberscount); //count($contextusers) ?></label>
f3f7610c
ML
116 <br />
117 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
b4e80a8b 118 onfocus="document.getElementById('assignform').add.disabled=true;
119 document.getElementById('assignform').remove.disabled=false;
120 document.getElementById('assignform').addselect.selectedIndex=-1;">
62d63838 121 <?php echo $currentmembersoptions ?>
f3f7610c
ML
122 </select></td>
123 <td valign="top">
124<?php // Hidden assignment? ?>
125
126 <?php check_theme_arrows(); ?>
127 <p class="arrow_button">
128 <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
129 <br />
130 <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
131 </p>
132 </td>
133 <td valign="top">
27c69abe 134 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
f3f7610c
ML
135 <br />
136 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
b4e80a8b 137 onfocus="document.getElementById('assignform').add.disabled=false;
138 document.getElementById('assignform').remove.disabled=true;
139 document.getElementById('assignform').removeselect.selectedIndex=-1;">
62d63838 140 <?php echo $potentialmembersoptions ?>
f3f7610c
ML
141 </select>
142 <br />
f3f7610c
ML
143 </td>
144 </tr>
145 <tr><td>
62d63838 146 <input type="submit" name="cancel" value="<?php print_string('backtogroupings', 'group'); ?>" />
f3f7610c
ML
147 </td></tr>
148 </table>
b4e80a8b 149 </div>
f3f7610c
ML
150 </form>
151</div>
152
153<?php
154 print_footer($course);
62d63838 155
156
f3f7610c 157?>