Commit eb91deaa authored by Andrew Nicols's avatar Andrew Nicols
Browse files

Merge branch 'MDL-51603-dataformat-MDL-34925-user-download' of...

Merge branch 'MDL-51603-dataformat-MDL-34925-user-download' of https://github.com/brendanheywood/moodle
parents ba443233 12142298
<?php
/**
* script for downloading of user lists
*/
// 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/>.
/**
* Bulk export user into any dataformat
*
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @copyright 2007 Petr Skoda
* @package core
*/
define('NO_OUTPUT_BUFFERING', true);
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/dataformatlib.php');
require_once($CFG->dirroot.'/user/profile/lib.php');
$format = optional_param('format', '', PARAM_ALPHA);
$dataformat = optional_param('dataformat', '', PARAM_ALPHA);
require_login();
admin_externalpage_setup('userbulk');
require_capability('moodle/user:update', context_system::instance());
$return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
if (empty($SESSION->bulk_users)) {
redirect($return);
redirect(new moodle_url('/admin/user/user_bulk.php'));
}
if ($format) {
if ($dataformat) {
$fields = array('id' => 'id',
'username' => 'username',
'email' => 'email',
......@@ -39,142 +59,47 @@ if ($format) {
'country' => 'country');
if ($extrafields = $DB->get_records('user_info_field')) {
foreach ($extrafields as $n=>$v){
$fields['profile_field_'.$v->shortname] = 'profile_field_'.$v->shortname;
}
}
switch ($format) {
case 'csv' : user_download_csv($fields);
case 'ods' : user_download_ods($fields);
case 'xls' : user_download_xls($fields);
}
die;
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('download', 'admin'));
echo $OUTPUT->box_start();
echo '<ul>';
echo '<li><a href="user_bulk_download.php?format=csv">'.get_string('downloadtext').'</a></li>';
echo '<li><a href="user_bulk_download.php?format=ods">'.get_string('downloadods').'</a></li>';
echo '<li><a href="user_bulk_download.php?format=xls">'.get_string('downloadexcel').'</a></li>';
echo '</ul>';
echo $OUTPUT->box_end();
echo $OUTPUT->continue_button($return);
echo $OUTPUT->footer();
function user_download_ods($fields) {
global $CFG, $SESSION, $DB;
require_once("$CFG->libdir/odslib.class.php");
require_once($CFG->dirroot.'/user/profile/lib.php');
$filename = clean_filename(get_string('users').'.ods');
$workbook = new MoodleODSWorkbook('-');
$workbook->send($filename);
$worksheet = array();
$worksheet[0] = $workbook->add_worksheet('');
$col = 0;
foreach ($fields as $fieldname) {
$worksheet[0]->write(0, $col, $fieldname);
$col++;
}
$row = 1;
foreach ($SESSION->bulk_users as $userid) {
if (!$user = $DB->get_record('user', array('id'=>$userid))) {
continue;
}
$col = 0;
profile_load_data($user);
foreach ($fields as $field=>$unused) {
$worksheet[0]->write($row, $col, $user->$field);
$col++;
foreach ($extrafields as $n => $field) {
$fields['profile_field_'.$field->shortname] = 'profile_field_'.$field->shortname;
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
}
$row++;
}
$workbook->close();
die;
}
function user_download_xls($fields) {
global $CFG, $SESSION, $DB;
require_once("$CFG->libdir/excellib.class.php");
require_once($CFG->dirroot.'/user/profile/lib.php');
$filename = clean_filename(get_string('users').'.xls');
$workbook = new MoodleExcelWorkbook('-');
$workbook->send($filename);
$worksheet = array();
$worksheet[0] = $workbook->add_worksheet('');
$col = 0;
foreach ($fields as $fieldname) {
$worksheet[0]->write(0, $col, $fieldname);
$col++;
}
$row = 1;
foreach ($SESSION->bulk_users as $userid) {
if (!$user = $DB->get_record('user', array('id'=>$userid))) {
continue;
}
$col = 0;
profile_load_data($user);
foreach ($fields as $field=>$unused) {
$worksheet[0]->write($row, $col, $user->$field);
$col++;
}
$row++;
}
$workbook->close();
die;
}
function user_download_csv($fields) {
global $CFG, $SESSION, $DB;
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->libdir . '/csvlib.class.php');
$filename = clean_filename(get_string('users'));
$csvexport = new csv_export_writer();
$csvexport->set_filename($filename);
$csvexport->add_data($fields);
$downloadusers = new ArrayObject($SESSION->bulk_users);
$iterator = $downloadusers->getIterator();
foreach ($SESSION->bulk_users as $userid) {
download_as_dataformat($filename, $dataformat, $fields, $iterator, function($userid) use ($extrafields, $fields) {
global $DB;
$row = array();
if (!$user = $DB->get_record('user', array('id'=>$userid))) {
continue;
if (!$user = $DB->get_record('user', array('id' => $userid))) {
return null;
}
foreach ($extrafields as $field) {
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $user->id);
$formfield->edit_load_user_data($user);
}
profile_load_data($user);
$userprofiledata = array();
foreach ($fields as $field=>$unused) {
foreach ($fields as $field => $unused) {
// Custom user profile textarea fields come in an array
// The first element is the text and the second is the format.
// We only take the text.
if (is_array($user->$field)) {
$userprofiledata[] = reset($user->$field);
$userprofiledata[$field] = reset($user->$field);
} else {
$userprofiledata[] = $user->$field;
$userprofiledata[$field] = $user->$field;
}
}
$csvexport->add_data($userprofiledata);
}
$csvexport->download_file();
die;
return $userprofiledata;
});
exit;
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('download', 'admin'));
echo $OUTPUT->download_dataformat_selector(get_string('userbulkdownload', 'admin'), 'user_bulk_download.php');
echo $OUTPUT->footer();
......@@ -1146,6 +1146,7 @@ $string['useblogassociations'] = 'Enable associations';
$string['useexternalyui'] = 'Use online YUI libraries';
$string['user'] = 'User';
$string['userbulk'] = 'Bulk user actions';
$string['userbulkdownload'] = 'Export users as';
$string['userlist'] = 'Browse list of users';
$string['userdefaultpreferences'] = 'User default preferences';
$string['userpreference'] = 'User preference';
......
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