Commit 9d6b85cf authored by Dmitrii Metelkin's avatar Dmitrii Metelkin Committed by Robert Lyon
Browse files

Bug #1595028: download CSV file with group membership



behatnotneeded

Change-Id: Ibbc5a70d5efca1415f0b781d71dca24d3e3d330b
Signed-off-by: default avatarDmitrii Metelkin <dmitriim@catalyst-au.net>
parent 3dd182b6
......@@ -32,6 +32,19 @@ if ($type == 'sitemap') {
}
$path = get_config('dataroot') . 'sitemaps/' . $name;
}
else if ($type == 'groupmembership') {
$group_id = param_integer('groupid');
$data = group_get_membership_file_data($group_id);
if (!$USER->is_logged_in() || empty($data)) {
throw new NotFoundException(get_string('filenotfound'));
}
$path = get_config('dataroot') . 'export/' . $USER->get('id') . '/' . $data['file'];
$name = $data['name'];
$mimetype = $data['mimetype'];
}
else {
$data = $SESSION->get('downloadfile');
......
......@@ -313,6 +313,8 @@ $string['downloadstatsascsv'] = 'statistics in CSV format';
$string['nostats'] = 'No statistics available';
$string['site'] = 'Site';
$string['exportgroupscsv'] = 'Export groups in CSV format';
$string['exportgroupmembershipscsv'] = 'Export group membership in CSV format';
$string['exportgroupmembershipscsvspecific'] = 'Export group membership in CSV format for "%s"';
// Institution statistics
$string['statistics'] = 'Statistics';
......
......@@ -2683,3 +2683,71 @@ function group_get_allowed_group_csv_keys() {
return $keys;
}
/**
* Generates group membership file data.
*
* @param int $group_id Id of the group.
* @param string $file_format A format of the file.
* @param string $mimetype A mimetype of the file.
*
* @return array An empty array if error || array with following keys 'mimetype', 'name' and 'file'.
*/
function group_get_membership_file_data($group_id, $file_format = 'csv', $mimetype = 'text/csv') {
global $USER;
$data = array();
if (!$USER->get('admin')) {
return $data;
}
$group = get_record('group', 'id', $group_id);
if (!$group) {
return $data;
}
$membership_data = get_records_sql_array(
"SELECT g.shortname, u.username, gm.role, u.firstname, u.lastname, u.email, u.preferredname
FROM {group} g
INNER JOIN {group_member} gm ON g.id = gm.group
INNER JOIN {usr} u ON gm.member = u.id
WHERE g.id = ?
ORDER BY u.username",
array($group_id)
);
$csv_fields = array(
'shortname',
'username',
'role',
'firstname',
'lastname',
'email',
'preferredname',
);
$file_content = generate_csv($membership_data, $csv_fields);
if (empty($file_content)) {
return $data;
}
$filename = get_random_key();
$dir = get_config('dataroot') . 'export/' . $USER->get('id') . '/';
if (!check_dir_exists($dir)) {
return $data;
}
if (!file_put_contents($dir . $filename, $file_content)) {
return $data;
}
$data['mimetype'] = $mimetype;
$data['name'] = $group->shortname . '.' . $file_format;
$data['file'] = $filename;
return $data;
}
......@@ -18,6 +18,9 @@
<a class="btn btn-default btn-sm" title="{str tag="groupmanage" section="admin"}" href="{$WWWROOT}admin/groups/manage.php?id={$group->id}">
<span class="icon icon-cog icon-lg" role="presentation" aria-hidden="true"></span><span class="sr-only">{str(tag=groupmanagespecific section=admin arg1=$group->name)|escape:html|safe}</span>
</a>
<a class="btn btn-default btn-sm" title="{str tag="exportgroupmembershipscsv" section="admin"}" href="{$WWWROOT}download.php?type=groupmembership&groupid={$group->id}">
<span class="icon icon-users icon-lg" role="presentation" aria-hidden="true"></span><span class="sr-only">{str(tag=exportgroupmembershipscsvspecific section=admin arg1=$group->name)|escape:html|safe}</span>
</a>
<a class="btn btn-default btn-sm" title="{str tag="delete"}" href="{$WWWROOT}admin/groups/delete.php?id={$group->id}">
<span class="icon icon-trash text-danger icon-lg" role="presentation" aria-hidden="true"></span><span class="sr-only">{str(tag=deletespecific arg1=$group->name)|escape:html|safe}</span>
</a>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment