2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Bulk export user into any dataformat
20 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
21 * @copyright 2007 Petr Skoda
25 define('NO_OUTPUT_BUFFERING', true);
26 require_once('../../config.php');
27 require_once($CFG->libdir
.'/adminlib.php');
28 require_once($CFG->libdir
.'/dataformatlib.php');
29 require_once($CFG->dirroot
.'/user/profile/lib.php');
31 $dataformat = optional_param('dataformat', '', PARAM_ALPHA
);
34 admin_externalpage_setup('userbulk');
35 require_capability('moodle/user:update', context_system
::instance());
37 if (empty($SESSION->bulk_users
)) {
38 redirect(new moodle_url('/admin/user/user_bulk.php'));
42 $fields = array('id' => 'id',
43 'username' => 'username',
45 'firstname' => 'firstname',
46 'lastname' => 'lastname',
47 'idnumber' => 'idnumber',
48 'institution' => 'institution',
49 'department' => 'department',
59 'country' => 'country');
61 if ($extrafields = $DB->get_records('user_info_field')) {
62 foreach ($extrafields as $n => $field) {
63 $fields['profile_field_'.$field->shortname
] = 'profile_field_'.$field->shortname
;
64 require_once($CFG->dirroot
.'/user/profile/field/'.$field->datatype
.'/field.class.php');
68 $filename = clean_filename(get_string('users'));
70 $downloadusers = new ArrayObject($SESSION->bulk_users
);
71 $iterator = $downloadusers->getIterator();
73 download_as_dataformat($filename, $dataformat, $fields, $iterator, function($userid) use ($extrafields, $fields) {
76 if (!$user = $DB->get_record('user', array('id' => $userid))) {
79 foreach ($extrafields as $field) {
80 $newfield = 'profile_field_'.$field->datatype
;
81 $formfield = new $newfield($field->id
, $user->id
);
82 $formfield->edit_load_user_data($user);
84 $userprofiledata = array();
85 foreach ($fields as $field => $unused) {
86 // Custom user profile textarea fields come in an array
87 // The first element is the text and the second is the format.
88 // We only take the text.
89 if (is_array($user->$field)) {
90 $userprofiledata[$field] = reset($user->$field);
92 $userprofiledata[$field] = $user->$field;
95 return $userprofiledata;
101 echo $OUTPUT->header();
102 echo $OUTPUT->heading(get_string('download', 'admin'));
103 echo $OUTPUT->download_dataformat_selector(get_string('userbulkdownload', 'admin'), 'user_bulk_download.php');
104 echo $OUTPUT->footer();