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/ -> overview.php (source)

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * Print an overview of groupings & group membership
  20   *
  21   * @copyright  Matt Clarkson mattc@catalyst.net.nz
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @package    core_group
  24   */
  25  
  26  require_once('../config.php');
  27  require_once($CFG->libdir . '/filelib.php');
  28  
  29  define('OVERVIEW_NO_GROUP', -1); // The fake group for users not in a group.
  30  define('OVERVIEW_GROUPING_GROUP_NO_GROUPING', -1); // The fake grouping for groups that have no grouping.
  31  define('OVERVIEW_GROUPING_NO_GROUP', -2); // The fake grouping for users with no group.
  32  
  33  $courseid   = required_param('id', PARAM_INT);
  34  $groupid    = optional_param('group', 0, PARAM_INT);
  35  $groupingid = optional_param('grouping', 0, PARAM_INT);
  36  
  37  $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
  38  $rooturl   = $CFG->wwwroot.'/group/overview.php?id='.$courseid;
  39  
  40  if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
  41      print_error('invalidcourse');
  42  }
  43  
  44  $url = new moodle_url('/group/overview.php', array('id'=>$courseid));
  45  if ($groupid !== 0) {
  46      $url->param('group', $groupid);
  47  }
  48  if ($groupingid !== 0) {
  49      $url->param('grouping', $groupingid);
  50  }
  51  $PAGE->set_url($url);
  52  
  53  // Make sure that the user has permissions to manage groups.
  54  require_login($course);
  55  
  56  $context = context_course::instance($courseid);
  57  require_capability('moodle/course:managegroups', $context);
  58  
  59  $strgroups           = get_string('groups');
  60  $strparticipants     = get_string('participants');
  61  $stroverview         = get_string('overview', 'group');
  62  $strgrouping         = get_string('grouping', 'group');
  63  $strgroup            = get_string('group', 'group');
  64  $strnotingrouping    = get_string('notingrouping', 'group');
  65  $strfiltergroups     = get_string('filtergroups', 'group');
  66  $strnogroups         = get_string('nogroups', 'group');
  67  $strdescription      = get_string('description');
  68  $strnotingroup       = get_string('notingrouplist', 'group');
  69  $strnogroup          = get_string('nogroup', 'group');
  70  $strnogrouping       = get_string('nogrouping', 'group');
  71  
  72  // This can show all users and all groups in a course.
  73  // This is lots of data so allow this script more resources.
  74  raise_memory_limit(MEMORY_EXTRA);
  75  
  76  // Get all groupings and sort them by formatted name.
  77  $groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name');
  78  foreach ($groupings as $gid => $grouping) {
  79      $groupings[$gid]->formattedname = format_string($grouping->name, true, array('context' => $context));
  80  }
  81  core_collator::asort_objects_by_property($groupings, 'formattedname');
  82  $members = array();
  83  foreach ($groupings as $grouping) {
  84      $members[$grouping->id] = array();
  85  }
  86  // Groups not in a grouping.
  87  $members[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = array();
  88  
  89  // Get all groups
  90  $groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
  91  
  92  $params = array('courseid'=>$courseid);
  93  if ($groupid) {
  94      $groupwhere = "AND g.id = :groupid";
  95      $params['groupid']   = $groupid;
  96  } else {
  97      $groupwhere = "";
  98  }
  99  
 100  if ($groupingid) {
 101      if ($groupingid < 0) { // No grouping filter.
 102          $groupingwhere = "AND gg.groupingid IS NULL";
 103      } else {
 104          $groupingwhere = "AND gg.groupingid = :groupingid";
 105          $params['groupingid'] = $groupingid;
 106      }
 107  } else {
 108      $groupingwhere = "";
 109  }
 110  
 111  list($sort, $sortparams) = users_order_by_sql('u');
 112  
 113  $userfieldsapi = \core_user\fields::for_identity($context)->with_userpic();
 114  [
 115      'selects' => $userfieldsselects,
 116      'joins' => $userfieldsjoin,
 117      'params' => $userfieldsparams
 118  ] = (array)$userfieldsapi->get_sql('u', true);
 119  $extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
 120  $allnames = 'u.id ' . $userfieldsselects;
 121  
 122  $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
 123            FROM {groups} g
 124                 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
 125                 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
 126                 LEFT JOIN {user} u ON gm.userid = u.id
 127                 $userfieldsjoin
 128           WHERE g.courseid = :courseid $groupwhere $groupingwhere
 129        ORDER BY g.name, $sort";
 130  
 131  $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams, $userfieldsparams));
 132  foreach ($rs as $row) {
 133      $user = username_load_fields_from_object((object) [], $row, null,
 134          array_merge(['id' => 'userid', 'username', 'idnumber'], $extrafields));
 135  
 136      if (!$row->groupingid) {
 137          $row->groupingid = OVERVIEW_GROUPING_GROUP_NO_GROUPING;
 138      }
 139      if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
 140          $members[$row->groupingid][$row->groupid] = array();
 141      }
 142      if (!empty($user->id)) {
 143          $members[$row->groupingid][$row->groupid][] = $user;
 144      }
 145  }
 146  $rs->close();
 147  
 148  // Add 'no groupings' / 'no groups' selectors.
 149  $groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = (object)array(
 150      'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING,
 151      'formattedname' => $strnogrouping,
 152  );
 153  $groups[OVERVIEW_NO_GROUP] = (object)array(
 154      'id' => OVERVIEW_NO_GROUP,
 155      'courseid' => $courseid,
 156      'idnumber' => '',
 157      'name' => $strnogroup,
 158      'description' => '',
 159      'descriptionformat' => FORMAT_HTML,
 160      'enrolmentkey' => '',
 161      'picture' => 0,
 162      'timecreated' => 0,
 163      'timemodified' => 0,
 164  );
 165  
 166  // Add users who are not in a group.
 167  if ($groupid <= 0 && $groupingid <= 0) {
 168      list($esql, $params) = get_enrolled_sql($context, null, 0, true);
 169      $sql = "SELECT u.id, $allnames, u.idnumber, u.username
 170                FROM {user} u
 171                JOIN ($esql) e ON e.id = u.id
 172           LEFT JOIN (
 173                    SELECT gm.userid
 174                      FROM {groups_members} gm
 175                      JOIN {groups} g ON g.id = gm.groupid
 176                     WHERE g.courseid = :courseid
 177                     ) grouped ON grouped.userid = u.id
 178                    $userfieldsjoin
 179               WHERE grouped.userid IS NULL";
 180      $params['courseid'] = $courseid;
 181  
 182      $nogroupusers = $DB->get_records_sql($sql, array_merge($params, $userfieldsparams));
 183  
 184      if ($nogroupusers) {
 185          $members[OVERVIEW_GROUPING_NO_GROUP][OVERVIEW_NO_GROUP] = $nogroupusers;
 186      }
 187  }
 188  
 189  navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
 190  $PAGE->navbar->add(get_string('overview', 'group'));
 191  
 192  /// Print header
 193  $PAGE->set_title($strgroups);
 194  $PAGE->set_heading($course->fullname);
 195  $PAGE->set_pagelayout('standard');
 196  echo $OUTPUT->header();
 197  
 198  // Add tabs
 199  $currenttab = 'overview';
 200  require ('tabs.php');
 201  
 202  /// Print overview
 203  echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
 204  
 205  echo $strfiltergroups;
 206  
 207  $options = array();
 208  $options[0] = get_string('all');
 209  foreach ($groupings as $grouping) {
 210      $options[$grouping->id] = strip_tags($grouping->formattedname);
 211  }
 212  $popupurl = new moodle_url($rooturl.'&group='.$groupid);
 213  $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
 214  $select->label = $strgrouping;
 215  $select->formid = 'selectgrouping';
 216  echo $OUTPUT->render($select);
 217  
 218  $options = array();
 219  $options[0] = get_string('all');
 220  foreach ($groups as $group) {
 221      $options[$group->id] = strip_tags(format_string($group->name));
 222  }
 223  $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
 224  $select = new single_select($popupurl, 'group', $options, $groupid, array());
 225  $select->label = $strgroup;
 226  $select->formid = 'selectgroup';
 227  echo $OUTPUT->render($select);
 228  
 229  /// Print table
 230  $printed = false;
 231  $hoverevents = array();
 232  foreach ($members as $gpgid=>$groupdata) {
 233      if ($groupingid and $groupingid != $gpgid) {
 234          if ($groupingid > 0 || $gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
 235              continue; // Do not show.
 236          }
 237      }
 238      $table = new html_table();
 239      $table->head  = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
 240      $table->size  = array('20%', '70%', '10%');
 241      $table->align = array('left', 'left', 'center');
 242      $table->width = '90%';
 243      $table->data  = array();
 244      foreach ($groupdata as $gpid=>$users) {
 245          if ($groupid and $groupid != $gpid) {
 246              continue;
 247          }
 248          $line = array();
 249          $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
 250          $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
 251          $options = new stdClass;
 252          $options->noclean = true;
 253          $options->overflowdiv = true;
 254          $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options));
 255          if (empty($jsdescription)) {
 256              $line[] = $name;
 257          } else {
 258              $line[] = html_writer::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid));
 259              $hoverevents[$gpid] = get_string('descriptiona', null, $jsdescription);
 260          }
 261          $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
 262          $fullnames = array();
 263          foreach ($users as $user) {
 264              $displayname = fullname($user, $viewfullnames);
 265              if ($extrafields) {
 266                  $extrafieldsdisplay = [];
 267                  foreach ($extrafields as $field) {
 268                      $extrafieldsdisplay[] = s($user->{$field});
 269                  }
 270                  $displayname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
 271              }
 272  
 273              $fullnames[] = html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $course->id]),
 274                  $displayname);
 275          }
 276          $line[] = implode(', ', $fullnames);
 277          $line[] = count($users);
 278          $table->data[] = $line;
 279      }
 280      if ($groupid and empty($table->data)) {
 281          continue;
 282      }
 283      if ($gpgid < 0) {
 284          // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
 285          if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) {
 286              echo $OUTPUT->heading($strnotingroup, 3);
 287          } else {
 288              echo $OUTPUT->heading($strnotingrouping, 3);
 289          }
 290      } else {
 291          echo $OUTPUT->heading($groupings[$gpgid]->formattedname, 3);
 292          $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
 293          $options = new stdClass;
 294          $options->overflowdiv = true;
 295          echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter');
 296      }
 297      echo html_writer::table($table);
 298      $printed = true;
 299  }
 300  
 301  if (count($hoverevents)>0) {
 302      $PAGE->requires->string_for_js('description', 'moodle');
 303      $PAGE->requires->js_init_call('M.core_group.init_hover_events', array($hoverevents));
 304  }
 305  
 306  echo $OUTPUT->footer();