members.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 members from group.
  18. *
  19. * @copyright 2006 The Open University and others, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk and others
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_group
  22. */
  23. require_once(__DIR__ . '/../config.php');
  24. require_once(__DIR__ . '/lib.php');
  25. require_once($CFG->dirroot . '/user/selector/lib.php');
  26. require_once($CFG->dirroot . '/course/lib.php');
  27. require_once($CFG->libdir . '/filelib.php');
  28. $groupid = required_param('group', PARAM_INT);
  29. $cancel = optional_param('cancel', false, PARAM_BOOL);
  30. $group = $DB->get_record('groups', array('id'=>$groupid), '*', MUST_EXIST);
  31. $course = $DB->get_record('course', array('id'=>$group->courseid), '*', MUST_EXIST);
  32. $PAGE->set_url('/group/members.php', array('group'=>$groupid));
  33. $PAGE->set_pagelayout('admin');
  34. require_login($course);
  35. $context = context_course::instance($course->id);
  36. require_capability('moodle/course:managegroups', $context);
  37. $returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&group='.$group->id;
  38. if ($cancel) {
  39. redirect($returnurl);
  40. }
  41. $groupmembersselector = new group_members_selector('removeselect', array('groupid' => $groupid, 'courseid' => $course->id));
  42. $potentialmembersselector = new group_non_members_selector('addselect', array('groupid' => $groupid, 'courseid' => $course->id));
  43. if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
  44. $userstoadd = $potentialmembersselector->get_selected_users();
  45. if (!empty($userstoadd)) {
  46. foreach ($userstoadd as $user) {
  47. if (!groups_add_member($groupid, $user->id)) {
  48. print_error('erroraddremoveuser', 'group', $returnurl);
  49. }
  50. $groupmembersselector->invalidate_selected_users();
  51. $potentialmembersselector->invalidate_selected_users();
  52. }
  53. }
  54. }
  55. if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
  56. $userstoremove = $groupmembersselector->get_selected_users();
  57. if (!empty($userstoremove)) {
  58. foreach ($userstoremove as $user) {
  59. if (!groups_remove_member_allowed($groupid, $user->id)) {
  60. print_error('errorremovenotpermitted', 'group', $returnurl,
  61. $user->fullname);
  62. }
  63. if (!groups_remove_member($groupid, $user->id)) {
  64. print_error('erroraddremoveuser', 'group', $returnurl);
  65. }
  66. $groupmembersselector->invalidate_selected_users();
  67. $potentialmembersselector->invalidate_selected_users();
  68. }
  69. }
  70. }
  71. // Print the page and form
  72. $strgroups = get_string('groups');
  73. $strparticipants = get_string('participants');
  74. $stradduserstogroup = get_string('adduserstogroup', 'group');
  75. $strusergroupmembership = get_string('usergroupmembership', 'group');
  76. $groupname = format_string($group->name);
  77. $PAGE->requires->js('/group/clientlib.js');
  78. $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$course->id)));
  79. $PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$course->id)));
  80. $PAGE->navbar->add($stradduserstogroup);
  81. /// Print header
  82. $PAGE->set_title("$course->shortname: $strgroups");
  83. $PAGE->set_heading($course->fullname);
  84. echo $OUTPUT->header();
  85. echo $OUTPUT->heading(get_string('adduserstogroup', 'group').": $groupname", 3);
  86. // Store the rows we want to display in the group info.
  87. $groupinforow = array();
  88. // Check if there is a picture to display.
  89. if (!empty($group->picture)) {
  90. $picturecell = new html_table_cell();
  91. $picturecell->attributes['class'] = 'left side picture';
  92. $picturecell->text = print_group_picture($group, $course->id, true, true, false);
  93. $groupinforow[] = $picturecell;
  94. }
  95. // Check if there is a description to display.
  96. $group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group', 'description', $group->id);
  97. if (!empty($group->description)) {
  98. if (!isset($group->descriptionformat)) {
  99. $group->descriptionformat = FORMAT_MOODLE;
  100. }
  101. $options = new stdClass;
  102. $options->overflowdiv = true;
  103. $contentcell = new html_table_cell();
  104. $contentcell->attributes['class'] = 'content';
  105. $contentcell->text = format_text($group->description, $group->descriptionformat, $options);
  106. $groupinforow[] = $contentcell;
  107. }
  108. // Check if we have something to show.
  109. if (!empty($groupinforow)) {
  110. $groupinfotable = new html_table();
  111. $groupinfotable->attributes['class'] = 'groupinfobox';
  112. $groupinfotable->data[] = new html_table_row($groupinforow);
  113. echo html_writer::table($groupinfotable);
  114. }
  115. /// Print the editing form
  116. ?>
  117. <div id="addmembersform">
  118. <form id="assignform" method="post" action="<?php echo $CFG->wwwroot; ?>/group/members.php?group=<?php echo $groupid; ?>">
  119. <div>
  120. <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
  121. <table class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
  122. <tr>
  123. <td id='existingcell'>
  124. <p>
  125. <label for="removeselect"><?php print_string('groupmembers', 'group'); ?></label>
  126. </p>
  127. <?php $groupmembersselector->display(); ?>
  128. </td>
  129. <td id='buttonscell'>
  130. <p class="arrow_button">
  131. <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
  132. <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
  133. </p>
  134. </td>
  135. <td id='potentialcell'>
  136. <p>
  137. <label for="addselect"><?php print_string('potentialmembs', 'group'); ?></label>
  138. </p>
  139. <?php $potentialmembersselector->display(); ?>
  140. </td>
  141. <td>
  142. <p><?php echo($strusergroupmembership) ?></p>
  143. <div id="group-usersummary"></div>
  144. </td>
  145. </tr>
  146. <tr><td colspan="3" id='backcell'>
  147. <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
  148. </td></tr>
  149. </table>
  150. </div>
  151. </form>
  152. </div>
  153. <?php
  154. //outputs the JS array used to display the other groups users are in
  155. $potentialmembersselector->print_user_summaries($course->id);
  156. //this must be after calling display() on the selectors so their setup JS executes first
  157. $PAGE->requires->js_init_call('init_add_remove_members_page', null, false, $potentialmembersselector->get_js_module());
  158. echo $OUTPUT->footer();