Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/group/ -> index.php (source)

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * The main group management user interface.
  20   *
  21   * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @package   core_group
  24   */
  25  require_once('../config.php');
  26  require_once ('lib.php');
  27  
  28  $courseid = required_param('id', PARAM_INT);
  29  $groupid  = optional_param('group', false, PARAM_INT);
  30  $userid   = optional_param('user', false, PARAM_INT);
  31  $action   = groups_param_action();
  32  // Support either single group= parameter, or array groups[]
  33  if ($groupid) {
  34      $groupids = array($groupid);
  35  } else {
  36      $groupids = optional_param_array('groups', array(), PARAM_INT);
  37  }
  38  $singlegroup = (count($groupids) == 1);
  39  
  40  $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
  41  
  42  // Get the course information so we can print the header and
  43  // check the course id is valid
  44  
  45  $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
  46  
  47  $url = new moodle_url('/group/index.php', array('id'=>$courseid));
  48  navigation_node::override_active_url($url);
  49  if ($userid) {
  50      $url->param('user', $userid);
  51  }
  52  if ($groupid) {
  53      $url->param('group', $groupid);
  54  }
  55  $PAGE->set_url($url);
  56  
  57  // Make sure that the user has permissions to manage groups.
  58  require_login($course);
  59  
  60  $context = context_course::instance($course->id);
  61  require_capability('moodle/course:managegroups', $context);
  62  
  63  $PAGE->requires->js('/group/clientlib.js', true);
  64  $PAGE->requires->js('/group/module.js', true);
  65  
  66  // Check for multiple/no group errors
  67  if (!$singlegroup) {
  68      switch($action) {
  69          case 'ajax_getmembersingroup':
  70          case 'showgroupsettingsform':
  71          case 'showaddmembersform':
  72          case 'updatemembers':
  73              print_error('errorselectone', 'group', $returnurl);
  74      }
  75  }
  76  
  77  switch ($action) {
  78      case false: //OK, display form.
  79          break;
  80  
  81      case 'ajax_getmembersingroup':
  82          $roles = array();
  83  
  84          $extrafields = get_extra_user_fields($context);
  85          if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid,
  86                  'u.id, ' . user_picture::fields('u', $extrafields))) {
  87  
  88              $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
  89  
  90              foreach($groupmemberroles as $roleid=>$roledata) {
  91                  $shortroledata = new stdClass();
  92                  $shortroledata->name = $roledata->name;
  93                  $shortroledata->users = array();
  94                  foreach($roledata->users as $member) {
  95                      $shortmember = new stdClass();
  96                      $shortmember->id = $member->id;
  97                      $shortmember->name = fullname($member, $viewfullnames);
  98                      if ($extrafields) {
  99                          $extrafieldsdisplay = [];
 100                          foreach ($extrafields as $field) {
 101                              $extrafieldsdisplay[] = s($member->{$field});
 102                          }
 103                          $shortmember->name .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
 104                      }
 105  
 106                      $shortroledata->users[] = $shortmember;
 107                  }
 108                  $roles[] = $shortroledata;
 109              }
 110          }
 111          echo json_encode($roles);
 112          die;  // Client side JavaScript takes it from here.
 113  
 114      case 'deletegroup':
 115          if (count($groupids) == 0) {
 116              print_error('errorselectsome','group',$returnurl);
 117          }
 118          $groupidlist = implode(',', $groupids);
 119          redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
 120          break;
 121  
 122      case 'showcreateorphangroupform':
 123          redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
 124          break;
 125  
 126      case 'showautocreategroupsform':
 127          redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
 128          break;
 129  
 130      case 'showimportgroups':
 131          redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
 132          break;
 133  
 134      case 'showgroupsettingsform':
 135          redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
 136          break;
 137  
 138      case 'updategroups': //Currently reloading.
 139          break;
 140  
 141      case 'removemembers':
 142          break;
 143  
 144      case 'showaddmembersform':
 145          redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
 146          break;
 147  
 148      case 'updatemembers': //Currently reloading.
 149          break;
 150  
 151      default: //ERROR.
 152          print_error('unknowaction', '', $returnurl);
 153          break;
 154  }
 155  
 156  // Print the page and form
 157  $strgroups = get_string('groups');
 158  $strparticipants = get_string('participants');
 159  
 160  /// Print header
 161  $PAGE->set_title($strgroups);
 162  $PAGE->set_heading($course->fullname);
 163  $PAGE->set_pagelayout('standard');
 164  echo $OUTPUT->header();
 165  
 166  // Add tabs
 167  $currenttab = 'groups';
 168  require ('tabs.php');
 169  
 170  echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$strgroups, 3);
 171  
 172  $groups = groups_get_all_groups($courseid);
 173  $selectedname = null;
 174  $preventgroupremoval = array();
 175  
 176  // Get list of groups to render.
 177  $groupoptions = array();
 178  if ($groups) {
 179      foreach ($groups as $group) {
 180          $selected = false;
 181          $usercount = $DB->count_records('groups_members', array('groupid' => $group->id));
 182          $groupname = format_string($group->name) . ' (' . $usercount . ')';
 183          if (in_array($group->id, $groupids)) {
 184              $selected = true;
 185              if ($singlegroup) {
 186                  // Only keep selected name if there is one group selected.
 187                  $selectedname = $groupname;
 188              }
 189          }
 190          if (!empty($group->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
 191              $preventgroupremoval[$group->id] = true;
 192          }
 193  
 194          $groupoptions[] = (object) [
 195              'value' => $group->id,
 196              'selected' => $selected,
 197              'text' => $groupname
 198          ];
 199      }
 200  }
 201  
 202  // Get list of group members to render if there is a single selected group.
 203  $members = array();
 204  if ($singlegroup) {
 205      $extrafields = get_extra_user_fields($context);
 206      if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid,
 207              'u.id, ' . user_picture::fields('u', $extrafields))) {
 208  
 209          $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
 210  
 211          foreach ($groupmemberroles as $roleid => $roledata) {
 212              $users = array();
 213              foreach ($roledata->users as $member) {
 214                  $shortmember = new stdClass();
 215                  $shortmember->value = $member->id;
 216                  $shortmember->text = fullname($member, $viewfullnames);
 217                  if ($extrafields) {
 218                      $extrafieldsdisplay = [];
 219                      foreach ($extrafields as $field) {
 220                          $extrafieldsdisplay[] = s($member->{$field});
 221                      }
 222                      $shortmember->text .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
 223                  }
 224  
 225                  $users[] = $shortmember;
 226              }
 227              $members[] = (object)[
 228                  'role' => s($roledata->name),
 229                  'rolemembers' => $users
 230              ];
 231          }
 232      }
 233  }
 234  
 235  $disableaddedit = !$singlegroup;
 236  $disabledelete = !empty($groupids);
 237  $renderable = new \core_group\output\index_page($courseid, $groupoptions, $selectedname, $members, $disableaddedit, $disabledelete,
 238          $preventgroupremoval);
 239  $output = $PAGE->get_renderer('core_group');
 240  echo $output->render($renderable);
 241  
 242  echo $OUTPUT->footer();
 243  
 244  /**
 245   * Returns the first button action with the given prefix, taken from
 246   * POST or GET, otherwise returns false.
 247   * @see /lib/moodlelib.php function optional_param().
 248   * @param string $prefix 'act_' as in 'action'.
 249   * @return string The action without the prefix, or false if no action found.
 250   */
 251  function groups_param_action($prefix = 'act_') {
 252      $action = false;
 253  //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
 254  
 255      if ($_POST) {
 256          $form_vars = $_POST;
 257      }
 258      elseif ($_GET) {
 259          $form_vars = $_GET;
 260      }
 261      if ($form_vars) {
 262          foreach ($form_vars as $key => $value) {
 263              if (preg_match("/$prefix(.+)/", $key, $matches)) {
 264                  $action = $matches[1];
 265                  break;
 266              }
 267          }
 268      }
 269      if ($action && !preg_match('/^\w+$/', $action)) {
 270          $action = false;
 271          print_error('unknowaction');
 272      }
 273      ///if (debugging()) echo 'Debug: '.$action;
 274      return $action;
 275  }