Skip to content

Commit

Permalink
MDL-22570 group: Export groups/groupings to ods/xls/txt
Browse files Browse the repository at this point in the history
A new feature that enables teachers to export groups and
groupings through the groups overview page, making use of the dataformat api.
  • Loading branch information
tasosb committed May 11, 2022
1 parent 1a74403 commit 3df0908
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
89 changes: 88 additions & 1 deletion group/overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
$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;
Expand Down Expand Up @@ -176,7 +177,8 @@
WHERE g.courseid = :courseid
) grouped ON grouped.userid = u.id
$userfieldsjoin
WHERE grouped.userid IS NULL";
WHERE grouped.userid IS NULL
ORDER BY $sort";
$params['courseid'] = $courseid;

$nogroupusers = $DB->get_records_sql($sql, array_merge($params, $userfieldsparams));
Expand All @@ -186,6 +188,84 @@
}
}

// Export groups if requested.
if ($dataformat !== '') {
$columnnames = array(
'grouping' => $strgrouping,
'group' => $strgroup,
'firstname' => get_string('firstname'),
'lastname' => get_string('lastname'),
);
$extrafields = \core_user\fields::get_identity_fields($context, false);
foreach ($extrafields as $field) {
$columnnames[$field] = \core_user\fields::get_display_name($field);
}
$alldata = array();
// Generate file name.
$shortname = format_string($course->shortname, true, array('context' => $context))."_groups";
$i = 0;
foreach ($members as $gpgid => $groupdata) {
if ($groupingid and $groupingid != $gpgid) {
if ($groupingid > 0 || $gpgid > 0) {
// Still show 'not in group' when 'no grouping' selected.
continue; // Do not export.
}
}
if ($gpgid < 0) {
// Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) {
$groupingname = $strnotingroup;
} else {
$groupingname = $strnotingrouping;
}
} else {
$groupingname = $groupings[$gpgid]->formattedname;
}
if (empty($groupdata)) {
$alldata[$i] = array_fill_keys(array_keys($columnnames), '');
$alldata[$i]['grouping'] = $groupingname;
$i++;
}
foreach ($groupdata as $gpid => $users) {
if ($groupid and $groupid != $gpid) {
continue;
}
if (empty($users)) {
$alldata[$i] = array_fill_keys(array_keys($columnnames), '');
$alldata[$i]['grouping'] = $groupingname;
$alldata[$i]['group'] = $groups[$gpid]->name;
$i++;
}
foreach ($users as $option => $user) {
$alldata[$i]['grouping'] = $groupingname;
$alldata[$i]['group'] = $groups[$gpid]->name;
$alldata[$i]['firstname'] = $user->firstname;
$alldata[$i]['lastname'] = $user->lastname;
foreach ($extrafields as $field) {
$alldata[$i][$field] = $user->$field;
}
$i++;
}
}
}

\core\dataformat::download_data(
$shortname,
$dataformat,
$columnnames,
$alldata,
function($record, $supportshtml) use ($extrafields) {
if ($supportshtml) {
foreach ($extrafields as $extrafield) {
$record[$extrafield] = s($record[$extrafield]);
}
}
return $record;
});
die;
}

// Main page content.
navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
$PAGE->navbar->add(get_string('overview', 'group'));

Expand Down Expand Up @@ -288,4 +368,11 @@
$printed = true;
}

// 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();
1 change: 1 addition & 0 deletions lang/en/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
$string['eventgroupinggroupunassigned'] = 'Group unassigned from grouping';
$string['eventgroupingupdated'] = 'Grouping updated';
$string['existingmembers'] = 'Existing members: {$a}';
$string['exportgroupsgroupings'] = 'Export groups and groupings as';
$string['filtergroups'] = 'Filter groups by:';
$string['group'] = 'Group';
$string['groupaddedsuccesfully'] = 'Group {$a} added successfully';
Expand Down

0 comments on commit 3df0908

Please sign in to comment.