MDL-10383 - groups/groupings refactoring and improvements - basic UI for groupings...
[moodle.git] / group / assign.php
1 <?php
2 /**
3  * Add/remove group from grouping.
4  * @package groups
5  */
6 require_once('../config.php');
7 require_once('lib.php');
9 $groupingid = required_param('id', PARAM_INT);
11 if (!$grouping = get_record('groupings', 'id', $groupingid)) {
12     error('Incorrect group id');
13 }
15 if (! $course = get_record('course', 'id', $grouping->courseid)) {
16     print_error('invalidcourse');
17 }
18 $courseid = $course->id;
20 require_login($course);
21 $context = get_context_instance(CONTEXT_COURSE, $courseid);
22 require_capability('moodle/course:managegroups', $context);
24 $returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$courseid;
27 if ($frm = data_submitted() and confirm_sesskey()) {
29     if (isset($frm->cancel)) {
30         redirect($returnurl);
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;
37             }
38             $assign = new object();
39             $assign->groupingid = $grouping->id;
40             $assign->groupid = $groupid;
41             $assign->timeadded = time();
42             insert_record('groupings_groups', $assign);
43         }
45     } else if (isset($frm->remove) and !empty($frm->removeselect)) {
47         foreach ($frm->removeselect as $groupid) {
48             $groupid = (int)$groupid;
49             delete_records('groupings_groups', 'groupingid', $grouping->id, 'groupid', $groupid);
50         }
51     }
52 }
55 $currentmembers = array();
56 $potentialmembers  = array();
58 if ($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         }
64     }
65     $potentialmembers = $groups;
66 }
68 $currentmembersoptions = '';
69 $currentmemberscount = 0;
70 if ($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 }
79 $potentialmembersoptions = '';
80 $potentialmemberscount = 0;
81 if ($potentialmembers) {
82     foreach($potentialmembers as $group) {
83         $potentialmembersoptions .= '<option value="'.$group->id.'.">'.format_string($group->name).'</option>';
84         $potentialmemberscount ++;
85     }
86 } else {
87     $potentialmembersoptions .= '<option>&nbsp;</option>';
88 }
90 // Print the page and form
91 $strgroups = get_string('groups');
92 $strparticipants = get_string('participants');
94 $groupingname = format_string($grouping->name);
96 print_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));
103 ?>
104 <div id="addmembersform">
105     <h3 class="main"><?php print_string('addgroupstogroupings', 'group'); echo ": $groupingname"; ?></h3>
107     <form id="assignform" method="post" action="">
108     <div>
109     <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
110     <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
112     <table summary="" cellpadding="5" cellspacing="0">
113     <tr>
114       <td valign="top">
115           <label for="removeselect"><?php print_string('existingmembers', 'group', $currentmemberscount); //count($contextusers) ?></label>
116           <br />
117           <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
118                   onfocus="document.getElementById('assignform').add.disabled=true;
119                            document.getElementById('assignform').remove.disabled=false;
120                            document.getElementById('assignform').addselect.selectedIndex=-1;">
121           <?php echo $currentmembersoptions ?>
122           </select></td>
123       <td valign="top">
124 <?php // Hidden assignment? ?>
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">
134           <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
135           <br />
136           <select name="addselect[]" size="20" id="addselect" multiple="multiple"
137                   onfocus="document.getElementById('assignform').add.disabled=false;
138                            document.getElementById('assignform').remove.disabled=true;
139                            document.getElementById('assignform').removeselect.selectedIndex=-1;">
140          <?php echo $potentialmembersoptions ?>
141          </select>
142          <br />
143        </td>
144     </tr>
145     <tr><td>
146         <input type="submit" name="cancel" value="<?php print_string('backtogroupings', 'group'); ?>" />
147     </td></tr>
148     </table>
149     </div>
150     </form>
151 </div>
153 <?php
154     print_footer($course);
157 ?>