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)
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.


/**
 * Print an overview of groupings & group membership
 *
 * @copyright  Matt Clarkson mattc@catalyst.net.nz
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @package    core_group
 */

require_once('../config.php');
require_once($CFG->libdir . '/filelib.php');

define('OVERVIEW_NO_GROUP', -1); // The fake group for users not in a group.
define('OVERVIEW_GROUPING_GROUP_NO_GROUPING', -1); // The fake grouping for groups that have no grouping.
define('OVERVIEW_GROUPING_NO_GROUP', -2); // The fake grouping for users with no group.

$courseid   = required_param('id', PARAM_INT);
$groupid    = optional_param('group', 0, PARAM_INT);
$groupingid = optional_param('grouping', 0, PARAM_INT);
> $dataformat = optional_param('dataformat', '', PARAM_ALPHA);
$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid; $rooturl = $CFG->wwwroot.'/group/overview.php?id='.$courseid; if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
< print_error('invalidcourse');
> throw new \moodle_exception('invalidcourse');
} $url = new moodle_url('/group/overview.php', array('id'=>$courseid)); if ($groupid !== 0) { $url->param('group', $groupid); } if ($groupingid !== 0) { $url->param('grouping', $groupingid); } $PAGE->set_url($url); // Make sure that the user has permissions to manage groups. require_login($course); $context = context_course::instance($courseid); require_capability('moodle/course:managegroups', $context); $strgroups = get_string('groups'); $strparticipants = get_string('participants'); $stroverview = get_string('overview', 'group'); $strgrouping = get_string('grouping', 'group'); $strgroup = get_string('group', 'group'); $strnotingrouping = get_string('notingrouping', 'group'); $strfiltergroups = get_string('filtergroups', 'group'); $strnogroups = get_string('nogroups', 'group'); $strdescription = get_string('description'); $strnotingroup = get_string('notingrouplist', 'group'); $strnogroup = get_string('nogroup', 'group'); $strnogrouping = get_string('nogrouping', 'group'); // This can show all users and all groups in a course. // This is lots of data so allow this script more resources. raise_memory_limit(MEMORY_EXTRA); // Get all groupings and sort them by formatted name. $groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name'); foreach ($groupings as $gid => $grouping) { $groupings[$gid]->formattedname = format_string($grouping->name, true, array('context' => $context)); } core_collator::asort_objects_by_property($groupings, 'formattedname'); $members = array(); foreach ($groupings as $grouping) { $members[$grouping->id] = array(); } // Groups not in a grouping. $members[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = array();
< // Get all groups
> // Get all groups and sort them by formatted name.
$groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
> foreach ($groups as $id => $group) { > $groups[$id]->formattedname = format_string($group->name, true, ['context' => $context]); $params = array('courseid'=>$courseid); > } if ($groupid) { > core_collator::asort_objects_by_property($groups, 'formattedname');
$groupwhere = "AND g.id = :groupid"; $params['groupid'] = $groupid; } else { $groupwhere = ""; } if ($groupingid) { if ($groupingid < 0) { // No grouping filter. $groupingwhere = "AND gg.groupingid IS NULL"; } else { $groupingwhere = "AND gg.groupingid = :groupingid"; $params['groupingid'] = $groupingid; } } else { $groupingwhere = ""; } list($sort, $sortparams) = users_order_by_sql('u');
< $extrafields = get_extra_user_fields($context); < $allnames = 'u.id, ' . user_picture::fields('u', $extrafields);
> $userfieldsapi = \core_user\fields::for_identity($context)->with_userpic(); > [ > 'selects' => $userfieldsselects, > 'joins' => $userfieldsjoin, > 'params' => $userfieldsparams > ] = (array)$userfieldsapi->get_sql('u', true); > $extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]); > $allnames = 'u.id ' . $userfieldsselects;
$sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username FROM {groups} g LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid LEFT JOIN {groups_members} gm ON g.id = gm.groupid LEFT JOIN {user} u ON gm.userid = u.id
> $userfieldsjoin
WHERE g.courseid = :courseid $groupwhere $groupingwhere ORDER BY g.name, $sort";
< $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
> $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams, $userfieldsparams));
foreach ($rs as $row) { $user = username_load_fields_from_object((object) [], $row, null, array_merge(['id' => 'userid', 'username', 'idnumber'], $extrafields)); if (!$row->groupingid) { $row->groupingid = OVERVIEW_GROUPING_GROUP_NO_GROUPING; } if (!array_key_exists($row->groupid, $members[$row->groupingid])) { $members[$row->groupingid][$row->groupid] = array(); } if (!empty($user->id)) { $members[$row->groupingid][$row->groupid][] = $user; } } $rs->close(); // Add 'no groupings' / 'no groups' selectors. $groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = (object)array( 'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING, 'formattedname' => $strnogrouping, ); $groups[OVERVIEW_NO_GROUP] = (object)array( 'id' => OVERVIEW_NO_GROUP, 'courseid' => $courseid, 'idnumber' => '', 'name' => $strnogroup,
> 'formattedname' => $strnogroup,
'description' => '', 'descriptionformat' => FORMAT_HTML, 'enrolmentkey' => '', 'picture' => 0,
< 'hidepicture' => 0,
'timecreated' => 0, 'timemodified' => 0, ); // Add users who are not in a group. if ($groupid <= 0 && $groupingid <= 0) { list($esql, $params) = get_enrolled_sql($context, null, 0, true); $sql = "SELECT u.id, $allnames, u.idnumber, u.username FROM {user} u JOIN ($esql) e ON e.id = u.id LEFT JOIN ( SELECT gm.userid FROM {groups_members} gm JOIN {groups} g ON g.id = gm.groupid WHERE g.courseid = :courseid ) grouped ON grouped.userid = u.id
< WHERE grouped.userid IS NULL";
> $userfieldsjoin > WHERE grouped.userid IS NULL > ORDER BY $sort";
$params['courseid'] = $courseid;
< $nogroupusers = $DB->get_records_sql($sql, $params);
> $nogroupusers = $DB->get_records_sql($sql, array_merge($params, $userfieldsparams));
if ($nogroupusers) { $members[OVERVIEW_GROUPING_NO_GROUP][OVERVIEW_NO_GROUP] = $nogroupusers; } }
> // Export groups if requested. navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid))); > if ($dataformat !== '') { $PAGE->navbar->add(get_string('overview', 'group')); > $columnnames = array( > 'grouping' => $strgrouping, /// Print header > 'group' => $strgroup, $PAGE->set_title($strgroups); > 'firstname' => get_string('firstname'), $PAGE->set_heading($course->fullname); > 'lastname' => get_string('lastname'), $PAGE->set_pagelayout('standard'); > ); echo $OUTPUT->header(); > $extrafields = \core_user\fields::get_identity_fields($context, false); > foreach ($extrafields as $field) { // Add tabs > $columnnames[$field] = \core_user\fields::get_display_name($field); $currenttab = 'overview'; > } require('tabs.php'); > $alldata = array(); > // Generate file name. /// Print overview > $shortname = format_string($course->shortname, true, array('context' => $context))."_groups"; echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3); > $i = 0; > foreach ($members as $gpgid => $groupdata) { echo $strfiltergroups; > if ($groupingid and $groupingid != $gpgid) { > if ($groupingid > 0 || $gpgid > 0) { $options = array(); > // Still show 'not in group' when 'no grouping' selected. $options[0] = get_string('all'); > continue; // Do not export. foreach ($groupings as $grouping) { > } $options[$grouping->id] = strip_tags($grouping->formattedname); > } } > if ($gpgid < 0) { $popupurl = new moodle_url($rooturl.'&group='.$groupid); > // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP. $select = new single_select($popupurl, 'grouping', $options, $groupingid, array()); > if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) { $select->label = $strgrouping; > $groupingname = $strnotingroup; $select->formid = 'selectgrouping'; > } else { echo $OUTPUT->render($select); > $groupingname = $strnotingrouping; > } $options = array(); > } else { $options[0] = get_string('all'); > $groupingname = $groupings[$gpgid]->formattedname; foreach ($groups as $group) { > } $options[$group->id] = strip_tags(format_string($group->name)); > if (empty($groupdata)) { } > $alldata[$i] = array_fill_keys(array_keys($columnnames), ''); $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid); > $alldata[$i]['grouping'] = $groupingname; $select = new single_select($popupurl, 'group', $options, $groupid, array()); > $i++; $select->label = $strgroup; > } $select->formid = 'selectgroup'; > foreach ($groupdata as $gpid => $users) { echo $OUTPUT->render($select); > if ($groupid and $groupid != $gpid) { > continue; /// Print table > } $printed = false; > if (empty($users)) { $hoverevents = array(); > $alldata[$i] = array_fill_keys(array_keys($columnnames), ''); foreach ($members as $gpgid=>$groupdata) { > $alldata[$i]['grouping'] = $groupingname; if ($groupingid and $groupingid != $gpgid) { > $alldata[$i]['group'] = $groups[$gpid]->formattedname; if ($groupingid > 0 || $gpgid > 0) { // Still show 'not in group' when 'no grouping' selected. > $i++; continue; // Do not show. > } } > foreach ($users as $option => $user) { } > $alldata[$i]['grouping'] = $groupingname; $table = new html_table(); > $alldata[$i]['group'] = $groups[$gpid]->formattedname; $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group')); > $alldata[$i]['firstname'] = $user->firstname; $table->size = array('20%', '70%', '10%'); > $alldata[$i]['lastname'] = $user->lastname; $table->align = array('left', 'left', 'center'); > foreach ($extrafields as $field) { $table->width = '90%'; > $alldata[$i][$field] = $user->$field; $table->data = array(); > } foreach ($groupdata as $gpid=>$users) { > $i++; if ($groupid and $groupid != $gpid) { > } continue; > } } > } $line = array(); > $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name); > \core\dataformat::download_data( $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid); > $shortname, $options = new stdClass; > $dataformat, $options->noclean = true; > $columnnames, $options->overflowdiv = true; > $alldata, $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options)); > function($record, $supportshtml) use ($extrafields) { if (empty($jsdescription)) { > if ($supportshtml) { $line[] = $name; > foreach ($extrafields as $extrafield) { } else { > $record[$extrafield] = s($record[$extrafield]); $line[] = html_writer::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid)); > } $hoverevents[$gpid] = get_string('descriptiona', null, $jsdescription); > } } > return $record; $viewfullnames = has_capability('moodle/site:viewfullnames', $context); > }); $fullnames = array(); > die; foreach ($users as $user) { > } $displayname = fullname($user, $viewfullnames); > if ($extrafields) { > // Main page content.
< echo $OUTPUT->header(); < < // Add tabs < $currenttab = 'overview'; < require('tabs.php');
< /// Print overview < echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
> echo $OUTPUT->header(); > echo $OUTPUT->render_participants_tertiary_nav($course);
< $options[$group->id] = strip_tags(format_string($group->name));
> $options[$group->id] = $group->formattedname;
< $hoverevents = array();
< $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
> $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . $groups[$gpid]->formattedname;
< $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options)); < if (empty($jsdescription)) {
< } else { < $line[] = html_writer::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid)); < $hoverevents[$gpid] = get_string('descriptiona', null, $jsdescription); < }
} if ($gpgid < 0) { // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP. if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) { echo $OUTPUT->heading($strnotingroup, 3); } else { echo $OUTPUT->heading($strnotingrouping, 3); } } else { echo $OUTPUT->heading($groupings[$gpgid]->formattedname, 3); $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid); $options = new stdClass; $options->overflowdiv = true; echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter'); } echo html_writer::table($table); $printed = true; }
< if (count($hoverevents)>0) { < $PAGE->requires->string_for_js('description', 'moodle'); < $PAGE->requires->js_init_call('M.core_group.init_hover_events', array($hoverevents)); < }
> // Add buttons for exporting groups/groupings. > echo $OUTPUT->download_dataformat_selector(get_string('exportgroupsgroupings', 'group'), 'overview.php', 'dataformat', [ > 'id' => $courseid, > 'group' => $groupid, > 'grouping' => $groupingid, > ]);
echo $OUTPUT->footer();