assign.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Add/remove group from grouping.
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_group
  22. */
  23. require_once('../config.php');
  24. require_once('lib.php');
  25. $groupingid = required_param('id', PARAM_INT);
  26. $PAGE->set_url('/group/assign.php', array('id'=>$groupingid));
  27. if (!$grouping = $DB->get_record('groupings', array('id'=>$groupingid))) {
  28. print_error('invalidgroupid');
  29. }
  30. if (!$course = $DB->get_record('course', array('id'=>$grouping->courseid))) {
  31. print_error('invalidcourse');
  32. }
  33. $courseid = $course->id;
  34. require_login($course);
  35. $context = context_course::instance($courseid);
  36. require_capability('moodle/course:managegroups', $context);
  37. $returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$courseid;
  38. if ($frm = data_submitted() and confirm_sesskey()) {
  39. if (isset($frm->cancel)) {
  40. redirect($returnurl);
  41. } else if (isset($frm->add) and !empty($frm->addselect)) {
  42. foreach ($frm->addselect as $groupid) {
  43. // Ask this method not to purge the cache, we'll do it ourselves afterwards.
  44. groups_assign_grouping($grouping->id, (int)$groupid, null, false);
  45. }
  46. // Invalidate the course groups cache seeing as we've changed it.
  47. cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
  48. } else if (isset($frm->remove) and !empty($frm->removeselect)) {
  49. foreach ($frm->removeselect as $groupid) {
  50. // Ask this method not to purge the cache, we'll do it ourselves afterwards.
  51. groups_unassign_grouping($grouping->id, (int)$groupid, false);
  52. }
  53. // Invalidate the course groups cache seeing as we've changed it.
  54. cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
  55. }
  56. }
  57. $currentmembers = array();
  58. $potentialmembers = array();
  59. if ($groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name')) {
  60. if ($assignment = $DB->get_records('groupings_groups', array('groupingid'=>$grouping->id))) {
  61. foreach ($assignment as $ass) {
  62. $currentmembers[$ass->groupid] = $groups[$ass->groupid];
  63. unset($groups[$ass->groupid]);
  64. }
  65. }
  66. $potentialmembers = $groups;
  67. }
  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. // Get course managers so they can be highlighted in the list
  76. if ($managerroles = get_config('', 'coursecontact')) {
  77. $coursecontactroles = explode(',', $managerroles);
  78. foreach ($coursecontactroles as $roleid) {
  79. $role = $DB->get_record('role', array('id'=>$roleid));
  80. $managers = get_role_users($roleid, $context, true, 'u.id', 'u.id ASC');
  81. }
  82. }
  83. } else {
  84. $currentmembersoptions .= '<option>&nbsp;</option>';
  85. }
  86. $potentialmembersoptions = '';
  87. $potentialmemberscount = 0;
  88. if ($potentialmembers) {
  89. foreach($potentialmembers as $group) {
  90. $potentialmembersoptions .= '<option value="'.$group->id.'.">'.format_string($group->name).'</option>';
  91. $potentialmemberscount ++;
  92. }
  93. } else {
  94. $potentialmembersoptions .= '<option>&nbsp;</option>';
  95. }
  96. // Print the page and form
  97. $strgroups = get_string('groups');
  98. $strparticipants = get_string('participants');
  99. $straddgroupstogroupings = get_string('addgroupstogroupings', 'group');
  100. $groupingname = format_string($grouping->name);
  101. navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$course->id)));
  102. $PAGE->set_pagelayout('admin');
  103. $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
  104. $PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
  105. $PAGE->navbar->add($straddgroupstogroupings);
  106. /// Print header
  107. $PAGE->set_title("$course->shortname: $strgroups");
  108. $PAGE->set_heading($course->fullname);
  109. echo $OUTPUT->header();
  110. ?>
  111. <div id="addmembersform">
  112. <h3 class="main"><?php print_string('addgroupstogroupings', 'group'); echo ": $groupingname"; ?></h3>
  113. <form id="assignform" method="post" action="">
  114. <div>
  115. <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
  116. <table summary="" class="generaltable generalbox groupmanagementtable boxaligncenter">
  117. <tr>
  118. <td id="existingcell">
  119. <label for="removeselect"><?php print_string('existingmembers', 'group', $currentmemberscount); ?></label>
  120. <div class="userselector" id="removeselect_wrapper">
  121. <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
  122. onfocus="document.getElementById('assignform').add.disabled=true;
  123. document.getElementById('assignform').remove.disabled=false;
  124. document.getElementById('assignform').addselect.selectedIndex=-1;">
  125. <?php echo $currentmembersoptions ?>
  126. </select></div></td>
  127. <td id="buttonscell">
  128. <p class="arrow_button">
  129. <input name="add" id="add" type="submit"
  130. value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
  131. title="<?php print_string('add'); ?>" /><br>
  132. <input name="remove" id="remove" type="submit"
  133. value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
  134. title="<?php print_string('remove'); ?>" />
  135. </p>
  136. </td>
  137. <td id="potentialcell">
  138. <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); ?></label>
  139. <div class="userselector" id="addselect_wrapper">
  140. <select name="addselect[]" size="20" id="addselect" multiple="multiple"
  141. onfocus="document.getElementById('assignform').add.disabled=false;
  142. document.getElementById('assignform').remove.disabled=true;
  143. document.getElementById('assignform').removeselect.selectedIndex=-1;">
  144. <?php echo $potentialmembersoptions ?>
  145. </select>
  146. </div>
  147. </td>
  148. </tr>
  149. <tr><td colspan="3" id="backcell">
  150. <input type="submit" name="cancel" value="<?php print_string('backtogroupings', 'group'); ?>" />
  151. </td></tr>
  152. </table>
  153. </div>
  154. </form>
  155. </div>
  156. <?php
  157. echo $OUTPUT->footer();