Merge branch 'MDL-63303_master-deleteduserfix' of https://github.com/markn86/moodle
[moodle.git] / admin / user / user_bulk_download.php
blobbabede0d9bd985c1f0e4e4e2e660ac8123ce62cb
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Bulk export user into any dataformat
20 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
21 * @copyright 2007 Petr Skoda
22 * @package core
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);
33 admin_externalpage_setup('userbulk');
34 require_capability('moodle/user:update', context_system::instance());
36 if (empty($SESSION->bulk_users)) {
37 redirect(new moodle_url('/admin/user/user_bulk.php'));
40 if ($dataformat) {
41 $fields = array('id' => 'id',
42 'username' => 'username',
43 'email' => 'email',
44 'firstname' => 'firstname',
45 'lastname' => 'lastname',
46 'idnumber' => 'idnumber',
47 'institution' => 'institution',
48 'department' => 'department',
49 'phone1' => 'phone1',
50 'phone2' => 'phone2',
51 'city' => 'city',
52 'url' => 'url',
53 'icq' => 'icq',
54 'skype' => 'skype',
55 'aim' => 'aim',
56 'yahoo' => 'yahoo',
57 'msn' => 'msn',
58 'country' => 'country');
60 if ($extrafields = $DB->get_records('user_info_field')) {
61 foreach ($extrafields as $n => $field) {
62 $fields['profile_field_'.$field->shortname] = 'profile_field_'.$field->shortname;
63 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
67 $filename = clean_filename(get_string('users'));
69 $downloadusers = new ArrayObject($SESSION->bulk_users);
70 $iterator = $downloadusers->getIterator();
72 download_as_dataformat($filename, $dataformat, $fields, $iterator, function($userid) use ($extrafields, $fields) {
73 global $DB;
74 $row = array();
75 if (!$user = $DB->get_record('user', array('id' => $userid))) {
76 return null;
78 foreach ($extrafields as $field) {
79 $newfield = 'profile_field_'.$field->datatype;
80 $formfield = new $newfield($field->id, $user->id);
81 $formfield->edit_load_user_data($user);
83 $userprofiledata = array();
84 foreach ($fields as $field => $unused) {
85 // Custom user profile textarea fields come in an array
86 // The first element is the text and the second is the format.
87 // We only take the text.
88 if (is_array($user->$field)) {
89 $userprofiledata[$field] = reset($user->$field);
90 } else {
91 $userprofiledata[$field] = $user->$field;
94 return $userprofiledata;
95 });
97 exit;
100 echo $OUTPUT->header();
101 echo $OUTPUT->heading(get_string('download', 'admin'));
102 echo $OUTPUT->download_dataformat_selector(get_string('userbulkdownload', 'admin'), 'user_bulk_download.php');
103 echo $OUTPUT->footer();