Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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/ -> overview.php (source)

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * 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  $extrafields = get_extra_user_fields($context);
 114  $allnames = 'u.id, ' . user_picture::fields('u', $extrafields);
 115  
 116  $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
 117            FROM {groups} g
 118                 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
 119                 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
 120                 LEFT JOIN {user} u ON gm.userid = u.id
 121           WHERE g.courseid = :courseid $groupwhere $groupingwhere
 122        ORDER BY g.name, $sort";
 123  
 124  $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
 125  foreach ($rs as $row) {
 126      $user = username_load_fields_from_object((object) [], $row, null,
 127          array_merge(['id' => 'userid', 'username', 'idnumber'], $extrafields));
 128  
 129      if (!$row->groupingid) {
 130          $row->groupingid = OVERVIEW_GROUPING_GROUP_NO_GROUPING;
 131      }
 132      if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
 133          $members[$row->groupingid][$row->groupid] = array();
 134      }
 135      if (!empty($user->id)) {
 136          $members[$row->groupingid][$row->groupid][] = $user;
 137      }
 138  }
 139  $rs->close();
 140  
 141  // Add 'no groupings' / 'no groups' selectors.
 142  $groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = (object)array(
 143      'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING,
 144      'formattedname' => $strnogrouping,
 145  );
 146  $groups[OVERVIEW_NO_GROUP] = (object)array(
 147      'id' => OVERVIEW_NO_GROUP,
 148      'courseid' => $courseid,
 149      'idnumber' => '',
 150      'name' => $strnogroup,
 151      'description' => '',
 152      'descriptionformat' => FORMAT_HTML,
 153      'enrolmentkey' => '',
 154      'picture' => 0,
 155      'hidepicture' => 0,
 156      'timecreated' => 0,
 157      'timemodified' => 0,
 158  );
 159  
 160  // Add users who are not in a group.
 161  if ($groupid <= 0 && $groupingid <= 0) {
 162      list($esql, $params) = get_enrolled_sql($context, null, 0, true);
 163      $sql = "SELECT u.id, $allnames, u.idnumber, u.username
 164                FROM {user} u
 165                JOIN ($esql) e ON e.id = u.id
 166           LEFT JOIN (
 167                    SELECT gm.userid
 168                      FROM {groups_members} gm
 169                      JOIN {groups} g ON g.id = gm.groupid
 170                     WHERE g.courseid = :courseid
 171                     ) grouped ON grouped.userid = u.id
 172               WHERE grouped.userid IS NULL";
 173      $params['courseid'] = $courseid;
 174  
 175      $nogroupusers = $DB->get_records_sql($sql, $params);
 176  
 177      if ($nogroupusers) {
 178          $members[OVERVIEW_GROUPING_NO_GROUP][OVERVIEW_NO_GROUP] = $nogroupusers;
 179      }
 180  }
 181  
 182  navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
 183  $PAGE->navbar->add(get_string('overview', 'group'));
 184  
 185  /// Print header
 186  $PAGE->set_title($strgroups);
 187  $PAGE->set_heading($course->fullname);
 188  $PAGE->set_pagelayout('standard');
 189  echo $OUTPUT->header();
 190  
 191  // Add tabs
 192  $currenttab = 'overview';
 193  require ('tabs.php');
 194  
 195  /// Print overview
 196  echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
 197  
 198  echo $strfiltergroups;
 199  
 200  $options = array();
 201  $options[0] = get_string('all');
 202  foreach ($groupings as $grouping) {
 203      $options[$grouping->id] = strip_tags($grouping->formattedname);
 204  }
 205  $popupurl = new moodle_url($rooturl.'&group='.$groupid);
 206  $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
 207  $select->label = $strgrouping;
 208  $select->formid = 'selectgrouping';
 209  echo $OUTPUT->render($select);
 210  
 211  $options = array();
 212  $options[0] = get_string('all');
 213  foreach ($groups as $group) {
 214      $options[$group->id] = strip_tags(format_string($group->name));
 215  }
 216  $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
 217  $select = new single_select($popupurl, 'group', $options, $groupid, array());
 218  $select->label = $strgroup;
 219  $select->formid = 'selectgroup';
 220  echo $OUTPUT->render($select);
 221  
 222  /// Print table
 223  $printed = false;
 224  $hoverevents = array();
 225  foreach ($members as $gpgid=>$groupdata) {
 226      if ($groupingid and $groupingid != $gpgid) {
 227          if ($groupingid > 0 || $gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
 228              continue; // Do not show.
 229          }
 230      }
 231      $table = new html_table();
 232      $table->head  = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
 233      $table->size  = array('20%', '70%', '10%');
 234      $table->align = array('left', 'left', 'center');
 235      $table->width = '90%';
 236      $table->data  = array();
 237      foreach ($groupdata as $gpid=>$users) {
 238          if ($groupid and $groupid != $gpid) {
 239              continue;
 240          }
 241          $line = array();
 242          $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
 243          $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
 244          $options = new stdClass;
 245          $options->noclean = true;
 246          $options->overflowdiv = true;
 247          $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options));
 248          if (empty($jsdescription)) {
 249              $line[] = $name;
 250          } else {
 251              $line[] = html_writer::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid));
 252              $hoverevents[$gpid] = get_string('descriptiona', null, $jsdescription);
 253          }
 254          $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
 255          $fullnames = array();
 256          foreach ($users as $user) {
 257              $displayname = fullname($user, $viewfullnames);
 258              if ($extrafields) {
 259                  $extrafieldsdisplay = [];
 260                  foreach ($extrafields as $field) {
 261                      $extrafieldsdisplay[] = s($user->{$field});
 262                  }
 263                  $displayname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
 264              }
 265  
 266              $fullnames[] = html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $course->id]),
 267                  $displayname);
 268          }
 269          $line[] = implode(', ', $fullnames);
 270          $line[] = count($users);
 271          $table->data[] = $line;
 272      }
 273      if ($groupid and empty($table->data)) {
 274          continue;
 275      }
 276      if ($gpgid < 0) {
 277          // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
 278          if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) {
 279              echo $OUTPUT->heading($strnotingroup, 3);
 280          } else {
 281              echo $OUTPUT->heading($strnotingrouping, 3);
 282          }
 283      } else {
 284          echo $OUTPUT->heading($groupings[$gpgid]->formattedname, 3);
 285          $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
 286          $options = new stdClass;
 287          $options->overflowdiv = true;
 288          echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter');
 289      }
 290      echo html_writer::table($table);
 291      $printed = true;
 292  }
 293  
 294  if (count($hoverevents)>0) {
 295      $PAGE->requires->string_for_js('description', 'moodle');
 296      $PAGE->requires->js_init_call('M.core_group.init_hover_events', array($hoverevents));
 297  }
 298  
 299  echo $OUTPUT->footer();