From 3df0908e726d78105b377953f2008abba7740aa4 Mon Sep 17 00:00:00 2001 From: Anastasios Bithas Date: Tue, 14 Dec 2021 14:02:53 +0200 Subject: [PATCH] MDL-22570 group: Export groups/groupings to ods/xls/txt A new feature that enables teachers to export groups and groupings through the groups overview page, making use of the dataformat api. --- group/overview.php | 89 +++++++++++++++++++++++++++++++++++++++++++++- lang/en/group.php | 1 + 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/group/overview.php b/group/overview.php index 8a99ba3a890ca..392bcdf341376 100644 --- a/group/overview.php +++ b/group/overview.php @@ -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; @@ -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)); @@ -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')); @@ -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(); diff --git a/lang/en/group.php b/lang/en/group.php index 5dcaaa5a91e81..0e2577adf376e 100644 --- a/lang/en/group.php +++ b/lang/en/group.php @@ -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';