Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/group/ -> members.php (source)

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

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